Alan Lam 3 лет назад
Родитель
Сommit
21d6d504e7
5 измененных файлов с 57 добавлено и 11 удалено
  1. 15 5
      controllers/api.php
  2. 2 0
      controllers/locations.php
  3. 6 3
      engine/Customer.php
  4. 29 3
      public/js/map.js
  5. 5 0
      views/locations.php

+ 15 - 5
controllers/api.php

@@ -2,6 +2,7 @@
 
 namespace App;
 
+use App\Constraint\HasTextConstraint;
 use RuntimeException;
 
 /**
@@ -17,14 +18,23 @@ try {
 
 		case "get-markers":
 			$customer_id = $request->int("customer_id");
-			$customer = Customer::get($customer_id);
-			if(!$customer) {
-				throw new RuntimeException(__("Customer not found."));
-			}
+			$customer = Customer::require($customer_id);
 
 			$markers = [];
 
-			$locations = $customer->getLocations();
+			$nvp = [];
+
+			$location_id = $request->int("location_id");
+			if($location_id) {
+				$nvp["id"] = $location_id;
+			}
+
+			$name = $request->string("name");
+			if($name) {
+				$nvp["name"] = new HasTextConstraint($name);
+			}
+
+			$locations = $customer->getLocations($nvp);
 			Address::load(array_column($locations, "address_id"));
 
 			foreach($locations as $location) {

+ 2 - 0
controllers/locations.php

@@ -37,6 +37,8 @@ $locations = Location::query($nvp, [
 	"limit" => $pagination->getPageSize()
 ]);
 
+$core->addScript("/js/map.js");
+
 View::include("header");
 View::include("locations", compact("locations", "pagination", "customer_id", "location_id", "name"));
 View::include("footer");

+ 6 - 3
engine/Customer.php

@@ -58,13 +58,16 @@ class Customer extends Model {
 	}
 
 	/**
+	 * @param array $nvp
 	 * @return Location[]
 	 */
-	public function getLocations(): array {
-		return Location::query([
+	public function getLocations(array $nvp = []): array {
+		$nvp = array_merge($nvp, [
 			"customer_id" => $this->id,
 			"dts_deleted" => null
-		], [
+		]);
+
+		return Location::query($nvp, [
 			"sort" => "name"
 		]);
 	}

+ 29 - 3
public/js/map.js

@@ -42,16 +42,42 @@ class Map {
 
 		const $map = $("#map");
 		const data = $map.data();
-		const { customerId } = data;
+		const { customerId, locationsPage } = data;
 		if(customerId) {
 			this.plotForCustomerId(customerId);
 		}
+
+		if(locationsPage) {
+			const $customerIdDropdown = $("#customer-id");
+			const customerId = $customerIdDropdown.val();
+			if(customerId) {
+				this.plotForCustomerId(customerId);
+			}
+			else {
+				$customerIdDropdown.find("option[value!='']").each((index, element) => {
+					this.plotForCustomerId(element.value);
+				});
+			}
+		}
 	}
 
 	plotForCustomerId(customerId) {
-		return $.post("/api/get-markers", {
+
+		const data = {
 			customer_id: customerId
-		}).then(response => {
+		};
+
+		const locationId = $("#location-id").val();
+		if(locationId) {
+			data.location_id = locationId;
+		}
+
+		const name = $("#name").val();
+		if(name) {
+			data.name = name;
+		}
+
+		return $.post("/api/get-markers", data).then(response => {
 			if(typeof this.markers[customerId] === "undefined") {
 				this.markers[customerId] = [];
 			}

+ 5 - 0
views/locations.php

@@ -66,6 +66,11 @@ if(empty($locations)) {
 	<?php
 }
 else {
+	?>
+	<div class="mb-3" id="map" style="height: 480px;" data-locations-page="true">
+
+	</div>
+	<?php
 	View::include("locations_table", compact("locations", "pagination"));
 	$pagination->render();
 }