瀏覽代碼

Added map to locations page.

Alan Lam 8 年之前
父節點
當前提交
e9dba20adb
共有 6 個文件被更改,包括 147 次插入 和 35 次删除
  1. 46 0
      controllers/customer_ajax.php
  2. 1 0
      controllers/locations.php
  3. 48 0
      public/js/app-locations.js
  4. 2 0
      routes.php
  5. 37 35
      views/location_list.php
  6. 13 0
      views/locations.php

+ 46 - 0
controllers/customer_ajax.php

@@ -0,0 +1,46 @@
+<?php
+
+JSON::header();
+
+$customer = Customer::get($id);
+if(!$customer) JSON::fail("Customer not found.");
+
+switch($action) {
+	case "get-locations":
+
+		$nvp = ["customer_id" => $customer->id];
+
+		$id = http::value("id", "int");
+		$internal_id = http::value("internal_id", "trim");
+		$name = http::value("name", "trim");
+
+		if($id) $nvp["id"] = $id;
+		if($internal_id) $nvp["internal_id"] = db::escape($internal_id);
+		if($name) $nvp["name"] = (new Constraint("CONTAINS"))->setValues(db::escape($name));
+
+		$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
+			];
+		}
+
+		JSON::success([
+			"locations" => $output
+		]);
+
+		break;
+}

+ 1 - 0
controllers/locations.php

@@ -1,5 +1,6 @@
 <?php
 <?php
 
 
+Core::addScript("public/js/app-locations.js");
 
 
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account/locations", "Locations");
 Core::addBreadcrumb("/account/locations", "Locations");

+ 48 - 0
public/js/app-locations.js

@@ -0,0 +1,48 @@
+function initialiseLocations() {
+
+	$.get("/api/customers/" + customerId + "/get-locations", {
+		id: $("#id").val(),
+		internal_id: $("#internal-id").val(),
+		name: $("#name").val()
+	}, function(response) {
+		if(!response.success) return false;
+		if(!response.locations) return false;
+
+		let map = maps["map"];
+
+		let bounds = new google.maps.LatLngBounds;
+
+		for(let i in response.locations) {
+
+			let location = response.locations[i];
+			let position = {
+				lat: location.latitude,
+				lng: location.longitude
+			};
+
+			let options = {
+				map: map,
+				position: position,
+				title: location.name
+			}
+
+			if(location.icon) {
+				options.icon = "/img/markers/" + location.icon;
+			}
+
+			bounds.extend(position);
+
+			let marker = new google.maps.Marker(options);
+			marker.locationId = location.id;
+
+			marker.addListener("click", function() {
+				window.open("/account/locations/" + this.locationId, "_blank");
+			});
+
+		}
+
+		map.fitBounds(bounds);
+
+	});
+
+}

+ 2 - 0
routes.php

@@ -94,6 +94,8 @@ $routes = [
 	"/^account\/customers\/add$/" => ["controller" => "customer_add", "require_user_level" => User::LEVEL_SUPERADMIN],
 	"/^account\/customers\/add$/" => ["controller" => "customer_add", "require_user_level" => User::LEVEL_SUPERADMIN],
 	"/^account\/customers\/$id_pattern$/" => ["controller" => "customer_view", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/customers\/$id_pattern$/" => ["controller" => "customer_view", "params" => ["id"], "require_user_level" => User::LEVEL_ADMIN],
 
 
+	"/^api\/customers\/$id_pattern\/(get-locations)$/" => ["controller" => "customer_ajax", "params" => ["id", "action"], "require_user_level" => User::LEVEL_ADMIN],
+
 	"/^account\/submit-id$/" => ["controller" => "submit_id", "require_user_level" => User::LEVEL_REGISTERED],
 	"/^account\/submit-id$/" => ["controller" => "submit_id", "require_user_level" => User::LEVEL_REGISTERED],
 
 
 
 

+ 37 - 35
views/location_list.php

@@ -1,39 +1,41 @@
-<table class="table table-sm table-hover">
-	<thead>
-		<tr>
-			<th class="text-right pr-3">ID</th>
-			<th>Name / Address</th>
-			<th></th>
-		</tr>
-	</thead>
-	<tbody>
-	<?php
-	foreach($locations as $location) {
-		?>
-		<tr>
-			<td class="text-right pr-3">
-				<?=$location->id?>
-			</td>
-			<td>
-				<a href="/account/locations/<?=$location->id?>"><?=htmlspecialchars($location->name)?></a><br />
-				<?php
-				if($location->internal_id) {
-					?>
-					<small class="text-info">Internal ID: <?=htmlspecialchars($location->internal_id)?></small><br />
-					<?php
-				}
-				?>
-				<small><?=htmlspecialchars($location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]))?></small>
-			</td>
-			<td class="text-right align-middle">
-				<a class="btn btn-outline-primary" href="/account/locations/<?=$location->id?>/edit">Edit</a>
-			</td>
-		</tr>
+<div class="table-responsive limit-height-2">
+	<table class="table table-sm table-hover">
+		<thead>
+			<tr>
+				<th class="text-right pr-3">ID</th>
+				<th>Name / Address</th>
+				<th></th>
+			</tr>
+		</thead>
+		<tbody>
 		<?php
 		<?php
-	}
-	?>
-	</tbody>
-</table>
+		foreach($locations as $location) {
+			?>
+			<tr>
+				<td class="text-right pr-3">
+					<?=$location->id?>
+				</td>
+				<td>
+					<a href="/account/locations/<?=$location->id?>"><?=htmlspecialchars($location->name)?></a><br />
+					<?php
+					if($location->internal_id) {
+						?>
+						<small class="text-info">Internal ID: <?=htmlspecialchars($location->internal_id)?></small><br />
+						<?php
+					}
+					?>
+					<small><?=htmlspecialchars($location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]))?></small>
+				</td>
+				<td class="text-right align-middle">
+					<a class="btn btn-outline-primary" href="/account/locations/<?=$location->id?>/edit">Edit</a>
+				</td>
+			</tr>
+			<?php
+		}
+		?>
+		</tbody>
+	</table>
+</div>
 
 
 <?php
 <?php
 if(empty($locations)) {
 if(empty($locations)) {

+ 13 - 0
views/locations.php

@@ -1,3 +1,7 @@
+<script>
+var customerId = <?=$customer->id?>;
+</script>
+
 <div class="container mt-3 py-4">
 <div class="container mt-3 py-4">
 	<div class="row">
 	<div class="row">
 		<div class="col">
 		<div class="col">
@@ -61,6 +65,15 @@
 		</div>
 		</div>
 	</div>
 	</div>
 
 
+	<?php
+	if(!empty($locations)) {
+		?>
+		<div class="mb-3" id="map" data-maps data-lat="0" data-lng="0" data-zoom="7" style="height: 320px;" data-callback="initialiseLocations">
+		</div>
+		<?php
+	}
+	?>
+
 	<?php
 	<?php
 	View::include("location_list", compact("locations"));
 	View::include("location_list", compact("locations"));
 	?>
 	?>