|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HiPanel core package. |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://hipanel.com/ |
|
6
|
|
|
* @package hipanel-core |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2014-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\behaviors; |
|
12
|
|
|
|
|
13
|
|
|
use hipanel\components\UiOptionsStorage; |
|
14
|
|
|
use hipanel\grid\RepresentationCollectionFinder; |
|
15
|
|
|
use hipanel\models\IndexPageUiOptions; |
|
16
|
|
|
use hiqdev\higrid\representations\RepresentationCollectionInterface; |
|
17
|
|
|
use Yii; |
|
18
|
|
|
use yii\base\Behavior; |
|
19
|
|
|
use yii\base\InvalidConfigException; |
|
20
|
|
|
use yii\web\Controller; |
|
21
|
|
|
|
|
22
|
|
|
class UiOptionsBehavior extends Behavior |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var mixed |
|
26
|
|
|
*/ |
|
27
|
|
|
public $modelClass; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var IndexPageUiOptions |
|
31
|
|
|
*/ |
|
32
|
|
|
private $_model; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var RepresentationCollectionInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
public $representationsCollection; |
|
38
|
|
|
|
|
39
|
|
|
public function init() |
|
40
|
|
|
{ |
|
41
|
|
|
parent::init(); |
|
42
|
|
|
|
|
43
|
|
|
if ($this->representationsCollection === null) { |
|
44
|
|
|
$this->representationsCollection = RepresentationCollectionFinder::forCurrentRoute()->findOrFallback(); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function events() |
|
49
|
|
|
{ |
|
50
|
|
|
return [Controller::EVENT_BEFORE_ACTION => 'ensureUiOptions']; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function ensureUiOptions($event) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
if (!$this->isRouteAllowed()) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$options = []; |
|
60
|
|
|
$params = Yii::$app->request->get(); |
|
61
|
|
|
$model = $this->getModel(); |
|
62
|
|
|
$model->attributes = $this->getUiOptionsStorage()->get($this->getRoute()); |
|
63
|
|
|
$model->availableRepresentations = $this->findRepresentations(); |
|
64
|
|
|
if ($params) { |
|
65
|
|
|
foreach ($params as $key => $value) { |
|
66
|
|
|
if (in_array($key, array_keys($model->toArray()), true)) { |
|
67
|
|
|
$options[$key] = $value; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
$model->attributes = $options; |
|
71
|
|
|
|
|
72
|
|
|
if ($model->validate()) { |
|
73
|
|
|
$this->getUiOptionsStorage()->set($this->getRoute(), $model->toArray()); |
|
74
|
|
|
} else { |
|
75
|
|
|
$errors = json_encode($model->getErrors()); |
|
76
|
|
|
Yii::warning('UiOptionsBehavior - IndexPageUiModel validation errors: ' . $errors); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function getModel() |
|
82
|
|
|
{ |
|
83
|
|
|
if ($this->_model === null) { |
|
84
|
|
|
$this->_model = $this->findModel(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->_model; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function findModel() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->owner->indexPageUiOptionsModel; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function isRouteAllowed() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->owner->action->id === 'index'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return UiOptionsStorage |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getUiOptionsStorage() |
|
104
|
|
|
{ |
|
105
|
|
|
return Yii::$app->get('uiOptionsStorage'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function getRoute() |
|
109
|
|
|
{ |
|
110
|
|
|
return Yii::$app->request->pathInfo; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function findRepresentations() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->representationsCollection->getAll(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.