Răsfoiți Sursa

Add ability to search tests via location name

Alan Lam 4 ani în urmă
părinte
comite
64b859d11e
3 a modificat fișierele cu 55 adăugiri și 43 ștergeri
  1. 12 5
      controllers/tests_admin_list.php
  2. 17 4
      engine/constraint.php
  3. 26 34
      views/test_list.php

+ 12 - 5
controllers/tests_admin_list.php

@@ -79,6 +79,7 @@ $status_tester_invoice = http::value("status_tester_invoice");
 $customer_id = http::value("customer_id", "int");
 $project_id = http::value("project_id", "int");
 $location_id = http::value("location_id", "int");
+$location_name = http::value("location_name", "trim");
 $test_id = http::value("test_id", "int");
 $tester_id = http::value("tester_id", "int");
 $location_internal_id = http::value("location_internal_id", "int");
@@ -111,12 +112,18 @@ else if($customer_id) {
 }
 
 // Location
-if($location_id) $nvp["location_id"] = $location_id;
+if($location_id) {
+	$nvp["location_id"] = $location_id;
+}
 else if($location_internal_id) {
 	$locations = Location::query(["internal_id" => $location_internal_id]);
-	if($locations) {
-		$nvp["location_id"] = array_column($locations, "id");
-	}
+	$nvp["location_id"] = $locations ? array_column($locations, "id") : "0";
+}
+else if($location_name) {
+	$locations = Location::query([
+		"name" => Constraint::like("%" . DB::escape($location_name) . "%")
+	]);
+	$nvp["location_id"] = $locations ? array_column($locations, "id") : "0";
 }
 
 // Test ID
@@ -235,4 +242,4 @@ $sorts = [
 Core::addScript("public/js/app-test-list.js");
 
 View::includeOnce("header", compact("page_title"));
-View::include("test_list", compact("tests", "pagination_object", "sort", "sorts", "projects", "customers", "customer_id", "project_id", "location_id", "location_internal_id", "pass", "min_days_overdue", "custom", "test_id", "tester_id", "filter_count", "status", "status_tester_invoice"));
+View::include("test_list", compact("tests", "pagination_object", "sort", "sorts", "projects", "customers", "customer_id", "project_id", "location_id", "location_internal_id", "location_name", "pass", "min_days_overdue", "custom", "test_id", "tester_id", "filter_count", "status", "status_tester_invoice"));

+ 17 - 4
engine/constraint.php

@@ -20,6 +20,7 @@ class Constraint {
 	const TYPE_IS_NOT_NULL = 11;
 
 	const TYPE_NOT_IN = 12;
+	const TYPE_LIKE = 13;
 
 	private $type;
 	private $values = [];
@@ -48,6 +49,12 @@ class Constraint {
 		return $constraint;
 	}
 
+	public static function like(string $value): Constraint {
+		$constraint = new Constraint("LIKE");
+		$constraint->setValues($value);
+		return $constraint;
+	}
+
 	/**
 	 * Constraint constructor.
 	 * @param string $type
@@ -55,11 +62,13 @@ class Constraint {
 	 */
 	public function __construct(string $type) {
 		$type = "TYPE_" . strtoupper($type);
-		if(!defined("Constraint::$type")) throw new Exception("Constraint::$type is not defined.");
+		if(!defined("Constraint::$type")) {
+			throw new LogicException("Constraint::$type is not defined.");
+		}
 		$this->type = constant("Constraint::$type");
 	}
 
-	public function setValues(...$values) {
+	public function setValues(...$values): Constraint {
 		$this->values = array_map("db::escape", $values);
 		return $this;
 	}
@@ -108,9 +117,13 @@ class Constraint {
 				}, $this->values);
 
 				return sprintf(" NOT IN (%s)", implode(", ", $values));
-		}
 
-		return false;
+			case self::TYPE_LIKE:
+				return sprintf(" LIKE '%s'", $value_1);
+
+			default:
+				throw new LogicException("getConstraintString not implemented for type: $this->type");
+		}
 	}
 
 }

+ 26 - 34
views/test_list.php

@@ -14,7 +14,8 @@
  * @var int $test_id
  * @var int $tester_id
  * @var int $location_id
- * @var int $location_internal_id
+ * @var string $location_internal_id
+ * @var string $location_name
  * @var int $min_days_overdue
  * @var int $custom
  * @var object $pagination_object
@@ -26,8 +27,7 @@ unset($query["path"]);
 
 $query["sort"] = "SORT";
 
-$results = [
-	"" => "Any",
+$result_options = [
 	"1" => "Pass",
 	"0" => "Fail",
 ];
@@ -37,6 +37,16 @@ $custom_filters = [
 	"overdue_test" => "Overdue (Test Due Date)",
 ];
 
+$customer_options = [];
+foreach($customers as $customer) {
+	$customer_options[$customer->id] = $customer->name;
+}
+
+$project_options = [];
+foreach($projects as $project) {
+	$project_options[$project->id] = $project->name;
+}
+
 ?>
 <div class="mt-3 py-4">
 	<div class="container-fluid">
@@ -63,12 +73,7 @@ $custom_filters = [
 							</div>
 							<select class="form-control" id="sort-dropdown" data-url-template="/account/tests?<?=htmlspecialchars(http_build_query($query))?>">
 							<?php
-							foreach($sorts as $value => $display) {
-								$selected = ($value == $sort) ? "selected" : "";
-								?>
-								<option value="<?=$value?>" <?=$selected?>><?=$display?></option>
-								<?php
-							}
+							HtmlHelper::renderOptions($sorts, $sort);
 							?>
 							</select>
 						</div>
@@ -142,12 +147,7 @@ $custom_filters = [
 									<select class="form-control" name="customer_id" id="customer-id">
 										<option value="">Select customer</option>
 										<?php
-										foreach($customers as $customer) {
-											$selected = ($customer->id == $customer_id) ? "selected" : "";
-											?>
-											<option value="<?=$customer->id?>" <?=$selected?>><?=htmlspecialchars($customer->name)?></option>
-											<?php
-										}
+										HtmlHelper::renderOptions($customer_options, $customer_id);
 										?>
 									</select>
 								</div>
@@ -158,12 +158,7 @@ $custom_filters = [
 									<select class="form-control" name="project_id" id="project-id">
 										<option value="">Select project</option>
 										<?php
-										foreach($projects as $project) {
-											$selected = ($project->id == $project_id) ? "selected" : "";
-											?>
-											<option value="<?=$project->id?>" <?=$selected?>><?=htmlspecialchars($project->name)?></option>
-											<?php
-										}
+										HtmlHelper::renderOptions($project_options, $project_id);
 										?>
 									</select>
 								</div>
@@ -172,13 +167,9 @@ $custom_filters = [
 								<label class="col-sm-4 col-form-label" for="pass">Result</label>
 								<div class="col-sm">
 									<select class="form-control" name="pass" id="pass">
+										<option value=""><?=htmlspecialchars(__("Any"))?></option>
 										<?php
-										foreach($results as $value => $display) {
-											$selected = (strval($value) === strval($pass)) ? "selected" : "";
-											?>
-											<option value="<?=$value?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
-											<?php
-										}
+										HtmlHelper::renderOptions($result_options, $pass);
 										?>
 									</select>
 								</div>
@@ -206,7 +197,13 @@ $custom_filters = [
 							<div class="row mb-1">
 								<label class="col-sm-4 col-form-label" for="location-internal-id">Internal ID</label>
 								<div class="col-sm">
-									<input class="form-control" type="text" name="location_internal_id" id="location-internal-id" placeholder="Enter location internal ID" value="<?=$location_internal_id?>" />
+									<input class="form-control" type="text" name="location_internal_id" id="location-internal-id" placeholder="Enter location internal ID" value="<?=htmlspecialchars($location_internal_id)?>" />
+								</div>
+							</div>
+							<div class="row mb-1">
+								<label class="col-sm-4 col-form-label" for="location-name">Location Name</label>
+								<div class="col-sm">
+									<input class="form-control" type="text" name="location_name" id="location-name" placeholder="Enter location name" value="<?=htmlspecialchars($location_name)?>" />
 								</div>
 							</div>
 							<div class="row mb-1">
@@ -223,12 +220,7 @@ $custom_filters = [
 									<select class="form-control" name="custom" id="custom">
 										<option value="">Select custom filter</option>
 										<?php
-										foreach($custom_filters as $value => $display) {
-											$selected = (strval($value) === strval($custom)) ? "selected" : "";
-											?>
-											<option value="<?=$value?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
-											<?php
-										}
+										HtmlHelper::renderOptions($custom_filters, $custom);
 										?>
 									</select>
 								</div>