addBreadcrumb("/locations", __("Locations")); $user = $core->getUser(); if(isset($id)) { $location = Location::get($id); if(!$location) { throw new RuntimeException(__("Location not found.")); } $customer = $location->getCustomer(); $location->dts_updated = Database::dts(); $core->addBreadcrumb($location->getUrl(), $location->name); $title = __("Update Location"); } else { $customer_id = $request->int("customer_id"); $customer = Customer::get($customer_id); if(!$customer) { throw new RuntimeException(__("Customer not found.")); } $location = new Location(); $location->customer_id = $customer_id; $location->dts_created = Database::dts(); $title = __("Add Location"); } $core->addBreadcrumb($_SERVER["REQUEST_URI"], $title); $address = $location->getAddress(); $submit = $request->int("submit"); if($submit) { try { $user->requireValidCsrfToken(); $location->customer_id = $customer->id; $location->name = $request->string("name"); $location->phone = $request->string("phone"); $location->fax = $request->string("fax"); $location->email = $request->email("email"); $address->address_1 = $request->string("address_1"); $address->address_2 = $request->string("address_2"); $address->city = $request->string("city"); $address->county = $request->string("county"); $address->postal_code = $request->string("postal_code"); $address->country = $request->string("country"); $address->latitude = $request->float("latitude"); $address->longitude = $request->float("longitude"); if(!$location->name) { throw new RuntimeException(__("Name is required.")); } $address->commit(); $location->address_id = $address->id; $location->commit(); Session::addAlert("success", __("Location saved successfully.")); Http::redirect($location->getUrl() . "/update"); } catch(RuntimeException $e) { Session::addAlert("danger", $e->getMessage()); } } View::include("header"); View::include("entity_form", [ "title" => $title, "model" => $location, "address" => $address ]); View::include("footer");