فهرست منبع

Add support for archiving locations

Alan Lam 6 سال پیش
والد
کامیت
675b1905b9

+ 7 - 1
controllers/customer_ajax.php

@@ -1,5 +1,10 @@
 <?php
 
+/**
+ * @var int $id
+ * @var string $action
+ */
+
 JSON::header();
 
 $customer = Customer::get($id);
@@ -36,7 +41,8 @@ switch($action) {
 				"name" => $location->name,
 				"icon" => $icon,
 				"latitude" => (float) $location->latitude,
-				"longitude" => (float) $location->longitude
+				"longitude" => (float) $location->longitude,
+				"archived" => $location->isArchived()
 			];
 		}
 

+ 4 - 1
controllers/location_add.php

@@ -42,6 +42,10 @@ if($submit) {
 		$location->area_manager = http::value("area_manager", "trim");
 		$location->area_manager_email = http::value("area_manager_email", "email");
 
+		$location->do_not_test = http::value("do_not_test", "int") ? 1 : 0;
+		$location->closed = http::value("closed", "int") ? 1 : 0;
+		$location->archived = http::value("archived", "int") ? 1 : 0;
+
 		if(empty($location->name)) $form_exception->addError("Name is required.");
 		if(empty($location->address_1)) $form_exception->addError("Location line 1 is required.");
 		if(empty($location->city)) $form_exception->addError("City is required.");
@@ -91,4 +95,3 @@ View::includeOnce("header", compact("page_title"));
 View::include("location_form", compact("location"));
 View::includeOnce("footer");
 
-?>

+ 1 - 0
controllers/location_edit.php

@@ -43,6 +43,7 @@ if($submit) {
 
 		$location->do_not_test = http::value("do_not_test", "int") ? 1 : 0;
 		$location->closed = http::value("closed", "int") ? 1 : 0;
+		$location->archived = http::value("archived", "int") ? 1 : 0;
 
 		if(empty($location->name)) $form_exception->addError("Name is required.");
 		if(empty($location->address_1)) $form_exception->addError("Location line 1 is required.");

+ 6 - 1
controllers/map_overview_ajax.php

@@ -1,5 +1,9 @@
 <?php /** @noinspection PhpUnhandledExceptionInspection */
 
+/**
+ * @var string $action
+ */
+
 $date_format = "/^2[0-9]{3}\-[0-1][0-9]\-[0-3][0-9]$/";
 
 JSON::header();
@@ -27,7 +31,7 @@ if($submit) {
 				if(!$customer) throw new Exception("Customer not found.");
 
 				// Get locations
-				$locations = $customer->getLocations();
+				$locations = $customer->getLocations(["archived" => 0]);
 
 				// Get tests
 				$nvp = [
@@ -142,6 +146,7 @@ if($submit) {
 						"test" => null,
 						"doNotTest" => (bool) $location->do_not_test,
 						"closed" => (bool) $location->closed,
+						"archived" => $location->isArchived()
 					];
 
 					if($test) {

+ 1 - 1
controllers/project_add.php

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

+ 12 - 0
controllers/project_edit.php

@@ -1,5 +1,9 @@
 <?php
 
+/**
+ * @var int $id
+ */
+
 $project = Project::get($id);
 if(!$project) throw new Exception("Project not found.");
 
@@ -129,7 +133,15 @@ if($submit) {
 	}
 }
 
+$project_locations = $project->getLocations();
+$project_location_ids = array_column($project_locations, "id");
+
 $locations = $customer->getLocations();
+$locations = array_filter($locations, function(Location $location) use ($project_location_ids) {
+	if(in_array($location->id, $project_location_ids)) return true;
+	if($location->closed || $location->isArchived()) return false;
+	return true;
+});
 
 View::includeOnce("header", compact("page_title"));
 View::include("project_form", compact("customer", "project", "templates", "locations", "location_ids"));

+ 11 - 0
db/migrations/20200812002817_add_archived_field_to_location_table.php

@@ -0,0 +1,11 @@
+<?php
+
+use Phinx\Migration\AbstractMigration;
+
+class AddArchivedFieldToLocationTable extends AbstractMigration {
+	public function change() {
+		$this->table("location")
+			->addColumn("archived", "boolean", ["signed" => false, "default" => "0", "after" => "closed"])
+			->update();
+	}
+}

+ 6 - 1
engine/location.php

@@ -27,6 +27,7 @@
  * @property string $operations_manager_name
  * @property bool $do_not_test
  * @property bool $closed
+ * @property bool $archived
  * @property double $latitude
  * @property double $longitude
  * @property string $dts_verified
@@ -36,7 +37,7 @@ class Location extends Model {
 
 	protected static $table = "location";
 	protected static $id_field = "id";
-	protected static $fillable_fields = ["customer_id", "name", "legal_entity_name", "address_1", "address_2", "city", "county", "postal_code", "state", "country", "email", "phone", "opening_hours", "shop_left", "shop_right", "brand", "region", "region_name", "internal_id", "area_manager", "area_manager_name", "area_manager_email", "operations_manager_name", "do_not_test", "closed", "latitude", "longitude", "dts_verified"];
+	protected static $fillable_fields = ["customer_id", "name", "legal_entity_name", "address_1", "address_2", "city", "county", "postal_code", "state", "country", "email", "phone", "opening_hours", "shop_left", "shop_right", "brand", "region", "region_name", "internal_id", "area_manager", "area_manager_name", "area_manager_email", "operations_manager_name", "do_not_test", "closed", "archived", "latitude", "longitude", "dts_verified"];
 
 	// Object methods
 
@@ -197,4 +198,8 @@ class Location extends Model {
         return $modified;
     }
 
+    public function isArchived(): bool {
+    	return (bool) $this->archived;
+	}
+
 }

+ 26 - 17
public/js/app-locations.js

@@ -9,7 +9,7 @@ function initialiseLocations() {
 		infoWindow = new google.maps.InfoWindow();
 	}
 
-	$.get("/api/customers/" + customerId + "/get-locations", {
+	$.get(`/api/customers/${customerId}/get-locations`, {
 		id: $("#id").val(),
 		internal_id: $("#internal-id").val(),
 		name: $("#name").val()
@@ -21,11 +21,8 @@ function initialiseLocations() {
 
 		let bounds = new google.maps.LatLngBounds;
 
-		for(let i in response.locations) {
+		response.locations.forEach(location => {
 
-			if(!response.locations.hasOwnProperty(i)) continue;
-
-			let location = response.locations[i];
 			let position = {
 				lat: location.latitude,
 				lng: location.longitude
@@ -54,7 +51,7 @@ function initialiseLocations() {
 
 			markerMap[key] = marker;
 
-		}
+		});
 
 		map.fitBounds(bounds);
 
@@ -121,8 +118,15 @@ function initialiseLocations() {
 
 function displayInfoWindow(marker) {
 
-	let locationId = parseInt(marker.location.id);
-	let index = exportIds.indexOf(locationId);
+	const locationId = parseInt(marker.location.id);
+	const index = exportIds.indexOf(locationId);
+	const archived = marker.location.archived;
+
+	let badge = "";
+
+	if(archived) {
+		badge = `<span class="badge badge-secondary">Archived</span>`;
+	}
 
 	let exportText = "Select";
 	let exportClass = "btn-primary";
@@ -132,15 +136,20 @@ function displayInfoWindow(marker) {
 		exportClass = "btn-success";
 	}
 
-	let content = "<div class=\"container-fluid\" style=\"font-size: 1rem; font-weight: normal;\">";
-	content += "<p>" + marker.location.name + "</p>\n";
-
-	content += "<div class=\"row\">";
-
-	content += "<div class=\"col\"><a class=\"btn btn-block " + exportClass + "\" href=\"#\" data-export-location-id=\"" + marker.location.id + "\">" + exportText + "</a></div>";
-	content += "<div class=\"col\"><a class=\"btn btn-block btn-primary\" href=\"/account/locations/" + marker.location.id + "\" target=\"_blank\">View</a></div>";
-	content += "</div>";
-	content += "</div>";
+	const content = `<div class="container-fluid" style="font-size: 1rem; font-weight: normal; min-width: 16rem;">
+	<p>
+		${marker.location.name}
+		${badge}
+	</p>
+	<div class="form-row">
+		<div class="col">
+			<a class="btn btn-block ${exportClass}" href="#" data-export-location-id="${marker.location.id}">${exportText}</a>
+		</div>
+		<div class="col">
+			<a class="btn btn-block btn-primary" href="/account/locations/${marker.location.id}" target="_blank">View</a>
+		</div>
+	</div>
+</div>`;
 
 	infoWindow.setContent(content);
 	infoWindow.open(map, marker);

+ 3 - 3
public/js/app-map-overview.js

@@ -5,7 +5,7 @@ var infoWindow = false;
 $(function() {
 
 	// On customer select
-	$("a[data-customer-id]").click(function() {
+	$("a[data-customer-id]").on("click", function() {
 		let customerId = $(this).data("customer-id");
 
 		let key = "customer-" + customerId;
@@ -32,7 +32,7 @@ $(function() {
 	});
 
 	// On project select
-	$("a[data-project-id]").click(function() {
+	$("a[data-project-id]").on("click", function() {
 		let projectId = $(this).data("project-id");
 
 		let key = "project-" + projectId;
@@ -58,7 +58,7 @@ $(function() {
 	});
 
 	// On all addresses select
-	$("a[data-all-addresses]").click(function() {
+	$("a[data-all-addresses]").on("click", function() {
 
 		let key = "all-addresses";
 		if(typeof markerGroups[key] !== "undefined") {

+ 10 - 1
views/location.php

@@ -1,5 +1,9 @@
 <?php
 
+/**
+ * @var Location $location
+ */
+
 $tabs = [];
 $tabs[] = ["details", __("Details")];
 if(Core::$user->isAdmin()) $tabs[] = ["photos", __("Photos")];
@@ -37,11 +41,16 @@ if($venue_types) $details["Types"] = $type_names_csv;
 					<span class="badge badge-danger">Closed</span>
 					<?php
 				}
+				if($location->isArchived()) {
+					?>
+					<span class="badge badge-secondary">Archived</span>
+					<?php
+				}
 				?>
 			</h2>
 		</div>
 		<div class="col-auto">
-			<a class="btn btn-outline-primary" href="/account/locations/<?=$location->id?>/edit">Edit</a>
+			<a class="btn btn-primary" href="/account/locations/<?=$location->id?>/edit">Edit</a>
 		</div>
 	</div>
 

+ 33 - 15
views/location_form.php

@@ -1,4 +1,9 @@
 <?php
+
+/**
+ * @var Location $location
+ */
+
 $tabs = [
 	["details", "Details"],
 ];
@@ -12,7 +17,10 @@ $venue_type_count = count($venue_types);
 $venue_type_columns = array_chunk($venue_types, ceil($venue_type_count / 2));
 
 $selected_venue_type_ids = [];
-if($location->id) {
+
+$is_edit = !empty($location->id);
+
+if($is_edit) {
 	$location_venue_types = $location->getVenueTypes();
 	$selected_venue_type_ids = array_column($location_venue_types, "id");
 }
@@ -121,20 +129,30 @@ if($location->id) {
 
                         <div class="row mb-1">
                             <div class="col-sm offset-sm-4">
-                                <div class="form-check">
-                                    <input class="form-check-input" type="checkbox" name="do_not_test" id="do-not-test" value="1" <?=Form::checked($location->do_not_test)?> />
-                                    <label for="do-not-test">Do Not Test</label>
+                                <div class="custom-control custom-switch">
+                                    <input class="custom-control-input" type="checkbox" name="do_not_test" id="do-not-test" value="1" <?=Form::checked($location->do_not_test)?> />
+                                    <label class="custom-control-label" for="do-not-test">Do Not Test</label>
                                 </div>
                             </div>
                         </div>
 
 						<div class="row mb-1">
 							<div class="col-sm offset-sm-4">
-								<div class="form-check">
-									<input class="form-check-input" type="checkbox" name="closed" id="closed" value="1" <?=Form::checked($location->closed)?> />
-									<label for="closed">Closed</label>
+								<div class="custom-control custom-switch">
+									<input class="custom-control-input" type="checkbox" name="closed" id="closed" value="1" <?=Form::checked($location->closed)?> />
+									<label class="custom-control-label" for="closed">Closed</label>
+								</div>
+								<small class="text-muted form-text">If a location is closed, it will be unable to be selected for any new tests.</small>
+							</div>
+						</div>
+
+						<div class="row mb-1">
+							<div class="col-sm offset-sm-4">
+								<div class="custom-control custom-switch">
+									<input class="custom-control-input" type="checkbox" name="archived" id="archived" value="1" <?=Form::checked($location->archived)?> />
+									<label class="custom-control-label" for="archived">Archived</label>
 								</div>
-								<small class="text-muted form-text">By marking a location as closed, the location will be unable to be selected for any new tests.</small>
+								<small class="text-muted form-text">If a location is archived, it will be unable to be selected for any new tests and will not appear in the Map Overview.</small>
 							</div>
 						</div>
 					</div>
@@ -207,9 +225,9 @@ if($location->id) {
 							foreach($types as $type) {
 								$checked = in_array($type->id, $selected_venue_type_ids) ? "checked" : "";
 								?>
-								<div class="form-check">
-									<input class="form-check-input" id="venue-type-id-<?=$type->id?>" name="venue_type_ids[]" type="checkbox" value="<?=$type->id?>" <?=$checked?> />
-									<label class="form-check-label" for="venue-type-id-<?=$type->id?>"><?=htmlspecialchars($type->name)?></label>
+								<div class="custom-control custom-checkbox">
+									<input class="custom-control-input" id="venue-type-id-<?=$type->id?>" name="venue_type_ids[]" type="checkbox" value="<?=$type->id?>" <?=$checked?> />
+									<label class="custom-control-label" for="venue-type-id-<?=$type->id?>"><?=htmlspecialchars($type->name)?></label>
 								</div>
 								<?php
 							}
@@ -268,9 +286,9 @@ if($location->id) {
 										<div class="card">
 											<img class="card-img-top img-cover" src="/img/blank_4_3.png" alt="Photo" style="background-image: url(/account/locations/6/photos/<?=$photo->id?>)" />
 											<div class="card-body">
-												<div class="form-check">
-													<input class="form-check-input" type="checkbox" name="delete_file_ids[]" id="photo-<?=$photo->id?>" value="<?=$photo->id?>" />
-													<label class="form-check-label" for="photo-<?=$photo->id?>">Delete</label>
+												<div class="custom-control custom-checkbox">
+													<input class="custom-control-input" type="checkbox" name="delete_file_ids[]" id="photo-<?=$photo->id?>" value="<?=$photo->id?>" />
+													<label class="custom-control-label" for="photo-<?=$photo->id?>">Delete</label>
 												</div>
 											</div>
 										</div>
@@ -302,7 +320,7 @@ if($location->id) {
 			<div class="col-lg">
 				<div class="row">
 					<div class="col-sm offset-sm-4">
-						<button class="btn btn-primary" type="submit" name="submit" value="<?=$_SESSION["csrf_token"]?>">Submit</button>
+						<button class="btn btn-success" type="submit" name="submit" value="<?=$_SESSION["csrf_token"]?>">Submit</button>
 					</div>
 				</div>
 			</div>

+ 14 - 2
views/location_list.php

@@ -1,3 +1,8 @@
+<?php
+/**
+ * @var Location[] $locations
+ */
+?>
 <div class="table-responsive limit-height-2">
 	<table class="table table-sm table-sm-text table-hover">
 		<thead>
@@ -11,8 +16,10 @@
 		<tbody>
 		<?php
 		foreach($locations as $location) {
+			$class = "";
+			if($location->isArchived()) $class = "text-muted";
 			?>
-			<tr>
+			<tr class="<?=$class?>">
 				<td class="text-right pr-3">
 					<?=$location->id?>
 				</td>
@@ -24,6 +31,11 @@
 						<span class="badge badge-danger">Closed</span>
 						<?php
 					}
+					if($location->isArchived()) {
+						?>
+						<span class="badge badge-secondary"><?=htmlspecialchars(__("Archived"))?></span>
+						<?php
+					}
 					?>
 					<br />
 					<small><?=htmlspecialchars($location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]))?></small>
@@ -50,4 +62,4 @@ if(empty($locations)) {
 	</div>
 	<?php
 }
-?>
+?>

+ 3 - 4
views/map_overview.php

@@ -5,7 +5,7 @@
 	<h2>Map Overview</h2>
 
 	<div class="row">
-		<div class="col-6">
+		<div class="col-lg-6">
 			<div class="form-group">
 				<label for="location">Location</label>
 				<input type="text" class="form-control" id="location" placeholder="Type location" data-autocomplete="map" />
@@ -14,13 +14,13 @@
 				</div>
 			</div>
 		</div>
-		<div class="col-3">
+		<div class="col-6 col-lg-3">
 			<div class="form-group">
 				<label for="min-date">Min Test Date</label>
 				<input type="date" class="form-control" id="min-date" placeholder="Type date" />
 			</div>
 		</div>
-		<div class="col-3">
+		<div class="col-6 col-lg-3">
 			<div class="form-group">
 				<label for="max-date">Max Test Date</label>
 				<input type="date" class="form-control" id="max-date" placeholder="Type date" />
@@ -28,7 +28,6 @@
 		</div>
 	</div>
 
-
 	<div class="mb-4" id="map" data-maps data-lat="51.3555177" data-lng="-1.1743197" data-zoom="7" style="height: 512px;"></div>
 
 	<div class="row">

+ 0 - 3
views/project_form.php

@@ -101,9 +101,6 @@ $output_locations = function(array $locations, bool $selected) use ($location_te
 	}
 }
 
-
-
-
 ?>
 <div class="container mt-3 py-4">
 	<h2>