1
0

template_add.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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->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 added successfully.");
  35. HTTP::redirect("/account/templates?customer_id=$customer->id");
  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");