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

Add additional filters for locations page

Alan Lam 3 лет назад
Родитель
Сommit
18181c5fe1

+ 2 - 2
controllers/customer_ajax.php

@@ -26,9 +26,9 @@ switch($action) {
 		$legal_entity_name = http::value("legal_entity_name", "trim");
 		$include_archived = http::value("include_archived", "int");
 		$include_closed = http::value("include_closed", "int");
-		$hide_tested = Http::value("hide_tested", "int");
 		$min_date = http::value("min_date");
 		$max_date = http::value("max_date");
+		$filter = http::value("filter");
 
 		$search->setId($id);
 		$search->setInternalId($internal_id);
@@ -36,9 +36,9 @@ switch($action) {
 		$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);
+		$search->setFilter($filter);
 
 		$locations = $search->run();
 

+ 12 - 6
controllers/locations.php

@@ -24,6 +24,14 @@ $sorts = [
 	"retest-count" => "Highest retest count first",
 ];
 
+$filter_options = [
+	"" => "No Filter",
+	"untested-current" => "Untested Only (Current Cycle)",
+	"failed-current" => "Failed Only (Current Cycle)",
+	"untested-all" => "Untested Only (All Time)",
+	"failed-all" => "Failed Only (All Time)",
+];
+
 $page_title = "$customer->name Locations";
 
 Core::addBreadcrumb("/account/locations?customer_id=$customer->id", $page_title);
@@ -63,8 +71,8 @@ $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 = http::value("filter");
+$search->setFilter($filter);
 
 $filter_count = 0;
 
@@ -101,7 +109,7 @@ if($max_date) {
 	$filter_count++;
 }
 
-if($hide_tested) {
+if($filter) {
 	$filter_count++;
 }
 
@@ -169,9 +177,7 @@ if(!empty($action)) {
 	}
 }
 
-$latest_tests = $search->getLatestTests();
-
 View::includeOnce("header", compact("page_title"));
-View::include("locations", compact("customer", "include_archived", "include_closed", "hide_tested", "filter_count", "sorts", "sort", "locations", "latest_tests"));
+View::include("locations", compact("customer", "search", "filter_count", "sorts", "sort", "filter_options", "locations"));
 View::include("modal_add_locations_to_project", compact("customer"));
 View::includeOnce("footer");

+ 73 - 30
engine/searches/location_search.php

@@ -20,10 +20,13 @@ class LocationSearch {
 	private bool $includeClosed = false;
 	private ?string $minTestDate = null;
 	private ?string $maxTestDate = null;
-	private bool $includeTested = true;
 	private ?string $filter = null;
 
-	private array $latestTests = [];
+	/** @var Test[] */
+	private array $liveTests = [];
+
+	/** @var Test[] */
+	private array $allTests = [];
 
 	public function __construct(Customer $customer) {
 		$this->customer = $customer;
@@ -101,14 +104,6 @@ class LocationSearch {
 		$this->maxTestDate = $maxTestDate;
 	}
 
-	public function isIncludeTested(): bool {
-		return $this->includeTested;
-	}
-
-	public function setIncludeTested(bool $includeTested): void {
-		$this->includeTested = $includeTested;
-	}
-
 	public function getFilter(): ?string {
 		return $this->filter;
 	}
@@ -143,40 +138,88 @@ class LocationSearch {
 			"location_id" => array_column($locations, "id")
 		];
 
-		if(!$this->includeTested) {
-			$test_nvp = $base_test_nvp;
+		$this->populateAllTests($base_test_nvp);
+		$this->populateLiveTests($base_test_nvp);
 
-			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);
+		if($this->filter) {
+			$exclude_location_flags = [];
+
+			$tests = $this->allTests;
+			if(preg_match("/current$/", $this->filter)) {
+				$tests = $this->liveTests;
 			}
 
-			$tests = Test::query($test_nvp);
-			$location_test_flags = [];
-			foreach($tests as $test) {
-				$location_test_flags[$test->location_id] = true;
+			switch($this->filter) {
+				case "untested-current":
+				case "untested-all":
+					foreach($tests as $test) {
+						$exclude_location_flags[$test->location_id] = true;
+					}
+					break;
+
+				case "failed-current":
+				case "failed-all":
+					foreach($tests as $test) {
+						if(!$test->isFail() || $test->isVoid()) {
+							$exclude_location_flags[$test->location_id] = true;
+						}
+					}
+					foreach($locations as $location) {
+						if(!isset($tests[$location->id])) {
+							$exclude_location_flags[$location->id] = true;
+						}
+					}
+					break;
 			}
-			unset($tests);
 
-			$locations = array_filter($locations, static function(Location $location) use ($location_test_flags) {
-				return !isset($location_test_flags[$location->id]);
+			$locations = array_filter($locations, static function(Location $location) use ($exclude_location_flags) {
+				return empty($exclude_location_flags[$location->id]);
 			});
 		}
 
-		$all_tests = Test::query($base_test_nvp, ["sort" => "dts_start", "order" => "asc"]);
+		return $locations;
+	}
+
+	/**
+	 * @param array $nvp
+	 * @return Test[]
+	 * @throws \Exception
+	 */
+	public function populateAllTests(array $nvp): array {
+		$all_tests = Test::query($nvp, ["sort" => "dts_start", "order" => "asc"]);
 
 		foreach($all_tests as $test) {
-			$this->latestTests[$test->location_id] = $test;
+			$this->allTests[$test->location_id] = $test;
 		}
 
-		return $locations;
+		return $this->allTests;
+	}
+
+	/**
+	 * @param array $nvp
+	 * @return Test[]
+	 * @throws \Exception
+	 */
+	public function populateLiveTests(array $nvp): array {
+		$nvp["dts_start"] = $this->getDateTimeConstraint();
+		$this->liveTests = Test::query($nvp);
+		return $this->liveTests;
+	}
+
+	public function getAllTests(): array {
+		return $this->allTests;
 	}
 
-	public function getLatestTests(): array {
-		return $this->latestTests;
+	private function getDateTimeConstraint(): Constraint {
+		if($this->minTestDate && $this->maxTestDate) {
+			$constraint = Constraint::between($this->minTestDate, $this->maxTestDate);
+		}
+		else {
+			[$min_dts, $max_dts] = $this->customer->getYearCycleDates();
+			$constraint = Constraint::between($min_dts, $max_dts);
+		}
+
+		return $constraint;
 	}
 
 }

+ 1 - 1
public/js/app-locations.js

@@ -20,7 +20,7 @@ function initialiseLocations() {
 		legal_entity_name: $("#legal-entity-name").val(),
 		include_archived: $("#include-archived").prop("checked") ? 1 : 0,
 		include_closed: $("#include-closed").prop("checked") ? 1 : 0,
-		hide_tested: $("#hide-tested").prop("checked") ? 1 : 0,
+		filter: $("#filter").val(),
 		min_date: $("#min-date").val(),
 		max_date: $("#max-date").val(),
 	}, function(response) {

+ 19 - 13
views/locations.php

@@ -2,16 +2,16 @@
 
 /**
  * @var Customer $customer
- * @var int $include_archived
- * @var int $include_closed
- * @var int $hide_tested
+ * @var LocationSearch $search
  * @var int $filter_count
  * @var string[] $sorts
  * @var string $sort
  * @var Location[] $locations
- * @var Test[] $latest_tests
+ * @var string[] $filter_options
  */
 
+use Searches\LocationSearch;
+
 $query = $_GET;
 unset($query["path"]);
 $query["sort"] = "SORT";
@@ -123,6 +123,18 @@ var customerId = <?=$customer->id?>;
 							</div>
 						</div>
 					</div>
+					<div class="col-lg-6">
+						<div class="row form-group">
+							<label class="col-sm-4 col-form-label" for="filter">Filter</label>
+							<div class="col-sm">
+								<select class="form-control" name="filter" id="filter">
+									<?php
+									HtmlHelper::renderOptions($filter_options, $search->getFilter());
+									?>
+								</select>
+							</div>
+						</div>
+					</div>
 				</div>
 				<div class="row">
 					<div class="col-lg">
@@ -132,24 +144,17 @@ var customerId = <?=$customer->id?>;
 								<div class="form-group">
 									<div class="custom-control custom-switch">
 										<input class="custom-control-input" id="include-archived" name="include_archived"
-											   type="checkbox" value="1" <?=HtmlHelper::checked($include_archived)?> />
+											   type="checkbox" value="1" <?=HtmlHelper::checked($search->isIncludeArchived())?> />
 										<label class="custom-control-label"
 											   for="include-archived"><?= htmlspecialchars(__("Include Archived")) ?></label>
 									</div>
 									<div class="custom-control custom-switch">
 										<input class="custom-control-input" id="include-closed" name="include_closed"
 											   type="checkbox"
-											   value="1" <?=HtmlHelper::checked($include_closed)?> />
+											   value="1" <?=HtmlHelper::checked($search->isIncludeClosed())?> />
 										<label class="custom-control-label"
 											   for="include-closed"><?= htmlspecialchars(__("Include Closed")) ?></label>
 									</div>
-									<div class="custom-control custom-switch">
-										<input class="custom-control-input" id="hide-tested" name="hide_tested"
-											   type="checkbox"
-											   value="1" <?=HtmlHelper::checked($hide_tested)?>/>
-										<label class="custom-control-label"
-											   for="hide-tested"><?= htmlspecialchars(__("Only Show Untested")) ?></label>
-									</div>
 								</div>
 
 								<button class="btn btn-primary" type="submit">Submit</button>
@@ -217,6 +222,7 @@ var customerId = <?=$customer->id?>;
 	</div>
 
 	<?php
+	$latest_tests = $search->getAllTests();
 	View::include("location_list", compact("customer", "locations", "latest_tests"));
 	?>
 </div>