Răsfoiți Sursa

Add ability to set URL for product

Alan Lam 3 ani în urmă
părinte
comite
c382a8961e

+ 1 - 6
controllers/product.php

@@ -2,16 +2,11 @@
 
 namespace App;
 
-use RuntimeException;
-
 /**
  * @var int $id
  */
 
-$product = Product::get($id);
-if(!$product) {
-	throw new RuntimeException(__("Product not found."));
-}
+$product = Product::require($id);
 
 $core = Core::getInstance();
 $core->addBreadcrumb("/products", __("Products"));

+ 2 - 4
controllers/product_form.php

@@ -51,6 +51,7 @@ if($submit) {
 		$product->sku = $request->string("sku");
 		$product->type = $request->int("type");
 		$product->price = $request->decimal("price", 2);
+		$product->url = $request->url("url");
 
 		if(!$product->name) {
 			throw new RuntimeException(__("Name is required."));
@@ -71,8 +72,5 @@ if($submit) {
 }
 
 View::include("header");
-View::include("product_form", [
-	"title" => $title,
-	"product" => $product,
-]);
+View::include("product_form", compact("title", "product"));
 View::include("footer");

+ 12 - 0
db/migrations/20221230145805_add_url_field_to_product_table.php

@@ -0,0 +1,12 @@
+<?php
+declare(strict_types=1);
+
+use Phinx\Migration\AbstractMigration;
+
+final class AddUrlFieldToProductTable extends AbstractMigration {
+	public function change(): void {
+		$this->table("product")
+			->addColumn("url", "string", ["null" => true, "after" => "price"])
+			->update();
+	}
+}

+ 15 - 4
engine/Product.php

@@ -2,6 +2,8 @@
 
 namespace App;
 
+use App\Traits\DateTimeTrait;
+use App\Traits\SoftDeleteTrait;
 use Exception;
 
 /**
@@ -12,16 +14,21 @@ use Exception;
  * @property string $sku
  * @property int $type
  * @property string $price
- * @property string $dts_created
- * @property string $dts_updated
- * @property string $dts_deleted
+ * @property string $url
  */
 
 class Product extends Model implements \JsonSerializable {
 
 	protected static $table = "product";
 	protected static $idField = "id";
-	protected static $fillableFields = ["supplier_id", "name", "type", "sku", "price", "dts_created", "dts_updated", "dts_deleted"];
+	protected static $fillableFields = [
+		"supplier_id",
+		"name", "type", "sku", "price", "url",
+		"dts_created", "dts_updated", "dts_deleted"
+	];
+
+	use DateTimeTrait;
+	use SoftDeleteTrait;
 
 	public const TYPE_MACHINE = 1;
 	public const TYPE_SPARE_PART = 2;
@@ -55,6 +62,10 @@ class Product extends Model implements \JsonSerializable {
 		return sprintf("/products/%s", $this->getSlug());
 	}
 
+	public function getWebsiteUrl(): ?string {
+		return $this->url;
+	}
+
 	public function getDisplayType(): string {
 		return self::getTypeOptions()[$this->type] ?? "";
 	}

+ 11 - 3
engine/Request.php

@@ -167,7 +167,7 @@ class Request {
 		return ($value !== "" && $value !== null) ? trim($value) : null;
 	}
 
-	public function email(string $name) {
+	private function filter(string $name, int $filter) {
 		$value = $this->data[$name] ?? null;
 
 		if(is_array($value)) {
@@ -175,16 +175,24 @@ class Request {
 
 			$output = [];
 			foreach($values as $value) {
-				$value = filter_var($value, FILTER_VALIDATE_EMAIL);
+				$value = filter_var($value, $filter);
 				$output[] = ($value !== false) ? $value : null;
 			}
 			return $output;
 		}
 
-		$value = filter_var($value, FILTER_VALIDATE_EMAIL);
+		$value = filter_var($value, $filter);
 		return ($value !== false) ? $value : null;
 	}
 
+	public function email(string $name) {
+		return $this->filter($name, FILTER_VALIDATE_EMAIL);
+	}
+
+	public function url(string $name) {
+		return $this->filter($name, FILTER_VALIDATE_URL);
+	}
+
 	/**
 	 * @param string $name
 	 * @param string $format

+ 32 - 21
views/product.php

@@ -1,4 +1,4 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
 namespace App;
 
@@ -23,25 +23,36 @@ $supplier = $product->getSupplier();
 	</div>
 </div>
 
-<h4><?=htmlspecialchars(__("Details"))?></h4>
+<section class="mb-4">
+	<h4 class="mb-3"><?=htmlspecialchars(__("Details"))?></h4>
 
-<div class="list-group">
-	<div class="list-group-item d-flex justify-content-between">
-		<strong><?=htmlspecialchars(__("Supplier"))?></strong>
-		<a href="<?=htmlspecialchars($supplier->getUrl())?>">
-			<?=htmlspecialchars($supplier->name)?>
-		</a>
-	</div>
-	<div class="list-group-item d-flex justify-content-between">
-		<strong><?=htmlspecialchars(__("SKU"))?></strong>
-		<?=htmlspecialchars($product->sku)?>
-	</div>
-	<div class="list-group-item d-flex justify-content-between">
-		<strong><?=htmlspecialchars(__("Type"))?></strong>
-		<?=htmlspecialchars($product->getDisplayType())?>
+	<div class="list-group">
+		<div class="list-group-item d-flex justify-content-between">
+			<strong><?=htmlspecialchars(__("Supplier"))?></strong>
+			<a href="<?=htmlspecialchars($supplier->getUrl())?>">
+				<?=htmlspecialchars($supplier->name)?>
+			</a>
+		</div>
+		<div class="list-group-item d-flex justify-content-between">
+			<strong><?=htmlspecialchars(__("SKU"))?></strong>
+			<?=htmlspecialchars($product->sku)?>
+		</div>
+		<div class="list-group-item d-flex justify-content-between">
+			<strong><?=htmlspecialchars(__("Type"))?></strong>
+			<?=htmlspecialchars($product->getDisplayType())?>
+		</div>
+		<div class="list-group-item d-flex justify-content-between">
+			<strong><?=htmlspecialchars(__("Price"))?></strong>
+			<?=htmlspecialchars($product->getDisplayPrice())?>
+		</div>
+		<div class="list-group-item d-flex justify-content-between">
+			<strong><?=htmlspecialchars(__("URL"))?></strong>
+			<span>
+			<i class="fa fa-fw fa-external-link"></i>
+			<a href="<?=htmlspecialchars($product->url)?>" target="_blank">
+				<?=htmlspecialchars(__("View"))?>
+			</a>
+		</span>
+		</div>
 	</div>
-	<div class="list-group-item d-flex justify-content-between">
-		<strong><?=htmlspecialchars(__("Price"))?></strong>
-		<?=htmlspecialchars($product->getDisplayPrice())?>
-	</div>
-</div>
+</section>

+ 8 - 7
views/product_form.php

@@ -2,6 +2,8 @@
 
 namespace App;
 
+use App\Helpers\HtmlHelper;
+
 /**
  * @var string $title
  * @var Product $product
@@ -33,13 +35,7 @@ else {
 		<select class="form-select" name="supplier_id" id="supplier-id" required>
 			<option value=""><?=htmlspecialchars(__("Select Supplier"))?></option>
 			<?php
-			foreach($suppliers as $supplier) {
-				?>
-				<option value="<?=$supplier->id?>" <?=Html::selected($supplier->id, $supplier_id)?>>
-					<?=htmlspecialchars($supplier->name)?>
-				</option>
-				<?php
-			}
+			HtmlHelper::renderOptions(Supplier::getOptions(), $supplier_id);
 			?>
 		</select>
 	</div>
@@ -79,6 +75,11 @@ else {
 		</div>
 	</div>
 
+	<div class="mb-3">
+		<label class="form-label" for="url"><?=htmlspecialchars(__("URL"))?></label>
+		<input class="form-control" type="url" name="url" id="url" placeholder="<?=htmlspecialchars(__("Type URL"))?>" value="<?=htmlspecialchars($product->url)?>" />
+	</div>
+
 	<div class="mb-3">
 		<button class="btn btn-primary" type="submit" name="submit" value="1">
 			<?=htmlspecialchars(__("Submit"))?>