Преглед изворни кода

Adds filters to test list page.

Alan Lam пре 8 година
родитељ
комит
023bbac2d9
2 измењених фајлова са 66 додато и 37 уклоњено
  1. 11 1
      controllers/tests.php
  2. 55 36
      views/test_list.php

+ 11 - 1
controllers/tests.php

@@ -11,9 +11,15 @@ $user = Core::$user;
 if($user->level >= User::LEVEL_ADMIN) {
 
 	$status = http::value("status", "int");
+	$project_id = http::value("project_id", "int");
+	$location_id = http::value("location_id", "int");
+	$test_id = http::value("test_id", "int");
 
 	$nvp = [];
 	if($status) $nvp["status"] = $status;
+	if($project_id) $nvp["project_id"] = $project_id;
+	if($location_id) $nvp["location_id"] = $location_id;
+	if($test_id) $nvp["id"] = $test_id;
 
 	$filter_count = count($nvp);
 	if(!$filter_count) $filter_count = "";
@@ -23,8 +29,12 @@ if($user->level >= User::LEVEL_ADMIN) {
 	$testers = User::load(array_column($tests, "tester_id"));
 	$projects = Project::load(array_column($tests, "project_id"));
 
+	usort($projects, function($a, $b) {
+		return $a->name <=> $b->name;
+	});
+
 	View::includeOnce("header", compact("page_title"));
-	View::include("test_list", compact("tests", "filter_count", "status"));
+	View::include("test_list", compact("tests", "projects", "project_id", "location_id", "test_id", "filter_count", "status"));
 	View::includeOnce("footer");
 }
 

+ 55 - 36
views/test_list.php

@@ -1,3 +1,7 @@
+<?php
+$filters_collapse = $filter_count ? "collapse show" : "collapse";
+?>
+
 <div class="container mt-3 py-4">
 
 	<div class="row">
@@ -11,49 +15,64 @@
 		</div>
 	</div>
 
-	<div class="collapse" id="filters">
-		<div class="card card-body mb-3">
-			<h3>Filters</h3>
-			<form method="get">
-
-				<div class="row">
-					<div class="col-3">
-						<div class="form-group">
-							<label for="status">Status</label>
-							<select class="form-control" name="status" id="status">
-								<option value="">Any</option>
-								<?php
-								foreach(Test::STATUSES as $value => $display) {
-									$selected = ($value === $status) ? "selected" : "";
-									?>
-									<option value="<?=$value?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
+	<div class="<?=$filters_collapse?>" id="filters">
+		<form method="get">
+			<div class="card mb-3">
+				<div class="card-header">
+					Filters
+				</div>
+				<div class="card-body">
+					<div class="row">
+						<div class="col-3">
+							<div class="form-group">
+								<label for="status">Status</label>
+								<select class="form-control" name="status" id="status">
+									<option value="">Any</option>
 									<?php
-								}
-								?>
-							</select>
+									foreach(Test::STATUSES as $value => $display) {
+										$selected = ($value === $status) ? "selected" : "";
+										?>
+										<option value="<?=$value?>" <?=$selected?>><?=htmlspecialchars($display)?></option>
+										<?php
+									}
+									?>
+								</select>
+							</div>
 						</div>
-					</div>
-					<div class="col-3">
-						<div class="form-group">
-							<label for="project-id">Project</label>
+						<div class="col-3">
+							<div class="form-group">
+								<label for="project-id">Project</label>
+								<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
+									}
+									?>
+								</select>
+							</div>
 						</div>
-					</div>
-					<div class="col-3">
-						<div class="form-group">
-							<label for="location-id">Location ID</label>
+						<div class="col-3">
+							<div class="form-group">
+								<label for="location-id">Location ID</label>
+								<input class="form-control" type="text" name="location_id" id="location-id" placeholder="Enter location ID" value="<?=$location_id?>" />
+							</div>
 						</div>
-					</div>
-					<div class="col-3">
-						<div class="form-group">
-							<label for="test-id">Test ID</label>
+						<div class="col-3">
+							<div class="form-group">
+								<label for="test-id">Test ID</label>
+								<input class="form-control" type="text" name="test_id" id="test-id" placeholder="Enter test ID" value="<?=$test_id?>" />
+							</div>
 						</div>
 					</div>
+					<button class="btn btn-primary" type="submit">Filter</button>
+					<a class="btn btn-secondary" href="/account/tests">Clear Filters</a>
 				</div>
-
-				<button class="btn btn-primary" type="submit">Filter</button>
-				<a class="btn btn-secondary" href="/account/tests">Clear Filters</a>
-			</form>
-		</div>
+			</div>
+		</form>
 	</div>
 
 	<?php