186 lines
5.2 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace System\Controllers;
use Lang;
use Flash;
use Backend;
use Redirect;
use BackendMenu;
use System\Classes\SettingsManager;
use Backend\Classes\Controller;
use System\Classes\ApplicationException;
use Exception;
/**
* Settings controller
*
* @package october\system
* @author Alexey Bobkov, Samuel Georges
*
*/
class Settings extends Controller
{
/**
* @var WidgetBase Reference to the widget object.
*/
2014-08-01 18:20:55 +10:00
protected $formWidget;
2014-05-14 23:24:20 +10:00
public $requiredPermissions = ['system.manage_settings'];
public function __construct()
{
parent::__construct();
if ($this->action == 'mysettings') {
$this->requiredPermissions = null;
}
2014-05-24 16:57:38 +10:00
$this->addCss('/modules/system/assets/css/settings.css', 'core');
2014-05-14 23:24:20 +10:00
BackendMenu::setContext('October.System', 'system', 'settings');
}
public function index()
{
$this->pageTitle = 'system::lang.settings.menu_label';
$this->vars['items'] = SettingsManager::instance()->listItems('system');
2014-07-28 15:09:01 +11:00
$this->bodyClass = 'compact-container sidenav-tree-root';
}
public function mysettings()
{
BackendMenu::setContextSideMenu('mysettings');
$this->pageTitle = 'backend::lang.mysettings.menu_label';
$this->vars['items'] = SettingsManager::instance()->listItems('mysettings');
2014-05-14 23:24:20 +10:00
$this->bodyClass = 'compact-container';
}
//
// Generated Form
//
public function update($author, $plugin, $code = null)
{
SettingsManager::setContext($author.'.'.$plugin, $code);
$this->vars['parentLink'] = Backend::url('system/settings');
$this->vars['parentLabel'] = Lang::get('system::lang.settings.menu_label');
2014-05-14 23:24:20 +10:00
try {
2014-10-18 11:58:50 +02:00
if (!$item = $this->findSettingItem($author, $plugin, $code)) {
throw new ApplicationException(Lang::get('system::lang.settings.not_found'));
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
$this->pageTitle = $item->label;
if ($item->context == 'mysettings') {
$this->vars['parentLink'] = Backend::url('system/settings/mysettings');
$this->vars['parentLabel'] = Lang::get('backend::lang.mysettings.menu_label');
}
2014-05-14 23:24:20 +10:00
$model = $this->createModel($item);
$this->initWidgets($model);
}
catch (Exception $ex) {
2014-05-14 23:24:20 +10:00
$this->handleError($ex);
}
}
public function update_onSave($author, $plugin, $code = null)
{
$item = $this->findSettingItem($author, $plugin, $code);
$model = $this->createModel($item);
$this->initWidgets($model);
$saveData = $this->formWidget->getSaveData();
foreach ($saveData as $attribute => $value) {
$model->{$attribute} = $value;
}
$model->save(null, $this->formWidget->getSessionKey());
Flash::success(Lang::get('system::lang.settings.update_success', ['name' => Lang::get($item->label)]));
2014-05-14 23:24:20 +10:00
/*
* Handle redirect
*/
if ($redirectUrl = post('redirect', true)) {
$redirectUrl = ($item->context == 'mysettings')
? Backend::url('system/settings/mysettings')
: Backend::url('system/settings');
return Redirect::to($redirectUrl);
}
2014-05-14 23:24:20 +10:00
}
public function update_onResetDefault($author, $plugin, $code = null)
{
$item = $this->findSettingItem($author, $plugin, $code);
$model = $this->createModel($item);
$model->resetDefault();
$redirectUrl = Backend::url('system/settings/update/'.$author.'/'.$plugin.'/'.$code);
return Redirect::to($redirectUrl);
}
2014-05-14 23:24:20 +10:00
/**
* Render the form.
*/
public function formRender($options = [])
{
2014-10-18 11:58:50 +02:00
if (!$this->formWidget) {
2014-05-14 23:24:20 +10:00
throw new ApplicationException(Lang::get('backend::lang.form.behavior_not_ready'));
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
return $this->formWidget->render($options);
}
/**
* Prepare the widgets used by this action
* Model $model
*/
protected function initWidgets($model)
{
$config = $model->getFieldConfig();
$config->model = $model;
2014-09-29 13:12:34 +10:00
$config->arrayName = class_basename($model);
2014-05-14 23:24:20 +10:00
$config->context = 'update';
$widget = $this->makeWidget('Backend\Widgets\Form', $config);
$widget->bindToController();
$this->formWidget = $widget;
}
/**
* Internal method, prepare the list model object
*/
protected function createModel($item)
{
2014-10-18 11:58:50 +02:00
if (!isset($item->class) || !strlen($item->class)) {
2014-05-14 23:24:20 +10:00
throw new ApplicationException(Lang::get('system::lang.settings.missing_model'));
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
$class = $item->class;
$model = $class::instance();
return $model;
}
/**
* Locates a setting item for a module or plugin
*/
2014-08-01 18:18:09 +10:00
protected function findSettingItem($author, $plugin, $code)
2014-05-14 23:24:20 +10:00
{
$manager = SettingsManager::instance();
$moduleOwner = $author;
$moduleCode = $plugin;
$item = $manager->findSettingItem($moduleOwner, $moduleCode);
if (!$item) {
$pluginOwner = $author . '.' . $plugin;
$pluginCode = $code;
$item = $manager->findSettingItem($pluginOwner, $pluginCode);
}
return $item;
}
2014-10-18 11:58:50 +02:00
}