customer_add.php 1.8 KB

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