Преглед на файлове

Add marker info window for tester nearby locations

Alan Lam преди 8 години
родител
ревизия
63529a792f
променени са 2 файла, в които са добавени 147 реда и са изтрити 7 реда
  1. 95 4
      controllers/user_ajax.php
  2. 52 3
      public/js/app-user.js

+ 95 - 4
controllers/user_ajax.php

@@ -41,6 +41,7 @@ switch($action) {
 
 		if(!$address) throw new Exception("Cannot find address.");
 
+		// Get locations according to bounds
 		$bounds = Geolocation::getBounds($address->getGeolocation(), $max_distance);
 
 		list($min_lat, $min_lng) = $bounds->southWest;
@@ -52,32 +53,102 @@ switch($action) {
 			"closed" => 0
 		]);
 
+		// Get customers
 		$customers = Customer::load(array_column($locations, "customer_id"));
 
+		// Sort locations by distance
 		usort($locations, function($a, $b) use ($address) {
 			return
 			Geolocation::getDistance($address->getGeolocation(), $a->getGeolocation())
 			<=>
 			Geolocation::getDistance($address->getGeolocation(), $b->getGeolocation());
 		});
+		
+		// Get tests
+		$tests = Test::query([
+			"status" => (new Constraint("NOT_EQUALS"))->setValues(Test::STATUS_CANCELLED),
+			"location_id" => array_column($locations, "id")
+		]);
 
-		foreach($locations as $location) {
+		$location_test_array = [];
+		foreach($tests as $test) {
+			$exists = isset($location_test_array[$test->location_id]);
+			if(!$exists) {
+				$location_test_array[$test->location_id] = $test;
+			}
+			else {
+				$existing = $location_test_array[$test->location_id];
+
+				$unix_start_existing = is_null($existing->dts_start) ? 0 : strtotime($existing->dts_start . " UTC");
+				$unix_start_current = is_null($test->dts_start) ? 0 : strtotime($test->dts_start . " UTC");
 
+				if($unix_start_current > $unix_start_existing) {
+					$location_test_array[$test->location_id] = $test;
+				}
+			}
+		}
+		
+		// Start output
+		foreach($locations as $location) {
+			
+			// Filter by distance
 			$distance = Geolocation::getDistance($address->getGeolocation(), $location->getGeolocation());
 			if($distance > $max_distance) continue;
 
-			$customer = $location->getCustomer();
+			// Get test
+			$test = $location_test_array[$location->id] ?? false;
 
+			// Get customer
+			$customer = $location->getCustomer();
 			$customer_slug = str_replace("-", "_", Util::slug($customer->name));
 
+			// Test
+			$tester = null;
+			$formatted_start_dts = null;
+			$formatted_due_dts = null;
+			$past_due = false;
+
+			// Icon
 			$icon = null;
-			$icon_file = "{$customer_slug}.png";
+			$icon_file = "{$customer_slug}_none.png";
+			
+			// Process test	
+			if($test) {
+				$tester = $test->getTester();
+
+				if(!is_null($test->dts_start)) {
+					$unix_start = strtotime($test->dts_start);
+					$formatted_start_dts = date("d/m/Y H:i", $unix_start);
+				}
+
+				if(is_null($test->pass)) {
+					$icon_file = "{$customer_slug}_pending.png";
+
+					// Has due date
+					if(!is_null($test->dts_due)) {
+						$unix_due = strtotime($test->dts_due);
+						$formatted_due_dts = date("d/m/Y H:i", $unix_due);
+						
+						if($tester && time() > $unix_due) {
+							$icon_file = "{$customer_slug}_past_due.png";
+							$past_due = true;
+						}
+					}
+
+				}
+				else if($test->pass) {
+					$icon_file = "{$customer_slug}_pass.png";
+				}
+				else if(!$test->pass) {
+					$icon_file = "{$customer_slug}_fail.png";
+				}
+			}
 
 			if($icon_file && file_exists("public/img/markers/$icon_file")) {
 				$icon = $icon_file;
 			}
 
-			$output[] = [
+			$data = [
 				"id" => (int) $location->id,
 				"internalId" => (int) $location->internal_id,
 				"name" => $location->name,
@@ -86,7 +157,27 @@ switch($action) {
 				"latitude" => (double) $location->latitude,
 				"longitude" => (double) $location->longitude,
 				"icon" => $icon,
+				"test" => null,
 			];
+
+			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([

+ 52 - 3
public/js/app-user.js

@@ -4,6 +4,8 @@ var locationsInitialised = false;
 var addressMarkers = [];
 var locationMarkers = [];
 
+var infoWindow = false;
+
 $(function() {
 
 	$("[data-toggle=tab]").on("shown.bs.tab", function(event) {
@@ -62,10 +64,12 @@ function initialiseAddresses() {
 
 }
 
-
-
 function initialiseLocations() {
 
+	if(infoWindow == false) {
+		infoWindow = new google.maps.InfoWindow();
+	}
+
 	for(var i in locationMarkers) {
 		let marker = locationMarkers[i];
 		marker.setMap(null);
@@ -146,10 +150,55 @@ function initialiseLocations() {
 
 			listGroupItem.appendTo("#nearby-locations-list-group").show();
 
+			addMarkerClickListenerForLocation(marker, location);
+
 		}
 
 		map.fitBounds(bounds);
 
 	});
 
-}
+}
+
+function addMarkerClickListenerForLocation(m, l) {
+
+	let map = maps["map"];
+
+	m.addListener("click", function() {
+		let content = "<strong><a href=\"/account/locations/" + l.id + "\" target=\"_blank\">" + l.name + "</a></strong><br />";
+		if(l.internalId) {
+			content += "<div class=\"small\"><strong>Internal ID:</strong> " + l.internalId + "</div>";
+		}
+		content += "<div class=\"small\"><strong>Location ID:</strong> " + l.id + "</div>";
+
+		if(l.test) {
+
+			content += '<hr />';
+
+			content += '<div class="small"><strong>Test ID:</strong> <a href="/account/tests/' + l.test.id + '" target="_blank">' + l.test.id + '</a></div>';
+
+			if(l.test.formattedStartDts) {
+				content += '<div class="small"><strong>Date/Time:</strong> ' + l.test.formattedStartDts + '</div>';
+			}
+
+			if(l.test.tester) {
+				content += '<div class="small"><strong>Tester ID:</strong> ' + l.test.tester.id + '</div>';
+				content += '<div class="small"><strong>Tester Name:</strong> <a href="/account/users/' + l.test.tester.id + '" target="_blank">' + l.test.tester.name + '</a></div>';
+			}
+
+			let pastDueBadge = '';
+			if(l.test.pastDue) {
+				pastDueBadge = '<span class="badge badge-lg badge-primary">Past Due</span>';
+			}
+
+			if(l.test.formattedDueDts) {
+				content += '<div class="small"><strong>Due:</strong> ' + l.test.formattedDueDts + ' ' + pastDueBadge + '</div>';
+			}
+
+		}
+
+		infoWindow.setContent(content);
+		infoWindow.open(map, m);
+	});
+
+}