| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- $page_title = "Edit Location";
- $location = Location::get($id);
- if(!$location) throw new Exception("Location not found.");
- if(!Core::$user->hasPermission($location)) throw new Exception("You do not have have permission to access this page.");
- $customer = $location->getCustomer();
- $submit = http::value("submit");
- if($submit) {
- try {
- Core::validateCSRFToken($submit);
- $form_exception = new FormException;
- $location->name = http::value("name", "trim");
- $location->address_1 = http::value("address_1", "trim");
- $location->address_2 = http::value("address_2", "trim");
- $location->city = http::value("city", "trim");
- $location->county = http::value("county", "trim");
- $location->postal_code = http::value("postal_code", "trim");
- $location->opening_hours = http::value("opening_hours", "trim");
- $location->shop_left = http::value("shop_left", "trim");
- $location->shop_right = http::value("shop_right", "trim");
- $location->latitude = http::value("latitude", "float");
- $location->longitude = http::value("longitude", "float");
- $location->legal_entity_name = http::value("legal_entity_name", "trim");
- $location->region = http::value("region", "trim");
- $location->internal_id = http::value("internal_id", "trim");
- $location->area_manager = http::value("area_manager", "trim");
- if(empty($location->name)) $form_exception->addError("Name is required.");
- if(empty($location->address_1)) $form_exception->addError("Location line 1 is required.");
- if(empty($location->city)) $form_exception->addError("City is required.");
- if(empty($location->postal_code)) $form_exception->addError("Postal code 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.");
- // 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;
- $success = $location->commit();
- Core::addAlert("success", "Location updated successfully.");
- HTTP::redirect("/account/locations/$location->id/edit");
- }
- catch(FormException $e) {
- $e->generateAlerts();
- }
- catch(Exception $e) {
- Core::addAlert("danger", $e->getMessage());
- }
- }
- $photos = LocationFile::query([
- "location_id" => $id,
- "type" => LocationFile::TYPE_PHOTO
- ]);
- Core::addBreadcrumb("/account", "Account");
- Core::addBreadcrumb("/account/locations", "Locations");
- Core::addBreadcrumb("/account/locations?customer_id=$customer->id", "$customer->name Locations");
- Core::addBreadcrumb("/account/locations/$id", $location->name);
- Core::addBreadcrumb("/account/locations/$id/edit", $page_title);
- View::includeOnce("header", compact("page_title"));
- View::include("location_form", compact("location", "photos"));
- View::includeOnce("footer");
- ?>
|