template_add.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. $page_title = "Add Template";
  3. Core::addBreadcrumb("/account", "Account");
  4. Core::addBreadcrumb("/account/templates", "Templates");
  5. $customer = Customer::getForAdmin();
  6. Core::addBreadcrumb("/account/templates?customer_id=$customer->id", "$customer->name Templates");
  7. Core::addBreadcrumb($_SERVER["REQUEST_URI"], $page_title);
  8. $template = new Template;
  9. $template->customer_id = $customer->id;
  10. $template->json = "";
  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 added successfully.");
  28. HTTP::redirect("/account/templates?customer_id=$customer->id");
  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. ?>