| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php /** @noinspection PhpUnhandledExceptionInspection */
- namespace App;
- use RuntimeException;
- /**
- * @var int $id
- */
- $request = Request::fromRequest();
- $core = Core::getInstance();
- $core->addBreadcrumb("/locations", __("Locations"));
- $user = $core->getUser();
- if(isset($id)) {
- $location = Location::require($id);
- $location->dts_updated = Database::dts();
- $core->addBreadcrumb($location->getUrl(), $location->name);
- $title = __("Update Location");
- }
- else {
- $customer_id = $request->int("customer_id");
- $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();
- $customer_id = $request->int("customer_id");
- $customer = Customer::require($customer_id);
- $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");
- $location->contact_name = $request->string("contact_name");
- $location->contact_phone = $request->string("contact_phone");
- $location->contact_email = $request->email("contact_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."));
- }
- if(isset($address->postal_code, $address->country) && !$address->hasGeolocation()) {
- $address->geocode();
- }
- $address->commit();
- $location->address_id = $address->id;
- $location->commit();
- Session::addAlert("success", __("Location saved successfully."));
- Http::redirect($location->getUrl());
- }
- catch(RuntimeException $e) {
- Session::addAlert("danger", $e->getMessage());
- }
- }
- View::include("header");
- View::include("entity_form", [
- "title" => $title,
- "model" => $location,
- "address" => $address
- ]);
- View::include("footer");
|