| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- $template = Template::get($id);
- if(!$template) throw new Exception("Template not found.");
- $customer = $template->getCustomer();
- $page_title = "Edit Template";
- Core::addBreadcrumb("/account", "Account");
- Core::addBreadcrumb("/account/templates", "Templates");
- Core::addBreadcrumb("/account/templates?customer_id=$customer->id", "$customer->name Templates");
- Core::addBreadcrumb("/account/templates/$id", $template->name);
- Core::addBreadcrumb($_SERVER["REQUEST_URI"], $page_title);
- $submit = http::value("submit");
- if($submit) {
- try {
- Core::validateCSRFToken($submit);
- $form_exception = new FormException;
- $template->name = http::value("name", "trim");
- $template->json = http::value("json", "trim");
- $template->json_file = http::value("json_file", "trim");
- $template->practice = http::value("practice", "int");
- if(empty($template->name)) $form_exception->addError("Name is required.");
- if(empty($template->json) && empty($template->json_file)) $form_exception->addError("JSON or JSON File is required.");
- if($template->json) {
- $json = json_decode($template->json);
- if(!$json) {
- $form_exception->addError("JSON error: " . json_last_error_msg());
- }
- }
- if($template->json_file) {
- $path = ROOT_DIR . "/json/" . $template->json_file;
- if(!file_exists($path)) $form_exception->addError("JSON file does not exist.");
- }
- if($form_exception->hasErrors()) throw $form_exception;
- $success = $template->commit();
- Core::addAlert("success", "Template updated successfully.");
- HTTP::redirect("/account/templates/$id/edit");
- }
- catch(FormException $e) {
- $e->generateAlerts();
- }
- catch(Exception $e) {
- Core::addAlert("danger", $e->getMessage());
- }
- }
- View::includeOnce("header", compact("page_title"));
- View::include("template_form", compact("page_title", "template"));
- View::includeOnce("footer");
|