id", "$customer->name Locations"); Core::addBreadcrumb("/account/locations/add", $page_title); $location = new Location; $location->customer_id = $customer->id; $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->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->region = http::value("region", "trim"); $location->internal_id = http::value("internal_id", "trim"); $location->area_manager = http::value("area_manager", "trim"); 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."); // Geocode try { $results = Geolocation::geocode($location->getDisplayAddress(["address_1", "address_2", "city", "postal_code"])); $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; if(in_array("country", $types)) $location->country = $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"); ?>