winter/modules/cms/ServiceProvider.php

426 lines
15 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 Cms\Models\ThemeLog;
use Cms\Models\ThemeData;
use Cms\Classes\CmsObject;
use Backend\Models\UserRole;
use Cms\Classes\Page as CmsPage;
use Cms\Classes\ComponentManager;
2021-03-09 14:21:32 -06:00
use System\Classes\CombineAssets;
use Cms\Classes\Theme as CmsTheme;
2014-05-14 23:24:20 +10:00
use Backend\Classes\WidgetManager;
2014-07-19 13:05:50 +11:00
use System\Classes\SettingsManager;
use Winter\Storm\Support\ModuleServiceProvider;
class ServiceProvider extends ModuleServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerConsole();
$this->registerTwigParser();
$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 command line specifics
*/
protected function registerConsole()
{
$this->registerConsoleCommand('theme.install', \Cms\Console\ThemeInstall::class);
$this->registerConsoleCommand('theme.remove', \Cms\Console\ThemeRemove::class);
$this->registerConsoleCommand('theme.list', \Cms\Console\ThemeList::class);
$this->registerConsoleCommand('theme.use', \Cms\Console\ThemeUse::class);
$this->registerConsoleCommand('theme.sync', \Cms\Console\ThemeSync::class);
}
/*
* Register Twig Environments and other Twig modifications provided by the module
*/
protected function registerTwigParser()
{
// Register CMS Twig environment
App::singleton('twig.environment.cms', function ($app) {
$twig = MarkupManager::makeBaseTwigEnvironment();
// @TODO: move logic from Cms\Classes\Controller into here and adjust controller to use $twig->getLoader() instead
});
}
/**
* Register asset bundles
*/
protected function registerAssetBundles()
{
CombineAssets::registerCallback(function ($combiner) {
$combiner->registerBundle('~/modules/cms/assets/less/winter.components.less');
$combiner->registerBundle('~/modules/cms/assets/less/winter.theme-selector.less');
});
}
/**
* 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
]
]
]
]);
Merge branch 'wip/rebrand-1.0' into wip/rebrand-develop # Conflicts: # .github/workflows/code-quality-pr.yaml # .github/workflows/code-quality-push.yaml # .github/workflows/tests.yml # README.md # composer.json # modules/backend/behaviors/ImportExportController.php # modules/backend/behaviors/ReorderController.php # modules/backend/composer.json # modules/backend/models/ExportModel.php # modules/cms/ServiceProvider.php # modules/cms/classes/Asset.php # modules/cms/classes/CmsObject.php # modules/cms/classes/CmsObjectCollection.php # modules/cms/composer.json # modules/system/ServiceProvider.php # modules/system/aliases.php # modules/system/assets/js/lang/lang.es.js # modules/system/assets/js/lang/lang.it.js # modules/system/assets/js/lang/lang.zh-tw.js # modules/system/assets/ui/storm-min.js # modules/system/classes/CombineAssets.php # modules/system/classes/ErrorHandler.php # modules/system/classes/SystemController.php # modules/system/classes/UpdateManager.php # modules/system/composer.json # modules/system/console/WinterFresh.php # modules/system/console/WinterUtil.php # modules/system/controllers/updates/_list_toolbar.htm # modules/system/providers.php # tests/UiTestCase.php # tests/functional/backend/AuthTest.php # tests/functional/phpunit.xml # tests/unit/backend/classes/NavigationManagerTest.php # tests/unit/cms/classes/CmsObjectTest.php # tests/unit/cms/classes/ControllerTest.php # tests/unit/cms/classes/ThemeTest.php # tests/unit/plugins/database/PluginModelTest.php # tests/unit/plugins/database/ValidationModelTest.php # tests/unit/system/console/WinterEnvTest.php
2021-03-11 00:22:09 -06:00
$manager->registerQuickActions('Winter.Cms', [
'preview' => [
'label' => 'backend::lang.tooltips.preview_website',
'icon' => 'icon-crosshairs',
'url' => Url::to('/'),
'order' => 10,
'attributes' => [
'target' => '_blank',
'rel' => 'noopener noreferrer',
],
],
]);
$manager->registerOwnerAlias('Winter.Cms', 'October.Cms');
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',
'roles' => [UserRole::CODE_DEVELOPER, UserRole::CODE_PUBLISHER],
'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
];
});
}
}