From 4011f8ffadd2a411316eb6fa82241e1ebfc92af1 Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 28 May 2019 12:44:10 +0300 Subject: [PATCH] Flextype Box Plugin: Admin #125 #117 - Templates Controller/Views implementation --- .../app/Controllers/TemplatesController.php | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/site/plugins/admin/app/Controllers/TemplatesController.php b/site/plugins/admin/app/Controllers/TemplatesController.php index 7b7df183..5018d0e5 100644 --- a/site/plugins/admin/app/Controllers/TemplatesController.php +++ b/site/plugins/admin/app/Controllers/TemplatesController.php @@ -3,6 +3,7 @@ namespace Flextype; use Flextype\Component\Filesystem\Filesystem; +use Flextype\Component\Text\Text; use function Flextype\Component\I18n\__; class TemplatesController extends Controller @@ -33,12 +34,47 @@ class TemplatesController extends Controller public function add($request, $response, $args) { - + return $this->view->render($response, + 'plugins/admin/views/templates/extends/templates/add.html', [ + 'menu_item' => 'templates', + 'links' => [ + 'templates' => [ + 'link' => $this->router->pathFor('admin.templates.index'), + 'title' => __('admin_templates'), + 'attributes' => ['class' => 'navbar-item active'] + ], + ] + ]); } public function addProcess($request, $response, $args) { + $type = $request->getParsedBody()['type']; + if ($type == 'partial') { + $_type = '/templates/partials/'; + } else { + $_type = '/templates/'; + } + + $id = Text::safeString($request->getParsedBody()['id'], '-', true) . '.html'; + + $file = PATH['themes'] . '/' . $this->registry->get('settings.theme') . $_type . $id; + + if (!Filesystem::has($file)) { + if (Filesystem::write( + $file, + "" + )) { + $this->flash->addMessage('success', __('admin_message_'.$type.'_created')); + } else { + $this->flash->addMessage('error', __('admin_message_'.$type.'_was_not_created')); + } + } else { + $this->flash->addMessage('error', __('admin_message_'.$type.'_was_not_created')); + } + + return $response->withRedirect($this->container->get('router')->pathFor('admin.templates.index')); } public function edit($request, $response, $args) @@ -71,9 +107,9 @@ class TemplatesController extends Controller $_type = '/templates/'; } - $template_path = PATH['themes'] . '/' . $this->registry->get('settings.theme') . $_type . $request->getParsedBody()[$type.'-id'] . '.html'; + $file_path = PATH['themes'] . '/' . $this->registry->get('settings.theme') . $_type . $request->getParsedBody()[$type.'-id'] . '.html'; - if (Filesystem::delete($template_path)) { + if (Filesystem::delete($file_path)) { $this->flash->addMessage('success', __('admin_message_'.$type.'_deleted')); } else { $this->flash->addMessage('error', __('admin_message_'.$type.'_was_not_deleted')); @@ -92,10 +128,10 @@ class TemplatesController extends Controller $_type = '/templates/'; } - $template_path = PATH['themes'] . '/' . $this->registry->get('settings.theme') . $_type . $request->getParsedBody()[$type.'-id'] . '.html'; - $template_path_new = PATH['themes'] . '/' . $this->registry->get('settings.theme') . $_type . $request->getParsedBody()[$type.'-id'] . '-duplicate-' . date("Ymd_His") . '.html'; + $file_path = PATH['themes'] . '/' . $this->registry->get('settings.theme') . $_type . $request->getParsedBody()[$type.'-id'] . '.html'; + $file_path_new = PATH['themes'] . '/' . $this->registry->get('settings.theme') . $_type . $request->getParsedBody()[$type.'-id'] . '-duplicate-' . date("Ymd_His") . '.html'; - if (Filesystem::copy($template_path, $template_path_new)) { + if (Filesystem::copy($file_path, $file_path_new)) { $this->flash->addMessage('success', __('admin_message_'.$type.'_duplicated')); } else { $this->flash->addMessage('error', __('admin_message_'.$type.'_was_not_duplicated'));