template_add.php 1.3 KB

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