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

Adds support for location files/photos.

Alan Lam 8 лет назад
Родитель
Сommit
5b0cac400e

+ 54 - 2
controllers/location_edit.php

@@ -36,12 +36,59 @@ if($submit) {
 		if(empty($location->latitude)) $form_exception->addError("Map location is required.");
 		if(empty($location->latitude)) $form_exception->addError("Map location is required.");
 		else if(empty($location->longitude)) $form_exception->addError("Map location is required.");
 		else if(empty($location->longitude)) $form_exception->addError("Map location is required.");
 
 
+		// Photo
+		$photo = (object) $_FILES["photo"];
+		if($photo->error != UPLOAD_ERR_NO_FILE) {
+			try {
+				if($photo->error != UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($photo->error));
+				if(!Image::isJPEG($photo->tmp_name)) throw new Exception("Photo is not a JPEG image.");
+
+				// Photo OK
+				$file = new LocationFile;
+				$file->location_id = $id;
+				$file->type = LocationFile::TYPE_PHOTO;
+				$file->dts_uploaded = Util::getDateTime();
+				$file->commit();
+
+				$id = $file->id;
+
+				$directory = $location->getDirectory();
+				$file_path = $directory . "/$id.jpg";
+
+				$output_image = Image::normalise($photo->tmp_name);
+				$success = imagejpeg($output_image, $file_path);
+				imagedestroy($output_image);
+				if(!$success) throw new Exception("Failed to save photo.");
+			}
+			catch(Exception $e) {
+				$form_exception->addError($e->getMessage());
+			}
+		}
+
+		// Delete Photos
+		$delete_file_ids = http::value("delete_file_ids", "int");
+		$files = LocationFile::query([
+			"id" => $delete_file_ids,
+			"location_id" => $id,
+		]);
+		if($files) {
+			$directory = $location->getDirectory();
+			foreach($files as $file) {
+				$pattern = $directory . "/$file->id.*";
+				foreach(glob($pattern) as $path) {
+					unlink($path);
+				}
+				$file->delete();
+			}
+		}
+
+
 		if($form_exception->hasErrors()) throw $form_exception;
 		if($form_exception->hasErrors()) throw $form_exception;
 
 
 		$success = $location->commit();
 		$success = $location->commit();
 
 
 		Core::addAlert("success", "Location updated successfully.");
 		Core::addAlert("success", "Location updated successfully.");
-		HTTP::redirect("/account/locations/$location->id");
+		HTTP::redirect("/account/locations/$location->id/edit");
 	}
 	}
 	catch(FormException $e) {
 	catch(FormException $e) {
 		$e->generateAlerts();
 		$e->generateAlerts();
@@ -51,6 +98,11 @@ if($submit) {
 	}
 	}
 }
 }
 
 
+$photos = LocationFile::query([
+	"location_id" => $id,
+	"type" => LocationFile::TYPE_PHOTO
+]);
+
 
 
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account", "Account");
 Core::addBreadcrumb("/account/locations", "Locations");
 Core::addBreadcrumb("/account/locations", "Locations");
@@ -59,7 +111,7 @@ Core::addBreadcrumb("/account/locations/$id", $location->name);
 Core::addBreadcrumb("/account/locations/$id/edit", $page_title);
 Core::addBreadcrumb("/account/locations/$id/edit", $page_title);
 
 
 View::includeOnce("header", compact("page_title"));
 View::includeOnce("header", compact("page_title"));
-View::include("location_edit_form", compact("location"));
+View::include("location_edit_form", compact("location", "photos"));
 View::includeOnce("footer");
 View::includeOnce("footer");
 
 
 ?>
 ?>

+ 31 - 0
controllers/location_photo.php

@@ -0,0 +1,31 @@
+<?php
+
+if(!empty($id)) {
+	$file = LocationFile::get($id);
+	if(!$file) throw new Exception("Photo not found.");
+	if($file->location_id != $location_id) throw new Exception("Photo not found.");
+
+	$location = Location::get($location_id);
+}
+else {
+	$location = Location::get($location_id);
+	if(!$location) throw new Exception("Location not found.");
+
+	$photo = LocationFile::queryOne([
+		"location_id" => $location->id,
+		"type" => LocationFile::TYPE_PHOTO
+	]);
+	if(!$photo) {
+		header("Content-Type: image/png");
+		readfile("public/img/no_image_4_3.png");
+		exit;
+	}
+
+	$id = $photo->id;
+}
+
+$path = $location->getDirectory() . "/$id.jpg";
+if(!file_exists($path)) HTTP::redirect("/img/blank.png");
+
+header("Content-Type: image/jpeg");
+readfile($path);

+ 1 - 1
engine/http.php

@@ -59,7 +59,7 @@ class HTTP {
 		switch($validation) {
 		switch($validation) {
 
 
 			case "int": {
 			case "int": {
-				if(strlen($value)===0) return false;
+				if(!is_array($value) && strlen($value)===0) return false;
 				return is_array($value) ? array_map("intval", $value) : intval($value);
 				return is_array($value) ? array_map("intval", $value) : intval($value);
 			}
 			}
 
 

+ 8 - 0
engine/image.php

@@ -4,6 +4,14 @@ class Image {
 
 
 	// Takes an image and makes the largest dimension 1920 pixels large and rotates it according to EXIF data.
 	// Takes an image and makes the largest dimension 1920 pixels large and rotates it according to EXIF data.
 
 
+	public static function isJPEG($path) {
+		$size = @getimagesize($path);
+		if(!$size) return false;
+		list($width, $height, $type) = $size;
+		if($type != IMAGETYPE_JPEG) return false;
+		return true;
+	}
+
 	public static function normalise($path, $size = 1920) {
 	public static function normalise($path, $size = 1920) {
 
 
 		$size = getimagesize($path);
 		$size = getimagesize($path);

+ 6 - 0
engine/location.php

@@ -14,6 +14,12 @@ class Location extends Model {
 		return Customer::get($this->customer_id);
 		return Customer::get($this->customer_id);
 	}
 	}
 
 
+	public function getDirectory() {
+		$directory = ROOT_DIR . "/files/locations/" . $this->id;
+		if(!file_exists($directory)) mkdir($directory, 0777, true);
+		return $directory;
+	}
+
 	public function getDisplayAddress($fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"]) {
 	public function getDisplayAddress($fields = ["address_1", "address_2", "city", "county", "postal_code", "state", "country"]) {
 		$parts = [];
 		$parts = [];
 
 

+ 17 - 0
engine/location_file.php

@@ -0,0 +1,17 @@
+<?php
+
+class LocationFile extends Model {
+
+	protected static $table = "location_file";
+	protected static $id_field = "id";
+	protected static $fillable_fields = ["location_id", "type", "dts_uploaded"];
+
+	const TYPE_PHOTO = 1;
+
+	// Object methods
+	public function getLocation() {
+		return Location::get($location_id);
+	}
+}
+
+?>

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
public/css/bootstrap.min.css


BIN
public/img/blank_4_3.png


BIN
public/img/no_image_4_3.png


+ 2 - 0
routes.php

@@ -69,6 +69,8 @@ $routes = [
 	"/^account\/locations\/add$/" => ["controller" => "location_add", "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/add$/" => ["controller" => "location_add", "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/$id_pattern$/" => ["controller" => "location_view", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/$id_pattern$/" => ["controller" => "location_view", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/$id_pattern\/edit$/" => ["controller" => "location_edit", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
 	"/^account\/locations\/$id_pattern\/edit$/" => ["controller" => "location_edit", "params" => ["id"], "require_user_level" => User::LEVEL_CUSTOMER],
+	"/^account\/locations\/$id_pattern\/photo$/" => ["controller" => "location_photo", "params" => ["location_id"]],
+	"/^account\/locations\/$id_pattern\/photos\/$id_pattern$/" => ["controller" => "location_photo", "params" => ["location_id", "id"]],
 
 
 	"/^account\/customers$/" => ["controller" => "customers", "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/customers$/" => ["controller" => "customers", "require_user_level" => User::LEVEL_ADMIN],
 	"/^account\/customers\/add$/" => ["controller" => "customer_add", "require_user_level" => User::LEVEL_SUPERADMIN],
 	"/^account\/customers\/add$/" => ["controller" => "customer_add", "require_user_level" => User::LEVEL_SUPERADMIN],

+ 16 - 0
scss/_main.scss

@@ -7,6 +7,10 @@ body {
 	line-height: 1;
 	line-height: 1;
 }
 }
 
 
+.w-auto {
+	width: auto;
+}
+
 .navbar .nav-link {
 .navbar .nav-link {
 	font-weight: 500;
 	font-weight: 500;
 	text-transform: uppercase;
 	text-transform: uppercase;
@@ -16,6 +20,18 @@ body {
 	font-size: 100%;
 	font-size: 100%;
 }
 }
 
 
+.img-cover {
+	background-size: cover;
+	background-repeat: no-repeat;
+	background-position: center center;
+}
+
+.img-contain {
+	background-size: contain;
+	background-repeat: no-repeat;
+	background-position: center center;
+}
+
 .carousel-item {
 .carousel-item {
 	img {
 	img {
 		min-height: 300px;
 		min-height: 300px;

+ 45 - 2
views/location_edit_form.php

@@ -1,9 +1,12 @@
 <div class="container mt-3 py-4">
 <div class="container mt-3 py-4">
 	<h2>Edit Location</h2>
 	<h2>Edit Location</h2>
 
 
-	<form method="post">
+	<form method="post" enctype="multipart/form-data">
 		<div class="row">
 		<div class="row">
 			<div class="col-lg mb-3">
 			<div class="col-lg mb-3">
+
+				<h3>Details</h3>
+
 				<div class="row mb-1">
 				<div class="row mb-1">
 					<label class="col-sm-4 col-form-label" for="name">Name</label>
 					<label class="col-sm-4 col-form-label" for="name">Name</label>
 					<div class="col-sm">
 					<div class="col-sm">
@@ -47,14 +50,54 @@
 						<input class="form-control" type="text" name="shop_left" id="shop-left" placeholder="Enter shop name (left)" value="<?=htmlspecialchars($location->shop_left)?>" />
 						<input class="form-control" type="text" name="shop_left" id="shop-left" placeholder="Enter shop name (left)" value="<?=htmlspecialchars($location->shop_left)?>" />
 					</div>
 					</div>
 				</div>
 				</div>
-				<div class="row mb-1">
+				<div class="row mb-3">
 					<label class="col-sm-4 col-form-label" for="shop-right">Shop Name (Right)</label>
 					<label class="col-sm-4 col-form-label" for="shop-right">Shop Name (Right)</label>
 					<div class="col-sm">
 					<div class="col-sm">
 						<input class="form-control" type="text" name="shop_right" id="shop-right" placeholder="Enter shop name (right)" value="<?=htmlspecialchars($location->shop_right)?>" />
 						<input class="form-control" type="text" name="shop_right" id="shop-right" placeholder="Enter shop name (right)" value="<?=htmlspecialchars($location->shop_right)?>" />
 					</div>
 					</div>
 				</div>
 				</div>
+
+				<h3>Add Photo</h3>
+
+				<div class="row mb-3">
+					<label class="col-sm-4 col-form-label" for="photo">JPEG File</label>
+					<div class="col-sm">
+						<input class="form-control-file" type="file" name="photo" id="photo" />
+					</div>
+				</div>
+
+				<?php
+				if(!empty($photos)) {
+					?>
+					<h3>Manage Photos</h3>
+
+					<div class="row">
+					<?php
+					foreach($photos as $photo) {
+						?>
+						<div class="col-md-6">
+							<div class="card">
+								<img class="card-img-top img-cover" src="/img/blank_4_3.png" alt="Photo" style="background-image: url(/account/locations/6/photos/<?=$photo->id?>)" />
+								<div class="card-body">
+									<div class="form-check">
+										<input class="form-check-input" type="checkbox" name="delete_file_ids[]" id="photo-<?=$photo->id?>" value="<?=$photo->id?>" />
+										<label class="form-check-label" for="photo-<?=$photo->id?>">Delete</label>
+									</div>
+								</div>
+							</div>
+						</div>
+						<?php
+					}
+					?>
+					</div>
+					<?php
+				}
+				?>
 			</div>
 			</div>
 			<div class="col-lg">
 			<div class="col-lg">
+
+				<h3>Map</h3>
+
 				<div class="mb-3" id="map" style="height: 330px;" data-maps data-lat="<?=$location->latitude?>" data-lng="<?=$location->longitude?>" data-zoom="14" data-marker="true" data-marker-draggable="true" data-marker-dragend-target="#map-data"></div>
 				<div class="mb-3" id="map" style="height: 330px;" data-maps data-lat="<?=$location->latitude?>" data-lng="<?=$location->longitude?>" data-zoom="14" data-marker="true" data-marker-draggable="true" data-marker-dragend-target="#map-data"></div>
 
 
 				<div id="map-data">
 				<div id="map-data">

Некоторые файлы не были показаны из-за большого количества измененных файлов