|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hipanel\behaviors; |
|
4
|
|
|
|
|
5
|
|
|
use hipanel\models\IndexPageUiOptions; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
use yii\base\Behavior; |
|
8
|
|
|
use yii\base\InvalidConfigException; |
|
9
|
|
|
use yii\web\Controller; |
|
10
|
|
|
|
|
11
|
|
|
class UiOptionsBehavior extends Behavior |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var mixed |
|
15
|
|
|
*/ |
|
16
|
|
|
public $modelClass; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
public $allowedRoutes = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var IndexPageUiOptions |
|
25
|
|
|
*/ |
|
26
|
|
|
private $_model; |
|
27
|
|
|
|
|
28
|
|
|
public function init() |
|
29
|
|
|
{ |
|
30
|
|
|
parent::init(); |
|
31
|
|
|
|
|
32
|
|
|
foreach ($this->allowedRoutes as &$allowedRoute) { |
|
33
|
|
|
$allowedRoute = ltrim(Yii::getAlias($allowedRoute), '/'); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function events() |
|
38
|
|
|
{ |
|
39
|
|
|
return [Controller::EVENT_BEFORE_ACTION => 'ensureOptions']; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function ensureOptions($event) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
if ($this->isRouteAllowed($this->getRoute())) { |
|
45
|
|
|
$options = []; |
|
46
|
|
|
$params = Yii::$app->request->get(); |
|
47
|
|
|
if ($params) { |
|
48
|
|
|
$model = $this->getModel(); |
|
49
|
|
|
foreach ($params as $key => $value) { |
|
50
|
|
|
if (in_array($key, array_keys($model->toArray()))) { |
|
51
|
|
|
$options[$key] = $value; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
$model->attributes = $options; |
|
55
|
|
|
if ($model->validate()) { |
|
56
|
|
|
$this->getUiOptionsStorage()->set($this->getRoute(), $model->toArray()); |
|
57
|
|
|
} else { |
|
58
|
|
|
$errors = $model->getErrors(); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function getModel() |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->_model === null) { |
|
67
|
|
|
$this->_model = $this->findModel(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $this->_model; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function findModel() |
|
74
|
|
|
{ |
|
75
|
|
|
if (isset($this->modelClass['class']) && class_exists($this->modelClass['class'])) { |
|
76
|
|
|
return Yii::createObject($this->modelClass); |
|
|
|
|
|
|
77
|
|
|
} else { |
|
78
|
|
|
throw new InvalidConfigException('UiOptionsBehavior::$modelClass must contain `class` item'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function isRouteAllowed($route) |
|
83
|
|
|
{ |
|
84
|
|
|
return in_array($route, $this->allowedRoutes, true); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
protected function getUiOptionsStorage() |
|
88
|
|
|
{ |
|
89
|
|
|
return Yii::$app->get('uiOptionsStorage'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function getRoute() |
|
93
|
|
|
{ |
|
94
|
|
|
return Yii::$app->request->pathInfo; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.