|
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\actions; |
|
12
|
|
|
|
|
13
|
|
|
use hipanel\base\Model; |
|
14
|
|
|
use hipanel\base\SearchModelTrait; |
|
15
|
|
|
use hiqdev\hiart\ActiveDataProvider; |
|
16
|
|
|
use Yii; |
|
17
|
|
|
use yii\web\BadRequestHttpException; |
|
18
|
|
|
use yii\web\NotFoundHttpException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class SmartUpdateAction. |
|
22
|
|
|
* @property string view |
|
23
|
|
|
*/ |
|
24
|
|
|
class SmartUpdateAction extends SwitchAction |
|
25
|
|
|
{ |
|
26
|
|
|
const EVENT_BEFORE_FETCH_LOAD = 'beforeFetchLoad'; |
|
27
|
|
|
const EVENT_BEFORE_FETCH = 'beforeFetch'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array|\Closure additional data passed to view |
|
31
|
|
|
*/ |
|
32
|
|
|
public $data = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string The view that represents current update action |
|
36
|
|
|
*/ |
|
37
|
|
|
public $_view; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var mixed ID of object to be viewed. Defaults to `$_GET['id']` |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $_id; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var array additional data passed to model find method |
|
46
|
|
|
*/ |
|
47
|
|
|
public $findOptions = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var Model |
|
51
|
|
|
*/ |
|
52
|
|
|
private $_searchModel; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var \yii\data\ActiveDataProvider stores ActiveDataProvider after creating by [[getDataProvider]] |
|
56
|
|
|
* @see getDataProvider() |
|
57
|
|
|
*/ |
|
58
|
|
|
public $dataProvider; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Creates `ActiveDataProvider` with given options list, stores it to [[dataProvider]]. |
|
62
|
|
|
* |
|
63
|
|
|
* @throws BadRequestHttpException |
|
64
|
|
|
* @return ActiveDataProvider |
|
65
|
|
|
* @throws \yii\base\InvalidConfigException when failed to generate `WHERE` condition |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getDataProvider() |
|
68
|
|
|
{ |
|
69
|
|
|
if ($this->dataProvider === null) { |
|
70
|
|
|
$this->_id = $this->_id |
|
71
|
|
|
?: Yii::$app->request->post('selection') |
|
72
|
|
|
?: Yii::$app->request->get('selection') |
|
73
|
|
|
?: Yii::$app->request->get('id'); |
|
74
|
|
|
|
|
75
|
|
|
$this->dataProvider = $this->getSearchModel()->search([], ['pagination' => false]); |
|
|
|
|
|
|
76
|
|
|
$this->dataProvider->query->andFilterWhere( |
|
77
|
|
|
is_array($this->_id) ? ['in', 'id', $this->_id] : ['eq', 'id', $this->_id] |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
if (empty($this->dataProvider->query->where)) { |
|
81
|
|
|
throw new BadRequestHttpException('Where condition is empty!'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->dataProvider->query->andFilterWhere($this->findOptions); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->dataProvider; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param $model |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setSearchModel($model) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->_searchModel = $model; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return Model|SearchModelTrait |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getSearchModel() |
|
102
|
|
|
{ |
|
103
|
|
|
if (is_null($this->_searchModel)) { |
|
104
|
|
|
$this->_searchModel = $this->controller->searchModel(); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
return $this->_searchModel; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** {@inheritdoc} */ |
|
110
|
|
|
protected function getDefaultRules() |
|
111
|
|
|
{ |
|
112
|
|
|
return array_merge(parent::getDefaultRules(), [ |
|
113
|
|
|
'POST xeditable' => [ |
|
114
|
|
|
'class' => XEditableAction::class, |
|
115
|
|
|
], |
|
116
|
|
|
'GET html | POST selection' => [ |
|
117
|
|
|
'class' => RenderAction::class, |
|
118
|
|
|
'data' => $this->data, |
|
119
|
|
|
'view' => $this->view, |
|
120
|
|
View Code Duplication |
'params' => function ($action) { |
|
|
|
|
|
|
121
|
|
|
$models = $this->fetchModels(); |
|
122
|
|
|
if (empty($models)) { |
|
123
|
|
|
throw new NotFoundHttpException('Search result is empty.'); |
|
124
|
|
|
} |
|
125
|
|
|
foreach ($models as $model) { |
|
126
|
|
|
$model->scenario = $this->scenario; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
return [ |
|
129
|
|
|
'models' => $models, |
|
130
|
|
|
'model' => reset($models), |
|
131
|
|
|
]; |
|
132
|
|
|
}, |
|
133
|
|
|
], |
|
134
|
|
|
'GET ajax' => [ |
|
135
|
|
|
'success' => [ |
|
136
|
|
|
'class' => RenderAjaxAction::class, |
|
137
|
|
|
'data' => $this->data, |
|
138
|
|
|
'view' => $this->view, |
|
139
|
|
View Code Duplication |
'params' => function ($action) { |
|
|
|
|
|
|
140
|
|
|
$models = $this->fetchModels(); |
|
141
|
|
|
foreach ($models as $model) { |
|
|
|
|
|
|
142
|
|
|
$model->scenario = $this->scenario; |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
return [ |
|
145
|
|
|
'models' => $models, |
|
146
|
|
|
'model' => reset($models), |
|
147
|
|
|
]; |
|
148
|
|
|
}, |
|
149
|
|
|
], |
|
150
|
|
|
], |
|
151
|
|
|
'POST html' => [ |
|
152
|
|
|
'save' => true, |
|
153
|
|
|
'success' => [ |
|
154
|
|
|
'class' => RedirectAction::class, |
|
155
|
|
View Code Duplication |
'url' => function ($action) { |
|
|
|
|
|
|
156
|
|
|
return $action->collection->count() > 1 |
|
157
|
|
|
? $action->controller->getSearchUrl() |
|
158
|
|
|
: $action->controller->getActionUrl('view', ['id' => $action->collection->first->id]); |
|
159
|
|
|
}, |
|
160
|
|
|
], |
|
161
|
|
|
'error' => [ |
|
162
|
|
|
'class' => RenderAction::class, |
|
163
|
|
|
'view' => $this->view, |
|
164
|
|
|
'data' => $this->data, |
|
165
|
|
|
'params' => function ($action) { |
|
|
|
|
|
|
166
|
|
|
try { |
|
167
|
|
|
$models = $this->fetchModels(); |
|
168
|
|
|
|
|
169
|
|
|
foreach ($models as $model) { |
|
|
|
|
|
|
170
|
|
|
$model->scenario = $this->scenario; |
|
|
|
|
|
|
171
|
|
|
foreach ($this->collection->models as $payload) { |
|
172
|
|
|
if ($payload->id === $model->id) { |
|
173
|
|
|
$model->setAttributes(array_filter($payload->getAttributes())); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
} catch (BadRequestHttpException $e) { |
|
178
|
|
|
$models = $this->collection->models; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return [ |
|
182
|
|
|
'model' => reset($models), |
|
183
|
|
|
'models' => $models, |
|
184
|
|
|
]; |
|
185
|
|
|
}, |
|
186
|
|
|
], |
|
187
|
|
|
], |
|
188
|
|
|
'POST pjax' => [ |
|
189
|
|
|
'save' => true, |
|
190
|
|
|
'success' => [ |
|
191
|
|
|
'class' => ProxyAction::class, |
|
192
|
|
|
'action' => 'view', |
|
193
|
|
|
'params' => function ($action, $model) { |
|
194
|
|
|
return ['id' => $model->id]; |
|
195
|
|
|
}, |
|
196
|
|
|
], |
|
197
|
|
|
], |
|
198
|
|
|
'POST ajax' => [ |
|
199
|
|
|
'save' => true, |
|
200
|
|
|
'success' => [ |
|
201
|
|
|
'class' => RedirectAction::class, |
|
202
|
|
View Code Duplication |
'url' => function ($action) { |
|
|
|
|
|
|
203
|
|
|
return $action->collection->count() > 1 |
|
204
|
|
|
? $action->controller->getSearchUrl() |
|
205
|
|
|
: $action->controller->getActionUrl('view', ['id' => $action->collection->first->id]); |
|
206
|
|
|
}, |
|
207
|
|
|
], |
|
208
|
|
|
], |
|
209
|
|
|
]); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
|
|
public function getView() |
|
216
|
|
|
{ |
|
217
|
|
|
return $this->_view ?: $this->scenario; |
|
|
|
|
|
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param string $view |
|
222
|
|
|
*/ |
|
223
|
|
|
public function setView($view) |
|
224
|
|
|
{ |
|
225
|
|
|
$this->_view = $view; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Fetches models that will be edited. |
|
230
|
|
|
* |
|
231
|
|
|
* @throws BadRequestHttpException |
|
232
|
|
|
* @return array |
|
233
|
|
|
*/ |
|
234
|
|
|
public function fetchModels() |
|
235
|
|
|
{ |
|
236
|
|
|
$this->beforeFetchLoad(); |
|
237
|
|
|
$dataProvider = $this->getDataProvider(); |
|
238
|
|
|
$this->beforeFetch(); |
|
239
|
|
|
return $dataProvider->getModels(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
public function beforeFetchLoad() |
|
243
|
|
|
{ |
|
244
|
|
|
$this->trigger(static::EVENT_BEFORE_FETCH_LOAD); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function beforeFetch() |
|
248
|
|
|
{ |
|
249
|
|
|
$this->trigger(static::EVENT_BEFORE_FETCH); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: