1
0

template_edit.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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->json_file = http::value("json_file", "trim");
  19. $template->practice = http::value("practice", "int");
  20. if(empty($template->name)) $form_exception->addError("Name is required.");
  21. if(empty($template->json) && empty($template->json_file)) $form_exception->addError("JSON or JSON File is required.");
  22. if($template->json) {
  23. $json = json_decode($template->json);
  24. if(!$json) {
  25. $form_exception->addError("JSON error: " . json_last_error_msg());
  26. }
  27. }
  28. if($template->json_file) {
  29. $path = ROOT_DIR . "/json/" . $template->json_file;
  30. if(!file_exists($path)) $form_exception->addError("JSON file does not exist.");
  31. }
  32. if($form_exception->hasErrors()) throw $form_exception;
  33. $success = $template->commit();
  34. Core::addAlert("success", "Template updated successfully.");
  35. HTTP::redirect("/account/templates/$id/edit");
  36. }
  37. catch(FormException $e) {
  38. $e->generateAlerts();
  39. }
  40. catch(Exception $e) {
  41. Core::addAlert("danger", $e->getMessage());
  42. }
  43. }
  44. View::includeOnce("header", compact("page_title"));
  45. View::include("template_form", compact("page_title", "template"));
  46. View::includeOnce("footer");