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

Add ability to add locations to a project via "Locations" page

Alan Lam 5 лет назад
Родитель
Сommit
2aafff18b5

+ 1 - 0
controllers/locations.php

@@ -127,4 +127,5 @@ $filter_count = count($nvp) - 1;
 
 
 View::includeOnce("header", compact("page_title"));
 View::includeOnce("header", compact("page_title"));
 View::include("locations", compact("customer", "filter_count", "sorts", "sort", "locations"));
 View::include("locations", compact("customer", "filter_count", "sorts", "sort", "locations"));
+View::include("modal_add_locations_to_project", compact("customer"));
 View::includeOnce("footer");
 View::includeOnce("footer");

+ 43 - 0
controllers/project_ajax.php

@@ -1,5 +1,10 @@
 <?php
 <?php
 
 
+/**
+ * @var int $id
+ * @var string $action
+ */
+
 JSON::header();
 JSON::header();
 
 
 $project = Project::get($id);
 $project = Project::get($id);
@@ -308,6 +313,44 @@ if($submit) {
 				break;
 				break;
 			}
 			}
 
 
+			case "add-locations": {
+
+				$location_ids = http::value("location_ids", "int");
+
+				$customer = $project->getCustomer();
+
+				$locations = Location::query([
+					"customer_id" => $customer->id,
+					"id" => $location_ids
+				]);
+
+				$dupe_locations = [];
+
+				foreach($locations as $location) {
+					$test = $project->getActiveTestForLocationId($location->id);
+					if($test) {
+						$dupe_locations[] = $location;
+					}
+					else {
+						$project->createTestForLocationId($location->id);
+					}
+				}
+
+				$dupe_location_objects = [];
+				foreach($dupe_locations as $location) {
+					$dupe_location_objects[] = [
+						"id" => $location->id,
+						"name" => $location->name
+					];
+				}
+
+				JSON::success([
+					"dupeLocations" => $dupe_location_objects
+				]);
+
+				break;
+			}
+
 			default:
 			default:
 				throw new Exception("Invalid request.");
 				throw new Exception("Invalid request.");
 		}
 		}

+ 2 - 12
controllers/project_edit.php

@@ -106,22 +106,12 @@ if($submit) {
 
 
 		// Create test for new location IDs
 		// Create test for new location IDs
 		foreach($location_ids_to_add as $location_id) {
 		foreach($location_ids_to_add as $location_id) {
-			$test = new Test;
-			$test->project_id = $project->id;
-			$test->location_id = $location_id;
-			$test->status = Test::STATUS_UNASSIGNED;
-			$test->dts_created = Util::getDateTime();
-			$success = $test->commit();
-			if(!$success) throw new Exception("Test cannot be created, please try again later.");
+			$project->createTestForLocationId($location_id);
 		}
 		}
 
 
 		// Cancel tests for removed location IDs
 		// Cancel tests for removed location IDs
 		foreach($location_ids_to_delete as $location_id) {
 		foreach($location_ids_to_delete as $location_id) {
-			$test = Test::queryOne([
-				"project_id" => $project->id,
-				"location_id" => $location_id,
-				"status" => (new Constraint("MORE_THAN"))->setValues(Test::STATUS_CANCELLED)
-			]);
+			$test = $project->getActiveTestForLocationId($location_id);
 			if($test) {
 			if($test) {
 				$test->status = Test::STATUS_CANCELLED;
 				$test->status = Test::STATUS_CANCELLED;
 				$test->dts_updated = Util::getDateTime();
 				$test->dts_updated = Util::getDateTime();

+ 29 - 0
engine/project.php

@@ -550,4 +550,33 @@ class Project extends Model {
 		return CarbonImmutable::parse($this->dts_report, "UTC");
 		return CarbonImmutable::parse($this->dts_report, "UTC");
 	}
 	}
 
 
+	/**
+	 * @param int $location_id
+	 * @return Test
+	 * @throws Exception
+	 */
+	public function createTestForLocationId(int $location_id): Test {
+		$test = new Test;
+		$test->project_id = $this->id;
+		$test->location_id = $location_id;
+		$test->status = Test::STATUS_UNASSIGNED;
+		$test->dts_created = Util::getDateTime();
+		$success = $test->commit();
+		if(!$success) throw new Exception("Test cannot be created, please try again later.");
+		return $test;
+	}
+
+	/**
+	 * @param int $location_id
+	 * @return Test|null
+	 * @throws Exception
+	 */
+	public function getActiveTestForLocationId(int $location_id): ?Test {
+		return Test::queryOne([
+			"project_id" => $this->id,
+			"location_id" => $location_id,
+			"status" => (new Constraint("MORE_THAN"))->setValues(Test::STATUS_CANCELLED)
+		]);
+	}
+
 }
 }

+ 53 - 2
public/js/app-locations.js

@@ -13,7 +13,7 @@ function initialiseLocations() {
 		id: $("#id").val(),
 		id: $("#id").val(),
 		internal_id: $("#internal-id").val(),
 		internal_id: $("#internal-id").val(),
 		name: $("#name").val(),
 		name: $("#name").val(),
-		legal_entity_name: $("#legal-entity-name").val()
+		legal_entity_name: $("#legal-entity-name").val(),
 	}, function(response) {
 	}, function(response) {
 		if(!response.success) return false;
 		if(!response.success) return false;
 		if(!response.locations) return false;
 		if(!response.locations) return false;
@@ -81,6 +81,7 @@ function initialiseLocations() {
 		displayInfoWindow(marker);
 		displayInfoWindow(marker);
 
 
 		$createProjectButton.prop("disabled", (exportIds.length === 0));
 		$createProjectButton.prop("disabled", (exportIds.length === 0));
+		$addLocationsToProjectButton.prop("disabled", (exportIds.length === 0));
 	});
 	});
 
 
 	$("#export-csv-button, #export-kml-button").on("click", function(event) {
 	$("#export-csv-button, #export-kml-button").on("click", function(event) {
@@ -103,7 +104,6 @@ function initialiseLocations() {
 	});
 	});
 
 
 	const $createProjectButton = $("#create-project-button");
 	const $createProjectButton = $("#create-project-button");
-
 	$createProjectButton.on("click", function(event) {
 	$createProjectButton.on("click", function(event) {
 		event.preventDefault();
 		event.preventDefault();
 		if(exportIds.length === 0) return;
 		if(exportIds.length === 0) return;
@@ -115,6 +115,10 @@ function initialiseLocations() {
 		$form.trigger("submit");
 		$form.trigger("submit");
 	});
 	});
 
 
+	const $addLocationsToProjectButton = $("#add-locations-to-project-button");
+
+	new AddLocationsToProjectModal();
+
 }
 }
 
 
 function displayInfoWindow(marker) {
 function displayInfoWindow(marker) {
@@ -156,3 +160,50 @@ function displayInfoWindow(marker) {
 	infoWindow.open(map, marker);
 	infoWindow.open(map, marker);
 
 
 }
 }
+
+class AddLocationsToProjectModal {
+	constructor() {
+		this.$modal = $("#add-locations-to-project-modal");
+		this.$modal.on("show.bs.modal", event => this.onShow(event));
+
+		this.$form = this.$modal.find("form");
+		this.$form.on("submit", event => this.onSubmit(event));
+
+		this.$projectDropdown = this.$form.find("[name=project_id]");
+	}
+
+	onShow(event) {
+		this.$form.find(`input[name="location_ids[]"]`).remove();
+
+		exportIds.forEach(locationId => {
+			this.$form.append(`<input type="hidden" name="location_ids[]" value="${locationId}" />`);
+		});
+	}
+
+	onSubmit(event) {
+		event.preventDefault();
+
+		const projectId = this.$projectDropdown.val();
+
+		const data = this.$form.serialize();
+
+		$.post(`/account/projects/${projectId}/add-locations`, data).then(response => {
+
+			if(response.success) {
+				if(response.dupeLocations.length > 0) {
+					const locationNames = response.dupeLocations.map(location => location.name).join("\n");
+
+					alert(`Locations added to project successfully. The following locations were skipped as there were already active tests:\n\n${locationNames}`);
+				}
+				else {
+					alert("Locations added to project successfully.");
+				}
+			}
+
+			console.log(response);
+			this.$modal.modal("hide");
+		}).catch(error => {
+			console.error(error);
+		});
+	}
+}

+ 1 - 1
routes.php

@@ -72,7 +72,7 @@ $routes = [
 	"/^account\/projects\/$id_pattern\/export-kml$/" => ["controller" => "project_kml", "params" => ["id"], "require_user_level" => User::LEVEL_TESTER_MANAGER],
 	"/^account\/projects\/$id_pattern\/export-kml$/" => ["controller" => "project_kml", "params" => ["id"], "require_user_level" => User::LEVEL_TESTER_MANAGER],
 	"/^account\/projects\/$id_pattern\/invoice$/" => ["controller" => "project_invoice", "params" => ["id"], "require_user_level" => User::LEVEL_TESTER_MANAGER],
 	"/^account\/projects\/$id_pattern\/invoice$/" => ["controller" => "project_invoice", "params" => ["id"], "require_user_level" => User::LEVEL_TESTER_MANAGER],
 	"/^account\/projects\/$id_pattern\/(mark-finished|mark-unfinished)$/" => ["controller" => "project_mark_finished", "params" => ["id", "action"], "require_user_level" => User::LEVEL_TESTER_MANAGER],
 	"/^account\/projects\/$id_pattern\/(mark-finished|mark-unfinished)$/" => ["controller" => "project_mark_finished", "params" => ["id", "action"], "require_user_level" => User::LEVEL_TESTER_MANAGER],
-	"/^account\/projects\/$id_pattern\/(get-locations|get-testers|create-test|assign-tester)$/" => ["controller" => "project_ajax", "params" => ["id", "action"], "type" => "json", "require_user_level" => User::LEVEL_TESTER_MANAGER],
+	"/^account\/projects\/$id_pattern\/(get-locations|get-testers|create-test|assign-tester|add-locations)$/" => ["controller" => "project_ajax", "params" => ["id", "action"], "type" => "json", "require_user_level" => User::LEVEL_TESTER_MANAGER],
 
 
 	// Test Results
 	// Test Results
 	"/^account\/test-results$/" => ["controller" => "test_results_customer_list"],
 	"/^account\/test-results$/" => ["controller" => "test_results_customer_list"],

+ 7 - 2
views/locations.php

@@ -43,7 +43,12 @@ var customerId = <?=$customer->id?>;
 						Actions
 						Actions
 					</button>
 					</button>
 					<div class="dropdown-menu dropdown-menu-right" aria-labelledby="location-actions-button">
 					<div class="dropdown-menu dropdown-menu-right" aria-labelledby="location-actions-button">
-						<button id="create-project-button" class="dropdown-item" disabled>Create Project...</button>
+						<button id="create-project-button" class="dropdown-item" disabled>
+							<?=htmlspecialchars(__("Create Project"))?>
+						</button>
+						<button id="add-locations-to-project-button" class="dropdown-item" data-toggle="modal" data-target="#add-locations-to-project-modal" disabled>
+							<?=htmlspecialchars(__("Add to Project"))?>
+						</button>
 						<div class="dropdown-divider"></div>
 						<div class="dropdown-divider"></div>
 						<a id="export-csv-button" class="dropdown-item" href="/account/locations?customer_id=<?=$customer->id?>&amp;action=export" data-action="export">Export CSV</a>
 						<a id="export-csv-button" class="dropdown-item" href="/account/locations?customer_id=<?=$customer->id?>&amp;action=export" data-action="export">Export CSV</a>
 						<a id="export-kml-button" class="dropdown-item" href="/account/locations?customer_id=<?=$customer->id?>&amp;action=export-kml" data-action="export-kml">Export KML</a>
 						<a id="export-kml-button" class="dropdown-item" href="/account/locations?customer_id=<?=$customer->id?>&amp;action=export-kml" data-action="export-kml">Export KML</a>
@@ -117,7 +122,7 @@ var customerId = <?=$customer->id?>;
 	<?php
 	<?php
 	if(!empty($locations)) {
 	if(!empty($locations)) {
 		?>
 		?>
-		<div class="mb-3" id="map" data-maps data-lat="0" data-lng="0" data-zoom="7" style="height: 320px;" data-callback="initialiseLocations">
+		<div class="mb-3" id="map" data-maps data-lat="0" data-lng="0" data-zoom="7" style="height: 480px;" data-callback="initialiseLocations">
 		</div>
 		</div>
 		<?php
 		<?php
 	}
 	}

+ 42 - 0
views/modal_add_locations_to_project.php

@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * @var Customer $customer
+ */
+
+?>
+<div class="modal fade" role="dialog" tabindex="-1" id="add-locations-to-project-modal">
+	<div class="modal-dialog modal-lg">
+		<form class="modal-content" method="post">
+			<div class="modal-header">
+				<strong><?=htmlspecialchars(__("Add Locations To Project"))?></strong>
+			</div>
+			<div class="modal-body">
+				<div class="form-group">
+					<label for="add-locations-to-project-project-id">
+						<?=htmlspecialchars(__("Project"))?>
+					</label>
+					<select class="form-control" name="project_id" id="add-locations-to-project-project-id" required>
+						<option value=""><?=htmlspecialchars(__("Select Project"))?></option>
+						<?php
+						foreach($customer->getProjects() as $project) {
+							?>
+							<option value="<?=$project->id?>">
+								<?=htmlspecialchars($project->name)?>
+							</option>
+							<?php
+						}
+						?>
+					</select>
+				</div>
+			</div>
+			<div class="modal-footer">
+				<input type="hidden" name="submit" value="<?=htmlspecialchars($_SESSION["csrf_token"])?>" />
+
+				<button type="submit" class="btn btn-success">
+					<?= htmlspecialchars(__("Add Locations")) ?>
+				</button>
+			</div>
+		</form>
+	</div>
+</div>