Files
core/app/CMS/Helpers/CurrentTemplate.php
T

132 lines
3.6 KiB
PHP

<?php
namespace App\CMS\Helpers;
use App\CMS\Contracts\Modules\TemplateHandler;
use App\Models\Content\Content;
use App\Models\Module;
use App\Models\User;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Cache;
class CurrentTemplate implements TemplateHandler
{
protected static $template;
protected static $initialized = false;
/**
* Getter for currently set template
* @return Module
* @throws \ErrorException
*/
public static function get()
{
if (!self::$initialized) {
self::$template = Cache::rememberForever('current-template', function () {
$template = Module::where('is_template', true)
->where('enabled', true)
->first();
// todo remove after adding $casts into Module model
if ($template) {
$template->module_settings = json_decode($template->module_settings);
}
return $template;
});
self::$initialized = true;
}
return self::$template;
}
/**
* Shortcut for getting template handler
*
* @return string
* @throws \ErrorException
*/
public static function handler()
{
$template = self::get();
if (!$template) {
throw new \ErrorException('No template selected, no handler available!');
}
return $template->handler;
}
/**
* Reset currently set template and load another one on next call of get() method.
*/
public static function reset()
{
self::$initialized = false;
}
/**
* Register admin menu (or multiple ones)
*
* This cannot be in ServiceProvider because Session is started at middleware. And ServiceProvider is called before
* starting any middleware..
*
* @param User $user model of logged user
*/
public static function registerAdminMenu(User $user)
{
call_user_func_array([self::handler(), 'registerAdminMenu'], [$user]);
}
/**
* 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()
{
return call_user_func([self::handler(), 'getPublicTemplatePaths']);
}
/**
* Prepare Content object to be rendered using Blade template
*
* @param Content $content
*/
public static function beforeRender(Content & $content)
{
call_user_func_array([self::handler(), 'beforeRender'], [&$content]);
}
/**
* Modify input data into format which can be understood by edit template
*
* @param Content $content
*/
public static function beforePopulatingEditForm(Content & $content)
{
call_user_func_array([self::handler(), 'beforePopulatingEditForm'], [&$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
*/
public static function beforeSave(Content & $content, $request)
{
call_user_func_array([self::handler(), 'beforeSave'], [&$content, $request]);
}
}