template_add.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if(empty($template->name)) $form_exception->addError("Name is required.");
  19. if(empty($template->json)) $form_exception->addError("JSON is required.");
  20. $json = json_decode($template->json);
  21. if(!$json) {
  22. $form_exception->addError("JSON error: " . json_last_error_msg());
  23. }
  24. if($form_exception->hasErrors()) throw $form_exception;
  25. $success = $template->commit();
  26. Core::addAlert("success", "Template added successfully.");
  27. HTTP::redirect("/account/templates?customer_id=$customer->id");
  28. }
  29. catch(FormException $e) {
  30. $e->generateAlerts();
  31. }
  32. catch(Exception $e) {
  33. Core::addAlert("danger", $e->getMessage());
  34. }
  35. }
  36. View::includeOnce("header", compact("page_title"));
  37. View::include("template_add_form", compact("template"));
  38. View::includeOnce("footer");
  39. ?>