Ver Fonte

Update location ID field to be a dropdown

Alan Lam há 1 ano atrás
pai
commit
017bca39e8

+ 13 - 2
engine/Helpers/HtmlHelper.php

@@ -2,14 +2,25 @@
 
 namespace App\Helpers;
 
+use App\Interfaces\OptionInterface;
+
 class HtmlHelper {
 
 	public static function renderOptions(array $options, $selected_value = null): void {
-		foreach($options as $value => $display) {
+		foreach($options as $i => $x) {
+			if($x instanceof OptionInterface) {
+				$value = $x->getOptionValue();
+				$label = $x->getOptionLabel();
+			}
+			else {
+				$value = $i;
+				$label = $x;
+			}
+
 			$selected = ((string) $value === (string) $selected_value) ? "selected" : "";
 			?>
 			<option value="<?=htmlspecialchars($value)?>" <?=$selected?>>
-				<?=htmlspecialchars($display)?>
+				<?=htmlspecialchars($label)?>
 			</option>
 			<?php
 		}

+ 10 - 0
engine/Interfaces/OptionInterface.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace App\Interfaces;
+
+interface OptionInterface {
+
+	public function getOptionValue(): string;
+	public function getOptionLabel(): string;
+
+}

+ 10 - 1
engine/Location.php

@@ -5,6 +5,7 @@ namespace App;
 use App\Constraint\MoreThanOrEqualsConstraint;
 use App\Constraint\NotInConstraint;
 use App\Enums\SalesOrderStatuses;
+use App\Interfaces\OptionInterface;
 use App\Traits\DateTimeTrait;
 use App\Traits\SoftDeleteTrait;
 use Carbon\CarbonImmutable;
@@ -27,7 +28,7 @@ use RuntimeException;
  * @property string $contact_email
  */
 
-class Location extends Model {
+class Location extends Model implements OptionInterface {
 
 	protected static $table = "location";
 	protected static $idField = "id";
@@ -181,4 +182,12 @@ class Location extends Model {
 			"order" => "desc",
 		]);
 	}
+
+	public function getOptionValue(): string {
+		return $this->getIdString();
+	}
+
+	public function getOptionLabel(): string {
+		return $this->name ?? "";
+	}
 }

+ 20 - 2
views/components/location_id_field.php

@@ -4,9 +4,27 @@
  * @var int $location_id
  */
 
+use App\Customer;
+use App\Helpers\HtmlHelper;
+
+$customers = Customer::getAll();
+
 ?>
 
 <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"))?>" />
+	<label class="form-label" for="location-id"><?=htmlspecialchars(__("Location"))?></label>
+
+	<select class="form-control" name="location_id" id="location-id">
+		<?php
+		foreach($customers as $customer) {
+			?>
+			<optgroup label="<?=htmlspecialchars($customer->name)?>">
+				<?php
+				HtmlHelper::renderOptions($customer->getLocations(), $location_id);
+				?>
+			</optgroup>
+			<?php
+		}
+		?>
+	</select>
 </div>