Преглед на файлове

Add batch actions to ticket list

Alan Lam преди 4 години
родител
ревизия
2513a05b41
променени са 4 файла, в които са добавени 146 реда и са изтрити 46 реда
  1. 46 0
      controllers/tickets.php
  2. 1 1
      engine/Helpers/HtmlHelper.php
  3. 5 0
      engine/Ticket.php
  4. 94 45
      views/tickets.php

+ 46 - 0
controllers/tickets.php

@@ -2,6 +2,9 @@
 
 namespace App;
 
+use App\Enums\TicketStatuses;
+use RuntimeException;
+
 $core = Core::getInstance();
 $core->addBreadcrumb("/tickets", __("Tickets"));
 
@@ -10,6 +13,49 @@ $core->addScript("/js/ticket-modal.js");
 
 $request = Request::fromRequest();
 
+$batch_submit = $request->int("batch_submit");
+if($batch_submit) {
+	$ticket_ids = $request->int("ticket_ids");
+	$tickets = [];
+	foreach($ticket_ids as $ticket_id) {
+		$ticket = Ticket::get($ticket_id);
+		if(!$ticket) {
+			throw new RuntimeException(__("Ticket not found."));
+		}
+		$tickets[] = $ticket;
+	}
+
+	$assign_to_user = null;
+	$assign_to_user_id = $request->int("assign_to");
+	if($assign_to_user_id) {
+		$assign_to_user = User::get($assign_to_user_id);
+		if(!$assign_to_user) {
+			throw new RuntimeException(__("User not found."));
+		}
+		if(!$assign_to_user->isAdmin()) {
+			throw new RuntimeException(__("%s is not staff.", $assign_to_user->getDisplayName()));
+		}
+	}
+
+	$status = $request->int("status");
+	if($status !== null && !TicketStatuses::validate($status)) {
+		throw new RuntimeException(__("Invalid status."));
+	}
+
+	foreach($tickets as $ticket) {
+		if($assign_to_user_id) {
+			$ticket->assign($assign_to_user);
+		}
+		if($status !== null) {
+			$ticket->status = $status;
+			$ticket->commit();
+		}
+	}
+
+	Session::addAlert("success", __("Batch action applied successfully."));
+	Http::redirect("/tickets");
+}
+
 $nvp = [
 	"dts_deleted" => null
 ];

+ 1 - 1
engine/Helpers/HtmlHelper.php

@@ -4,7 +4,7 @@ namespace App\Helpers;
 
 class HtmlHelper {
 
-	public static function renderOptions(array $options, $selected_value): void {
+	public static function renderOptions(array $options, $selected_value = null): void {
 		foreach($options as $value => $display) {
 			$selected = ((string) $value === (string) $selected_value) ? "selected" : "";
 			?>

+ 5 - 0
engine/Ticket.php

@@ -96,4 +96,9 @@ class Ticket extends Model {
 		return sprintf("/tickets/%d", $this->id);
 	}
 
+	public function assign(User $user): bool {
+		$this->assigned_user_id = $user->id;
+		return $this->commit();
+	}
+
 }

+ 94 - 45
views/tickets.php

@@ -2,6 +2,9 @@
 
 namespace App;
 
+use App\Enums\TicketStatuses;
+use App\Helpers\HtmlHelper;
+
 /**
  * @var Ticket[] $tickets
  */
@@ -9,6 +12,12 @@ namespace App;
 $core = Core::getInstance();
 $user = $core->getUser();
 
+$staff_users = User::query(["admin" => 1]);
+$staff_options = [];
+foreach($staff_users as $staff_user) {
+	$staff_options[$staff_user->id] = $staff_user->getDisplayName();
+}
+
 ?>
 
 <div class="row mb-3">
@@ -34,54 +43,94 @@ if(!$tickets) {
 }
 else {
 	?>
-	<table class="table table-bordered table-hover">
-		<colgroup>
-			<col style="width: 1%;" />
-			<col style="width: 1%;" />
-			<col style="width: 30%;" />
-			<col style="width: 15%;" />
-			<col style="width: 15%;" />
-			<col style="width: 15%;" />
-			<col style="width: 15%;" />
-		</colgroup>
-		<thead>
-		<tr>
-			<th></th>
-			<th><?=htmlspecialchars("#")?></th>
-			<th><?=htmlspecialchars(__("Subject / Location"))?></th>
-			<th><?=htmlspecialchars(__("Status"))?></th>
-			<th><?=htmlspecialchars(__("Assigned To"))?></th>
-			<th><?=htmlspecialchars(__("Created"))?></th>
-			<th><?=htmlspecialchars(__("Updated"))?></th>
-		</tr>
-		</thead>
-		<tbody>
-		<?php
-		foreach($tickets as $ticket) {
-			$location = $ticket->getLocation();
-			?>
+	<form method="post">
+		<table class="table table-bordered table-hover">
+			<colgroup>
+				<col style="width: 1%;" />
+				<col style="width: 1%;" />
+				<col style="width: 30%;" />
+				<col style="width: 15%;" />
+				<col style="width: 15%;" />
+				<col style="width: 15%;" />
+				<col style="width: 15%;" />
+			</colgroup>
+			<thead>
 			<tr>
-				<td>
-					<label for="ticket-checkbox-<?=$ticket->id?>" class="sr-only"></label>
-					<input type="checkbox" id="ticket-checkbox-<?=$ticket->id?>" name="ticket_ids[]" value="<?=$ticket->id?>" />
-				</td>
-				<td class="text-end"><?=$ticket->id?></td>
-				<td>
-					<strong>
-						<a href="/tickets/<?=$ticket->id?>"><?=htmlspecialchars($ticket->subject)?></a>
-					</strong><br />
-					<?=htmlspecialchars($location->name)?>
-				</td>
-				<td><?=htmlspecialchars($ticket->getDisplayStatus())?></td>
-				<td><?=htmlspecialchars($ticket->getDisplayAssignedTo())?></td>
-				<td><?=htmlspecialchars($user->formatDateTime($ticket->getDateTimeCreated()))?></td>
-				<td><?=htmlspecialchars($user->formatDateTime($ticket->getDateTimeUpdated()))?></td>
+				<th></th>
+				<th><?=htmlspecialchars("#")?></th>
+				<th><?=htmlspecialchars(__("Subject / Location"))?></th>
+				<th><?=htmlspecialchars(__("Status"))?></th>
+				<th><?=htmlspecialchars(__("Assigned To"))?></th>
+				<th><?=htmlspecialchars(__("Created"))?></th>
+				<th><?=htmlspecialchars(__("Updated"))?></th>
 			</tr>
+			</thead>
+			<tbody>
 			<?php
-		}
-		?>
-		</tbody>
-	</table>
+			foreach($tickets as $ticket) {
+				$location = $ticket->getLocation();
+				?>
+				<tr>
+					<td>
+						<div class="form-check">
+							<label for="ticket-checkbox-<?=$ticket->id?>" class="form-check-label"></label>
+							<input class="form-check-input" type="checkbox" id="ticket-checkbox-<?=$ticket->id?>" name="ticket_ids[]" value="<?=$ticket->id?>" />
+						</div>
+					</td>
+					<td class="text-end"><?=$ticket->id?></td>
+					<td>
+						<strong>
+							<a href="/tickets/<?=$ticket->id?>"><?=htmlspecialchars($ticket->subject)?></a>
+						</strong><br />
+						<?=htmlspecialchars($location->name)?>
+					</td>
+					<td><?=htmlspecialchars($ticket->getDisplayStatus())?></td>
+					<td><?=htmlspecialchars($ticket->getDisplayAssignedTo())?></td>
+					<td><?=htmlspecialchars($user->formatDateTime($ticket->getDateTimeCreated()))?></td>
+					<td><?=htmlspecialchars($user->formatDateTime($ticket->getDateTimeUpdated()))?></td>
+				</tr>
+				<?php
+			}
+			?>
+			</tbody>
+		</table>
+
+		<div class="card">
+			<div class="card-header">
+				<?=htmlspecialchars(__("Batch Actions"))?>
+			</div>
+			<div class="card-body">
+				<div class="row">
+					<div class="col-lg">
+						<div class="mb-2">
+							<label class="form-label" for="assign-to"><?=htmlspecialchars(__("Assign To"))?></label>
+							<select class="form-control" name="assign_to" id="assign-to">
+								<option value=""><?=htmlspecialchars(__("Select User"))?></option>
+								<?php
+								HtmlHelper::renderOptions($staff_options);
+								?>
+							</select>
+						</div>
+					</div>
+					<div class="col-lg">
+						<div class="mb-2">
+							<label class="form-label" for="status"><?=htmlspecialchars(__("Status"))?></label>
+							<select class="form-control" name="status" id="status">
+								<option value=""><?=htmlspecialchars(__("Select Status"))?></option>
+								<?php
+								HtmlHelper::renderOptions(TicketStatuses::options());
+								?>
+							</select>
+						</div>
+					</div>
+				</div>
+
+				<button type="submit" class="btn btn-primary" name="batch_submit" value="1">
+					<?=htmlspecialchars(__("Submit"))?>
+				</button>
+			</div>
+		</div>
+	</form>
 	<?php
 }