|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Theme Manager for Yii2 |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/hiqdev/yii2-thememanager |
|
7
|
|
|
* @package yii2-thememanager |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace hiqdev\thememanager\controllers; |
|
13
|
|
|
|
|
14
|
|
|
use hiqdev\thememanager\models\Settings; |
|
15
|
|
|
use hiqdev\thememanager\Module; |
|
16
|
|
|
use hiqdev\thememanager\storage\SettingsStorageInterface; |
|
17
|
|
|
use hiqdev\thememanager\ThemeManager; |
|
18
|
|
|
use Yii; |
|
19
|
|
|
use yii\base\DynamicModel; |
|
20
|
|
|
use yii\web\Response; |
|
21
|
|
|
|
|
22
|
|
|
class SettingsController extends \yii\web\Controller |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Module |
|
26
|
|
|
*/ |
|
27
|
|
|
public $module; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return Settings |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getModel() |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->module->getManager()->getSettings(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return SettingsStorageInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getSettingsStorage() |
|
41
|
|
|
{ |
|
42
|
|
|
return Yii::$app->get('themeSettingsStorage'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Settings form |
|
47
|
|
|
* |
|
48
|
|
|
* @return Response |
|
49
|
|
|
*/ |
|
50
|
|
|
public function actionIndex() |
|
51
|
|
|
{ |
|
52
|
|
|
$model = $this->getModel(); |
|
53
|
|
|
|
|
54
|
|
|
$data = Yii::$app->request->post($model->formName()); |
|
55
|
|
|
if (empty($data)) { |
|
56
|
|
|
$data = $this->getSettingsStorage()->get(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (Yii::$app->request->getIsPost() && $model->load($data) && $model->validate()) { |
|
60
|
|
|
$this->getSettingsStorage()->set($model); |
|
61
|
|
|
Yii::$app->session->setFlash('success', 'Layout settings saved.'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (Yii::$app->request->isAjax) { |
|
65
|
|
|
return $this->renderAjax('//settings/_form', compact('model')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->render('index', compact('model')); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param $theme |
|
73
|
|
|
* @return Response |
|
74
|
|
|
*/ |
|
75
|
|
|
public function actionChangeTheme($theme) |
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->module->getManager()->has($theme)) { |
|
78
|
|
|
$model = $this->getModel(); |
|
79
|
|
|
$model->theme = $theme; |
|
80
|
|
|
$this->getSettingsStorage()->set($model); |
|
81
|
|
|
Yii::$app->session->setFlash('success', 'Theme changed'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->redirect(['index']); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function actionCollapsedSidebar() |
|
88
|
|
|
{ |
|
89
|
|
|
if (Yii::$app->request->isAjax) { |
|
90
|
|
|
$model = $this->getModel(); |
|
91
|
|
|
$model->collapsed_sidebar = Yii::$app->request->post('collapsed_sidebar'); |
|
|
|
|
|
|
92
|
|
|
$this->getSettingsStorage()->set($model); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write 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.