| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- $page_title = "Add Location";
- $customer = Customer::getForAdmin();
- if(!$customer) throw new Exception("You do not have permission to access this page.");
- Core::addBreadcrumb("/account", "Account");
- Core::addBreadcrumb("/account/locations", "Locations");
- Core::addBreadcrumb("/account/locations?customer_id=$customer->id", "$customer->name Locations");
- Core::addBreadcrumb("/account/locations/add", $page_title);
- $location = new Location;
- $location->customer_id = $customer->id;
- $location->country = DEFAULT_COUNTRY;
- $submit = http::value("submit");
- if($submit) {
- try {
- Core::validateCSRFToken($submit);
- $form_exception = new FormException;
- $location->name = http::value("name", "trim");
- $location->address_1 = http::value("address_1", "trim");
- $location->address_2 = http::value("address_2", "trim");
- $location->city = http::value("city", "trim");
- $location->county = http::value("county", "trim");
- $location->postal_code = http::value("postal_code", "trim");
- $location->country = http::value("country", "trim");
- $location->email = http::value("email", "email");
- $location->phone = http::value("phone", "trim");
- $location->opening_hours = http::value("opening_hours", "trim");
- $location->shop_left = http::value("shop_left", "trim");
- $location->shop_right = http::value("shop_right", "trim");
- $location->legal_entity_name = http::value("legal_entity_name", "trim");
- $location->internal_id = http::value("internal_id", "trim");
- $location->brand = http::value("brand", "trim");
- $location->region = http::value("region", "trim");
- $location->area_manager = http::value("area_manager", "trim");
- $location->area_manager_email = http::value("area_manager_email", "email");
- if(empty($location->name)) $form_exception->addError("Name is required.");
- if(empty($location->address_1)) $form_exception->addError("Location line 1 is required.");
- if(empty($location->city)) $form_exception->addError("City is required.");
- if(empty($location->postal_code)) $form_exception->addError("Postal code is required.");
- if(empty($location->country)) $form_exception->addError("Country is required.");
- // Geocode
- try {
- $results = Geolocation::geocode($location->getDisplayAddress(["address_1", "address_2", "city", "postal_code", "country"]));
- $result = $results[0];
- foreach($result->address_components as $component) {
- $types = $component->types;
- if(in_array("administrative_area_level_1", $types)) $location->state = $component->long_name;
- }
- $location->latitude = $result->geometry->location->lat;
- $location->longitude = $result->geometry->location->lng;
- }
- catch(Exception $e) {
- $form_exception->addError("Failed to get latitude/longitude from address.");
- }
- if($form_exception->hasErrors()) throw $form_exception;
- $success = $location->commit();
- if(!$success) throw new Exception("Failed to add location at this time. Please try again later.");
- Core::addAlert("success", "Location added successfully.");
- HTTP::redirect("/account/locations/$location->id/edit");
- }
- catch(FormException $e) {
- $e->generateAlerts();
- }
- catch(Exception $e) {
- Core::addAlert("danger", $e->getMessage());
- }
- }
- View::includeOnce("header", compact("page_title"));
- View::include("location_form", compact("location"));
- View::includeOnce("footer");
- ?>
|