location_add.php 3.6 KB

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