location_form.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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::get($id);
  13. if(!$location) {
  14. throw new RuntimeException(__("Location not found."));
  15. }
  16. $customer = $location->getCustomer();
  17. $location->dts_updated = Database::dts();
  18. $core->addBreadcrumb($location->getUrl(), $location->name);
  19. $title = __("Update Location");
  20. }
  21. else {
  22. $customer_id = $request->int("customer_id");
  23. $customer = Customer::get($customer_id);
  24. if(!$customer) {
  25. throw new RuntimeException(__("Customer not found."));
  26. }
  27. $location = new Location();
  28. $location->customer_id = $customer_id;
  29. $location->dts_created = Database::dts();
  30. $title = __("Add Location");
  31. }
  32. $core->addBreadcrumb($_SERVER["REQUEST_URI"], $title);
  33. $address = $location->getAddress();
  34. $submit = $request->int("submit");
  35. if($submit) {
  36. try {
  37. $user->requireValidCsrfToken();
  38. $customer_id = $request->int("customer_id");
  39. $customer = Customer::require($customer_id);
  40. $location->customer_id = $customer->id;
  41. $location->name = $request->string("name");
  42. $location->phone = $request->string("phone");
  43. $location->fax = $request->string("fax");
  44. $location->email = $request->email("email");
  45. $location->contact_name = $request->string("contact_name");
  46. $location->contact_phone = $request->string("contact_phone");
  47. $location->contact_email = $request->email("contact_email");
  48. $address->address_1 = $request->string("address_1");
  49. $address->address_2 = $request->string("address_2");
  50. $address->city = $request->string("city");
  51. $address->county = $request->string("county");
  52. $address->postal_code = $request->string("postal_code");
  53. $address->country = $request->string("country");
  54. $address->latitude = $request->float("latitude");
  55. $address->longitude = $request->float("longitude");
  56. if(!$location->name) {
  57. throw new RuntimeException(__("Name is required."));
  58. }
  59. if(isset($address->postal_code, $address->country) && !$address->hasGeolocation()) {
  60. $address->geocode();
  61. }
  62. $address->commit();
  63. $location->address_id = $address->id;
  64. $location->commit();
  65. Session::addAlert("success", __("Location saved successfully."));
  66. Http::redirect($location->getUrl());
  67. }
  68. catch(RuntimeException $e) {
  69. Session::addAlert("danger", $e->getMessage());
  70. }
  71. }
  72. View::include("header");
  73. View::include("entity_form", [
  74. "title" => $title,
  75. "model" => $location,
  76. "address" => $address
  77. ]);
  78. View::include("footer");