Просмотр исходного кода

Add functionality to auto-fill project names

Alan Lam 6 лет назад
Родитель
Сommit
3e6886cbf3
4 измененных файлов с 53 добавлено и 4 удалено
  1. 1 1
      controllers/project_add.php
  2. 1 1
      controllers/project_edit.php
  3. 35 0
      public/js/app-project-form.js
  4. 16 2
      views/project_form.php

+ 1 - 1
controllers/project_add.php

@@ -115,5 +115,5 @@ if($submit) {
 $locations = $customer->getLocations(["closed" => 0]);
 
 View::includeOnce("header", compact("page_title"));
-View::include("project_form", compact("project", "templates", "locations", "location_ids"));
+View::include("project_form", compact("customer", "project", "templates", "locations", "location_ids"));
 View::includeOnce("footer");

+ 1 - 1
controllers/project_edit.php

@@ -131,5 +131,5 @@ if($submit) {
 $locations = $customer->getLocations();
 
 View::includeOnce("header", compact("page_title"));
-View::include("project_form", compact("project", "templates", "locations", "location_ids"));
+View::include("project_form", compact("customer", "project", "templates", "locations", "location_ids"));
 View::includeOnce("footer");

+ 35 - 0
public/js/app-project-form.js

@@ -46,6 +46,17 @@ $(function() {
 
 	initialiseTooltips();
 
+	$("#template-id, #time-of-day, #date-start-month, #date-start-year").on("change", () => autogenProjectName());
+	autogenProjectName();
+
+	$("#autogen-name").on("change", event => {
+		const checked = event.currentTarget.checked;
+		$("#name").prop("readonly", checked);
+		if(checked) {
+			autogenProjectName();
+		}
+	});
+
 });
 
 function handleSelectButton() {
@@ -134,3 +145,27 @@ function refreshLocationAlerts() {
 		$("#selected-locations-alert").show();
 	}
 }
+
+function autogenProjectName() {
+
+	const autogenChecked = $("#autogen-name").is(":checked");
+	if(!autogenChecked) return;
+
+	const customerName = $("#customer-name").text();
+	const templateName = $("#template-id").find("option:selected").text();
+	const dateStartMonth = $("#date-start-month").find("option:selected").text();
+	const dateStartYear = $("#date-start-year").find("option:selected").text();
+	const timeOfDay = $("#time-of-day").find("option:selected").text();
+
+	const templateWords = templateName.split(" ");
+	const templateWordCount = templateWords.length;
+	const templateInitials = [];
+	for(let i = 0; i < templateWordCount; i++) {
+		templateInitials.push(templateWords[i].charAt(0));
+	}
+
+	const projectName = `${customerName} ${dateStartMonth} ${dateStartYear} ${templateInitials.join("")} ${timeOfDay}`;
+
+	$("#name").val(projectName);
+
+}

+ 16 - 2
views/project_form.php

@@ -1,5 +1,9 @@
 <?php
 
+/**
+ * @var Project $project
+ */
+
 $date_start = explode(" ", date("Y-m-d H:i:s", strtotime($project->dts_start . " UTC")))[0];
 $date_start_parts = explode("-", $date_start);
 
@@ -18,13 +22,18 @@ $time_of_day_options = [
 	"N" => "Nighttime",
 ];
 
+$name_readonly = $project->id ? "" : "readonly";
+$autogen_checked = $project->id ? "" : "checked";
+
 ?>
 <div class="container mt-3 py-4">
 	<h2>
 		<?=($project->id ? "Edit" : "Add")?>
-		Project
+		Project for <span id="customer-name"><?=htmlspecialchars($customer->name)?></span>
 	</h2>
 
+	<input type="hidden" id="project-id" value="<?=$project->id?>" />
+
 	<form method="post" id="project-form">
 
 		<h3>Project Details</h3>
@@ -35,8 +44,13 @@ $time_of_day_options = [
 				<div class="row mb-1">
 					<label class="col-sm-4 col-form-label" for="name">Name</label>
 					<div class="col-sm">
-						<input class="form-control" type="text" name="name" id="name" placeholder="Enter name" value="<?=htmlspecialchars($project->name)?>" required />
+						<input class="form-control" type="text" name="name" id="name" placeholder="Enter name" value="<?=htmlspecialchars($project->name)?>" required <?=$name_readonly?> />
+						<div class="custom-control custom-switch">
+							<input type="checkbox" class="custom-control-input" id="autogen-name" <?=$autogen_checked?> />
+							<label class="custom-control-label" for="autogen-name">Auto-fill name</label>
+						</div>
 					</div>
+
 				</div>
 				<div class="row mb-1">
 					<label class="col-sm-4 col-form-label" for="template-id">Template</label>