FEATURE: add functionality for modifying content page on template level
This commit is contained in:
@@ -1,14 +1,64 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace App\Handlers\Contracts;
|
namespace App\Handlers\Contracts;
|
||||||
|
|
||||||
|
use App\Http\Requests\Request;
|
||||||
use App\Models\Content;
|
use App\Models\Content;
|
||||||
use App\Models\Module;
|
use App\Models\Module;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface TemplateHandler
|
||||||
|
*
|
||||||
|
* Inerface used for registering template into CMS core.
|
||||||
|
*
|
||||||
|
* @package App\Handlers\Contracts
|
||||||
|
*/
|
||||||
interface TemplateHandler extends BaseModuleHandler
|
interface TemplateHandler extends BaseModuleHandler
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Uninstall template
|
||||||
|
*
|
||||||
|
* Delete some items in database, delete files etc.
|
||||||
|
*
|
||||||
|
* @param Module $template
|
||||||
|
*/
|
||||||
public static function uninstall(Module $template);
|
public static function uninstall(Module $template);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get list of registered templates and supported modules.
|
||||||
|
*
|
||||||
|
* Returned array is expected to be array of arrays with following items: module, namespace and templateName.
|
||||||
|
* Example:
|
||||||
|
* return [
|
||||||
|
* ['module' => '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 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);
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,7 @@ use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
|||||||
use Modules\Categories\Entities\Category;
|
use Modules\Categories\Entities\Category;
|
||||||
use Pingpong\Modules\Facades\Module as ModuleInstance;
|
use Pingpong\Modules\Facades\Module as ModuleInstance;
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
|
|
||||||
class CreateNewContent extends Controller
|
class CreateNewContent extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -87,6 +88,9 @@ class CreateNewContent extends Controller
|
|||||||
public function postNew(CreateNewContentRequest $request, $moduleId)
|
public function postNew(CreateNewContentRequest $request, $moduleId)
|
||||||
{
|
{
|
||||||
$page = new Content();
|
$page = new Content();
|
||||||
|
$template = Module::where('is_template', true)
|
||||||
|
->where('enabled', true)
|
||||||
|
->first();
|
||||||
try {
|
try {
|
||||||
$module = Module::findOrFail($moduleId);
|
$module = Module::findOrFail($moduleId);
|
||||||
} catch (NotFoundHttpException $e) {
|
} catch (NotFoundHttpException $e) {
|
||||||
@@ -118,6 +122,10 @@ class CreateNewContent extends Controller
|
|||||||
// upload file - if provided
|
// upload file - if provided
|
||||||
$page->uploadTitlePhoto($request);
|
$page->uploadTitlePhoto($request);
|
||||||
|
|
||||||
|
// let template to modify request and content page
|
||||||
|
|
||||||
|
call_user_func_array([$template->handler, 'beforeSave'], [&$page, $request]);
|
||||||
|
|
||||||
// save module-specific stuff
|
// save module-specific stuff
|
||||||
call_user_func_array([$module->handler, 'createNew'], [&$page, $request]);
|
call_user_func_array([$module->handler, 'createNew'], [&$page, $request]);
|
||||||
|
|
||||||
|
|||||||
@@ -56,23 +56,32 @@ class EditContent extends Controller
|
|||||||
$page = Content::findOrFail($id);
|
$page = Content::findOrFail($id);
|
||||||
$page->module_settings = json_decode($page->module_settings);
|
$page->module_settings = json_decode($page->module_settings);
|
||||||
|
|
||||||
$allModules = ModulesHandlerToId::getWithTranslations();
|
// select template
|
||||||
$moduleId = $allModules[$page->module_handler];
|
$template = Module::where('is_template', true)
|
||||||
$module = Module::findOrFail($moduleId);
|
->where('enabled', true)
|
||||||
|
->first();
|
||||||
|
|
||||||
$templates = Template::where('module_id', $moduleId)
|
// get module ID
|
||||||
|
$module = Module::where('handler', $page->module_handler)->firstOrFail();
|
||||||
|
|
||||||
|
// get all Blade templates for rendering page
|
||||||
|
$templates = Template::where('module_id', $module->id)
|
||||||
->select(['template_name', 'name', 'description'])
|
->select(['template_name', 'name', 'description'])
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
|
// get module for Categories - PingPong module
|
||||||
$categoryModule = ModuleInstance::get('categories');
|
$categoryModule = ModuleInstance::get('categories');
|
||||||
|
|
||||||
// add categories, if supported
|
// add categories, if supported
|
||||||
$categories = [];
|
$categories = [];
|
||||||
if ($module->support_categories && $categoryModule->enabled()) {
|
if ($module->support_categories && $categoryModule->enabled()) {
|
||||||
$categories = Category::where('module_id', $moduleId)
|
$categories = Category::where('module_id', $module->id)
|
||||||
->get();
|
->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// modify data for edit form according to installed template
|
||||||
|
call_user_func_array([$template->handler, 'beforePopulatingEditForm'], [&$page]);
|
||||||
|
|
||||||
$history = $this->history->getStructuredHistoryOf($page);
|
$history = $this->history->getStructuredHistoryOf($page);
|
||||||
|
|
||||||
// render view
|
// render view
|
||||||
@@ -107,6 +116,10 @@ class EditContent extends Controller
|
|||||||
* @var $page Content
|
* @var $page Content
|
||||||
*/
|
*/
|
||||||
$page = Content::findOrFail($id);
|
$page = Content::findOrFail($id);
|
||||||
|
|
||||||
|
$template = Module::where('is_template', true)
|
||||||
|
->where('enabled', true)
|
||||||
|
->first();
|
||||||
} catch (NotFoundHttpException $e) {
|
} catch (NotFoundHttpException $e) {
|
||||||
|
|
||||||
// log attempt of invalid edit
|
// log attempt of invalid edit
|
||||||
@@ -118,6 +131,9 @@ class EditContent extends Controller
|
|||||||
->with('error', trans('admin/contents.invalidPage'));
|
->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->url = Str::slug($request->url);
|
||||||
$request->parentPage = $request->parentPage ? $request->parentPage : null;
|
$request->parentPage = $request->parentPage ? $request->parentPage : null;
|
||||||
$request->titlePhotoFilename = $page->title_photo;
|
$request->titlePhotoFilename = $page->title_photo;
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ class ContentPage extends Controller
|
|||||||
$template = Module::where('is_template', true)->where('enabled', true)->first();
|
$template = Module::where('is_template', true)->where('enabled', true)->first();
|
||||||
|
|
||||||
// do something template-specific
|
// do something template-specific
|
||||||
$page = call_user_func([$template->handler, 'beforeRender'], $page);
|
call_user_func_array([$template->handler, 'beforeRender'], [&$page]);
|
||||||
|
|
||||||
// render page
|
// render page
|
||||||
return $handler->render($page, '');
|
return $handler->render($page, '');
|
||||||
|
|||||||
@@ -32,9 +32,4 @@ abstract class OnePage implements TemplateHandler
|
|||||||
public static function uninstall(Module $template)
|
public static function uninstall(Module $template)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function beforeRender(Content $content)
|
|
||||||
{
|
|
||||||
return $content;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,55 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Templates\Jednostrankovka;
|
namespace Templates\Jednostrankovka;
|
||||||
|
|
||||||
|
use App\Http\Requests\Request;
|
||||||
use App\Models\Content;
|
use App\Models\Content;
|
||||||
use Templates\Base\OnePage\Handlers\OnePage;
|
use Templates\Base\OnePage\Handlers\OnePage;
|
||||||
|
|
||||||
class Handler extends OnePage
|
class Handler extends OnePage
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get list of registered templates and supported modules.
|
||||||
|
*
|
||||||
|
* Returned array is expected to be array of arrays with following items: module, namespace and templateName.
|
||||||
|
* Example:
|
||||||
|
* return [
|
||||||
|
* ['module' => '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 getPublicTemplatePaths()
|
||||||
{
|
{
|
||||||
return [
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user