location_form.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. $location->customer_id = $customer->id;
  39. $location->name = $request->string("name");
  40. $location->phone = $request->string("phone");
  41. $location->fax = $request->string("fax");
  42. $location->email = $request->email("email");
  43. $address->address_1 = $request->string("address_1");
  44. $address->address_2 = $request->string("address_2");
  45. $address->city = $request->string("city");
  46. $address->county = $request->string("county");
  47. $address->postal_code = $request->string("postal_code");
  48. $address->country = $request->string("country");
  49. $address->latitude = $request->float("latitude");
  50. $address->longitude = $request->float("longitude");
  51. if(!$location->name) {
  52. throw new RuntimeException(__("Name is required."));
  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() . "/update");
  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");