1
0

customer_form.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App;
  3. use RuntimeException;
  4. /**
  5. * @var int $id
  6. */
  7. $core = Core::getInstance();
  8. $core->addBreadcrumb("/customers", __("Customers"));
  9. $user = $core->getUser();
  10. if(isset($id)) {
  11. $customer = Customer::require($id);
  12. $customer->dts_updated = Database::dts();
  13. $core->addBreadcrumb($customer->getUrl(), $customer->name);
  14. $title = __("Update Customer");
  15. }
  16. else {
  17. $customer = new Customer();
  18. $customer->dts_created = Database::dts();
  19. $title = __("Add Customer");
  20. }
  21. $core->addBreadcrumb($_SERVER["REQUEST_URI"], $title);
  22. $request = Request::fromPost();
  23. $submit = $request->int("submit");
  24. if($submit) {
  25. try {
  26. $user->requireValidCsrfToken();
  27. $customer = Customer::updateFromRequest($request, $customer);
  28. $create_location = $request->int("create_location");
  29. if($create_location) {
  30. $location = new Location();
  31. $location->dts_created = Database::dts();
  32. Location::updateFromRequest($request, $location, $customer);
  33. }
  34. Session::addAlert("success", __("Customer saved successfully."));
  35. Http::redirect($customer->getUrl());
  36. }
  37. catch(RuntimeException $e) {
  38. Session::addAlert("danger", $e->getMessage());
  39. }
  40. }
  41. $core->addScript("/js/entity-form.js");
  42. View::include("header");
  43. View::include("entity_form", [
  44. "title" => $title,
  45. "model" => $customer,
  46. "address" => $customer->getAddress()
  47. ]);
  48. View::include("footer");