1
0

template_edit.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. $template = Template::get($id);
  3. if(!$template) throw new Exception("Template not found.");
  4. $customer = $template->getCustomer();
  5. $page_title = "Edit Template";
  6. Core::addBreadcrumb("/account", "Account");
  7. Core::addBreadcrumb("/account/templates", "Templates");
  8. Core::addBreadcrumb("/account/templates?customer_id=$customer->id", "$customer->name Templates");
  9. Core::addBreadcrumb("/account/templates/$id", $template->name);
  10. Core::addBreadcrumb($_SERVER["REQUEST_URI"], $page_title);
  11. $submit = http::value("submit");
  12. if($submit) {
  13. try {
  14. Core::validateCSRFToken($submit);
  15. $form_exception = new FormException;
  16. $template->name = http::value("name", "trim");
  17. $template->json = http::value("json", "trim");
  18. $template->practice = http::value("practice", "int");
  19. if(empty($template->name)) $form_exception->addError("Name is required.");
  20. if(empty($template->json)) $form_exception->addError("JSON is required.");
  21. $json = json_decode($template->json);
  22. if(!$json) {
  23. $form_exception->addError("JSON error: " . json_last_error_msg());
  24. }
  25. if($form_exception->hasErrors()) throw $form_exception;
  26. $success = $template->commit();
  27. Core::addAlert("success", "Template updated successfully.");
  28. HTTP::redirect("/account/templates/$id/edit");
  29. }
  30. catch(FormException $e) {
  31. $e->generateAlerts();
  32. }
  33. catch(Exception $e) {
  34. Core::addAlert("danger", $e->getMessage());
  35. }
  36. }
  37. View::includeOnce("header", compact("page_title"));
  38. View::include("template_form", compact("page_title", "template"));
  39. View::includeOnce("footer");
  40. ?>