FEATURE: prepare handlers for categories
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
namespace App\Handlers;
|
||||
|
||||
use App\Enumerations\TemplatePathsHumanReadableRootOnly;
|
||||
use App\Enumerations\WidgetTypes;
|
||||
use App\Handlers\Contracts\AppHandler;
|
||||
use App\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-dashboard']);
|
||||
$menu->dropdown(trans('admin/mainMenu.userManagement'), function ($sub) {
|
||||
$sub->route('users.new', trans('admin/mainMenu.newUser'), [], 10, ['icon' => 'fa fa-user-plus']);
|
||||
$sub->route('users.detail', trans('admin/mainMenu.userDetail'), [], 20, ['icon' => 'fa fa-user']);
|
||||
$sub->route('users.overview', trans('admin/mainMenu.usersOverview'), [], 30, ['icon' => 'fa fa-users']);
|
||||
}, 20, ['icon' => 'fa fa-users']);
|
||||
$menu->dropdown(trans('admin/mainMenu.contentPages'), function ($sub) {
|
||||
$modules = \App\Models\Module::where('manages_content', true)
|
||||
->get();
|
||||
|
||||
if (!empty($modules)) {
|
||||
$sub->dropdown(trans('admin/mainMenu.newContentPage'), function ($sub) use ($modules) {
|
||||
$i = 1;
|
||||
foreach ($modules as $module) {
|
||||
$sub->route('content.new', trans($module->name), ['moduleId' => $module->id], $i++);
|
||||
}
|
||||
}, 10, ['icon' => 'fa fa-plus']);
|
||||
}
|
||||
|
||||
$sub->route('content.overview', trans('admin/mainMenu.contentPageOverview'), [], 20, ['icon' => 'fa fa-list']);
|
||||
}, 15, ['icon' => 'fa fa-database']);
|
||||
$menu->route('upload.overview', trans('admin/mainMenu.fileManager'), [], 30, ['icon' => 'fa fa-folder']);
|
||||
$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-plus']);
|
||||
}
|
||||
|
||||
$sub->route('widgets.overview', trans('admin/mainMenu.widgetsOverview'), [], 20, ['icon' => 'fa 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-plus']);
|
||||
$sub->route('widgetareas.overview', trans('admin/mainMenu.widgetAreaOverview'), [], 2, ['icon' => 'fa fa-list']);
|
||||
}, 50, ['icon' => 'fa fa-object-group']);
|
||||
}, 28, ['icon' => 'fa fa-gears']);
|
||||
|
||||
$templatePaths = TemplatePathsHumanReadableRootOnly::getWithTranslations();
|
||||
|
||||
if (!empty($templatePaths)) {
|
||||
$menu->dropdown(trans('admin/mainMenu.templates'), function ($sub) use ($templatePaths) {
|
||||
$i = 1;
|
||||
foreach ($templatePaths as $id => $name) {
|
||||
$sub->route('templates.overview', $name, ['id' => $id], $i++);
|
||||
}
|
||||
}, 29, ['icon' => 'fa fa-folder-open']);
|
||||
}
|
||||
// $menu->dropdown(trans('admin/mainMenu.customFields'), function ($sub) {
|
||||
// $sub->route('customfields.new', trans('admin/mainMenu.customFieldsNew'), [], 10, ['icon' => 'fa fa-code']);
|
||||
// $sub->route('customfields.overview', trans('admin/mainMenu.customFieldsOverview'), [], 50, ['icon' => 'fa fa-code-fork']);
|
||||
// }, 30, ['icon' => 'fa fa-code']);
|
||||
$menu->route('log.overview', trans('admin/mainMenu.actionLog'), [], 40, ['icon' => 'fa fa-history']);
|
||||
$menu->route('settings.overview', trans('admin/mainMenu.settings'), [], 50, ['icon' => 'fa fa-gear']);
|
||||
});
|
||||
|
||||
// order
|
||||
|
||||
// create menu with user profile
|
||||
\Menu::create('profileMenu', function ($menu) {
|
||||
$menu->route('users.detail', trans('admin/userManagement.profile'));
|
||||
$menu->route('logout', trans('admin/common.logout'));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,8 @@ namespace App\Handlers\Contracts;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use App\Models\Content;
|
||||
use App\Models\User;
|
||||
|
||||
interface AppHandler
|
||||
interface AppHandler extends BaseModuleHandler
|
||||
{
|
||||
/**
|
||||
* Render given module into visitor area.
|
||||
@@ -21,13 +20,13 @@ interface AppHandler
|
||||
|
||||
/**
|
||||
* Load some extra data for content management form?
|
||||
*
|
||||
*
|
||||
* @param Content $content
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function loadExtraData(Content $content);
|
||||
|
||||
|
||||
/**
|
||||
* Save module-specific values into database
|
||||
*
|
||||
@@ -55,23 +54,14 @@ interface AppHandler
|
||||
|
||||
/**
|
||||
* Method called right after content page is marked as deleted
|
||||
*
|
||||
*
|
||||
* This method is supposed to clean after "deleted" page.
|
||||
*
|
||||
*
|
||||
* @param Content $content
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function delete(Content $content);
|
||||
|
||||
/**
|
||||
* Does given module manage content? Or is it just module with widgets/something internal/something absolutely else?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function managesContent();
|
||||
|
||||
public static function getModuleId();
|
||||
|
||||
/**
|
||||
* Getter for section names in creating/editing content resource
|
||||
@@ -99,19 +89,9 @@ interface AppHandler
|
||||
public static function getExtendedFormInputsEdit();
|
||||
|
||||
/**
|
||||
* Register admin menu (or multiple ones)
|
||||
* Does this module support categories?
|
||||
*
|
||||
* 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
|
||||
* @return boolean
|
||||
*/
|
||||
public static function registerAdminMenu(User $user);
|
||||
|
||||
/**
|
||||
* Install module
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function install();
|
||||
public static function supportCategories();
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
namespace App\Handlers\Contracts;
|
||||
|
||||
use App\Models\User;
|
||||
|
||||
interface BaseModuleHandler
|
||||
{
|
||||
public static function getModuleId();
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
/**
|
||||
* Install module
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function install();
|
||||
}
|
||||
+11
-98
@@ -1,11 +1,8 @@
|
||||
<?php
|
||||
namespace App\Handlers;
|
||||
|
||||
use App\Enumerations\TemplatePathsHumanReadableRootOnly;
|
||||
use App\Enumerations\WidgetTypes;
|
||||
use App\Handlers\Contracts\AppHandler;
|
||||
use App\Models\Content;
|
||||
use App\Models\User;
|
||||
|
||||
/**
|
||||
* Class Handler
|
||||
@@ -15,8 +12,18 @@ use App\Models\User;
|
||||
*
|
||||
* @package App\Handlers
|
||||
*/
|
||||
abstract class Handler implements AppHandler
|
||||
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;
|
||||
@@ -53,98 +60,4 @@ abstract class Handler implements AppHandler
|
||||
{
|
||||
return self::getExtendedFormInputsCreate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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-dashboard']);
|
||||
$menu->dropdown(trans('admin/mainMenu.userManagement'), function ($sub) {
|
||||
$sub->route('users.new', trans('admin/mainMenu.newUser'), [], 10, ['icon' => 'fa fa-user-plus']);
|
||||
$sub->route('users.detail', trans('admin/mainMenu.userDetail'), [], 20, ['icon' => 'fa fa-user']);
|
||||
$sub->route('users.overview', trans('admin/mainMenu.usersOverview'), [], 30, ['icon' => 'fa fa-users']);
|
||||
}, 20, ['icon' => 'fa fa-users']);
|
||||
$menu->dropdown(trans('admin/mainMenu.contentPages'), function ($sub) {
|
||||
$modules = \App\Models\Module::all();
|
||||
|
||||
if (!empty($modules)) {
|
||||
$sub->dropdown(trans('admin/mainMenu.newContentPage'), function ($sub) use ($modules) {
|
||||
$i = 1;
|
||||
foreach ($modules as $module) {
|
||||
|
||||
// display only list of all modules, which manages content
|
||||
if (!call_user_func([$module->handler, 'managesContent'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$sub->route('content.new', trans($module->name), ['moduleId' => $module->id], $i++);
|
||||
}
|
||||
}, 10, ['icon' => 'fa fa-plus']);
|
||||
}
|
||||
|
||||
$sub->route('content.overview', trans('admin/mainMenu.contentPageOverview'), [], 20, ['icon' => 'fa fa-list']);
|
||||
}, 15, ['icon' => 'fa fa-database']);
|
||||
$menu->route('upload.overview', trans('admin/mainMenu.fileManager'), [], 30, ['icon' => 'fa fa-folder']);
|
||||
$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-plus']);
|
||||
}
|
||||
|
||||
$sub->route('widgets.overview', trans('admin/mainMenu.widgetsOverview'), [], 20, ['icon' => 'fa 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-plus']);
|
||||
$sub->route('widgetareas.overview', trans('admin/mainMenu.widgetAreaOverview'), [], 2, ['icon' => 'fa fa-list']);
|
||||
}, 50, ['icon' => 'fa fa-object-group']);
|
||||
}, 28, ['icon' => 'fa fa-gears']);
|
||||
|
||||
$templatePaths = TemplatePathsHumanReadableRootOnly::getWithTranslations();
|
||||
|
||||
if (!empty($templatePaths)) {
|
||||
$menu->dropdown(trans('admin/mainMenu.templates'), function ($sub) use ($templatePaths) {
|
||||
$i = 1;
|
||||
foreach ($templatePaths as $id => $name) {
|
||||
$sub->route('templates.overview', $name, ['id' => $id], $i++);
|
||||
}
|
||||
}, 29, ['icon' => 'fa fa-folder-open']);
|
||||
}
|
||||
// $menu->dropdown(trans('admin/mainMenu.customFields'), function ($sub) {
|
||||
// $sub->route('customfields.new', trans('admin/mainMenu.customFieldsNew'), [], 10, ['icon' => 'fa fa-code']);
|
||||
// $sub->route('customfields.overview', trans('admin/mainMenu.customFieldsOverview'), [], 50, ['icon' => 'fa fa-code-fork']);
|
||||
// }, 30, ['icon' => 'fa fa-code']);
|
||||
$menu->route('log.overview', trans('admin/mainMenu.actionLog'), [], 40, ['icon' => 'fa fa-history']);
|
||||
$menu->route('settings.overview', trans('admin/mainMenu.settings'), [], 50, ['icon' => 'fa fa-gear']);
|
||||
});
|
||||
|
||||
// order
|
||||
|
||||
// create menu with user profile
|
||||
\Menu::create('profileMenu', function ($menu) {
|
||||
$menu->route('users.detail', trans('admin/userManagement.profile'));
|
||||
$menu->route('logout', trans('admin/common.logout'));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Middleware;
|
||||
|
||||
use App\Handlers\Handler;
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class AdminMenuRegistrator
|
||||
{
|
||||
@@ -19,7 +20,7 @@ class AdminMenuRegistrator
|
||||
{
|
||||
// register menu only of logged users
|
||||
if (!\Auth::check()) {
|
||||
return;
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// create menu
|
||||
@@ -32,7 +33,11 @@ class AdminMenuRegistrator
|
||||
foreach ($modules as $key => $module) {
|
||||
$handler = config($module->getName() . '.handler');
|
||||
// try to register menus
|
||||
call_user_func([$handler, 'registerAdminMenu'], $user);
|
||||
try {
|
||||
call_user_func([$handler, 'registerAdminMenu'], $user);
|
||||
} catch (\Exception $e) {
|
||||
Log::debug('Error in '.$handler.'::registerAdminMenu: '.$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
|
||||
+15
-7
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Handlers\Contracts\AppHandler;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\App;
|
||||
|
||||
@@ -17,7 +18,9 @@ class Module extends Model
|
||||
'description',
|
||||
'alias',
|
||||
'module_settings',
|
||||
'enabled'
|
||||
'enabled',
|
||||
'support_categories',
|
||||
'manages_content'
|
||||
];
|
||||
protected $guarded = [];
|
||||
|
||||
@@ -40,14 +43,19 @@ class Module extends Model
|
||||
throw new \ErrorException('Modules can be installed only via Artisan!');
|
||||
}
|
||||
|
||||
$managesContent = $handler instanceof AppHandler;
|
||||
$supportCategories = $managesContent ? call_user_func([$handler, 'supportCategories']) : false;
|
||||
|
||||
// insert module into database
|
||||
$module = Module::create([
|
||||
'name' => $name,
|
||||
'description' => $description,
|
||||
'handler' => $handler,
|
||||
'alias' => $alias,
|
||||
'module_settings' => json_encode($settings),
|
||||
'enabled' => 1
|
||||
'name' => $name,
|
||||
'description' => $description,
|
||||
'handler' => $handler,
|
||||
'manages_content' => $managesContent,
|
||||
'support_categories' => $supportCategories,
|
||||
'alias' => $alias,
|
||||
'module_settings' => json_encode($settings),
|
||||
'enabled' => 1
|
||||
]);
|
||||
|
||||
// install something module-specific
|
||||
|
||||
Reference in New Issue
Block a user