Explorar o código

Add filters to tickets table

Alan Lam %!s(int64=3) %!d(string=hai) anos
pai
achega
a67302b72b

+ 6 - 2
controllers/dashboard.php

@@ -17,11 +17,15 @@ $core->addBreadcrumb("/dashboard", __("Dashboard"));
 $cards = [];
 
 $open_ticket_count = Ticket::count([
-	"status" => new NotEqualsConstraint(TicketStatuses::CLOSED),
+	"status" => new NotInConstraint(TicketStatuses::CLOSED, TicketStatuses::SOLVED),
 	"dts_deleted" => null
 ]);
 
-$cards[] = ["title" => "Tickets Open", "count" => $open_ticket_count];
+$cards[] = [
+	"title" => "Tickets Unsolved",
+	"count" => $open_ticket_count,
+	"url" => "/tickets?custom_filter=unsolved",
+];
 
 $open_sale_count = Sale::count([
 	"status" => new NotEqualsConstraint(SaleStatuses::COMPLETED),

+ 42 - 1
controllers/tickets.php

@@ -2,6 +2,8 @@
 
 namespace App;
 
+use App\Constraint\NotEqualsConstraint;
+use App\Constraint\NotInConstraint;
 use App\Enums\TicketStatuses;
 use RuntimeException;
 
@@ -54,6 +56,45 @@ $nvp = [
 	"dts_deleted" => null
 ];
 
+$customer_id = $request->int("customer_id");
+if($customer_id) {
+	$customer = Customer::get($customer_id);
+	if($customer) {
+		$nvp["location_id"] = array_column($customer->getLocations(), "id");
+	}
+	else {
+		$nvp["location_id"] = 0;
+	}
+}
+
+$location_id = $request->int("location_id");
+if($location_id) {
+	$nvp["location_id"] = $location_id;
+}
+
+$status = $request->int("status");
+if($status !== null) {
+	$nvp["status"] = $status;
+}
+
+$assigned_user_id = $request->int("assigned_user_id");
+if($assigned_user_id !== null) {
+	$nvp["assigned_user_id"] = $assigned_user_id;
+}
+
+$custom_filter = $request->string("custom_filter");
+switch($custom_filter) {
+	case "open":
+		$nvp["status"] = new NotEqualsConstraint(TicketStatuses::CLOSED);
+		break;
+	case "unsolved":
+		$nvp["status"] = new NotInConstraint(TicketStatuses::CLOSED, TicketStatuses::SOLVED);
+		break;
+	case "unassigned":
+		$nvp["assigned_user_id"] = null;
+		break;
+}
+
 $count = Ticket::count($nvp);
 
 $pagination = new Pagination($count);
@@ -67,6 +108,6 @@ $tickets = Ticket::query($nvp, [
 ]);
 
 View::include("header");
-View::include("tickets", compact("tickets", "pagination"));
+View::include("tickets", compact("tickets", "pagination", "customer_id", "location_id", "status", "assigned_user_id", "custom_filter"));
 View::include("modals/ticket");
 View::include("footer");

+ 12 - 0
engine/User.php

@@ -55,6 +55,18 @@ class User extends Model {
 		return $staff_options;
 	}
 
+	/**
+	 * @return array
+	 * @throws Exception
+	 */
+	public static function getOptions(): array {
+		$options = [];
+		foreach(self::getAll() as $user) {
+			$options[$user->id] = $user->getDisplayName();
+		}
+		return $options;
+	}
+
 	public static function requireStaff(?int $user_id): User {
 		$user = self::get($user_id);
 		if(!$user || !$user->isAdmin()) {

+ 12 - 0
views/components/customer_id_field.php

@@ -0,0 +1,12 @@
+<?php
+
+/**
+ * @var int $customer_id
+ */
+
+?>
+
+<div class="mb-3">
+	<label class="form-label" for="customer-id"><?=htmlspecialchars(__("Customer ID"))?></label>
+	<input class="form-control" type="number" name="customer_id" id="customer-id" value="<?=htmlspecialchars($customer_id)?>" placeholder="<?=htmlspecialchars(__("Type ID"))?>" />
+</div>

+ 12 - 0
views/components/location_id_field.php

@@ -0,0 +1,12 @@
+<?php
+
+/**
+ * @var int $location_id
+ */
+
+?>
+
+<div class="mb-3">
+	<label class="form-label" for="location-id"><?=htmlspecialchars(__("Location ID"))?></label>
+	<input class="form-control" type="number" name="location_id" id="location-id" value="<?=htmlspecialchars($location_id)?>" placeholder="<?=htmlspecialchars(__("Type ID"))?>" />
+</div>

+ 2 - 1
views/dashboard.php

@@ -13,6 +13,7 @@
 <div class="row g-3">
 	<?php
 	foreach($cards as $card) {
+		$url = $card["url"] ?? "#";
 		?>
 		<div class="col-6 col-md-4 col-lg-3">
 			<div class="card">
@@ -21,7 +22,7 @@
 						<?=htmlspecialchars($card["title"])?>
 					</strong><br />
 					<div class="fa-2x">
-						<?=htmlspecialchars(number_format($card["count"]))?>
+						<a href="<?=htmlspecialchars($url)?>"><?=htmlspecialchars(number_format($card["count"]))?></a>
 					</div>
 				</div>
 			</div>

+ 69 - 0
views/tickets.php

@@ -8,11 +8,22 @@ use App\Helpers\HtmlHelper;
 /**
  * @var Ticket[] $tickets
  * @var Pagination $pagination
+ * @var int $customer_id
+ * @var int $location_id
+ * @var int $status
+ * @var int $assigned_user_id
+ * @var string $custom_filter
  */
 
 $core = Core::getInstance();
 $user = $core->getUser();
 
+$custom_filter_options = [
+	"open" => __("Open Tickets"),
+	"unsolved" => __("Unsolved Tickets"),
+	"unassigned" => __("Unassigned Tickets"),
+];
+
 ?>
 
 <div class="row mb-3">
@@ -27,6 +38,64 @@ $user = $core->getUser();
 	</div>
 </div>
 
+<div class="card mb-3">
+	<div class="card-header">
+		<?=htmlspecialchars(__("Filters"))?>
+	</div>
+	<div class="card-body">
+		<form action="/tickets" 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">
+					<?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="status"><?=htmlspecialchars(__("Status"))?></label>
+						<select class="form-control" name="status" id="status">
+							<option value=""><?=htmlspecialchars(__("Any status"))?></option>
+							<?php
+							HtmlHelper::renderOptions(TicketStatuses::options(), $status);
+							?>
+						</select>
+					</div>
+				</div>
+				<div class="col-sm-4">
+					<div class="mb-3">
+						<label class="form-label" for="assigned-user-id"><?=htmlspecialchars(__("Assigned User"))?></label>
+						<select class="form-control" name="assigned_user_id" id="assigned-user-id">
+							<option value=""><?=htmlspecialchars(__("Any user"))?></option>
+							<?php
+							HtmlHelper::renderOptions(User::getOptions(), $assigned_user_id);
+							?>
+						</select>
+					</div>
+				</div>
+				<div class="col-sm-4">
+					<div class="mb-3">
+						<label class="form-label" for="custom-filter"><?=htmlspecialchars(__("Custom Filter"))?></label>
+						<select class="form-control" name="custom_filter" id="custom-filter">
+							<option value=""><?=htmlspecialchars(__("All tickets"))?></option>
+							<?php
+							HtmlHelper::renderOptions($custom_filter_options, $custom_filter);
+							?>
+						</select>
+					</div>
+				</div>
+			</div>
+			<button class="btn btn-primary" type="submit">
+				<?=htmlspecialchars(__("Submit"))?>
+			</button>
+		</form>
+	</div>
+</div>
+
 <?php
 if(!$tickets) {
 	?>