location_form.php 1.4 KB

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