location_form.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App;
  3. use RuntimeException;
  4. /**
  5. * @var int $id
  6. */
  7. $request = Request::fromRequest();
  8. $core = Core::getInstance();
  9. $core->addBreadcrumb("/locations", __("Locations"));
  10. $user = $core->getUser();
  11. if(isset($id)) {
  12. $location = Location::require($id);
  13. $location->dts_updated = Database::dts();
  14. $core->addBreadcrumb($location->getUrl(), $location->name);
  15. $title = __("Update Location");
  16. }
  17. else {
  18. $customer_id = $request->int("customer_id");
  19. $location = new Location();
  20. $location->customer_id = $customer_id;
  21. $location->dts_created = Database::dts();
  22. $title = __("Add Location");
  23. }
  24. $core->addBreadcrumb($_SERVER["REQUEST_URI"], $title);
  25. $address = $location->getAddress();
  26. $submit = $request->int("submit");
  27. if($submit) {
  28. try {
  29. $user->requireValidCsrfToken();
  30. $customer_id = $request->int("customer_id");
  31. $customer = Customer::require($customer_id);
  32. $location->customer_id = $customer->id;
  33. $location->name = $request->string("name");
  34. $location->phone = $request->string("phone");
  35. $location->fax = $request->string("fax");
  36. $location->email = $request->email("email");
  37. $location->contact_name = $request->string("contact_name");
  38. $location->contact_phone = $request->string("contact_phone");
  39. $location->contact_email = $request->email("contact_email");
  40. $address->address_1 = $request->string("address_1");
  41. $address->address_2 = $request->string("address_2");
  42. $address->city = $request->string("city");
  43. $address->county = $request->string("county");
  44. $address->postal_code = $request->string("postal_code");
  45. $address->country = $request->string("country");
  46. $address->latitude = $request->float("latitude");
  47. $address->longitude = $request->float("longitude");
  48. if(!$location->name) {
  49. throw new RuntimeException(__("Name is required."));
  50. }
  51. if(isset($address->postal_code, $address->country) && !$address->hasGeolocation()) {
  52. $address->geocode();
  53. }
  54. $address->commit();
  55. $location->address_id = $address->id;
  56. $location->commit();
  57. Session::addAlert("success", __("Location saved successfully."));
  58. Http::redirect($location->getUrl());
  59. }
  60. catch(RuntimeException $e) {
  61. Session::addAlert("danger", $e->getMessage());
  62. }
  63. }
  64. View::include("header");
  65. View::include("entity_form", [
  66. "title" => $title,
  67. "model" => $location,
  68. "address" => $address
  69. ]);
  70. View::include("footer");