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

Added basic Map Overview page.

Alan Lam 8 лет назад
Родитель
Сommit
53a67ccef0

+ 1 - 0
.gitignore

@@ -4,3 +4,4 @@ node_modules/
 vendor/
 reference/
 logs/
+tasks/PraesepeVenues.csv

+ 1 - 0
controllers/account.php

@@ -8,6 +8,7 @@ $links = [];
 
 if(Core::$user->level >= User::LEVEL_ADMIN) {
 	$links["/account/customers"] = "Customers";
+	$links["/account/map-overview"] = "Map Overview";
 	$links["/account/users"] = (Core::$user->level == User::LEVEL_SUPERADMIN ? "Users" : "Testers");
 }
 

+ 25 - 0
controllers/map_overview.php

@@ -0,0 +1,25 @@
+<?php
+
+$page_title = "Map Overview";
+
+Core::addScript("public/js/app-map-overview.js");
+
+Core::addBreadcrumb("/account", "Account");
+Core::addBreadcrumb("/account/map-overview", $page_title);
+
+$today = gmdate("Y-m-d") . " 00:00:00";
+
+$projects = Project::query([
+	"dts_deadline" => (new Constraint("MORE_THAN_OR_EQUAL"))->setValues($today)
+]);
+
+Customer::load(array_column($projects, "customer_id"));
+
+$customers = Customer::query([], ["sort" => "name"]);
+
+
+View::includeOnce("header", compact("page_title"));
+View::include("map_overview", compact("customers", "projects"));
+View::includeOnce("footer");
+
+?>

+ 148 - 0
controllers/map_overview_ajax.php

@@ -0,0 +1,148 @@
+<?php
+
+JSON::header();
+
+$submit = http::value("submit");
+
+if($submit) {
+	try {
+
+		Core::validateCSRFToken($submit);
+
+		switch($action) {
+
+			// Gets all locations for a customer
+			case "get-customer-locations": {
+
+				$customer_id = http::value("customer_id", "int");
+				$customer = Customer::get($customer_id);
+				if(!$customer) throw new Exception("Customer not found.");
+
+				$locations = $customer->getLocations();
+
+				$filename = str_replace("-", "_", Util::slug($customer->name));
+
+				$icon = null;
+				if(file_exists("public/img/markers/$filename.png")) {
+					$icon = "/img/markers/$filename.png";
+				}
+
+				$output = [];
+
+				foreach($locations as $location) {
+					$output[] = [
+						"id" => (int) $location->id,
+						"internalId" => $location->internal_id,
+						"name" => $location->name,
+						"latitude" => (float) $location->latitude,
+						"longitude" => (float) $location->longitude,
+						"icon" => $icon
+					];
+				}
+
+				JSON::success([
+					"locations" => $output
+				]);
+
+				break;
+
+			}
+
+			// Gets all locations for a project
+			case "get-project-locations": {
+
+				$project_id = http::value("project_id", "int");
+				$project = Project::get($project_id);
+				if(!$project) throw new Exception("Project not found.");
+
+				$customer = $project->getCustomer();
+				$tests = $project->getTests();
+
+				$locations = Location::load(array_column($tests, "location_id"));
+
+				$location_tests = [];
+
+				foreach($tests as $test) {
+					if($test->status == Test::STATUS_CANCELLED) continue;
+					$location_tests[$test->location_id] = $test;
+				}
+
+				$output = [];
+
+				foreach($locations as $location) {
+
+					$test = $location_tests[$location->id] ?? false;
+
+					$test_id = null;
+					$status = 0;
+					$status_text = "";
+
+					$pass = 0;
+					$pass_class = "";
+					$pass_text = "";
+					$pass_icon = "";
+
+					$class = "secondary";
+					if($test) {
+						$test_id = $test->id;
+						$class = $test->getStatusClass();
+						$status = $test->status;
+						$status_text = $test->getStatusText();
+						$pass = $test->pass;
+						$pass_class = $test->getPassClass();
+						$pass_text = $test->getPassText();
+						$pass_icon = $test->getPassIcon();
+					}
+
+					$filename = str_replace("-", "_", Util::slug($customer->name));
+
+					$icon = null;
+					if(file_exists("public/img/markers/$filename.png")) {
+						$icon = "/img/markers/$filename.png";
+					}
+
+					$output[] = (object) [
+						"id" => (int) $location->id,
+						"internalId" => $location->internal_id,
+						"testId" => (int) $test_id,
+						"name" => $location->name,
+						"address" => $location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]),
+						"address1" => $location->address_1,
+						"status" => $status,
+						"statusText" => $status_text,
+						"pass" => (int) $pass,
+						"passClass" => $pass_class,
+						"passText" => $pass_text,
+						"passIcon" => $pass_icon,
+						"class" => $class,
+						"latitude" => (float) $location->latitude,
+						"longitude" => (float) $location->longitude,
+						"icon" => $icon
+					];
+				}
+
+				usort($output, function($a, $b) {
+					return $a->status <=> $b->status;
+				});
+
+				JSON::success([
+					"locations" => $output
+				]);
+
+				break;
+			}
+
+
+			default:
+				throw new Exception("Invalid request.");
+		}
+
+	}
+	catch(Exception $e) {
+		JSON::fail($e->getMessage());
+	}
+}
+
+JSON::fail("Invalid request.");
+
+?>

BIN
public/img/markers/bacta.png


BIN
public/img/markers/praesepe.png


+ 136 - 0
public/js/app-map-overview.js

@@ -0,0 +1,136 @@
+var markerGroups = [];
+
+$(function() {
+
+	$("a[data-customer-id]").click(function(event) {
+		let customerId = $(this).data("customer-id");
+
+		let key = "customer-" + customerId;
+		if(typeof markerGroups[key] !== "undefined") {
+			for(let i in markerGroups[key]) {
+				let m = markerGroups[key][i];
+				m.setMap(null);
+			}
+		}
+
+		let selected = $(this).data("selected");
+		if(selected) {
+			$(this).data("selected", false);
+			$(this).find(".badge").removeClass("badge-success").text("");
+		}
+		else {
+			loadCustomerLocations(customerId);
+		}
+
+		return false;
+	});
+
+	$("a[data-project-id]").click(function(event) {
+		let projectId = $(this).data("project-id");
+
+		let key = "project-" + projectId;
+		if(typeof markerGroups[key] !== "undefined") {
+			for(let i in markerGroups[key]) {
+				let m = markerGroups[key][i];
+				m.setMap(null);
+			}
+		}
+
+		let selected = $(this).data("selected");
+		if(selected) {
+			$(this).data("selected", false);
+			$(this).find(".badge").removeClass("badge-success").text("");
+		}
+		else {
+			loadProjectLocations(projectId);
+		}
+
+		return false;
+	});
+
+});
+
+function loadCustomerLocations(customerId) {
+
+	$.get("/account/map-overview/get-customer-locations", {
+		customer_id: customerId,
+		submit: $("#submit").val()
+	}, function(response) {
+		if(!response.success) return false;
+
+		let map = maps["map"];
+		let key = "customer-" + customerId;
+
+		markerGroups[key] = [];
+
+		for(let i in response.locations) {
+
+			let l = response.locations[i];
+
+			let position = {
+				lat: l.latitude,
+				lng: l.longitude
+			};
+
+			var options = {
+				position: position,
+				title: l.name,
+				map: map
+			};
+
+			if(l.icon) options.icon = l.icon;
+
+			let marker = new google.maps.Marker(options);
+
+			markerGroups[key].push(marker);
+
+		}
+
+		$("a[data-customer-id=" + customerId + "]").data("selected", true);
+		$("a[data-customer-id=" + customerId + "]").find(".badge").addClass("badge-success").text("Selected");
+
+	});
+
+}
+
+function loadProjectLocations(projectId) {
+
+	$.get("/account/map-overview/get-project-locations", {
+		project_id: projectId,
+		submit: $("#submit").val()
+	}, function(response) {
+		if(!response.success) return false;
+
+		let map = maps["map"];
+		let key = "project-" + projectId;
+
+		markerGroups[key] = [];
+
+		for(let i in response.locations) {
+
+			let l = response.locations[i];
+
+			let position = {
+				lat: l.latitude,
+				lng: l.longitude
+			};
+
+			var options = {
+				position: position,
+				title: l.name,
+				map: map
+			};
+
+			if(l.icon) options.icon = l.icon;
+
+			let marker = new google.maps.Marker(options);
+
+			markerGroups[key].push(marker);
+
+		}
+
+		$("a[data-project-id=" + projectId + "]").data("selected", true);
+		$("a[data-project-id=" + projectId + "]").find(".badge").addClass("badge-success").text("Selected");
+
+	});
+}

+ 4 - 0
routes.php

@@ -44,6 +44,10 @@ $routes = [
 	"/^account\/addresses\/add$/" => ["controller" => "address_add", "require_log_in" => true],
 	"/^account\/addresses\/edit\/$id_pattern$/" => ["controller" => "address_edit", "params" => ["id"], "require_log_in" => true],
 
+	// Map Overview
+	"/^account\/map-overview$/" => ["controller" => "map_overview", "require_user_level" => User::LEVEL_ADMIN],
+	"/^account\/map-overview\/(get-customer-locations|get-project-locations)$/" => ["controller" => "map_overview_ajax", "params" => ["action"], "require_user_level" => User::LEVEL_ADMIN],
+
 	// Projects
 	"/^account\/projects$/" => ["controller" => "projects", "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/projects\/add$/" => ["controller" => "project_add", "require_user_level" => User::LEVEL_ADMIN],

+ 48 - 0
views/map_overview.php

@@ -0,0 +1,48 @@
+<div class="container">
+
+	<input type="hidden" id="submit" value="<?=$_SESSION["csrf_token"]?>" />
+
+	<h2>Map Overview</h2>
+
+	<div class="row">
+		<div class="col-sm mb-3">
+			<h3>Customers</h3>
+			<div class="list-group">
+			<?php
+			foreach($customers as $customer) {
+				?>
+				<a class="list-group-item list-group-item-action d-flex justify-content-between" data-customer-id="<?=$customer->id?>" data-selected="false">
+					<div><?=htmlspecialchars($customer->name)?></div>
+					<span class="badge badge-lg"></span>
+				</a>
+				<?php
+			}
+			?>
+			</div>
+		</div>
+		<div class="col-sm mb-3">
+			<h3>Projects</h3>
+			<div class="list-group">
+			<?php
+			foreach($projects as $project) {
+				$customer = $project->getCustomer();
+				?>
+				<a class="list-group-item list-group-item-action d-flex justify-content-between" data-project-id="<?=$project->id?>" data-selected="false">
+					<div>
+						<small><strong><?=strtoupper(htmlspecialchars($customer->name))?></strong></small><br />
+						<?=htmlspecialchars($project->name)?>
+					</div>
+					<span class="badge badge-lg align-self-center"></span>
+				</a>
+				<?php
+			}
+			?>
+			</div>
+		</div>
+	</div>
+
+	<h3>Map</h3>
+
+	<div id="map" data-maps data-lat="51.3555177" data-lng="-1.1743197" data-zoom="7" style="height: 512px;"></div>
+
+</div>