|
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\helpers\Html; |
|
20
|
|
|
use yii\web\Controller; |
|
21
|
|
|
|
|
22
|
|
|
class UiOptionsBehavior extends Behavior |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
public $allowedRoutes = ['index', 'export']; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
public $modelClass; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var IndexPageUiOptions |
|
36
|
|
|
*/ |
|
37
|
|
|
private $_model; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var RepresentationCollectionFinder |
|
41
|
|
|
*/ |
|
42
|
|
|
private $representationCollectionFinder; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(RepresentationCollectionFinder $representationCollectionFinder, array $config = []) |
|
45
|
|
|
{ |
|
46
|
|
|
parent::__construct($config); |
|
47
|
|
|
$this->representationCollectionFinder = $representationCollectionFinder; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function events() |
|
51
|
|
|
{ |
|
52
|
|
|
return [Controller::EVENT_BEFORE_ACTION => 'ensureUiOptions']; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function ensureUiOptions($event) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
if (!$this->isRouteAllowed()) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$options = []; |
|
62
|
|
|
$params = Yii::$app->request->get(); |
|
63
|
|
|
$model = $this->getModel(); |
|
64
|
|
|
$model->attributes = $this->getUiOptionsStorage()->get($this->getRoute()); |
|
65
|
|
|
$model->availableRepresentations = $this->findRepresentations(); |
|
66
|
|
|
if ($params) { |
|
67
|
|
|
foreach ($params as $key => $value) { |
|
68
|
|
|
if (in_array($key, array_keys($model->toArray()), true)) { |
|
69
|
|
|
$options[$key] = $value; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
$model->attributes = $options; |
|
73
|
|
|
|
|
74
|
|
|
if ($model->validate()) { |
|
75
|
|
|
$this->getUiOptionsStorage()->set($this->getRoute(), $model->toArray()); |
|
76
|
|
|
} else { |
|
77
|
|
|
$errors = json_encode($model->getErrors()); |
|
78
|
|
|
Yii::warning('UiOptionsBehavior - IndexPageUiModel validation errors: ' . $errors); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function getModel() |
|
84
|
|
|
{ |
|
85
|
|
|
if ($this->_model === null) { |
|
86
|
|
|
$this->_model = $this->findModel(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->_model; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function findModel() |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->owner->indexPageUiOptionsModel; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function isRouteAllowed() |
|
98
|
|
|
{ |
|
99
|
|
|
return in_array($this->owner->action->id, $this->allowedRoutes); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return UiOptionsStorage |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getUiOptionsStorage() |
|
106
|
|
|
{ |
|
107
|
|
|
return Yii::$app->get('uiOptionsStorage'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* example: store/part/index |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function getRoute() |
|
116
|
|
|
{ |
|
117
|
|
|
$request = Yii::$app->request; |
|
118
|
|
|
if ($this->isRouteAllowed() && $request->get('route', false)) { |
|
119
|
|
|
return Html::encode($request->get('route')); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return Yii::$app->request->pathInfo; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
protected function findRepresentations() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->representationCollectionFinder->findOrFallback()->getAll(); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.