location_form.php 1.4 KB

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