From f3873ed61c7dab6bbf00c8feeaf498f48ad59ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Tue, 20 Sep 2016 20:14:02 +0200 Subject: [PATCH] FEATURE: add functionality for modifying content page on template level --- app/Handlers/Contracts/TemplateHandler.php | 52 ++++++++++++++++++- .../Controllers/Admin/CreateNewContent.php | 8 +++ app/Http/Controllers/Admin/EditContent.php | 26 ++++++++-- app/Http/Controllers/Visitor/ContentPage.php | 2 +- templates/Base/OnePage/Handlers/OnePage.php | 5 -- templates/Jednostrankovka/Handler.php | 51 +++++++++++++++--- 6 files changed, 125 insertions(+), 19 deletions(-) diff --git a/app/Handlers/Contracts/TemplateHandler.php b/app/Handlers/Contracts/TemplateHandler.php index d21fb126..8be4abda 100644 --- a/app/Handlers/Contracts/TemplateHandler.php +++ b/app/Handlers/Contracts/TemplateHandler.php @@ -1,14 +1,64 @@ 'Article', 'templateName' => 'article::template.name'] + * ]; + * + * When no "namespace" is specified, value strtolower(module) is used instaed. + * + * @return array + */ public static function getPublicTemplatePaths(); - public static function beforeRender(Content $content); + /** + * Prepare Content object to be rendered using Blade template + * + * @param Content $content + */ + public static function beforeRender(Content & $content); + + /** + * Modify input data into format which can be understood by edit template + * + * @param Content $content + */ + public static function beforePopulatingEditForm(Content & $content); + + /** + * Slightly modify Content object before saving into database. + * + * This way we can do almost whatever we want - even in template + * + * @param Content $content + * @param Request $request + */ + public static function beforeSave(Content & $content, Request $request); } \ No newline at end of file diff --git a/app/Http/Controllers/Admin/CreateNewContent.php b/app/Http/Controllers/Admin/CreateNewContent.php index 1beee33b..f18c60c3 100644 --- a/app/Http/Controllers/Admin/CreateNewContent.php +++ b/app/Http/Controllers/Admin/CreateNewContent.php @@ -14,6 +14,7 @@ use Mcamara\LaravelLocalization\Facades\LaravelLocalization; use Modules\Categories\Entities\Category; use Pingpong\Modules\Facades\Module as ModuleInstance; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; + class CreateNewContent extends Controller { /** @@ -87,6 +88,9 @@ class CreateNewContent extends Controller public function postNew(CreateNewContentRequest $request, $moduleId) { $page = new Content(); + $template = Module::where('is_template', true) + ->where('enabled', true) + ->first(); try { $module = Module::findOrFail($moduleId); } catch (NotFoundHttpException $e) { @@ -118,6 +122,10 @@ class CreateNewContent extends Controller // upload file - if provided $page->uploadTitlePhoto($request); + // let template to modify request and content page + + call_user_func_array([$template->handler, 'beforeSave'], [&$page, $request]); + // save module-specific stuff call_user_func_array([$module->handler, 'createNew'], [&$page, $request]); diff --git a/app/Http/Controllers/Admin/EditContent.php b/app/Http/Controllers/Admin/EditContent.php index cae69a9a..e2d6e299 100644 --- a/app/Http/Controllers/Admin/EditContent.php +++ b/app/Http/Controllers/Admin/EditContent.php @@ -56,23 +56,32 @@ class EditContent extends Controller $page = Content::findOrFail($id); $page->module_settings = json_decode($page->module_settings); - $allModules = ModulesHandlerToId::getWithTranslations(); - $moduleId = $allModules[$page->module_handler]; - $module = Module::findOrFail($moduleId); + // select template + $template = Module::where('is_template', true) + ->where('enabled', true) + ->first(); + + // get module ID + $module = Module::where('handler', $page->module_handler)->firstOrFail(); - $templates = Template::where('module_id', $moduleId) + // get all Blade templates for rendering page + $templates = Template::where('module_id', $module->id) ->select(['template_name', 'name', 'description']) ->get(); + // get module for Categories - PingPong module $categoryModule = ModuleInstance::get('categories'); // add categories, if supported $categories = []; if ($module->support_categories && $categoryModule->enabled()) { - $categories = Category::where('module_id', $moduleId) + $categories = Category::where('module_id', $module->id) ->get(); } + // modify data for edit form according to installed template + call_user_func_array([$template->handler, 'beforePopulatingEditForm'], [&$page]); + $history = $this->history->getStructuredHistoryOf($page); // render view @@ -107,6 +116,10 @@ class EditContent extends Controller * @var $page Content */ $page = Content::findOrFail($id); + + $template = Module::where('is_template', true) + ->where('enabled', true) + ->first(); } catch (NotFoundHttpException $e) { // log attempt of invalid edit @@ -118,6 +131,9 @@ class EditContent extends Controller ->with('error', trans('admin/contents.invalidPage')); } + // let template to modify request and content page + call_user_func_array([$template->handler, 'beforeSave'], [&$page, $request]); + $request->url = Str::slug($request->url); $request->parentPage = $request->parentPage ? $request->parentPage : null; $request->titlePhotoFilename = $page->title_photo; diff --git a/app/Http/Controllers/Visitor/ContentPage.php b/app/Http/Controllers/Visitor/ContentPage.php index 28a384f4..ddfcef63 100644 --- a/app/Http/Controllers/Visitor/ContentPage.php +++ b/app/Http/Controllers/Visitor/ContentPage.php @@ -58,7 +58,7 @@ class ContentPage extends Controller $template = Module::where('is_template', true)->where('enabled', true)->first(); // do something template-specific - $page = call_user_func([$template->handler, 'beforeRender'], $page); + call_user_func_array([$template->handler, 'beforeRender'], [&$page]); // render page return $handler->render($page, ''); diff --git a/templates/Base/OnePage/Handlers/OnePage.php b/templates/Base/OnePage/Handlers/OnePage.php index 3c401045..5bfb3ffa 100644 --- a/templates/Base/OnePage/Handlers/OnePage.php +++ b/templates/Base/OnePage/Handlers/OnePage.php @@ -32,9 +32,4 @@ abstract class OnePage implements TemplateHandler public static function uninstall(Module $template) { } - - public static function beforeRender(Content $content) - { - return $content; - } } \ No newline at end of file diff --git a/templates/Jednostrankovka/Handler.php b/templates/Jednostrankovka/Handler.php index 6c4e1d35..d390e34a 100644 --- a/templates/Jednostrankovka/Handler.php +++ b/templates/Jednostrankovka/Handler.php @@ -1,11 +1,55 @@ 'Article', 'templateName' => 'article::template.name'] + * ]; + * + * When no "namespace" is specified, value strtolower(module) is used instaed. + * + * @return array + */ public static function getPublicTemplatePaths() { return [ @@ -19,11 +63,4 @@ class Handler extends OnePage ] ]; } - - public static function beforeRender(Content $content) - { - $content->content = str_replace('a', 'AAAA', $content->content); - - return $content; - } }