winter/modules/cms/ServiceProvider.php

387 lines
14 KiB
PHP
Raw Normal View History

<?php namespace Cms;
use App;
use Url;
use Lang;
use File;
use Event;
use Backend;
2014-05-14 23:24:20 +10:00
use BackendMenu;
use BackendAuth;
use Backend\Models\UserRole;
use Cms\Classes\Theme as CmsTheme;
2014-05-14 23:24:20 +10:00
use Backend\Classes\WidgetManager;
use October\Rain\Support\ModuleServiceProvider;
2014-07-19 13:05:50 +11:00
use System\Classes\SettingsManager;
use Cms\Classes\ComponentManager;
use Cms\Classes\Page as CmsPage;
use Cms\Classes\CmsObject;
use Cms\Models\ThemeData;
use Cms\Models\ThemeLog;
class ServiceProvider extends ModuleServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
parent::register('cms');
2014-05-14 23:24:20 +10:00
$this->registerAssetBundles();
$this->registerComponents();
$this->registerThemeLogging();
$this->registerCombinerEvents();
$this->registerHalcyonModels();
2015-06-27 11:04:44 +10:00
/*
* Backend specific
*/
if (App::runningInBackend()) {
$this->registerBackendNavigation();
$this->registerBackendReportWidgets();
$this->registerBackendPermissions();
$this->registerBackendWidgets();
$this->registerBackendSettings();
2015-06-27 11:04:44 +10:00
}
}
/**
* Bootstrap the module events.
*
* @return void
*/
public function boot()
{
parent::boot('cms');
$this->bootMenuItemEvents();
2015-06-25 19:26:47 +10:00
$this->bootRichEditorEvents();
if (App::runningInBackend()) {
$this->bootBackendLocalization();
}
}
/**
* Register components.
*/
protected function registerComponents()
{
ComponentManager::instance()->registerComponents(function ($manager) {
2017-06-03 10:31:24 +10:00
$manager->registerComponent(\Cms\Components\ViewBag::class, 'viewBag');
$manager->registerComponent(\Cms\Components\Resources::class, 'resources');
});
}
/**
* Registers theme logging on templates.
*/
protected function registerThemeLogging()
{
CmsObject::extend(function ($model) {
ThemeLog::bindEventsToModel($model);
});
}
/**
* Registers events for the asset combiner.
*/
protected function registerCombinerEvents()
{
if (App::runningInBackend() || App::runningInConsole()) {
return;
}
Event::listen('cms.combiner.beforePrepare', function ($combiner, $assets) {
$filters = array_flatten($combiner->getFilters());
ThemeData::applyAssetVariablesToCombinerFilters($filters);
});
Event::listen('cms.combiner.getCacheKey', function ($combiner, $holder) {
$holder->key = $holder->key . ThemeData::getCombinerCacheKey();
});
}
/*
* Register navigation
*/
protected function registerBackendNavigation()
{
2014-10-11 01:56:53 +02:00
BackendMenu::registerCallback(function ($manager) {
$manager->registerMenuItems('Winter.Cms', [
2014-05-14 23:24:20 +10:00
'cms' => [
'label' => 'cms::lang.cms.menu_label',
'icon' => 'icon-magic',
2016-02-19 22:12:41 -08:00
'iconSvg' => 'modules/cms/assets/images/cms-icon.svg',
2014-05-14 23:24:20 +10:00
'url' => Backend::url('cms'),
'order' => 100,
'permissions' => [
'cms.manage_content',
'cms.manage_assets',
'cms.manage_pages',
'cms.manage_layouts',
'cms.manage_partials'
],
2014-05-14 23:24:20 +10:00
'sideMenu' => [
'pages' => [
'label' => 'cms::lang.page.menu_label',
'icon' => 'icon-copy',
'url' => 'javascript:;',
'attributes' => ['data-menu-item' => 'pages'],
'permissions' => ['cms.manage_pages'],
2015-01-22 09:06:30 +01:00
'counterLabel' => 'cms::lang.page.unsaved_label'
2014-05-14 23:24:20 +10:00
],
'partials' => [
'label' => 'cms::lang.partial.menu_label',
'icon' => 'icon-tags',
'url' => 'javascript:;',
'attributes' => ['data-menu-item' => 'partials'],
'permissions' => ['cms.manage_partials'],
2015-01-22 09:06:30 +01:00
'counterLabel' => 'cms::lang.partial.unsaved_label'
2014-05-14 23:24:20 +10:00
],
'layouts' => [
'label' => 'cms::lang.layout.menu_label',
'icon' => 'icon-th-large',
'url' => 'javascript:;',
'attributes' => ['data-menu-item' => 'layouts'],
'permissions' => ['cms.manage_layouts'],
2015-01-22 09:06:30 +01:00
'counterLabel' => 'cms::lang.layout.unsaved_label'
2014-05-14 23:24:20 +10:00
],
'content' => [
'label' => 'cms::lang.content.menu_label',
'icon' => 'icon-file-text-o',
'url' => 'javascript:;',
'attributes' => ['data-menu-item' => 'content'],
'permissions' => ['cms.manage_content'],
2015-01-22 09:06:30 +01:00
'counterLabel' => 'cms::lang.content.unsaved_label'
2014-05-14 23:24:20 +10:00
],
'assets' => [
'label' => 'cms::lang.asset.menu_label',
'icon' => 'icon-picture-o',
'url' => 'javascript:;',
'attributes' => ['data-menu-item' => 'assets'],
'permissions' => ['cms.manage_assets'],
2015-01-22 09:06:30 +01:00
'counterLabel' => 'cms::lang.asset.unsaved_label'
2014-05-14 23:24:20 +10:00
],
'components' => [
'label' => 'cms::lang.component.menu_label',
'icon' => 'icon-puzzle-piece',
'url' => 'javascript:;',
'attributes' => ['data-menu-item' => 'components'],
'permissions' => ['cms.manage_pages', 'cms.manage_layouts', 'cms.manage_partials']
2014-05-14 23:24:20 +10:00
]
]
]
]);
$manager->registerQuickActions('October.Cms', [
'preview' => [
'label' => 'backend::lang.tooltips.preview_website',
'icon' => 'icon-crosshairs',
'url' => Url::to('/'),
'order' => 10,
'attributes' => [
'target' => '_blank',
'rel' => 'noopener noreferrer',
],
],
]);
2014-05-14 23:24:20 +10:00
});
}
2014-05-14 23:24:20 +10:00
/*
* Register report widgets
*/
protected function registerBackendReportWidgets()
{
WidgetManager::instance()->registerReportWidgets(function ($manager) {
2017-06-03 10:31:24 +10:00
$manager->registerReportWidget(\Cms\ReportWidgets\ActiveTheme::class, [
'label' => 'cms::lang.dashboard.active_theme.widget_title_default',
'context' => 'dashboard'
]);
});
}
/*
* Register permissions
*/
protected function registerBackendPermissions()
{
2014-10-11 01:56:53 +02:00
BackendAuth::registerCallback(function ($manager) {
$manager->registerPermissions('Winter.Cms', [
'cms.manage_content' => [
'label' => 'cms::lang.permissions.manage_content',
2015-05-21 22:54:44 +10:00
'tab' => 'cms::lang.permissions.name',
'roles' => UserRole::CODE_DEVELOPER,
2015-05-21 22:54:44 +10:00
'order' => 100
],
'cms.manage_assets' => [
'label' => 'cms::lang.permissions.manage_assets',
2015-05-21 22:54:44 +10:00
'tab' => 'cms::lang.permissions.name',
'roles' => UserRole::CODE_DEVELOPER,
2015-05-21 22:54:44 +10:00
'order' => 100
],
'cms.manage_pages' => [
'label' => 'cms::lang.permissions.manage_pages',
2015-05-21 22:54:44 +10:00
'tab' => 'cms::lang.permissions.name',
'roles' => UserRole::CODE_DEVELOPER,
2015-05-21 22:54:44 +10:00
'order' => 100
],
'cms.manage_layouts' => [
'label' => 'cms::lang.permissions.manage_layouts',
2015-05-21 22:54:44 +10:00
'tab' => 'cms::lang.permissions.name',
'roles' => UserRole::CODE_DEVELOPER,
2015-05-21 22:54:44 +10:00
'order' => 100
],
'cms.manage_partials' => [
'label' => 'cms::lang.permissions.manage_partials',
2015-05-21 22:54:44 +10:00
'tab' => 'cms::lang.permissions.name',
'roles' => UserRole::CODE_DEVELOPER,
2015-05-21 22:54:44 +10:00
'order' => 100
],
'cms.manage_themes' => [
'label' => 'cms::lang.permissions.manage_themes',
2015-05-21 22:54:44 +10:00
'tab' => 'cms::lang.permissions.name',
'roles' => UserRole::CODE_DEVELOPER,
2015-05-21 22:54:44 +10:00
'order' => 100
],
'cms.manage_theme_options' => [
'label' => 'cms::lang.permissions.manage_theme_options',
'tab' => 'cms::lang.permissions.name',
'order' => 100
],
2014-05-14 23:24:20 +10:00
]);
$manager->registerPermissionOwnerAlias('Winter.Cms', 'October.Cms');
2014-05-14 23:24:20 +10:00
});
}
2014-05-14 23:24:20 +10:00
/*
* Register widgets
*/
protected function registerBackendWidgets()
{
2014-10-11 01:56:53 +02:00
WidgetManager::instance()->registerFormWidgets(function ($manager) {
$manager->registerFormWidget(FormWidgets\Components::class);
2014-05-14 23:24:20 +10:00
});
}
2014-05-14 23:24:20 +10:00
/*
* Register settings
*/
protected function registerBackendSettings()
{
2014-10-11 01:56:53 +02:00
SettingsManager::instance()->registerCallback(function ($manager) {
$manager->registerSettingItems('Winter.Cms', [
2014-07-19 13:05:50 +11:00
'theme' => [
'label' => 'cms::lang.theme.settings_menu',
'description' => 'cms::lang.theme.settings_menu_description',
'category' => SettingsManager::CATEGORY_CMS,
2014-07-19 13:05:50 +11:00
'icon' => 'icon-picture-o',
2017-03-16 17:16:03 +11:00
'url' => Backend::url('cms/themes'),
'permissions' => ['cms.manage_themes', 'cms.manage_theme_options'],
2015-04-29 19:50:30 +10:00
'order' => 200
],
'maintenance_settings' => [
'label' => 'cms::lang.maintenance.settings_menu',
'description' => 'cms::lang.maintenance.settings_menu_description',
'category' => SettingsManager::CATEGORY_CMS,
'icon' => 'icon-plug',
'class' => Models\MaintenanceSetting::class,
'permissions' => ['cms.manage_themes'],
2015-04-29 19:50:30 +10:00
'order' => 300
],
'theme_logs' => [
'label' => 'cms::lang.theme_log.menu_label',
'description' => 'cms::lang.theme_log.menu_description',
'category' => SettingsManager::CATEGORY_LOGS,
'icon' => 'icon-magic',
'url' => Backend::url('cms/themelogs'),
'permissions' => ['system.access_logs'],
'order' => 910,
'keywords' => 'theme change log'
]
2014-07-19 13:05:50 +11:00
]);
$manager->registerOwnerAlias('Winter.Cms', 'October.Cms');
2014-07-19 13:05:50 +11:00
});
}
/**
* Boots localization from an active theme for backend items.
*/
protected function bootBackendLocalization()
{
$theme = CmsTheme::getActiveTheme();
if (is_null($theme)) {
return;
}
$langPath = $theme->getPath() . '/lang';
if (File::isDirectory($langPath)) {
Lang::addNamespace('themes.' . $theme->getId(), $langPath);
}
}
/**
* Registers events for menu items.
*/
protected function bootMenuItemEvents()
{
2014-10-11 01:56:53 +02:00
Event::listen('pages.menuitem.listTypes', function () {
return [
'cms-page' => 'cms::lang.page.cms_page'
];
});
2014-10-11 01:56:53 +02:00
Event::listen('pages.menuitem.getTypeInfo', function ($type) {
if ($type === 'cms-page') {
return CmsPage::getMenuTypeInfo($type);
2014-10-11 01:56:53 +02:00
}
});
2014-10-11 01:56:53 +02:00
Event::listen('pages.menuitem.resolveItem', function ($type, $item, $url, $theme) {
if ($type === 'cms-page') {
return CmsPage::resolveMenuItem($item, $url, $theme);
2014-10-11 01:56:53 +02:00
}
});
}
2015-06-25 19:26:47 +10:00
/**
* Registers events for rich editor page links.
*/
protected function bootRichEditorEvents()
{
Event::listen('backend.richeditor.listTypes', function () {
return [
'cms-page' => 'cms::lang.page.cms_page'
2015-06-25 19:26:47 +10:00
];
});
Event::listen('backend.richeditor.getTypeInfo', function ($type) {
if ($type === 'cms-page') {
2015-06-25 19:26:47 +10:00
return CmsPage::getRichEditorTypeInfo($type);
}
});
}
/**
* Registers the models to be made available to the theme database layer
*/
protected function registerHalcyonModels()
{
Event::listen('system.console.theme.sync.getAvailableModelClasses', function () {
return [
Classes\Meta::class,
Classes\Page::class,
Classes\Layout::class,
Classes\Content::class,
Classes\Partial::class
];
});
}
}