Jelajahi Sumber

Add filters to locations page

Alan Lam 3 tahun lalu
induk
melakukan
47a214bfea
2 mengubah file dengan 68 tambahan dan 5 penghapusan
  1. 24 5
      controllers/locations.php
  2. 44 0
      views/locations.php

+ 24 - 5
controllers/locations.php

@@ -1,23 +1,42 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 namespace App;
 
+use App\Constraint\HasTextConstraint;
+
 $core = Core::getInstance();
 $core->addBreadcrumb("/locations", __("Locations"));
 
-$user = $core->getUser();
+$nvp = ["dts_deleted" => null];
+
+$request = Request::fromRequest();
+
+$customer_id = $request->int("customer_id");
+if($customer_id) {
+	$nvp["customer_id"] = $customer_id;
+}
+
+$location_id = $request->int("location_id");
+if($location_id) {
+	$nvp["id"] = $location_id;
+}
+
+$name = $request->string("name");
+if($name) {
+	$nvp["name"] = new HasTextConstraint($name);
+}
 
-$count = Location::count(["dts_deleted" => null]);
+$count = Location::count($nvp);
 
 $pagination = new Pagination($count);
 $pagination->setTerms(__("%s location"), __("%s locations"));
 
-$locations = Location::query([], [
+$locations = Location::query($nvp, [
 	"sort" => "name",
 	"offset" => $pagination->getOffset(),
 	"limit" => $pagination->getPageSize()
 ]);
 
 View::include("header");
-View::include("locations", compact("locations", "pagination"));
+View::include("locations", compact("locations", "pagination", "customer_id", "location_id", "name"));
 View::include("footer");

+ 44 - 0
views/locations.php

@@ -2,9 +2,14 @@
 
 namespace App;
 
+use App\Helpers\HtmlHelper;
+
 /**
  * @var Location[] $locations
  * @var Pagination $pagination
+ * @var int $customer_id
+ * @var int $location_id
+ * @var string $name
  */
 
 ?>
@@ -12,6 +17,45 @@ namespace App;
 	<h3><?=htmlspecialchars(__("Locations"))?></h3>
 </div>
 
+<div class="card mb-3">
+	<div class="card-header">
+		<?=htmlspecialchars(__("Filters"))?>
+	</div>
+	<div class="card-body">
+		<form action="/locations" method="post">
+			<div class="row">
+				<div class="col-sm-4">
+					<div class="mb-3">
+						<label class="form-label" for="customer-id"><?= htmlspecialchars(__("Customer")) ?></label>
+						<select class="form-control" name="customer_id" id="customer-id">
+							<option value=""><?= htmlspecialchars(__("Any Customer")) ?></option>
+							<?php
+							HtmlHelper::renderOptions(Customer::getOptions(), $customer_id);
+							?>
+						</select>
+					</div>
+				</div>
+				<div class="col-sm-4">
+					<?php
+					View::include("components/location_id_field", compact("location_id"));
+					?>
+				</div>
+				<div class="col-sm-4">
+					<div class="mb-3">
+						<label class="form-label" for="name"><?= htmlspecialchars(__("Name")) ?></label>
+						<input class="form-control" type="text" name="name" id="name"
+							   placeholder="<?= htmlspecialchars(__("Type name")) ?>" value="<?=htmlspecialchars($name)?>" />
+					</div>
+
+				</div>
+			</div>
+			<button class="btn btn-primary" type="submit">
+				<?=htmlspecialchars(__("Submit"))?>
+			</button>
+		</form>
+	</div>
+</div>
+
 <?php
 if(empty($locations)) {
 	?>