FEATURE: change directory structure

This commit is contained in:
Michal Kváček
2016-10-17 00:07:21 +02:00
parent 87f1920373
commit 2ded2924a1
242 changed files with 742 additions and 1171 deletions
+123
View File
@@ -0,0 +1,123 @@
<?php
namespace App\CMS\Handlers;
use App\CMS\Enumerations\TemplatePathsHumanReadableRootOnly;
use App\CMS\Enumerations\WidgetTypes;
use App\CMS\Handlers\Contracts\AppHandler;
use App\CMS\Handlers\Contracts\BaseModuleHandler;
use App\Models\User;
abstract class BaseHandler implements BaseModuleHandler
{
/**
* 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)
{
$menu = \Menu::instance('mainMenu');
if (!is_null($menu)) {
// menus were already defined
return;
}
// create instance of main menu
\Menu::create('mainMenu', function ($menu) {
// $menu->route('admin.dashboard', trans('admin/mainMenu.dashboard'), [], 10, ['icon' => 'fa fa-fw fa-dashboard']);
/**
* Content pages
*
* Submenus can be registered later
*/
$menu->dropdown(trans('admin/mainMenu.contentPages'), function ($sub) {
$sub->addDivider(9999);
$sub->route('content.overview', trans('admin/mainMenu.contentPageOverview'), [], 10000, ['icon' => 'fa fa-fw fa-list']);
}, 20, ['icon' => 'fa fa-fw fa-database']);
/**
* File manager
*/
$menu->route('upload.overview', trans('admin/mainMenu.fileManager'), [], 40, ['icon' => 'fa fa-fw fa-folder']);
/**
* Widgets and widget areas
*/
$menu->dropdown(trans('admin/mainMenu.widgets'), function ($sub) {
// create list of all widget types
$widgets = WidgetTypes::getWithTranslations();
if (!empty($widgets)) {
$sub->dropdown(trans('admin/mainMenu.newWidget'), function ($sub) use ($widgets) {
$i = 1;
foreach ($widgets as $type => $name) {
$sub->route('widgets.new', $name, ['type' => $type], $i++);
}
}, 10, ['icon' => 'fa fa-fw fa-plus']);
}
$sub->route('widgets.overview', trans('admin/mainMenu.widgetsOverview'), [], 20, ['icon' => 'fa fa-fw fa-list']);
// menu for widget areas
$sub->dropdown(trans('admin/mainMenu.widgetAreas'), function ($sub) {
$sub->route('widgetareas.new', trans('admin/mainMenu.newWidgetArea'), [], 1, ['icon' => 'fa fa-fw fa-plus']);
$sub->route('widgetareas.overview', trans('admin/mainMenu.widgetAreaOverview'), [], 2, ['icon' => 'fa fa-fw fa-list']);
}, 50, ['icon' => 'fa fa-fw fa-object-group']);
}, 50, ['icon' => 'fa fa-fw fa-puzzle-piece']);
/**
* Divide system entries and other links in menu
*/
$menu->addDivider(9999);
$menu->dropdown(trans('admin/mainMenu.system'), function ($sub) {
/**
* User management
*/
$sub->dropdown(trans('admin/mainMenu.userManagement'), function ($sub) {
$sub->route('users.new', trans('admin/mainMenu.newUser'), [], 10, ['icon' => 'fa fa-fw fa-user-plus']);
$sub->route('users.overview', trans('admin/mainMenu.usersOverview'), [], 30, ['icon' => 'fa fa-fw fa-users']);
}, 20, ['icon' => 'fa fa-fw fa-users']);
/**
* Templates
*/
$templatePaths = TemplatePathsHumanReadableRootOnly::getWithTranslations();
if (!empty($templatePaths)) {
$sub->dropdown(trans('admin/mainMenu.templates'), function ($sub) use ($templatePaths) {
$i = 1;
foreach ($templatePaths as $id => $name) {
$sub->route('templates.overview', $name, ['id' => $id], $i++);
}
}, 60, ['icon' => 'fa fa-fw fa-file-code-o']);
}
// $menu->dropdown(trans('admin/mainMenu.customFields'), function ($sub) {
// $sub->route('customfields.new', trans('admin/mainMenu.customFieldsNew'), [], 10, ['icon' => 'fa fa-fw fa-code']);
// $sub->route('customfields.overview', trans('admin/mainMenu.customFieldsOverview'), [], 50, ['icon' => 'fa fa-fw fa-code-fork']);
// }, 30, ['icon' => 'fa fa-fw fa-code']);
$sub->route('log.overview', trans('admin/mainMenu.actionLog'), [], 70, ['icon' => 'fa fa-fw fa-history']);
$sub->route('settings.overview', trans('admin/mainMenu.settings'), [], 80, ['icon' => 'fa fa-fw fa-gear']);
}, 10000, ['icon' => 'fa fa-fw fa-gears']);
});
// order
// create menu with user profile
\Menu::create('profileMenu', function ($menu) {
$menu->route('users.detail', trans('admin/userManagement.profile'), [], 10, ['icon' => 'fa fa-fw fa-user icon']);
$menu->addDivider(999);
$menu->route('logout', trans('admin/common.logout'), [], 1000, ['icon' => 'fa fa-fw fa-power-off icon']);
});
}
}
+81
View File
@@ -0,0 +1,81 @@
<?php
namespace App\CMS\Handlers;
use App\CMS\Handlers\Contracts\AppHandler;
use App\Http\Controllers\Controller;
use App\Models\Content\Content;
/**
* Class Handler
*
* Handler is a class which handles module-specific behaviour which cannot be done by module itself, resp. cannot be
* done by some abstract magic.
*
* @package App\CMS\Handlers\
*/
abstract class Handler extends BaseHandler implements AppHandler
{
/**
* todo: Temporary method, will be implemented for each Handler
*
* @return bool
*/
public static function supportCategories()
{
return true;
}
public static function getHistoryHandler()
{
return History::class;
}
/**
* Render given module into visitor area.
*
* @param Content $content DB model from table contents
* @param array $parameters parameters used for modifications/custom settings of given model
*
* @return mixed
*/
public function render(Content $content, $parameters)
{
return Controller::VisitorsView($content->template, ['page' => $content, 'extra' => $parameters]);
}
public static function getExtendedFormSections()
{
return [
'basicInfo' => trans('admin/contents.basicInfo'),
'meta' => trans('admin/contents.metaOptions'),
'publication' => trans('admin/contents.publicationOptions'),
'appearance' => trans('admin/contents.appearance'),
'files' => trans('admin/contents.files')
];
}
public static function getExtendedFormInputsCreate()
{
return [
'basicInfo' => 'admin.content.formParts.basicInformation',
'meta' => 'admin.content.formParts.metaInformation',
'publication' => 'admin.content.formParts.publicationSetup',
'appearance' => 'admin.content.formParts.appearance',
'files' => 'admin.content.formParts.files'
];
}
public static function delete(Content $content)
{
// temporarily nothing to do
}
public static function loadExtraData(Content $content) {
// temporarily nothing to do
}
public static function getExtendedFormInputsEdit()
{
return self::getExtendedFormInputsCreate();
}
}
+57
View File
@@ -0,0 +1,57 @@
<?php
namespace App\CMS\Handlers;
use App\CMS\Enumerations\ContentStatuses;
use App\CMS\Handlers\Contracts\HistoryHandler;
use App\CMS\Helpers\Differ;
class History implements HistoryHandler
{
public static function parseColumnName($column)
{
return trans('admin/history.column' . ucfirst($column));
}
public static function parseChangedValue($changeset)
{
switch ($changeset->column) {
case 'pageContent':
$route = route('history.diff', [
'revision' => $changeset->edit_batch,
'id' => $changeset->id
]);
return "(<a href='" . $route . "'>" . trans('admin/history.diff') . "</a>)";
break;
case 'metaDescription':
return Differ::htmlDiff($changeset->old_value, $changeset->new_value);
break;
case 'file':
if (!$changeset->old_value) {
// old value is null -> new file was uploaded
return trans('admin/history.uploadedFile', ['file' => $changeset->new_value]);
} else {
return trans('admin/history.deletedFile', ['file' => $changeset->old_value]);
}
break;
case 'status':
$statuses = ContentStatuses::getWithTranslations();
return trans('admin/history.changedFromTo', [
'from' => $statuses[$changeset->old_value],
'to' => $statuses[$changeset->new_value]
]);
break;
case 'is_homepage':
return trans('admin/history.setupAsHomepage');
break;
default:
return trans('admin/history.changedFromTo', [
'from' => is_null($changeset->old_value) ? trans('admin/history.emptyValue') : $changeset->old_value,
'to' => is_null($changeset->new_value) ? trans('admin/history.emptyValue') : $changeset->new_value,
]);
break;
}
}
}
@@ -0,0 +1,56 @@
<?php
namespace App\CMS\Handlers\Registrators\Items;
class SettingExtension
{
/**
* @var string human readable ID of settings section (used e.g. as ID parameter in html)
*/
protected $id;
/**
* @var string translations string used for describing briefly settings section
*/
protected $name;
/**
* @var string view used for rendering given settings part
*/
protected $view;
/**
* SettingExtension constructor.
*
* @param string $id
* @param string $name
* @param string $view
*/
public function __construct($id, $name, $view)
{
$this->id = $id;
$this->name = $name;
$this->view = $view;
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return string
*/
public function getView()
{
return $this->view;
}
}
@@ -0,0 +1,20 @@
<?php
namespace App\CMS\Handlers\Registrators;
use App\CMS\Widgets\Contracts\MenuExtensionHandler;
class MenuExtensionRegistrator {
protected $menuExtensions;
/**
* @param MenuExtensionHandler $handler
*/
public function register(MenuExtensionHandler $handler) {
$this->menuExtensions[] = $handler;
}
public function get(){
return $this->menuExtensions;
}
}
@@ -0,0 +1,72 @@
<?php
namespace App\CMS\Handlers\Registrators;
use App\CMS\Enumerations\Exceptions\EnumerationExistsException;
use App\CMS\Handlers\Contracts\SearchHandler;
use App\CMS\Handlers\Contracts\SearchPresenterHandler;
use App\CMS\Handlers\Registrators\Items\SearchEngine;
use Symfony\Component\Process\Exception\InvalidArgumentException;
class SearchEngineRegistrator
{
const ADMIN = 'admin';
const VISITOR = 'visitor';
protected $searchEngines;
function __construct($app)
{
$this->searchEngines = [
self::ADMIN => [],
self::VISITOR => []
];
}
/**
* Method for registering new settings extensions/parts
*
* @param SearchHandler $search
* @param SearchPresenterHandler $presenter
*
* @param $type
*
* @throws EnumerationExistsException
*/
public function register(SearchHandler $search, SearchPresenterHandler $presenter, $type)
{
$this->checkIfRegistered($type, $search->getId());
// register new extension
$this->searchEngines[$type][$search->getId()] = [$search, $presenter];
}
/**
* Getter for all registered extensions
*
* @param $type
*
* @return array
*/
public function get($type)
{
if (!isset($this->searchEngines[$type])) {
throw new InvalidArgumentException('Invalid type ' . $type);
}
return $this->searchEngines[$type];
}
/**
* Checks if this section is not already defined
*
* @param $type
* @param $id
*
* @throws EnumerationExistsException
*/
private function checkIfRegistered($type, $id)
{
if (isset($this->searchEngines[$type][$id])) {
throw new EnumerationExistsException('Already existing search engine ' . $id . ' in ' . $type);
}
}
}
@@ -0,0 +1,71 @@
<?php
namespace App\CMS\Handlers\Registrators;
use App\CMS\Enumerations\Exceptions\EnumerationExistsException;
use App\CMS\Handlers\Registrators\Items\SettingExtension;
class SettingsExtensionRegistrator
{
protected $settingsExtensions;
function __construct($app)
{
$this->settingsExtensions = [];
}
/**
* Method for registering new settings extensions/parts
*
* @param $sectionId
* @param $sectionName
* @param $sectionView
*
* @throws EnumerationExistsException
*/
public function register($sectionId, $sectionName, $sectionView)
{
$this->checkIfRegistered($sectionId);
// register new extension
$this->settingsExtensions[$sectionId] = new SettingExtension($sectionId, $sectionName, $sectionView);
}
/**
* Registers new setting part - only as SettingExtension directly
*
* @param SettingExtension $extension
*/
public function registerObject(SettingExtension $extension)
{
$this->checkIfRegistered($extension->getId());
$this->settingsExtensions[$extension->getId()] = $extension;
}
/**
* Getter for all registered extensions
*
* @return array
*/
public function get()
{
// sort registered extensions by key
ksort($this->settingsExtensions);
return $this->settingsExtensions;
}
/**
* Checks if this section is not already defined
*
* @param $sectionId
*
* @throws EnumerationExistsException
*/
private function checkIfRegistered($sectionId)
{
if (isset($this->settingsExtensions[$sectionId])) {
throw new EnumerationExistsException('Already existing key ' . $sectionId);
}
}
}
@@ -0,0 +1,306 @@
<?php
namespace App\CMS\Handlers\Registrators;
use App\CMS\Enumerations\TemplatePaths;
use App\CMS\Enumerations\Templates;
use App\Models\Template;
use App\Models\TemplatePath;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Process\Exception\InvalidArgumentException;
class TemplateRegistrator
{
/**
* @var string absolute path to base directory of registered template paths
*/
protected $baseDirectory;
/**
* @var string namespace of registered templates
*/
protected $namespace;
/**
* @var string absolute path to currently registered directory
* This variable is set in register() method as $baseDirectory/$path
*/
protected $registeredPath;
/**
* @var int ID of module
*/
protected $moduleId;
/**
* @var array list of all registered template paths
*/
protected $paths = [];
/**
* Set base path
*
* @param mixed $baseDirectory
*/
public function setBaseDirectory($baseDirectory)
{
$baseDirectory = str_replace('/', DIRECTORY_SEPARATOR, $baseDirectory);
$baseDirectory = base_path($baseDirectory);
$baseDirectory = rtrim($baseDirectory, DIRECTORY_SEPARATOR);
if (!is_dir($baseDirectory)) {
throw new InvalidArgumentException('Invalid path ' . $baseDirectory);
}
$this->baseDirectory = $baseDirectory;
}
/**
* Set module ID for registered path
*
* @param int $moduleId
*/
public function setModuleId($moduleId)
{
$this->moduleId = $moduleId;
}
/**
* Set namespace for registered template path
*
* @param string $namespace
*/
public function setNamespace($namespace)
{
$this->namespace = $namespace;
}
/**
* Resets namespace
*/
public function unsetNamespace()
{
$this->namespace = null;
$this->setModuleId(null);
}
/**
* Registers new template path.
* This method registeres given path itself with all subdirectories
*
* @param $baseDirectory
* @param string $path path relatively to root of the project
* @param string $name human readable identification of given template path
*
* @throws \Exception
*/
public function register($baseDirectory, $path, $name)
{
$this->paths[] = [
'namespace' => $this->namespace,
'moduleId' => $this->moduleId,
'baseDirectory' => $baseDirectory,
'path' => $this->baseDirectory . DIRECTORY_SEPARATOR . $path,
'name' => $name
];
$this->unsetNamespace();
}
/**
* Save templates into database
*/
public function saveTemplates()
{
if (!is_array($this->paths) || empty($this->paths)) {
return;
}
foreach ($this->paths as $path) {
if (!empty($path['namespace'])) {
$this->setNamespace($path['namespace'] . '::');
}
$this->setModuleId($path['moduleId']);
$this->setBaseDirectory($path['baseDirectory']);
$this->registeredPath = $this->baseDirectory . DIRECTORY_SEPARATOR . $path['path'];
// register path
$this->registerPath($this->registeredPath, $path['name']);
$this->unsetNamespace();
}
}
/**
* Get relative path to project root
*
* @param $directory
*
* @return string
*/
protected function relativePath($directory)
{
return trim(str_replace(base_path(), '', $directory), '/');
}
/**
* Crawl subdirectories and register them
*
* @param string $directory path relative to $baseDirectory variable
* @param string $name name of parent directory
*/
protected function scanForSubdirectories($directory, $name)
{
$directories = File::directories($directory);
foreach ($directories as $dir) {
$base = basename($dir);
$name = preg_replace('/\.(.+)/', '.' . $base, $name);
$this->registerPath($dir, $name, $directory);
}
}
/**
* Registers template path and templates into system
*
* @param string $path relative path to $baseDirectory variable
* @param string $name human readable name of this path (used in overview)
* @param null|string $parent path to parent directory
*/
protected function registerPath($path, $name, $parent = null)
{
// skip custom directories (contains updated templates only)
$dirName = basename($path);
$ignoredDirectories = [
Template::EDITED_TEMPLATES_DIRECTORY,
Template::ASSETS_DIRECTORY,
Template::MODULES_DIRECTORY
];
if (in_array($dirName, $ignoredDirectories)) {
return;
}
// save template path into database
$templatePathId = $this->saveTemplatePath($path, $name, $parent);
// save all templates into database
$this->registerTemplates($path, $templatePathId);
// try to find some subdirectories and other templates
$this->scanForSubdirectories($path, $name);
}
/**
* Registers all templates
* This method takes all templates in given directory and registers them into database. Templates registered in
* this way may be used for further manipulation.
*
* @param $path
* @param $templatePathId
*
* @throws \App\CMS\Enumerations\Exceptions\EnumerationExistsException
*/
protected function registerTemplates($path, $templatePathId)
{
$templates = File::files($path);
foreach ($templates as $template) {
// skip all non-template files - we assume that every file with suffix ".blade.php" is Blade template
if (!Str::endsWith($template, Template::TEMPLATE_SUFFIX)) {
continue;
}
// get all templates and template name (path/to/template.blade.php -> path.to.template)
$templateName = $this->getTemplateName($template);
$registeredTemplates = Templates::getWithTranslations();
if (!in_array($templateName, $registeredTemplates)) {
$params = [
'template_name' => $templateName,
'name' => $templateName,
'path' => $templatePathId,
'module_id' => $this->moduleId,
'description' => '',
'editable' => 1
];
// save template into database
$newTemplate = Template::create($params);
// push template into enumeration - so it will not be registered again
Templates::push($newTemplate->id, $templateName);
}
}
}
/**
* Save template into database and return its ID
*
* @param $path
* @param $name
* @param null $parent
*
* @return mixed|null
* @throws \App\CMS\Enumerations\Exceptions\EnumerationExistsException
*/
protected function saveTemplatePath($path, $name, $parent = null)
{
$registeredPaths = TemplatePaths::getWithTranslations();
$relativePath = $this->relativePath($path);
if (!in_array($relativePath, $registeredPaths)) {
$parentId = $this->getId($this->relativePath($parent));
$params = [
'path' => $relativePath,
'name' => $name,
'namespace' => $this->namespace,
'parent_id' => $parentId
];
// save into database
$path = TemplatePath::create($params);
// add to enumeration
TemplatePaths::push($path->id, $relativePath);
return $path->id;
}
return $this->getId($relativePath);
}
/**
* Get ID of registered template
*
* @param $path
*
* @return null|int null when parent not found, ID of parent otherwise
*/
private function getId($path)
{
// get registered paths
$paths = TemplatePaths::getWithTranslations();
$paths = array_flip($paths);
if (is_null($path) || !isset($paths[$path])) {
return null;
}
return $paths[$path];
}
/**
* Get template name
* Converts path to template to corresponding name
*
* @param $template
*
* @return string
*/
private function getTemplateName($template)
{
$templateName = str_replace($this->registeredPath . DIRECTORY_SEPARATOR, '', $template);
$templateName = str_replace(DIRECTORY_SEPARATOR, '.', $templateName);
$templateName = str_replace(Template::TEMPLATE_SUFFIX, '', $templateName);
return $this->namespace . $templateName;
}
}
@@ -0,0 +1,58 @@
<?php
namespace App\CMS\Handlers\Registrators;
use App\CMS\Enumerations\WidgetTypesWithHandlers;
use App\Models\Widgets\WidgetType;
class WidgetRegistrator
{
protected $registeredWidgets = [];
/**
* Method used for registering new widgets
*
* @param $classname
* @param null $templateId
* @param null $moduleId
*/
public function register($classname, $templateId = null, $moduleId = null)
{
$this->registeredWidgets[] = [
'class' => $classname,
'templateId' => $templateId,
'moduleId' => $moduleId
];
}
/**
* Install all non-installed widgets
*/
public function installWidgets()
{
$installed = WidgetTypesWithHandlers::getSimple();
foreach ($this->registeredWidgets as $registeredWidget) {
$classname = $registeredWidget['class'];
$templateId = $registeredWidget['templateId'];
$moduleId = $registeredWidget['moduleId'];
// register widget as new widget type
if (!in_array($classname, $installed)) {
$widgetName = call_user_func($classname . '::getName');
$widgetDescription = call_user_func($classname . '::getDescription');
// create row in widget types
WidgetType::create([
'handler' => $classname,
'name' => $widgetName,
'module_id' => $moduleId,
'template_id' => $templateId,
'description' => $widgetDescription
]);
// install widget (if neccessary)
call_user_func($classname . '::install');
}
}
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace App\CMS\Handlers\Search;
use App\Models\Content\Content as ContentModel;
class Content extends Search
{
/**
* Search for given patttern/term
*
* @param string $term searched word
*
* @return array list of found objects
*/
public function search($term)
{
$token = $this->tokenize($term);
return ContentModel::where('content', 'LIKE', $token)
->orWhere('meta_description', 'LIKE', $token)
->orWhere('meta_keywords', 'LIKE', $token)
->orWhere('title', 'LIKE', $token)
->orWhere('url', 'LIKE', $token)
->get();
}
public function getId()
{
return 'content';
}
public function getName()
{
return trans('search.content');
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace App\CMS\Handlers\Search;
use App\Models\FileManager\File;
class Files extends Search
{
public function getId()
{
return "files";
}
public function getName()
{
return trans('search.files');
}
/**
* Search for given patttern/term
*
* @param string $term searched word
*
* @return array list of found objects
*/
public function search($term)
{
$token = $this->tokenize($term);
return File::where('original_filename', 'LIKE', $token)
->orWhere('name', 'LIKE', $token)
->orWhere('keywords', 'LIKE', $token)
->orWhere('description', 'LIKE', $token)
->orWhere('mime_type', 'LIKE', $token)
->get();
}
}
@@ -0,0 +1,16 @@
<?php
namespace App\CMS\Handlers\Search\Presenters;
use App\CMS\Enumerations\ContentStatuses;
use App\CMS\Handlers\Contracts\SearchPresenterHandler;
class AdminContent implements SearchPresenterHandler
{
public function render($foundItems)
{
return view('admin.search.results.content', [
'pages' => $foundItems,
'statuses' => ContentStatuses::getWithTranslations(),
]);
}
}
@@ -0,0 +1,19 @@
<?php
namespace App\CMS\Handlers\Search\Presenters;
use App\CMS\Handlers\Contracts\SearchPresenterHandler;
class AdminFiles implements SearchPresenterHandler
{
/**
* Method for rendering list of found results
*
* @param $foundItems
*
* @return mixed
*/
public function render($foundItems)
{
return view('admin.fileManager.filesTiles', ['files' => $foundItems, 'hideDelete' => true]);
}
}
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\CMS\Handlers\Search;
use App\CMS\Handlers\Contracts\SearchHandler;
use Symfony\Component\Process\Exception\InvalidArgumentException;
abstract class Search implements SearchHandler
{
/**
* Tokenize string for usage in LIKE clausules
*
* @param $term
*
* @return string
*/
protected function tokenize($term)
{
$term = trim($term);
if (empty($term)) {
throw new InvalidArgumentException(trans('search.emptyString'));
}
return '%' . str_replace(' ', '%', $term) . '%';
}
}