1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Theme Manager for Yii2 |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-thememanager |
6
|
|
|
* @package yii2-thememanager |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\thememanager\controllers; |
12
|
|
|
|
13
|
|
|
use hipanel\models\IndexPageUiOptions; |
14
|
|
|
use hiqdev\thememanager\models\Settings; |
15
|
|
|
use hiqdev\thememanager\Module; |
16
|
|
|
use hiqdev\thememanager\storage\SettingsStorageInterface; |
17
|
|
|
use Yii; |
18
|
|
|
use yii\web\Response; |
19
|
|
|
|
20
|
|
|
class SettingsController extends \yii\web\Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Module |
24
|
|
|
*/ |
25
|
|
|
public $module; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return Settings |
29
|
|
|
*/ |
30
|
|
|
public function getModel() |
31
|
|
|
{ |
32
|
|
|
return $this->module->getManager()->getSettings(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return SettingsStorageInterface |
37
|
|
|
*/ |
38
|
|
|
public function getThemeSettingsStorage() |
39
|
|
|
{ |
40
|
|
|
return Yii::$app->get('themeSettingsStorage'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getUiOptionsStorage() |
44
|
|
|
{ |
45
|
|
|
return Yii::$app->get('uiOptionsStorage'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Settings form. |
50
|
|
|
* |
51
|
|
|
* @return string|Response |
52
|
|
|
*/ |
53
|
|
|
public function actionIndex() |
54
|
|
|
{ |
55
|
|
|
$model = $this->getModel(); |
56
|
|
|
|
57
|
|
|
$data = Yii::$app->request->post($model->formName()); |
58
|
|
|
if (empty($data)) { |
59
|
|
|
$data = $this->getThemeSettingsStorage()->get(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (Yii::$app->request->getIsPost() && $model->load($data) && $model->validate()) { |
63
|
|
|
$this->getThemeSettingsStorage()->set($model); |
64
|
|
|
$this->setGlobalOrientation($model->filterOrientation); |
|
|
|
|
65
|
|
|
Yii::$app->session->setFlash('success', Yii::t('hiqdev:thememanager', 'Layout settings saved.')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (Yii::$app->request->isAjax) { |
69
|
|
|
return $this->renderAjax('//settings/_form', compact('model')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->render('index', compact('model')); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param null $orientation |
77
|
|
|
*/ |
78
|
|
|
public function setGlobalOrientation($orientation = null) |
79
|
|
|
{ |
80
|
|
|
if ($orientation !== null) { |
81
|
|
|
$settings = $this->getUiOptionsStorage()->getAllUiOptions(); |
82
|
|
|
foreach ($settings as $route => $data) { |
83
|
|
|
$model = new IndexPageUiOptions($data); |
84
|
|
|
$model->orientation = $orientation; |
85
|
|
|
if ($model->validate()) { |
86
|
|
|
$this->getUiOptionsStorage()->set($route, $model->toArray()); |
87
|
|
|
} else { |
88
|
|
|
Yii::warning('SettingsController - IndexPageUiModel validation errors: ' . json_encode($model->getErrors())); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $theme |
96
|
|
|
* @return Response |
97
|
|
|
*/ |
98
|
|
|
public function actionChangeTheme($theme) |
99
|
|
|
{ |
100
|
|
|
if ($this->module->getManager()->has($theme)) { |
101
|
|
|
$model = $this->getModel(); |
102
|
|
|
$model->theme = $theme; |
103
|
|
|
$this->getThemeSettingsStorage()->set($model); |
104
|
|
|
Yii::$app->session->setFlash('success', Yii::t('hiqdev:thememanager', 'Theme changed')); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->redirect(['index']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function actionCollapsedSidebar() |
111
|
|
|
{ |
112
|
|
|
if (Yii::$app->request->isAjax) { |
113
|
|
|
$model = $this->getModel(); |
114
|
|
|
$model->collapsed_sidebar = Yii::$app->request->post('collapsed_sidebar'); |
|
|
|
|
115
|
|
|
$this->getThemeSettingsStorage()->set($model); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.