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->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"); 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"); ?>