location_add.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. $page_title = "Add Location";
  3. $customer = Customer::getForAdmin();
  4. if(!$customer) throw new Exception("You do not have permission to access this page.");
  5. Core::addBreadcrumb("/account", "Account");
  6. Core::addBreadcrumb("/account/locations", "Locations");
  7. Core::addBreadcrumb("/account/locations?customer_id=$customer->id", "$customer->name Locations");
  8. Core::addBreadcrumb("/account/locations/add", $page_title);
  9. $location = new Location;
  10. $location->customer_id = $customer->id;
  11. $submit = http::value("submit");
  12. if($submit) {
  13. try {
  14. Core::validateCSRFToken($submit);
  15. $form_exception = new FormException;
  16. $location->name = http::value("name", "trim");
  17. $location->address_1 = http::value("address_1", "trim");
  18. $location->address_2 = http::value("address_2", "trim");
  19. $location->city = http::value("city", "trim");
  20. $location->county = http::value("county", "trim");
  21. $location->postal_code = http::value("postal_code", "trim");
  22. $location->email = http::value("email", "email");
  23. $location->phone = http::value("phone", "trim");
  24. $location->opening_hours = http::value("opening_hours", "trim");
  25. $location->shop_left = http::value("shop_left", "trim");
  26. $location->shop_right = http::value("shop_right", "trim");
  27. $location->legal_entity_name = http::value("legal_entity_name", "trim");
  28. $location->region = http::value("region", "trim");
  29. $location->internal_id = http::value("internal_id", "trim");
  30. $location->area_manager = http::value("area_manager", "trim");
  31. if(empty($location->name)) $form_exception->addError("Name is required.");
  32. if(empty($location->address_1)) $form_exception->addError("Location line 1 is required.");
  33. if(empty($location->city)) $form_exception->addError("City is required.");
  34. if(empty($location->postal_code)) $form_exception->addError("Postal code is required.");
  35. // Geocode
  36. try {
  37. $results = Geolocation::geocode($location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"]));
  38. $result = $results[0];
  39. foreach($result->address_components as $component) {
  40. $types = $component->types;
  41. if(in_array("administrative_area_level_1", $types)) $location->state = $component->long_name;
  42. if(in_array("country", $types)) $location->country = $component->long_name;
  43. }
  44. $location->latitude = $result->geometry->location->lat;
  45. $location->longitude = $result->geometry->location->lng;
  46. }
  47. catch(Exception $e) {
  48. $form_exception->addError("Failed to get latitude/longitude from address.");
  49. }
  50. if($form_exception->hasErrors()) throw $form_exception;
  51. $success = $location->commit();
  52. if(!$success) throw new Exception("Failed to add location at this time. Please try again later.");
  53. Core::addAlert("success", "Location added successfully.");
  54. HTTP::redirect("/account/locations/$location->id/edit");
  55. }
  56. catch(FormException $e) {
  57. $e->generateAlerts();
  58. }
  59. catch(Exception $e) {
  60. Core::addAlert("danger", $e->getMessage());
  61. }
  62. }
  63. View::includeOnce("header", compact("page_title"));
  64. View::include("location_form", compact("location"));
  65. View::includeOnce("footer");
  66. ?>