location_add.php 2.9 KB

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