Просмотр исходного кода

Add ability to add location in ticket modal

Alan Lam 4 лет назад
Родитель
Сommit
4c5c2cdf06
4 измененных файлов с 106 добавлено и 9 удалено
  1. 39 1
      controllers/tickets_ajax.php
  2. 8 0
      engine/Customer.php
  3. 18 0
      public/js/ticket-modal.js
  4. 41 8
      views/modals/ticket.php

+ 39 - 1
controllers/tickets_ajax.php

@@ -39,8 +39,46 @@ $controller = new class($action) {
 
 		$request = Request::fromRequest();
 
+		$location_id = $request->int("location_id");
+
+		$customer_id = $request->string("customer_id");
+		$location_name = $request->string("location_name");
+		$postal_code = $request->string("postal_code");
+
+		$location = null;
+		if($location_id) {
+			$location = Location::get($location_id);
+			if(!$location) {
+				throw new RuntimeException(__("Location not found."));
+			}
+		}
+		else if($customer_id) {
+			$customer = Customer::get($customer_id);
+			if(!$customer) {
+				throw new RuntimeException(__("Customer not found."));
+			}
+
+			if(!$location_name) {
+				throw new RuntimeException(__("Location name is required."));
+			}
+			if(!$postal_code) {
+				throw new RuntimeException(__("Postcode is required."));
+			}
+
+			$address = new Address();
+			$address->postal_code = $postal_code;
+			$address->commit();
+
+			$location = new Location();
+			$location->customer_id = $customer->id;
+			$location->address_id = $address->id;
+			$location->name = $location_name;
+			$location->dts_created = Database::dts();
+			$location->commit();
+		}
+
 		$ticket = new Ticket();
-		$ticket->location_id = $request->int("location_id");
+		$ticket->location_id = $location->id;
 		$ticket->creator_user_id = $this->user->id;
 		$ticket->subject = $request->string("subject");
 		$ticket->contact_name = $request->string("contact_name");

+ 8 - 0
engine/Customer.php

@@ -30,6 +30,14 @@ class Customer extends Model {
 		return self::query([], ["sort" => "name"]);
 	}
 
+	public static function getOptions(): array {
+		$options = [];
+		foreach(self::getAll() as $customer) {
+			$options[$customer->id] = $customer->name;
+		}
+		return $options;
+	}
+
 	// Object methods
 
 	/**

+ 18 - 0
public/js/ticket-modal.js

@@ -13,7 +13,17 @@ class TicketModal {
 		this.$form.on("submit", event => this.onSubmit(event));
 
 		this.$subjectField = this.$form.find("[name=subject]");
+
+		this.$locationRow = $("#location-row");
 		this.$locationDropdown = this.$form.find("[name=location_id]");
+
+		this.$noLocationAction = $("#no-location-action");
+		this.$noLocationAction.on("click", event => {
+			event.preventDefault();
+			this.onNoLocation();
+		});
+
+		this.$noLocationRow = $("#no-location-row");
 	}
 
 	// Modal lifecycle
@@ -39,6 +49,14 @@ class TicketModal {
 	onHidden() {
 		this.$form.get(0).reset();
 		this.$locationDropdown.find("option[value!='']").remove();
+
+		this.$locationRow.slideDown("fast").find("select").prop("required", true);
+		this.$noLocationRow.slideUp("fast").find("select, input").prop("required", false);
+	}
+
+	onNoLocation() {
+		this.$locationRow.slideUp("fast").find("select, input").prop("required", false);
+		this.$noLocationRow.slideDown("fast").find("select, input").prop("required", true);
 	}
 
 	onSubmit(event) {

+ 41 - 8
views/modals/ticket.php

@@ -5,6 +5,9 @@ namespace App;
 	<div class="modal-dialog modal-lg">
 		<form class="modal-content" method="post">
 			<?php
+
+			use App\Helpers\HtmlHelper;
+
 			View::include("modals/header", ["title" => __("New Ticket")]);
 			?>
 			<div class="modal-body">
@@ -16,22 +19,52 @@ namespace App;
 					<label class="form-label" for="ticket-content"><?=htmlspecialchars(__("Content"))?></label>
 					<textarea class="form-control" name="content" id="ticket-content" rows="8" placeholder="<?=htmlspecialchars(__("Type content"))?>" required></textarea>
 				</div>
-				<div class="row">
-					<div class="col-lg">
+				<div class="mb-2">
+					<label class="form-label" for="ticket-contact-name"><?=htmlspecialchars(__("Contact Name"))?></label>
+					<input class="form-control" name="contact_name" id="ticket-contact-name" type="text" maxlength="255" placeholder="<?=htmlspecialchars(__("Type contact name"))?>" />
+				</div>
+				<div id="location-row" class="mb-2">
+					<label class="form-label" for="ticket-location">
+						<?=htmlspecialchars(__("Location"))?>
+					</label>
+					<select class="form-control" name="location_id" id="ticket-location" required>
+						<option value=""><?=htmlspecialchars(__("Select Location"))?></option>
+					</select>
+					<a id="no-location-action" href="#"><?=htmlspecialchars(__("Click here if the location is not listed."))?></a>
+				</div>
+
+				<div id="no-location-row" class="row" style="display: none;">
+					<div class="col-lg-4">
 						<div class="mb-2">
-							<label class="form-label" for="ticket-location"><?=htmlspecialchars(__("Location"))?></label>
-							<select class="form-control" name="location_id" id="ticket-location" required>
-								<option value=""><?=htmlspecialchars(__("Select Location"))?></option>
+							<label class="form-label" for="ticket-customer-id">
+								<?=htmlspecialchars(__("Customer"))?>
+							</label>
+							<select class="form-control" name="customer_id" id="ticket-customer-id">
+								<option value=""><?=htmlspecialchars(__("Select Customer"))?></option>
+								<?php
+								HtmlHelper::renderOptions(Customer::getOptions());
+								?>
 							</select>
 						</div>
 					</div>
-					<div class="col-lg">
+					<div class="col-lg-4">
+						<div class="mb-2">
+							<label class="form-label" for="ticket-location-name">
+								<?=htmlspecialchars(__("Location Name"))?>
+							</label>
+							<input class="form-control" type="text" name="location_name" id="ticket-location-name" maxlength="255" placeholder="<?=htmlspecialchars(__("Type location name"))?>" />
+						</div>
+					</div>
+					<div class="col-lg-4">
 						<div class="mb-2">
-							<label class="form-label" for="ticket-contact-name"><?=htmlspecialchars(__("Contact Name"))?></label>
-							<input class="form-control" name="contact_name" id="ticket-contact-name" type="text" maxlength="255" placeholder="<?=htmlspecialchars(__("Type contact name"))?>" />
+							<label class="form-label" for="ticket-postal-code">
+								<?=htmlspecialchars(__("Location Postcode"))?>
+							</label>
+							<input class="form-control" type="text" name="postal_code" id="ticket-postal-code" maxlength="255" placeholder="<?=htmlspecialchars(__("Type postcode"))?>" />
 						</div>
 					</div>
 				</div>
+
 				<div class="mb-2">
 					<label class="form-label" for="ticket-attachments"><?=htmlspecialchars(__("Attachments"))?></label>
 					<input class="form-control" type="file" name="attachments[]" multiple />