Przeglądaj źródła

Sync locations page map and list

Alan Lam 3 lat temu
rodzic
commit
2bbf3051f2

+ 12 - 19
controllers/customer_ajax.php

@@ -6,6 +6,7 @@
  */
 
 use Helpers\LocationHelper;
+use Searches\LocationSearch;
 
 JSON::header();
 
@@ -17,11 +18,7 @@ if(!Core::$user->hasPermission($customer)) throw new Exception(__("No permission
 switch($action) {
 	case "get-locations":
 
-		$nvp = [
-			"customer_id" => $customer->id,
-			"archived" => 0,
-			"closed" => 0
-		];
+		$search = new LocationSearch($customer);
 
 		$id = http::value("id", "int");
 		$internal_id = http::value("internal_id", "trim");
@@ -33,24 +30,20 @@ switch($action) {
 		$min_date = http::value("min_date");
 		$max_date = http::value("max_date");
 
-		if($id) $nvp["id"] = $id;
-		if($internal_id) $nvp["internal_id"] = db::escape($internal_id);
-		if($name) $nvp["name"] = Constraint::contains($name);
-		if($legal_entity_name) $nvp["$legal_entity_name"] = Constraint::contains($legal_entity_name);
-		if($include_archived) unset($nvp["archived"]);
-		if($include_closed) unset($nvp["closed"]);
+		$search->setId($id);
+		$search->setInternalId($internal_id);
+		$search->setName($name);
+		$search->setLegalEntityName($legal_entity_name);
+		$search->setIncludeArchived((bool) $include_archived);
+		$search->setIncludeClosed((bool) $include_closed);
+		$search->setIncludeTested(!$hide_tested);
+		$search->setMinTestDate($min_date);
+		$search->setMaxTestDate($max_date);
 
-		$locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
+		$locations = $search->run();
 
 		$output = LocationHelper::getMarkerDataForLocations($locations, $min_date, $max_date);
 
-		if($hide_tested) {
-			$output = array_filter($output, static function(array $result) {
-				return !isset($result["test"]);
-			});
-			$output = array_values($output);
-		}
-
 		JSON::success([
 			"locations" => $output
 		]);

+ 29 - 20
controllers/locations.php

@@ -2,6 +2,7 @@
 
 use Exports\LocationCsvExport;
 use Exports\LocationKmlExport;
+use Searches\LocationSearch;
 
 Core::addScript("public/js/app-locations.js");
 
@@ -9,7 +10,9 @@ Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account/locations", "Locations");
 
 $customer = Customer::getForAdmin();
-if(!$customer) throw new Exception("You do not have permission to access this page.");
+if(!$customer) {
+	throw new Exception("You do not have permission to access this page.");
+}
 
 $customer_slug = Util::slug($customer->name);
 
@@ -31,59 +34,64 @@ $nvp = [
 	"closed" => 0,
 ];
 
+$search = new LocationSearch($customer);
+
 $id = http::value("id", "int");
+$search->setId($id);
+
 $ids = http::value("ids", "int");
+$search->setIds($ids);
+
 $internal_id = http::value("internal_id", "trim");
+$search->setInternalId($internal_id);
+
 $name = http::value("name", "trim");
+$search->setName($name);
+
 $legal_entity_name = http::value("legal_entity_name", "trim");
+$search->setLegalEntityName($legal_entity_name);
 
 $min_date = http::value("min_date");
+$search->setMinTestDate($min_date);
+
 $max_date = http::value("max_date");
+$search->setMaxTestDate($max_date);
 
 $include_archived = (int) http::value("include_archived");
+$search->setIncludeArchived($include_archived);
+
 $include_closed = (int) http::value("include_closed");
+$search->setIncludeClosed($include_closed);
+
 $hide_tested = (int) http::value("hide_tested");
+$search->setIncludeTested(!$hide_tested);
 
 $filter_count = 0;
 
 $where_strings = [];
 
 if($id) {
-	$where_strings[] = "`location`.`id`='$id'";
 	$filter_count++;
 }
 
-if($ids) {
-	$where_strings[] = sprintf("`location`.`id` IN (%s)", implode(",", $ids));
-}
-
 if($internal_id) {
-	$where_strings[] = sprintf("`location`.`internal_id` LIKE '%%%s%%'", DB::escape($internal_id));
 	$filter_count++;
 }
 
 if($name) {
-	$where_strings[] = sprintf("`location`.`name` LIKE '%%%s%%'", DB::escape($name));
 	$filter_count++;
 }
 if($legal_entity_name) {
-	$where_strings[] = sprintf("`location`.`legal_entity_name` LIKE '%%%s%%'", DB::escape($legal_entity_name));
 	$filter_count++;
 }
 
 if($include_archived) {
 	$filter_count++;
 }
-else {
-	$where_strings[] = "`location`.`archived`='0'";
-}
 
 if($include_closed) {
 	$filter_count++;
 }
-else {
-	$where_strings[] = "`location`.`closed`='0'";
-}
 
 if($min_date) {
 	$filter_count++;
@@ -120,17 +128,18 @@ switch($sort) {
 		break;
 }
 
-$where_string = "";
-if($where_strings) {
-	$where_string = "AND " . implode(" AND ", $where_strings);
-}
+
+$locations = $search->run();
+
+$location_ids = array_column($locations, "id") ?: [0];
+$location_ids_csv = implode(", ", $location_ids);
 
 [$date_start, $date_end] = $customer->getYearCycleDates();
 
 $sql = "SELECT `location`.*, COUNT(`test`.`id`) AS `test_count`, SUM(IF(`test`.`retest` = 1, 1, 0)) AS `retest_count` 
 	FROM `location` 
     LEFT JOIN `test` ON `test`.`location_id`=`location`.`id` AND `dts_start` BETWEEN '$date_start' AND '$date_end' 
-	WHERE `customer_id`='$customer->id' $where_string GROUP BY `location`.`id`
+	WHERE `customer_id`='$customer->id' AND `location`.`id` IN ($location_ids_csv) GROUP BY `location`.`id`
 	ORDER BY $query_sort $order
 ";
 

+ 3 - 1
engine/helpers/location_helper.php

@@ -27,7 +27,9 @@ class LocationHelper {
 			return [];
 		}
 
-		$customer = $locations[0]->getCustomer();
+		$location = reset($locations);
+
+		$customer = $location->getCustomer();
 
 		$init_result = static function(Location $location): object {
 			return (object) [

+ 159 - 0
engine/searches/location_search.php

@@ -0,0 +1,159 @@
+<?php
+
+namespace Searches;
+
+use Constraint;
+use Customer;
+use DB;
+use Location;
+use Test;
+
+class LocationSearch {
+
+	private Customer $customer;
+	private ?int $id = null;
+	private ?array $ids = null;
+	private ?string $internalId = null;
+	private ?string $name = null;
+	private ?string $legalEntityName = null;
+	private bool $includeArchived = false;
+	private bool $includeClosed = false;
+	private ?string $minTestDate = null;
+	private ?string $maxTestDate = null;
+	private bool $includeTested = true;
+
+	public function __construct(Customer $customer) {
+		$this->customer = $customer;
+	}
+
+	public function getId(): ?int {
+		return $this->id;
+	}
+
+	public function setId(?int $id): void {
+		$this->id = $id;
+	}
+
+	public function getIds(): ?array {
+		return $this->ids;
+	}
+
+	public function setIds(?array $ids): void {
+		$this->ids = $ids;
+	}
+
+	public function getInternalId(): ?string {
+		return $this->internalId;
+	}
+
+	public function setInternalId(?string $internalId): void {
+		$this->internalId = $internalId;
+	}
+
+	public function getName(): ?string {
+		return $this->name;
+	}
+
+	public function setName(?string $name): void {
+		$this->name = $name;
+	}
+
+	public function getLegalEntityName(): ?string {
+		return $this->legalEntityName;
+	}
+
+	public function setLegalEntityName(?string $legalEntityName): void {
+		$this->legalEntityName = $legalEntityName;
+	}
+
+	public function isIncludeArchived(): bool {
+		return $this->includeArchived;
+	}
+
+	public function setIncludeArchived(bool $includeArchived): void {
+		$this->includeArchived = $includeArchived;
+	}
+
+	public function isIncludeClosed(): bool {
+		return $this->includeClosed;
+	}
+
+	public function setIncludeClosed(bool $includeClosed): void {
+		$this->includeClosed = $includeClosed;
+	}
+
+	public function getMinTestDate(): ?string {
+		return $this->minTestDate;
+	}
+
+	public function setMinTestDate(?string $minTestDate): void {
+		$this->minTestDate = $minTestDate;
+	}
+
+	public function getMaxTestDate(): ?string {
+		return $this->maxTestDate;
+	}
+
+	public function setMaxTestDate(?string $maxTestDate): void {
+		$this->maxTestDate = $maxTestDate;
+	}
+
+	public function isIncludeTested(): bool {
+		return $this->includeTested;
+	}
+
+	public function setIncludeTested(bool $includeTested): void {
+		$this->includeTested = $includeTested;
+	}
+
+	/**
+	 * @return Location[]
+	 * @throws \Exception
+	 */
+	public function run(): array {
+		$nvp = [
+			"customer_id" => $this->customer->id,
+			"archived" => 0,
+			"closed" => 0
+		];
+
+		if($this->id) $nvp["id"] = $this->id;
+		if($this->ids) $nvp["id"] = $this->ids;
+		if($this->internalId) $nvp["internal_id"] = Db::escape($this->internalId);
+		if($this->name) $nvp["name"] = Constraint::contains($this->name);
+		if($this->legalEntityName) $nvp["legal_entity_name"] = Constraint::contains($this->legalEntityName);
+		if($this->includeArchived) unset($nvp["archived"]);
+		if($this->includeClosed) unset($nvp["closed"]);
+
+		$locations = Location::query($nvp, ["sort" => "name", "order" => "asc"]);
+
+		if(!$this->includeTested) {
+			$test_nvp = [
+				"status" => Constraint::notEquals(Test::STATUS_CANCELLED),
+				"location_id" => array_column($locations, "id")
+			];
+
+			if($this->minTestDate && $this->maxTestDate) {
+				$test_nvp["dts_start"] = Constraint::between($this->minTestDate, $this->maxTestDate);
+			}
+			else {
+				[$min_dts, $max_dts] = $this->customer->getYearCycleDates();
+				$test_nvp["dts_start"] = Constraint::between($min_dts, $max_dts);
+			}
+
+			$tests = Test::query($test_nvp);
+
+			$location_test_flags = [];
+			foreach($tests as $test) {
+				$location_test_flags[$test->location_id] = true;
+			}
+
+			$locations = array_filter($locations, static function(Location $location) use ($location_test_flags) {
+				return !isset($location_test_flags[$location->id]);
+			});
+		}
+
+		return $locations;
+	}
+
+}

+ 1 - 1
views/locations.php

@@ -147,7 +147,7 @@ var customerId = <?=$customer->id?>;
 											   type="checkbox"
 											   value="1" <?=HtmlHelper::checked($hide_tested)?>/>
 										<label class="custom-control-label"
-											   for="hide-tested"><?= htmlspecialchars(__("Hide Tested On Map")) ?></label>
+											   for="hide-tested"><?= htmlspecialchars(__("Only Show Untested")) ?></label>
 									</div>
 								</div>