Alan Lam 4 лет назад
Родитель
Сommit
c507052a8d
6 измененных файлов с 191 добавлено и 0 удалено
  1. 64 0
      controllers/api.php
  2. 16 0
      controllers/map.php
  3. 68 0
      public/js/app.js
  4. 3 0
      routes/default.php
  5. 2 0
      views/header.php
  6. 38 0
      views/map.php

+ 64 - 0
controllers/api.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace App;
+
+use RuntimeException;
+
+/**
+ * @var string $action
+ */
+
+$request = Request::fromRequest();
+
+header("Content-Type: application/json");
+
+try {
+	switch($action) {
+
+		case "get-markers":
+			$customer_id = $request->int("customer_id");
+			$customer = Customer::get($customer_id);
+			if(!$customer) {
+				throw new RuntimeException(__("Customer not found."));
+			}
+
+			$markers = [];
+
+			$locations = $customer->getLocations();
+			Address::load(array_column($locations, "address_id"));
+
+			foreach($locations as $location) {
+				$address = $location->getAddress();
+				if($address->hasGeolocation()) {
+					$markers[] = [
+						"id" => (int) $location->id,
+						"name" => $location->name,
+						"latLng" => [
+							"lat" => (float) $address->latitude,
+							"lng" => (float) $address->longitude,
+						],
+						"url" => $location->getUrl(),
+					];
+				}
+			}
+
+			echo json_encode([
+				"success" => true,
+				"markers" => $markers
+			]);
+
+			break;
+
+		default:
+			throw new RuntimeException(__("Invalid action."));
+
+	}
+}
+catch(\Exception $e) {
+	http_response_code(400);
+	echo json_encode([
+		"success" => false,
+		"message" => $e->getMessage()
+	]);
+}
+

+ 16 - 0
controllers/map.php

@@ -0,0 +1,16 @@
+<?php
+
+namespace App;
+
+$core = Core::getInstance();
+
+$user = $core->getUser();
+$user->requireAdmin();
+
+$core->addBreadcrumb("/map", __("Map"));
+
+$customers = Customer::getAll();
+
+View::include("header");
+View::include("map", compact("customers"));
+View::include("footer");

+ 68 - 0
public/js/app.js

@@ -70,10 +70,78 @@ class AddressMap {
 	}
 }
 
+/* Map Overview */
+class Map {
+	constructor(element) {
+		this.element = element;
+		this.markers = [];
+
+		this.map = new google.maps.Map(this.element, {
+			center: this.latLng,
+			zoom: 5
+		});
+
+		this.$customerList = $("#customer-list");
+		this.$customerList.on("click", ".list-group-item", event => {
+			const $item = $(event.currentTarget);
+			const customerId = $item.attr("data-id");
+			const selected = $item.attr("data-selected");
+
+			if(selected === "false") {
+				// Add markers
+				$.post("/api/get-markers", {
+					customer_id: customerId
+				}).then(response => {
+					if(typeof this.markers[customerId] === "undefined") {
+						this.markers[customerId] = [];
+					}
+
+					response.markers.forEach(markerData => {
+						const marker = new google.maps.Marker({
+							map: this.map,
+							position: markerData.latLng,
+						});
+						marker.addListener("click", () => {
+							window.location = markerData.url + "/update";
+						});
+
+						this.markers[customerId].push(marker);
+					});
+
+					$item.find("i").attr("class", "fa fa-fw fa-check text-success");
+					$item.attr("data-selected", "true");
+				});
+			}
+			else {
+				// Remove markers
+				this.markers[customerId].forEach(marker => {
+					marker.setMap(null);
+				});
+				this.markers[customerId] = [];
+
+				$item.find("i").attr("class", "fa fa-fw");
+				$item.attr("data-selected", "false");
+			}
+		});
+
+	}
+
+	get latLng() {
+		return {
+			lat: 54,
+			lng: -4
+		};
+	}
+}
 
 $(() => {
 	const addressMap = document.querySelector("#address-map");
 	if(addressMap) {
 		new AddressMap(addressMap);
 	}
+
+	const map = document.getElementById("map");
+	if(map) {
+		new Map(map);
+	}
 });

+ 3 - 0
routes/default.php

@@ -15,6 +15,8 @@ $this->addRoute("/^home$/", "home");
 
 $this->addRoute("/^dashboard$/", "dashboard", [], ["requireUser" => true]);
 
+$this->addRoute("/^map$/", "map", [], ["requireUser" => true]);
+
 $this->addRoute("/^customers$/", "customers", [], ["requireUser" => true]);
 $this->addRoute("/^customers\/add$/", "customer_form", [], ["requireUser" => true]);
 $this->addRoute("/^customers\/$slug_pattern$/", "customer", ["id"], ["requireUser" => true]);
@@ -49,3 +51,4 @@ $this->addRoute("/^reset-password$/", "reset_password");
 $this->addRoute("/^settings$/", "settings", [], ["requireUser" => true]);
 $this->addRoute("/^log-out$/", "log_out");
 
+$this->addRoute("/^api\/([a-z\-]+)$/", "api", ["action"], ["requireUser" => true]);

+ 2 - 0
views/header.php

@@ -68,6 +68,8 @@ $google_api_key = $_ENV["GOOGLE_API_KEY"] ?? null;
 					</div>
 					<div class="list-group list-group-flush">
 						<a class="list-group-item list-group-item-action" href="/dashboard"><?=htmlspecialchars(__("Dashboard"))?></a>
+						<a class="list-group-item list-group-item-action" href="/map"><?=htmlspecialchars(__("Map"))?></a>
+
 						<a class="list-group-item list-group-item-action" href="/customers"><?=htmlspecialchars(__("Customers"))?></a>
 						<a class="list-group-item list-group-item-action" href="/locations"><?=htmlspecialchars(__("Locations"))?></a>
 

+ 38 - 0
views/map.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App;
+
+/**
+ * @var Customer[] $customers
+ */
+
+?>
+<div class="row mb-3">
+	<div class="col">
+		<h3><?=htmlspecialchars(__("Map"))?></h3>
+	</div>
+</div>
+
+<div class="row">
+	<div class="col-lg-6">
+		<div class="mb-3" id="map" style="height: 480px; background: #eee;">
+
+		</div>
+	</div>
+	<div class="col-lg-6">
+		<h4><?=htmlspecialchars(__("Customers"))?></h4>
+
+		<div class="list-group" id="customer-list">
+			<?php
+			foreach($customers as $customer) {
+				?>
+				<a class="list-group-item list-group-item-action" data-id="<?=$customer->id?>" data-selected="false">
+					<i class="fa fa-fw"></i>
+					<?=htmlspecialchars($customer->name)?>
+				</a>
+				<?php
+			}
+			?>
+		</div>
+	</div>
+</div>