location_form.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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->fax = $customer->fax;
  29. $location->email = $customer->email;
  30. $core->addBreadcrumb($customer->getUrl(), $customer->name);
  31. $address = $customer->getAddress();
  32. }
  33. else {
  34. $address = $location->getAddress();
  35. }
  36. $location->dts_created = Database::dts();
  37. $title = __("Add Location");
  38. }
  39. $core->addBreadcrumb($_SERVER["REQUEST_URI"], $title);
  40. $submit = $request->int("submit");
  41. if($submit) {
  42. try {
  43. $user->requireValidCsrfToken();
  44. Location::updateFromRequest($request, $location);
  45. Session::addAlert("success", __("Location saved successfully."));
  46. Http::redirect($location->getUrl());
  47. }
  48. catch(RuntimeException $e) {
  49. Session::addAlert("danger", $e->getMessage());
  50. }
  51. }
  52. View::include("header");
  53. View::include("entity_form", [
  54. "title" => $title,
  55. "model" => $location,
  56. "address" => $address
  57. ]);
  58. View::include("footer");