location_add.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. $location->country = DEFAULT_COUNTRY;
  12. $submit = http::value("submit");
  13. if($submit) {
  14. try {
  15. Core::validateCSRFToken($submit);
  16. $form_exception = new FormException;
  17. $location->name = http::value("name", "trim");
  18. $location->address_1 = http::value("address_1", "trim");
  19. $location->address_2 = http::value("address_2", "trim");
  20. $location->city = http::value("city", "trim");
  21. $location->county = http::value("county", "trim");
  22. $location->postal_code = http::value("postal_code", "trim");
  23. $location->country = http::value("country", "trim");
  24. $location->email = http::value("email", "email");
  25. $location->phone = http::value("phone", "trim");
  26. $location->opening_hours = http::value("opening_hours", "trim");
  27. $location->notes = http::value("notes", "trim");
  28. $location->shop_left = http::value("shop_left", "trim");
  29. $location->shop_right = http::value("shop_right", "trim");
  30. $location->legal_entity_name = http::value("legal_entity_name", "trim");
  31. $location->internal_id = http::value("internal_id", "trim");
  32. $location->brand = http::value("brand", "trim");
  33. $location->region = http::value("region", "trim");
  34. $location->area_manager = http::value("area_manager", "trim");
  35. $location->area_manager_email = http::value("area_manager_email", "email");
  36. $location->do_not_test = http::value("do_not_test", "int") ? 1 : 0;
  37. $location->closed = http::value("closed", "int") ? 1 : 0;
  38. $location->archived = http::value("archived", "int") ? 1 : 0;
  39. if(empty($location->name)) $form_exception->addError("Name is required.");
  40. if(empty($location->address_1)) $form_exception->addError("Location line 1 is required.");
  41. if(empty($location->city)) $form_exception->addError("City is required.");
  42. if(empty($location->postal_code)) $form_exception->addError("Postal code is required.");
  43. if(empty($location->country)) $form_exception->addError("Country is required.");
  44. // Geocode
  45. try {
  46. $results = Geolocation::geocode($location->getDisplayAddress(["address_1", "address_2", "city", "postal_code", "country"]));
  47. $result = $results[0];
  48. foreach($result->address_components as $component) {
  49. $types = $component->types;
  50. if(in_array("administrative_area_level_1", $types)) $location->state = $component->long_name;
  51. }
  52. $location->latitude = $result->geometry->location->lat;
  53. $location->longitude = $result->geometry->location->lng;
  54. }
  55. catch(Exception $e) {
  56. $form_exception->addError("Failed to get latitude/longitude from address.");
  57. }
  58. if($form_exception->hasErrors()) throw $form_exception;
  59. $success = $location->commit();
  60. if(!$success) throw new Exception("Failed to add location at this time. Please try again later.");
  61. // Venue types
  62. $venue_type_ids = http::value("venue_type_ids", "int");
  63. $venue_types = VenueType::query(["id" => $venue_type_ids]);
  64. $location->assignVenueTypes($venue_types);
  65. Core::addAlert("success", "Location added successfully.");
  66. HTTP::redirect("/account/locations/$location->id/edit");
  67. }
  68. catch(FormException $e) {
  69. $e->generateAlerts();
  70. }
  71. catch(Exception $e) {
  72. Core::addAlert("danger", $e->getMessage());
  73. }
  74. }
  75. View::includeOnce("header", compact("page_title"));
  76. View::include("location_form", compact("location"));
  77. View::includeOnce("footer");