1
0

location_edit.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. $page_title = "Edit Location";
  3. $location = Location::get($id);
  4. if(!$location) throw new Exception("Location not found.");
  5. if(!Core::$user->hasPermission($location)) throw new Exception("You do not have have permission to access this page.");
  6. $customer = $location->getCustomer();
  7. $submit = http::value("submit");
  8. if($submit) {
  9. try {
  10. Core::validateCSRFToken($submit);
  11. $form_exception = new FormException;
  12. $location->name = http::value("name", "trim");
  13. $location->address_1 = http::value("address_1", "trim");
  14. $location->address_2 = http::value("address_2", "trim");
  15. $location->city = http::value("city", "trim");
  16. $location->county = http::value("county", "trim");
  17. $location->postal_code = http::value("postal_code", "trim");
  18. $location->opening_hours = http::value("opening_hours", "trim");
  19. $location->shop_left = http::value("shop_left", "trim");
  20. $location->shop_right = http::value("shop_right", "trim");
  21. $location->latitude = http::value("latitude", "float");
  22. $location->longitude = http::value("longitude", "float");
  23. $location->legal_entity_name = http::value("legal_entity_name", "trim");
  24. $location->region = http::value("region", "trim");
  25. $location->internal_id = http::value("internal_id", "trim");
  26. $location->area_manager = http::value("area_manager", "trim");
  27. if(empty($location->name)) $form_exception->addError("Name is required.");
  28. if(empty($location->address_1)) $form_exception->addError("Location line 1 is required.");
  29. if(empty($location->city)) $form_exception->addError("City is required.");
  30. if(empty($location->postal_code)) $form_exception->addError("Postal code is required.");
  31. if(empty($location->latitude)) $form_exception->addError("Map location is required.");
  32. else if(empty($location->longitude)) $form_exception->addError("Map location is required.");
  33. // Photo
  34. $photo = (object) $_FILES["photo"];
  35. if($photo->error != UPLOAD_ERR_NO_FILE) {
  36. try {
  37. if($photo->error != UPLOAD_ERR_OK) throw new Exception(HTTP::getUploadErrorString($photo->error));
  38. if(!Image::isJPEG($photo->tmp_name)) throw new Exception("Photo is not a JPEG image.");
  39. // Photo OK
  40. $file = new LocationFile;
  41. $file->location_id = $id;
  42. $file->type = LocationFile::TYPE_PHOTO;
  43. $file->dts_uploaded = Util::getDateTime();
  44. $file->commit();
  45. $id = $file->id;
  46. $directory = $location->getDirectory();
  47. $file_path = $directory . "/$id.jpg";
  48. $output_image = Image::normalise($photo->tmp_name);
  49. $success = imagejpeg($output_image, $file_path);
  50. imagedestroy($output_image);
  51. if(!$success) throw new Exception("Failed to save photo.");
  52. }
  53. catch(Exception $e) {
  54. $form_exception->addError($e->getMessage());
  55. }
  56. }
  57. // Delete Photos
  58. $delete_file_ids = http::value("delete_file_ids", "int");
  59. $files = LocationFile::query([
  60. "id" => $delete_file_ids,
  61. "location_id" => $id,
  62. ]);
  63. if($files) {
  64. $directory = $location->getDirectory();
  65. foreach($files as $file) {
  66. $pattern = $directory . "/$file->id.*";
  67. foreach(glob($pattern) as $path) {
  68. unlink($path);
  69. }
  70. $file->delete();
  71. }
  72. }
  73. if($form_exception->hasErrors()) throw $form_exception;
  74. $success = $location->commit();
  75. Core::addAlert("success", "Location updated successfully.");
  76. HTTP::redirect("/account/locations/$location->id/edit");
  77. }
  78. catch(FormException $e) {
  79. $e->generateAlerts();
  80. }
  81. catch(Exception $e) {
  82. Core::addAlert("danger", $e->getMessage());
  83. }
  84. }
  85. $photos = LocationFile::query([
  86. "location_id" => $id,
  87. "type" => LocationFile::TYPE_PHOTO
  88. ]);
  89. Core::addBreadcrumb("/account", "Account");
  90. Core::addBreadcrumb("/account/locations", "Locations");
  91. Core::addBreadcrumb("/account/locations?customer_id=$customer->id", "$customer->name Locations");
  92. Core::addBreadcrumb("/account/locations/$id", $location->name);
  93. Core::addBreadcrumb("/account/locations/$id/edit", $page_title);
  94. View::includeOnce("header", compact("page_title"));
  95. View::include("location_form", compact("location", "photos"));
  96. View::includeOnce("footer");
  97. ?>