location_form.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. $customer = $location->getCustomer();
  14. $location->dts_updated = Database::dts();
  15. $core->addBreadcrumb($customer->getUrl(), $customer->name);
  16. $core->addBreadcrumb($location->getUrl(), $location->name);
  17. $address = $location->getAddress();
  18. $title = __("Update Location");
  19. }
  20. else {
  21. $location = new Location();
  22. $customer_id = $request->int("customer_id");
  23. if($customer_id) {
  24. $customer = Customer::require($customer_id);
  25. $location->customer_id = $customer_id;
  26. $location->name = $customer->name;
  27. $location->phone = $customer->phone;
  28. $location->email = $customer->email;
  29. $core->addBreadcrumb($customer->getUrl(), $customer->name);
  30. $address = $customer->getAddress();
  31. }
  32. else {
  33. $address = $location->getAddress();
  34. }
  35. $location->dts_created = Database::dts();
  36. $title = __("Add Location");
  37. }
  38. $core->addBreadcrumb($_SERVER["REQUEST_URI"], $title);
  39. $submit = $request->int("submit");
  40. if($submit) {
  41. try {
  42. $user->requireValidCsrfToken();
  43. Location::updateFromRequest($request, $location);
  44. Session::addAlert("success", __("Location saved successfully."));
  45. Http::redirect($location->getUrl());
  46. }
  47. catch(RuntimeException $e) {
  48. Session::addAlert("danger", $e->getMessage());
  49. }
  50. }
  51. View::include("header");
  52. View::include("entity_form", [
  53. "title" => $title,
  54. "model" => $location,
  55. "address" => $address
  56. ]);
  57. View::include("footer");