customer_edit.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. $customer = Customer::get($id);
  3. if(!$customer) throw new Exception("Customer not found.");
  4. $page_title = "Edit Customer";
  5. Core::addScript("public/js/app-customer-form.js");
  6. Core::addBreadcrumb("/account", "Account");
  7. Core::addBreadcrumb("/account/customers", "Customers");
  8. Core::addBreadcrumb("/account/customers/$id", $customer->name);
  9. Core::addBreadcrumb("/account/customers/$id/edit", $page_title);
  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->invoice_contact_name = http::value("invoice_contact_name", "trim");
  25. $customer->email_after_test = http::value("email_after_test", "int");
  26. $customer->email_locations_after_test = http::value("email_locations_after_test", "int");
  27. $customer->year_cycle_month = http::value("year_cycle_month", "int");
  28. $customer->show_in_map_overview = (int) http::value("show_in_map_overview", "int");
  29. $customer->archived = (int) http::value("archived", "int");
  30. $customer->config_json = http::value("config_json", "trim");
  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 updated successfully.");
  44. HTTP::redirect("/account/customers/$id/edit");
  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");