customer_add.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. $customer->archived = (int) http::value("archived", "int");
  31. $cc_email_addresses = HTTP::value("cc_email_addresses", "trim");
  32. if($cc_email_addresses) {
  33. try {
  34. $customer->setCcEmailAddresses($cc_email_addresses);
  35. }
  36. catch(Exception $e) {
  37. $form_exception->addError($e->getMessage());
  38. }
  39. }
  40. if(empty($customer->name)) $form_exception->addError("Name is required.");
  41. if($form_exception->hasErrors()) throw $form_exception;
  42. $success = $customer->commit();
  43. Core::addAlert("success", "Customer added successfully.");
  44. HTTP::redirect("/account/customers");
  45. }
  46. catch(FormException $e) {
  47. $e->generateAlerts();
  48. }
  49. catch(Exception $e) {
  50. Core::addAlert("danger", $e->getMessage());
  51. }
  52. }
  53. View::includeOnce("header", compact("page_title"));
  54. View::include("customer_form", compact("page_title", "customer"));
  55. View::includeOnce("footer");