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

Add date range fields and status aware markers to locations page

Alan Lam 4 лет назад
Родитель
Сommit
6fd319f296

+ 6 - 19
controllers/customer_ajax.php

@@ -1,10 +1,12 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 /**
  * @var int $id
  * @var string $action
  */
 
+use Helpers\LocationHelper;
+
 JSON::header();
 
 $customer = Customer::get($id);
@@ -27,6 +29,8 @@ switch($action) {
 		$legal_entity_name = http::value("legal_entity_name", "trim");
 		$include_archived = http::value("include_archived", "int");
 		$include_closed = http::value("include_closed", "int");
+		$min_date = http::value("min_date");
+		$max_date = http::value("max_date");
 
 		if($id) $nvp["id"] = $id;
 		if($internal_id) $nvp["internal_id"] = db::escape($internal_id);
@@ -37,24 +41,7 @@ switch($action) {
 
 		$locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
 
-		$output = [];
-
-		foreach($locations as $location) {
-			$filename = Util::slug($customer->name);
-			$icon = false;
-			if(file_exists("public/img/markers/$filename.png")) {
-				$icon = $filename . ".png";
-			}
-
-			$output[] = [
-				"id" => $location->id,
-				"name" => $location->name,
-				"icon" => $icon,
-				"latitude" => (float) $location->latitude,
-				"longitude" => (float) $location->longitude,
-				"archived" => $location->isArchived()
-			];
-		}
+		$output = LocationHelper::getMarkerDataForLocations($locations, $min_date, $max_date);
 
 		JSON::success([
 			"locations" => $output

+ 6 - 156
controllers/map_overview_ajax.php

@@ -4,6 +4,8 @@
  * @var string $action
  */
 
+use Helpers\LocationHelper;
+
 $date_format = "/^2\d{3}-[01]\d-[0-3]\d$/";
 
 JSON::header();
@@ -20,18 +22,6 @@ if($submit) {
 			// Gets all locations for a customer
 			case "get-customer-locations": {
 
-				// Initialise Results
-				$results = [];
-
-				$init_result = static function(Location $location): object {
-					return (object) [
-						"location" => $location,
-						"latestTest" => null,
-						"testerId" => null,
-						"testCount" => 0
-					];
-				};
-
 				// Input
 				$customer_id = http::value("customer_id", "int");
 
@@ -40,27 +30,19 @@ if($submit) {
 
 				// Get customer
 				$customer = Customer::get($customer_id);
-				if(!$customer) throw new RuntimeException("Customer not found.");
+				if(!$customer) {
+					throw new RuntimeException("Customer not found.");
+				}
 
 				// Get locations
 				$locations = $customer->getLocations(["archived" => 0]);
 
-				foreach($locations as $location) {
-					$results[$location->id] = $init_result($location);
-				}
-
-				// Get tests
-				$nvp = [
-					"status" => Constraint::notEquals(Test::STATUS_CANCELLED),
-					"location_id" => array_column($locations, "id")
-				];
+				$output = LocationHelper::getMarkerDataForLocations($locations, $min_date, $max_date);
 
 				if($min_date && $max_date) {
 					if(preg_match($date_format, $min_date) && preg_match($date_format, $max_date)) {
 						$min_dts = $min_date;
 						$max_dts = $max_date;
-
-						$nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
 					}
 					else {
 						throw new RuntimeException("Invalid dates submitted.");
@@ -68,137 +50,6 @@ if($submit) {
 				}
 				else {
 					[$min_dts, $max_dts] = $customer->getYearCycleDates();
-					$nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
-				}
-
-				$tests = Test::query($nvp);
-
-				foreach($tests as $test) {
-
-					$result = $results[$test->location_id];
-					$result->testCount++;
-
-					$assign_test = false;
-					if($result->latestTest === null) {
-						$assign_test = true;
-					}
-					else {
-						/** @var Test $existing */
-						$existing = $result->latestTest;
-
-						$unix_start_existing = ($existing->dts_start === null) ? 0 : strtotime($existing->dts_start . " UTC");
-						$unix_start_current = ($test->dts_start === null) ? 0 : strtotime($test->dts_start . " UTC");
-
-						if($unix_start_current > $unix_start_existing) {
-							$assign_test = true;
-						}
-					}
-
-					if($assign_test) {
-						$result->latestTest = $test;
-						$result->testerId = $test->tester_id;
-					}
-				}
-
-				$tester_ids = array_column($results, "testerId");
-				User::load($tester_ids);
-
-				$output = [];
-
-				foreach($results as $result) {
-
-					/** @var Location $location */
-					$location = $result->location;
-
-					/** @var Test $test */
-					$test = $result->latestTest;
-
-					$customer_slug = Util::slug($customer->name, "_");
-
-					/** @var User $tester */
-					$tester = null;
-					$formatted_start_dts = null;
-					$formatted_due_dts = null;
-
-					$icon = null;
-					$icon_file = "{$customer_slug}_none.png";
-
-					$past_due = false;
-
-					if($test) {
-
-						$tester = $test->getTester();
-
-						if($test->dts_start !== null) {
-							$unix_start = strtotime($test->dts_start);
-							$formatted_start_dts = date("d/m/Y H:i", $unix_start);
-						}
-
-						if($test->isPending()) {
-							$icon_file = "{$customer_slug}_pending.png";
-
-							$unix_due = null;
-							if($test->dts_due !== null) {
-								$unix_due = strtotime($test->dts_due);
-								$formatted_due_dts = date("d/m/Y H:i", $unix_due);
-							}
-
-							if($tester && $unix_due && time() > $unix_due) {
-								$icon_file = "{$customer_slug}_past_due.png";
-								$past_due = true;
-							}
-						}
-						else if($test->pass) {
-							$icon_file = "{$customer_slug}_pass.png";
-						}
-						else {
-							$icon_file = "{$customer_slug}_fail.png";
-						}
-					}
-
-					if($location->do_not_test) {
-						$icon_file = "do_not_test.png";
-					}
-					if($location->closed) {
-						$icon_file = "closed.png";
-					}
-
-					if(file_exists("public/img/markers/$icon_file")) {
-						$icon = $icon_file;
-					}
-
-					$data = [
-						"id" => (int) $location->id,
-						"internalId" => $location->internal_id,
-						"name" => $location->name,
-						"latitude" => (float) $location->latitude,
-						"longitude" => (float) $location->longitude,
-						"icon" => $icon,
-						"test" => null,
-						"doNotTest" => (bool) $location->do_not_test,
-						"closed" => (bool) $location->closed,
-						"archived" => $location->isArchived(),
-						"testCount" => $result->testCount
-					];
-
-					if($test) {
-						$data["test"] = [
-							"id" => $test->id,
-							"formattedStartDts" => $formatted_start_dts,
-							"formattedDueDts" => $formatted_due_dts,
-							"tester" => null,
-							"pastDue" => $past_due,
-						];
-
-						if($tester) {
-							$data["test"]["tester"] = [
-								"id" => $tester->id,
-								"name" => $tester->getDisplayName(),
-							];
-						}
-					}
-
-					$output[] = $data;
 				}
 
 				JSON::success([
@@ -207,7 +58,6 @@ if($submit) {
 				]);
 
 				break;
-
 			}
 
 			// Gets all locations for a project

+ 201 - 0
engine/helpers/location_helper.php

@@ -0,0 +1,201 @@
+<?php
+
+namespace Helpers;
+
+use Constraint;
+use Exception;
+use Location;
+use RuntimeException;
+use Test;
+use User;
+use Util;
+
+class LocationHelper {
+
+	public const DATE_FORMAT = "/^2\d{3}-[01]\d-[0-3]\d$/";
+
+	/**
+	 * @param Location[] $locations
+	 * @param string|null $min_date
+	 * @param string|null $max_date
+	 * @return array
+	 * @throws Exception
+	 */
+	public static function getMarkerDataForLocations(array $locations, ?string $min_date, ?string $max_date): array {
+
+		if(empty($locations)) {
+			return [];
+		}
+
+		$customer = $locations[0]->getCustomer();
+
+		$init_result = static function(Location $location): object {
+			return (object) [
+				"location" => $location,
+				"latestTest" => null,
+				"testerId" => null,
+				"testCount" => 0
+			];
+		};
+
+		$results = [];
+		foreach($locations as $location) {
+			$results[$location->id] = $init_result($location);
+		}
+
+		// Get tests
+		$nvp = [
+			"status" => Constraint::notEquals(Test::STATUS_CANCELLED),
+			"location_id" => array_column($locations, "id")
+		];
+
+		if($min_date && $max_date) {
+			if(preg_match(self::DATE_FORMAT, $min_date) && preg_match(self::DATE_FORMAT, $max_date)) {
+				$min_dts = $min_date;
+				$max_dts = $max_date;
+
+				$nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
+			}
+			else {
+				throw new RuntimeException("Invalid dates submitted.");
+			}
+		}
+		else {
+			[$min_dts, $max_dts] = $customer->getYearCycleDates();
+			$nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
+		}
+
+		$tests = Test::query($nvp);
+
+		foreach($tests as $test) {
+
+			$result = $results[$test->location_id];
+			$result->testCount++;
+
+			$assign_test = false;
+			if($result->latestTest === null) {
+				$assign_test = true;
+			}
+			else {
+				/** @var Test $existing */
+				$existing = $result->latestTest;
+
+				$unix_start_existing = ($existing->dts_start === null) ? 0 : strtotime($existing->dts_start . " UTC");
+				$unix_start_current = ($test->dts_start === null) ? 0 : strtotime($test->dts_start . " UTC");
+
+				if($unix_start_current > $unix_start_existing) {
+					$assign_test = true;
+				}
+			}
+
+			if($assign_test) {
+				$result->latestTest = $test;
+				$result->testerId = $test->tester_id;
+			}
+		}
+
+		$tester_ids = array_column($results, "testerId");
+		User::load($tester_ids);
+
+		$output = [];
+
+		foreach($results as $result) {
+
+			/** @var Location $location */
+			$location = $result->location;
+
+			/** @var Test $test */
+			$test = $result->latestTest;
+
+			$customer_slug = Util::slug($customer->name, "_");
+
+			/** @var User $tester */
+			$tester = null;
+			$formatted_start_dts = null;
+			$formatted_due_dts = null;
+
+			$icon = null;
+			$icon_file = "{$customer_slug}_none.png";
+
+			$past_due = false;
+
+			if($test) {
+
+				$tester = $test->getTester();
+
+				if($test->dts_start !== null) {
+					$unix_start = strtotime($test->dts_start);
+					$formatted_start_dts = date("d/m/Y H:i", $unix_start);
+				}
+
+				if($test->isPending()) {
+					$icon_file = "{$customer_slug}_pending.png";
+
+					$unix_due = null;
+					if($test->dts_due !== null) {
+						$unix_due = strtotime($test->dts_due);
+						$formatted_due_dts = date("d/m/Y H:i", $unix_due);
+					}
+
+					if($tester && $unix_due && time() > $unix_due) {
+						$icon_file = "{$customer_slug}_past_due.png";
+						$past_due = true;
+					}
+				}
+				else if($test->pass) {
+					$icon_file = "{$customer_slug}_pass.png";
+				}
+				else {
+					$icon_file = "{$customer_slug}_fail.png";
+				}
+			}
+
+			if($location->do_not_test) {
+				$icon_file = "do_not_test.png";
+			}
+			if($location->closed) {
+				$icon_file = "closed.png";
+			}
+
+			if(file_exists("public/img/markers/$icon_file")) {
+				$icon = $icon_file;
+			}
+
+			$data = [
+				"id" => (int) $location->id,
+				"internalId" => $location->internal_id,
+				"name" => $location->name,
+				"latitude" => (float) $location->latitude,
+				"longitude" => (float) $location->longitude,
+				"icon" => $icon,
+				"test" => null,
+				"doNotTest" => (bool) $location->do_not_test,
+				"closed" => (bool) $location->closed,
+				"archived" => $location->isArchived(),
+				"testCount" => $result->testCount
+			];
+
+			if($test) {
+				$data["test"] = [
+					"id" => $test->id,
+					"formattedStartDts" => $formatted_start_dts,
+					"formattedDueDts" => $formatted_due_dts,
+					"tester" => null,
+					"pastDue" => $past_due,
+				];
+
+				if($tester) {
+					$data["test"]["tester"] = [
+						"id" => $tester->id,
+						"name" => $tester->getDisplayName(),
+					];
+				}
+			}
+
+			$output[] = $data;
+		}
+
+		return $output;
+	}
+
+}

+ 2 - 2
engine/location.php

@@ -75,10 +75,10 @@ class Location extends Model {
 	}
 
 	/**
-	 * @return Customer|null
+	 * @return Customer
 	 * @throws Exception
 	 */
-	public function getCustomer(): ?Customer {
+	public function getCustomer(): Customer {
 		return Customer::get($this->customer_id);
 	}
 

+ 74 - 11
public/js/app-locations.js

@@ -22,7 +22,9 @@ function initialiseLocations() {
 		name: $("#name").val(),
 		legal_entity_name: $("#legal-entity-name").val(),
 		include_archived: includeArchived,
-		include_closed: includeClosed
+		include_closed: includeClosed,
+		min_date: $("#min-date").val(),
+		max_date: $("#max-date").val(),
 	}, function(response) {
 		if(!response.success) return false;
 		if(!response.locations) return false;
@@ -44,9 +46,12 @@ function initialiseLocations() {
 				title: location.name
 			};
 
-			if(location.icon) {
+			if(!location.icon) {
 				options.icon = "/img/markers/building-red.png";
 			}
+			else {
+				options.icon = "/img/markers/" + location.icon;
+			}
 
 			bounds.extend(position);
 
@@ -54,6 +59,7 @@ function initialiseLocations() {
 
 			let marker = new google.maps.Marker(options);
 			marker.location = location;
+			marker.originalIcon = options.icon;
 
 			marker.addListener("click", function() {
 				displayInfoWindow(this);
@@ -68,6 +74,7 @@ function initialiseLocations() {
 	});
 
 	$(document).on("click", "a[data-export-location-id]", function(event) {
+		event.preventDefault();
 
 		let locationId = parseInt($(event.currentTarget).attr("data-export-location-id"));
 		let index = exportIds.indexOf(locationId);
@@ -83,7 +90,7 @@ function initialiseLocations() {
 		else {
 			exportIds.splice(index, 1);
 			marker.setOptions({
-				icon: "/img/markers/building-red.png"
+				icon: marker.originalIcon
 			});
 		}
 
@@ -146,16 +153,69 @@ function initialiseLocations() {
 
 function displayInfoWindow(marker) {
 
+	const escapeText = (text) => {
+		const span = document.createElement("span");
+		span.innerText = text;
+		return span.innerHTML;
+	};
+
 	const locationId = parseInt(marker.location.id);
 	const index = exportIds.indexOf(locationId);
-	const archived = marker.location.archived;
+	const location = marker.location;
 
-	let badge = "";
+	const badges = [];
+	const lines = [];
 
-	if(archived) {
-		badge = `<span class="badge badge-secondary">Archived</span>`;
+	if(location.doNotTest) {
+		badges.push(`<span class="badge badge-warning">Do Not Test</span>`);
 	}
 
+	if(location.archived) {
+		badges.push(`<span class="badge badge-secondary">Archived</span>`);
+	}
+
+	if(location.closed) {
+		badges.push(`<span class="badge badge-danger">Closed</span>`);
+	}
+
+	lines.push({title: "Internal ID", text: location.internalId});
+	lines.push({title: "Location ID", text: location.id});
+
+	if(location.test) {
+		lines.push({divider: true});
+		lines.push({title: "Test ID", html: `<a href="/account/tests/${location.test.id}" target="_blank">${location.test.id}</a>`});
+		if(location.test.formattedStartDts) {
+			lines.push({title: "Date/Time", text: location.test.formattedStartDts});
+		}
+		if(location.test.tester) {
+			const { id, name } = location.test.tester;
+			lines.push({title: "Tester ID", text: id});
+			lines.push({title: "Tester Name", html: `<a href="/account/users/${id}" target="_blank">${name}</a>`});
+		}
+		if(location.test.pastDue) {
+			badges.push(`<span class="badge badge-danger">Past Die</span>`);
+		}
+		if(location.test.formattedDueDts) {
+			lines.push({title: "Due", text: location.test.formattedDueDts});
+		}
+	}
+
+	let linesHtml = "";
+	lines.forEach(line => {
+		if(line.divider) {
+			linesHtml += "<hr />\n";
+		}
+		if(line.title) {
+			const titleHtml = escapeText(line.title);
+			let contentHtml = line.html;
+			if(line.text) {
+				contentHtml = escapeText(line.text);
+			}
+
+			linesHtml += `<div class="small"><strong>${titleHtml}:</strong> ${contentHtml}</div>`;
+		}
+	});
+
 	let exportText = "Select";
 	let exportClass = "btn-primary";
 
@@ -165,10 +225,13 @@ function displayInfoWindow(marker) {
 	}
 
 	const content = `<div class="container-fluid" style="font-size: 1rem; font-weight: normal; min-width: 16rem;">
-	<p>
-		${marker.location.name}
-		${badge}
-	</p>
+	<div class="mb-1">
+		${marker.location.name}<br />
+		${badges.join(" ")}
+	</div>
+	<hr />
+	${linesHtml}
+	<hr />
 	<div class="form-row">
 		<div class="col">
 			<a class="btn btn-block ${exportClass}" href="#" data-export-location-id="${marker.location.id}">${exportText}</a>

+ 16 - 0
views/locations.php

@@ -108,6 +108,22 @@ var customerId = <?=$customer->id?>;
 							</div>
 						</div>
 					</div>
+					<div class="col-lg-6">
+						<div class="row form-group">
+							<label class="col-sm-4 col-form-label" for="min-date">Min. Test Date</label>
+							<div class="col-sm">
+								<input class="form-control" type="date" name="min_date" id="min-date" placeholder="Select date" value="<?=htmlspecialchars(http::value("min_date"))?>" />
+							</div>
+						</div>
+					</div>
+					<div class="col-lg-6">
+						<div class="row form-group">
+							<label class="col-sm-4 col-form-label" for="max-date">Max. Test Date</label>
+							<div class="col-sm">
+								<input class="form-control" type="date" name="max_date" id="max-date" placeholder="Select date" value="<?=htmlspecialchars(http::value("max_date"))?>" />
+							</div>
+						</div>
+					</div>
 				</div>
 				<div class="row">
 					<div class="col-lg">