Refactor solution from #2985

This commit is contained in:
Samuel Georges 2017-07-21 15:38:53 +10:00
parent 647a656f67
commit 3f40a6dbaf
8 changed files with 223 additions and 167 deletions

View File

@ -0,0 +1,142 @@
<?php namespace Cms\Controllers;
use File;
use Yaml;
use View;
use Flash;
use Config;
use Backend;
use Redirect;
use Response;
use BackendMenu;
use ValidationException;
use ApplicationException;
use Cms\Models\ThemeData;
use Cms\Models\ThemeExport;
use Cms\Models\ThemeImport;
use Cms\Classes\Theme as CmsTheme;
use Cms\Classes\ThemeManager;
use System\Classes\SettingsManager;
use Backend\Classes\Controller;
use Exception;
/**
* Theme customization controller
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*
*/
class ThemeOptions extends Controller
{
public $implement = [
'Backend.Behaviors.FormController'
];
public $formConfig = 'config_form.yaml';
public $requiredPermissions = ['cms.manage_themes', 'cms.manage_theme_options'];
/**
* Constructor.
*/
public function __construct()
{
parent::__construct();
$this->pageTitle = 'cms::lang.theme.settings_menu';
BackendMenu::setContext('October.System', 'system', 'settings');
SettingsManager::setContext('October.Cms', 'theme');
}
public function update($dirName = null)
{
/*
* Only the active theme can be managed without this permission
*/
if ($dirName && !$this->user->hasAccess('cms.manage_themes')) {
$dirName = null;
}
if ($dirName === null) {
$dirName = CmsTheme::getActiveThemeCode();
}
try {
$model = $this->getThemeData($dirName);
$this->asExtension('FormController')->update($model->id);
$this->vars['hasCustomData'] = $this->hasThemeData($dirName);
}
catch (Exception $ex) {
$this->handleError($ex);
}
}
public function update_onSave($dirName)
{
$model = $this->getThemeData($dirName);
$this->asExtension('FormController')->update_onSave($model->id);
}
public function update_onResetDefault($dirName)
{
$model = $this->getThemeData($dirName);
$model->delete();
return Backend::redirect('cms/themeoptions/update/'.$dirName);
}
protected function hasThemeData($dirName)
{
return $this->findThemeObject($dirName)->hasCustomData();
}
protected function getThemeData($dirName)
{
$theme = $this->findThemeObject($dirName);
$model = ThemeData::forTheme($theme);
return $model;
}
/**
* Add form fields defined in theme.yaml
*/
public function formExtendFields($form)
{
$model = $form->model;
$theme = $this->findThemeObject($model->theme);
$config = $theme->getConfigArray('form');
if ($fields = array_get($config, 'fields')) {
$form->addFields($fields);
}
if ($fields = array_get($config, 'tabs.fields')) {
$form->addTabFields($fields);
}
if ($fields = array_get($config, 'secondaryTabs.fields')) {
$form->addSecondaryTabFields($fields);
}
}
//
// Helpers
//
protected function findThemeObject($name = null)
{
if ($name === null) {
$name = post('theme');
}
if (!$name || (!$theme = CmsTheme::load($name))) {
throw new ApplicationException(trans('cms::lang.theme.not_found_name', ['name' => $name]));
}
return $theme;
}
}

View File

@ -2,12 +2,10 @@
use File;
use Yaml;
use View;
use Flash;
use Config;
use Backend;
use Redirect;
use Response;
use BackendMenu;
use ValidationException;
use ApplicationException;
@ -29,13 +27,7 @@ use Exception;
*/
class Themes extends Controller
{
public $implement = [
'Backend.Behaviors.FormController'
];
public $formConfig = 'config_form.yaml';
public $requiredPermissions = ['cms.manage_themes', 'cms.manage_theme_options'];
public $requiredPermissions = ['cms.manage_themes'];
/**
* Constructor.
@ -50,6 +42,15 @@ class Themes extends Controller
BackendMenu::setContext('October.System', 'system', 'settings');
SettingsManager::setContext('October.Cms', 'theme');
/*
* Custom redirect for unauthorized request
*/
$this->bindEvent('page.beforeDisplay', function() {
if (!$this->user->hasAnyAccess($this->requiredPermissions)) {
return Backend::redirect('cms/themeoptions/update');
}
});
/*
* Enable AJAX for Form widgets
*/
@ -58,43 +59,6 @@ class Themes extends Controller
}
}
/**
* Check user's permissions
*/
public function run($action = null, $params = [])
{
$canAccess = false;
$user = \BackendAuth::getUser();
if (!$user->hasAccess('cms.manage_themes')) {
$activeThemeCode = CmsTheme::getActiveThemeCode();
switch ($action) {
case 'update':
if (
$user->hasAccess('cms.manage_theme_options') &&
@$params[0] === $activeThemeCode
) {
$canAccess = true;
}
break;
default:
if ($user->hasAccess('cms.manage_theme_options')) {
return Redirect::to(Backend::url("cms/themes/update/$activeThemeCode"));
}
break;
}
} else {
$canAccess = true;
}
if ($canAccess) {
return parent::run($action, $params);
} else {
return Response::make(View::make('backend::access_denied'), 403);
}
}
public function index()
{
$this->bodyClass = 'compact-container';
@ -247,64 +211,6 @@ class Themes extends Controller
return Redirect::refresh();
}
//
// Theme customization
//
public function update($dirName)
{
try {
$model = $this->getThemeData($dirName);
$this->asExtension('FormController')->update($model->id);
}
catch (Exception $ex) {
$this->handleError($ex);
}
}
public function update_onSave($dirName)
{
$model = $this->getThemeData($dirName);
$this->asExtension('FormController')->update_onSave($model->id);
}
public function update_onResetDefault($dirName)
{
$model = $this->getThemeData($dirName);
$model->delete();
return Backend::redirect('cms/themes/update/'.$dirName);
}
protected function getThemeData($dirName)
{
$theme = $this->findThemeObject($dirName);
$model = ThemeData::forTheme($theme);
return $model;
}
/**
* Add form fields defined in theme.yaml
*/
public function formExtendFields($form)
{
$model = $form->model;
$theme = $this->findThemeObject($model->theme);
$config = $theme->getConfigArray('form');
if ($fields = array_get($config, 'fields')) {
$form->addFields($fields);
}
if ($fields = array_get($config, 'tabs.fields')) {
$form->addTabFields($fields);
}
if ($fields = array_get($config, 'secondaryTabs.fields')) {
$form->addSecondaryTabFields($fields);
}
}
//
// Theme export
//
@ -405,5 +311,4 @@ class Themes extends Controller
return $theme;
}
}

View File

@ -0,0 +1,68 @@
<?php Block::put('breadcrumb') ?>
<ul>
<li><a href="<?= Backend::url('cms/themes') ?>"><?= e(trans('cms::lang.theme.theme_title')) ?></a></li>
<li><?= e(trans($this->pageTitle)) ?></li>
</ul>
<?php Block::endPut() ?>
<?php if (!$this->fatalError): ?>
<?php if ($hasCustomData): ?>
<?= Form::open(['class'=>'layout']) ?>
<div class="layout-row">
<?= $this->formRender() ?>
</div>
<div class="form-buttons">
<div class="loading-indicator-container">
<button
type="submit"
data-request="onSave"
data-request-data="redirect:0"
data-hotkey="ctrl+s, cmd+s"
data-load-indicator="<?= e(trans('cms::lang.theme.saving')) ?>"
class="btn btn-primary">
<?= e(trans('backend::lang.form.save')) ?>
</button>
<button
type="button"
data-request="onSave"
data-request-data="close:1"
data-hotkey="ctrl+enter, cmd+enter"
data-load-indicator="<?= e(trans('cms::lang.theme.saving')) ?>"
class="btn btn-default">
<?= e(trans('backend::lang.form.save_and_close')) ?>
</button>
<span class="btn-text">
<?= e(trans('backend::lang.form.or')) ?> <a href="<?= Backend::url('cms/themes') ?>"><?= e(trans('backend::lang.form.cancel')) ?></a>
</span>
<button
type="button"
class="btn btn-danger pull-right"
data-request="onResetDefault"
data-load-indicator="<?= e(trans('backend::lang.form.resetting')) ?>"
data-request-confirm="<?= e(trans('backend::lang.form.action_confirm')) ?>">
<?= e(trans('backend::lang.form.reset_default')) ?>
</button>
</div>
</div>
<?= Form::close() ?>
<?php else: ?>
<div class="callout callout-info">
<div class="content">
<p>There are no theme options available to customize.</p>
</div>
</div>
<?php endif ?>
<?php else: ?>
<p class="flash-message static error"><?= e($this->fatalError) ?></p>
<p><a href="<?= Backend::url('cms/themes') ?>" class="btn btn-default"><?= e(trans('cms::lang.theme.return')) ?></a></p>
<?php endif ?>

View File

@ -36,7 +36,7 @@
<?php endif ?>
<?php if ($theme->hasCustomData()): ?>
<a
href="<?= Backend::url('cms/themes/update/'.$theme->getDirName()) ?>"
href="<?= Backend::url('cms/themeoptions/update/'.$theme->getDirName()) ?>"
class="btn btn-secondary">
<i class="icon-paint-brush"></i>
<?= e(trans('cms::lang.theme.customize_button')) ?>

View File

@ -1,59 +0,0 @@
<?php Block::put('breadcrumb') ?>
<ul>
<li><a href="<?= Backend::url('cms/themes') ?>"><?= e(trans('cms::lang.theme.theme_title')) ?></a></li>
<li><?= e(trans($this->pageTitle)) ?></li>
</ul>
<?php Block::endPut() ?>
<?php if (!$this->fatalError): ?>
<?= Form::open(['class'=>'layout']) ?>
<div class="layout-row">
<?= $this->formRender() ?>
</div>
<div class="form-buttons">
<div class="loading-indicator-container">
<button
type="submit"
data-request="onSave"
data-request-data="redirect:0"
data-hotkey="ctrl+s, cmd+s"
data-load-indicator="<?= e(trans('cms::lang.theme.saving')) ?>"
class="btn btn-primary">
<?= e(trans('backend::lang.form.save')) ?>
</button>
<button
type="button"
data-request="onSave"
data-request-data="close:1"
data-hotkey="ctrl+enter, cmd+enter"
data-load-indicator="<?= e(trans('cms::lang.theme.saving')) ?>"
class="btn btn-default">
<?= e(trans('backend::lang.form.save_and_close')) ?>
</button>
<span class="btn-text">
<?= e(trans('backend::lang.form.or')) ?> <a href="<?= Backend::url('cms/themes') ?>"><?= e(trans('backend::lang.form.cancel')) ?></a>
</span>
<button
type="button"
class="btn btn-danger pull-right"
data-request="onResetDefault"
data-load-indicator="<?= e(trans('backend::lang.form.resetting')) ?>"
data-request-confirm="<?= e(trans('backend::lang.form.action_confirm')) ?>">
<?= e(trans('backend::lang.form.reset_default')) ?>
</button>
</div>
</div>
<?= Form::close() ?>
<?php else: ?>
<p class="flash-message static error"><?= e($this->fatalError) ?></p>
<p><a href="<?= Backend::url('cms/themes') ?>" class="btn btn-default"><?= e(trans('cms::lang.theme.return')) ?></a></p>
<?php endif ?>

View File

@ -35,7 +35,7 @@ return [
'not_match' => "The object you're trying to access doesn't belong to the theme being edited. Please reload the page."
],
'settings_menu' => 'Front-end theme',
'settings_menu_description' => 'Manage the front-end theme',
'settings_menu_description' => 'Manage the front-end theme and customization options.',
'default_tab' => 'Properties',
'name_label' => 'Name',
'name_create_placeholder' => 'New theme name',

View File

@ -28,7 +28,7 @@
</li>
<?php if ($theme->hasCustomData()): ?>
<li>
<a href="<?= Backend::url('cms/themes/update/'.$theme->getDirName()) ?>">
<a href="<?= Backend::url('cms/themeoptions/update/'.$theme->getDirName()) ?>">
<?= e(trans('cms::lang.dashboard.active_theme.customize_theme')) ?>
</a>
</li>