Prechádzať zdrojové kódy

Add ability to upload a brochure for a product

Alan Lam 3 rokov pred
rodič
commit
46b18b7116

+ 19 - 0
controllers/product_brochure.php

@@ -0,0 +1,19 @@
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+namespace App;
+
+use RuntimeException;
+
+/**
+ * @var int $id
+ */
+
+$product = Product::require($id);
+if($product->hasBrochure()) {
+	header("Content-Type: application/pdf");
+	readfile($product->getBrochurePath());
+	exit(0);
+}
+
+http_response_code(404);
+throw new RuntimeException(__("Brochure not found."));

+ 18 - 0
controllers/product_form.php

@@ -63,6 +63,24 @@ if($submit) {
 
 
 		$product->commit();
 		$product->commit();
 
 
+		$delete_brochure = $request->int("delete_brochure");
+		if($delete_brochure && $product->hasBrochure()) {
+			$product->deleteBrochure();
+		}
+
+		$brochure = $_FILES["brochure"] ?? null;
+		if($brochure) {
+			$error = $brochure["error"];
+			if($error === UPLOAD_ERR_OK) {
+				Util::createDirectory($_ENV["DIR_ROOT"] . "/files/product");
+				move_uploaded_file($brochure["tmp_name"], $product->getBrochurePath());
+			}
+			else if($error !== UPLOAD_ERR_NO_FILE) {
+				Session::addAlert("danger", __("Brochure upload failed."));
+			}
+		}
+
+
 		Session::addAlert("success", __("Product saved successfully."));
 		Session::addAlert("success", __("Product saved successfully."));
 		Http::redirect($product->getUrl());
 		Http::redirect($product->getUrl());
 	}
 	}

+ 12 - 0
engine/Product.php

@@ -84,4 +84,16 @@ class Product extends Model implements \JsonSerializable {
 			"price" => $this->price,
 			"price" => $this->price,
 		];
 		];
 	}
 	}
+
+	public function getBrochurePath(): string {
+		return $_ENV["DIR_ROOT"] . "/files/product/" . $this->id . ".pdf";
+	}
+
+	public function hasBrochure(): bool {
+		return file_exists($this->getBrochurePath());
+	}
+
+	public function deleteBrochure(): bool {
+		return !$this->hasBrochure() || unlink($this->getBrochurePath());
+	}
 }
 }

+ 4 - 0
engine/Util.php

@@ -9,4 +9,8 @@ class Util {
 		return preg_replace("/[^a-z\d]+/i", $glue, $string);
 		return preg_replace("/[^a-z\d]+/i", $glue, $string);
 	}
 	}
 
 
+	public static function createDirectory(string $path): bool {
+		return !mkdir($path, 0777, true) && !is_dir($path);
+	}
+
 }
 }

+ 1 - 0
routes/default.php

@@ -44,6 +44,7 @@ $this->addRoute("/^products$/", "products", [], ["requireUser" => true]);
 $this->addRoute("/^products\/add$/", "product_form", [], ["requireUser" => true]);
 $this->addRoute("/^products\/add$/", "product_form", [], ["requireUser" => true]);
 $this->addRoute("/^products\/$slug_pattern$/", "product", ["id"], ["requireUser" => true]);
 $this->addRoute("/^products\/$slug_pattern$/", "product", ["id"], ["requireUser" => true]);
 $this->addRoute("/^products\/$slug_pattern\/update$/", "product_form", ["id"], ["requireUser" => true]);
 $this->addRoute("/^products\/$slug_pattern\/update$/", "product_form", ["id"], ["requireUser" => true]);
+$this->addRoute("/^products\/$slug_pattern\/view-brochure$/", "product_brochure", ["id"], ["requireUser" => true]);
 
 
 $this->addRoute("/^users$/", "users", [], ["requireUser" => true]);
 $this->addRoute("/^users$/", "users", [], ["requireUser" => true]);
 $this->addRoute("/^users\/add$/", "user_form", [], ["requireUser" => true]);
 $this->addRoute("/^users\/add$/", "user_form", [], ["requireUser" => true]);

+ 14 - 5
views/product.php

@@ -48,11 +48,20 @@ $supplier = $product->getSupplier();
 		<div class="list-group-item d-flex justify-content-between">
 		<div class="list-group-item d-flex justify-content-between">
 			<strong><?=htmlspecialchars(__("URL"))?></strong>
 			<strong><?=htmlspecialchars(__("URL"))?></strong>
 			<span>
 			<span>
-			<i class="fa fa-fw fa-external-link"></i>
-			<a href="<?=htmlspecialchars($product->url)?>" target="_blank">
-				<?=htmlspecialchars(__("View"))?>
-			</a>
-		</span>
+				<i class="fa fa-fw fa-external-link"></i>
+				<a href="<?=htmlspecialchars($product->url)?>" target="_blank">
+					<?=htmlspecialchars(__("View"))?>
+				</a>
+			</span>
+		</div>
+		<div class="list-group-item d-flex justify-content-between">
+			<strong><?=htmlspecialchars(__("Brochure"))?></strong>
+			<span>
+				<i class="fa fa-fw fa-external-link"></i>
+				<a href="<?=htmlspecialchars($product->getUrl())?>/view-brochure" target="_blank">
+					<?=htmlspecialchars(__("View"))?>
+				</a>
+			</span>
 		</div>
 		</div>
 	</div>
 	</div>
 </section>
 </section>

+ 20 - 1
views/product_form.php

@@ -25,7 +25,7 @@ else {
 ?>
 ?>
 <h3><?=htmlspecialchars($title)?></h3>
 <h3><?=htmlspecialchars($title)?></h3>
 
 
-<form method="post">
+<form method="post" enctype="multipart/form-data">
 	<?php
 	<?php
 	$user->insertCsrfToken();
 	$user->insertCsrfToken();
 	?>
 	?>
@@ -80,6 +80,25 @@ else {
 		<input class="form-control" type="url" name="url" id="url" placeholder="<?=htmlspecialchars(__("Type URL"))?>" value="<?=htmlspecialchars($product->url)?>" />
 		<input class="form-control" type="url" name="url" id="url" placeholder="<?=htmlspecialchars(__("Type URL"))?>" value="<?=htmlspecialchars($product->url)?>" />
 	</div>
 	</div>
 
 
+	<div class="mb-3">
+		<label class="form-label" for="brochure">
+			<?=htmlspecialchars(__("Upload Brochure (PDF)"))?>
+		</label>
+		<input class="form-control" type="file" name="brochure" id="brochure" accept="application/pdf" />
+
+		<?php
+		if($product->hasBrochure()) {
+			?>
+			<div class="form-check">
+				<input class="form-check-input" type="checkbox" name="delete_brochure" id="delete-brochure" value="1" />
+				<label class="form-check-label" for="delete-brochure"><?=htmlspecialchars(__("Delete Brochure"))?></label>
+			</div>
+			<?php
+		}
+		?>
+	</div>
+
+
 	<div class="mb-3">
 	<div class="mb-3">
 		<button class="btn btn-primary" type="submit" name="submit" value="1">
 		<button class="btn btn-primary" type="submit" name="submit" value="1">
 			<?=htmlspecialchars(__("Submit"))?>
 			<?=htmlspecialchars(__("Submit"))?>