Fixes #9 - Adds maintenance mode setting to the CMS

This commit is contained in:
Sam Georges 2014-11-10 20:34:42 +11:00
parent 7bcec1bd99
commit cfaf3228d9
8 changed files with 122 additions and 17 deletions

View File

@ -65,6 +65,7 @@ return [
'status' => [
'widget_title_default' => 'System status',
'online' => 'online',
'maintenance' => 'in maintenance',
'update_available' => '{0} updates available!|{1} update available!|[2,Inf] updates available!',
]
],

View File

@ -2,7 +2,6 @@
# Field Definitions
# ===================================
tabs:
fields:

View File

@ -18,6 +18,7 @@ use Cms\Twig\Loader as TwigLoader;
use Cms\Twig\DebugExtension;
use Cms\Twig\Extension as CmsTwigExtension;
use Cms\Classes\FileHelper as CmsFileHelper;
use Cms\Models\MaintenanceSettings;
use System\Models\RequestLog;
use System\Classes\ErrorHandler;
use System\Classes\CombineAssets;
@ -150,7 +151,7 @@ class Controller extends BaseController
}
/*
* Handle hidden pages
* Hidden page
*/
$page = $this->router->findByUrl($url);
if ($page && $page->hidden) {
@ -159,6 +160,17 @@ class Controller extends BaseController
}
}
/*
* Maintenance mode
*/
if (
MaintenanceSettings::isConfigured()
&& MaintenanceSettings::get('is_enabled', false)
&& !BackendAuth::getUser()
) {
$page = Page::loadCached($this->theme, MaintenanceSettings::get('cms_page'));
}
/*
* Extensibility
*/

View File

@ -0,0 +1,70 @@
<?php namespace Cms\Models;
use Model;
use Cms\Classes\Page;
use Cms\Classes\Theme;
/**
* Maintenance mode settings
*
* @package october\cms
* @author Alexey Bobkov, Samuel Georges
*/
class MaintenanceSettings extends Model
{
use \October\Rain\Database\Traits\Validation;
public $implement = ['System.Behaviors.SettingsModel'];
public $settingsCode = 'cms_maintenance_settings';
public $settingsFields = 'fields.yaml';
/**
* Validation rules
*/
public $rules = [];
public function initSettingsData()
{
$this->is_enabled = false;
}
public function getCmsPageOptions()
{
return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
}
/**
* Ensure each theme has its own CMS page, store it inside a mapping array.
* @return void
*/
public function beforeValidate()
{
if (!$theme = Theme::getEditTheme())
throw new ApplicationException('Unable to find the active theme.');
$themeMap = $this->getSettingsValue('theme_map', []);
$themeMap[$theme->getDirName()] = $this->getSettingsValue('cms_page');
$this->setSettingsValue('theme_map', $themeMap);
}
/**
* Restore the CMS page found in the mapping array, or disable the
* maintenance mode.
* @return void
*/
public function afterFetch()
{
if (
($theme = Theme::getEditTheme())
&& ($themeMap = array_get($this->attributes, 'theme_map'))
&& ($cmsPage = array_get($themeMap, $theme->getDirName()))
) {
$this->cms_page = $cmsPage;
}
else {
$this->is_enabled = false;
}
}
}

View File

@ -0,0 +1,14 @@
# ===================================
# Field Definitions
# ===================================
fields:
is_enabled:
label: cms::lang.maintenance.is_enabled
comment: cms::lang.maintenance.is_enabled_comment
type: checkbox
cms_page:
type: dropdown
cssClass: checkbox-align

View File

@ -56,7 +56,7 @@ class SettingsModel extends ModelBehavior
$this->model->bindEvent('model.afterFetch', [$this, 'afterModelFetch']);
$this->model->bindEvent('model.beforeSave', [$this, 'beforeModelSave']);
$this->model->bindEvent('model.afterSave', [$this, 'afterModelSave']);
$this->model->bindEvent('model.setAttribute', [$this, 'setModelAttribute']);
$this->model->bindEvent('model.setAttribute', [$this, 'setSettingsValue']);
$this->model->bindEvent('model.saveInternal', [$this, 'saveModelInternal']);
/*
@ -135,6 +135,18 @@ class SettingsModel extends ModelBehavior
return $default;
}
/**
* Set a single setting value, if allowed.
*/
public function setSettingsValue($key, $value)
{
if ($this->isKeyAllowed($key)) {
return;
}
$this->fieldValues[$key] = $value;
}
/**
* Default values to set for this model, override
*/
@ -182,18 +194,6 @@ class SettingsModel extends ModelBehavior
Cache::forget($this->getCacheKey());
}
/**
* Adulterate the model setter to use our field values instead.
*/
public function setModelAttribute($key, $value)
{
if ($this->isKeyAllowed($key)) {
return;
}
$this->fieldValues[$key] = $value;
}
/**
* Checks if a key is legitimate or should be added to
* the field value collection

View File

@ -2,6 +2,7 @@
use System\Models\Parameters;
use System\Classes\UpdateManager;
use Cms\Models\MaintenanceSettings;
use Backend\Classes\ReportWidgetBase;
use Exception;
@ -44,6 +45,7 @@ class Status extends ReportWidgetBase
protected function loadData()
{
$manager = UpdateManager::instance();
$this->vars['inMaintenance'] = MaintenanceSettings::get('is_enabled');
$this->vars['showUpdates'] = $this->controller->user->hasAccess('system.manage_updates');
$this->vars['updates'] = $manager->check();
}

View File

@ -4,8 +4,15 @@
<?php if (!isset($error)): ?>
<ul class="status-list">
<li>
<span class="status circle success"></span>
<?= e(trans('backend::lang.dashboard.status.online')) ?>
<a href="<?= Backend::url('system/settings/update/october/cms/maintenance_settings') ?>">
<?php if ($inMaintenance): ?>
<span class="status circle warning"></span>
<?= e(trans('backend::lang.dashboard.status.maintenance')) ?>
<?php else: ?>
<span class="status circle success"></span>
<?= e(trans('backend::lang.dashboard.status.online')) ?>
<?php endif ?>
</a>
</li>
<?php if ($showUpdates): ?>
<li>