Bläddra i källkod

Add filters to customers page

Alan Lam 3 år sedan
förälder
incheckning
1076ba7f36
2 ändrade filer med 50 tillägg och 4 borttagningar
  1. 20 4
      controllers/customers.php
  2. 30 0
      views/customers.php

+ 20 - 4
controllers/customers.php

@@ -1,21 +1,37 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 namespace App;
 
+use App\Constraint\HasTextConstraint;
+
 $core = Core::getInstance();
 $core->addBreadcrumb("/customers", __("Customers"));
 
-$count = Customer::count();
+$nvp = [];
+
+$request = Request::fromRequest();
+
+$customer_id = $request->int("customer_id");
+if($customer_id) {
+	$nvp["id"] = $customer_id;
+}
+
+$name = $request->string("name");
+if($name) {
+	$nvp["name"] = new HasTextConstraint($name);
+}
+
+$count = Customer::count($nvp);
 
 $pagination = new Pagination($count);
 $pagination->setTerms(__("%s customer"), __("%s customers"));
 
-$customers = Customer::query([], [
+$customers = Customer::query($nvp, [
 	"sort" => "name",
 	"offset" => $pagination->getOffset(),
 	"limit" => $pagination->getPageSize()
 ]);
 
 View::include("header");
-View::include("customers", compact("customers", "pagination"));
+View::include("customers", compact("customers", "pagination", "customer_id", "name"));
 View::include("footer");

+ 30 - 0
views/customers.php

@@ -5,6 +5,8 @@ namespace App;
 /**
  * @var Customer[] $customers
  * @var Pagination $pagination
+ * @var int $customer_id
+ * @var string $name
  */
 
 ?>
@@ -20,6 +22,34 @@ namespace App;
 	</div>
 </div>
 
+<div class="card mb-3">
+	<div class="card-header">
+		<?=htmlspecialchars(__("Filters"))?>
+	</div>
+	<div class="card-body">
+		<form action="/customers" method="post">
+			<div class="row">
+				<div class="col-sm-4">
+					<?php
+					View::include("components/customer_id_field", compact("customer_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($customers)) {
 	?>