customer_add.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. $page_title = "Add Customer";
  3. Core::addScript("public/js/app-customer-form.js");
  4. Core::addBreadcrumb("/account", "Account");
  5. Core::addBreadcrumb("/account/customers", "Customers");
  6. Core::addBreadcrumb("/account/customers/add", $page_title);
  7. $customer = new Customer();
  8. $customer->country = DEFAULT_COUNTRY;
  9. $customer->show_in_map_overview = 1;
  10. $customer->email_after_test = 1;
  11. $customer->email_locations_after_test = 1;
  12. $submit = http::value("submit");
  13. if($submit) {
  14. try {
  15. Core::validateCSRFToken($submit);
  16. $form_exception = new FormException();
  17. $customer->name = http::value("name", "trim");
  18. $customer->address_1 = http::value("address_1", "trim");
  19. $customer->address_2 = http::value("address_2", "trim");
  20. $customer->city = http::value("city", "trim");
  21. $customer->county = http::value("county", "trim");
  22. $customer->postal_code = http::value("postal_code", "trim");
  23. $customer->country = http::value("country", "trim");
  24. $customer->email = http::value("email", "email");
  25. $customer->phone = http::value("phone", "trim");
  26. $customer->email_after_test = (int) http::value("email_after_test", "int");
  27. $customer->email_locations_after_test = (int) http::value("email_locations_after_test", "int");
  28. $customer->year_cycle_month = http::value("year_cycle_month", "int");
  29. $customer->show_in_map_overview = (int) http::value("show_in_map_overview", "int");
  30. $cc_email_addresses = HTTP::value("cc_email_addresses", "trim");
  31. if($cc_email_addresses) {
  32. try {
  33. $customer->setCcEmailAddresses($cc_email_addresses);
  34. }
  35. catch(Exception $e) {
  36. $form_exception->addError($e->getMessage());
  37. }
  38. }
  39. if(empty($customer->name)) $form_exception->addError("Name is required.");
  40. if($form_exception->hasErrors()) throw $form_exception;
  41. $success = $customer->commit();
  42. Core::addAlert("success", "Customer added successfully.");
  43. HTTP::redirect("/account/customers");
  44. }
  45. catch(FormException $e) {
  46. $e->generateAlerts();
  47. }
  48. catch(Exception $e) {
  49. Core::addAlert("danger", $e->getMessage());
  50. }
  51. }
  52. View::includeOnce("header", compact("page_title"));
  53. View::include("customer_form", compact("page_title", "customer"));
  54. View::includeOnce("footer");