1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Active form |
5
|
|
|
* |
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
7
|
|
|
* @link http://inji.ru/ |
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ui; |
13
|
|
|
|
14
|
|
|
class ActiveForm extends \Object { |
15
|
|
|
|
16
|
|
|
public $model = null; |
17
|
|
|
public $modelName = ''; |
18
|
|
|
public $header = ""; |
19
|
|
|
public $action = ""; |
20
|
|
|
public $form = []; |
21
|
|
|
public $inputs = []; |
22
|
|
|
public $formName = 'noNameForm'; |
23
|
|
|
public $requestFormName = ''; |
24
|
|
|
public $requestFullFormName = ''; |
25
|
|
|
public $parent = null; |
26
|
|
|
|
27
|
|
|
public function __construct($model, $form = '') { |
28
|
|
|
if (is_array($model)) { |
29
|
|
|
$this->form = $model; |
30
|
|
|
if (is_string($form)) { |
31
|
|
|
$this->formName = $form; |
32
|
|
|
} |
33
|
|
|
} else { |
34
|
|
|
$this->model = $model; |
35
|
|
|
$this->modelName = get_class($model); |
36
|
|
|
if (is_array($form)) { |
37
|
|
|
if (empty($form)) { |
38
|
|
|
throw new \Exception('empty form'); |
39
|
|
|
} |
40
|
|
|
$this->form = $form; |
41
|
|
|
} else { |
42
|
|
|
$this->formName = $form; |
43
|
|
|
$this->form = \App::$cur->ui->getModelForm($this->modelName, $form); |
44
|
|
|
if (empty($this->form)) { |
45
|
|
|
throw new \Exception('empty form ' . $form); |
46
|
|
|
} |
47
|
|
|
$this->inputs = $this->getInputs(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
$this->requestFormName = "ActiveForm_{$this->formName}"; |
51
|
|
|
$modeName = $this->modelName; |
52
|
|
|
|
53
|
|
|
if (!empty($this->form['name'])) { |
54
|
|
|
$this->header = $this->form['name']; |
55
|
|
|
} elseif (!empty($modeName::$objectName)) { |
56
|
|
|
$this->header = $modeName::$objectName; |
57
|
|
|
} else { |
58
|
|
|
$this->header = $this->modelName; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getInputs() { |
63
|
|
|
$inputs = !empty($this->form['inputs']) ? $this->form['inputs'] : []; |
64
|
|
|
$modelName = $this->modelName; |
65
|
|
|
foreach ($this->form['map'] as $row) { |
66
|
|
|
foreach ($row as $col) { |
67
|
|
|
if (!$col || !empty($inputs[$col])) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
if (strpos($col, 'form:') === 0) { |
71
|
|
|
$colPath = explode(':', $col); |
72
|
|
|
if ($this->model->{$colPath[1]}) { |
73
|
|
|
$inputs[$col] = new ActiveForm($this->model->{$colPath[1]}, $colPath[2]); |
74
|
|
|
} else { |
75
|
|
|
$relOptions = $modelName::getRelation($colPath[1]); |
76
|
|
View Code Duplication |
if (!isset($this->model->_params[$modelName::index()])) { |
|
|
|
|
77
|
|
|
$this->model->_params[$modelName::index()] = 0; |
78
|
|
|
} |
79
|
|
|
$relOptions['model']::fixPrefix($relOptions['col']); |
80
|
|
|
$inputs[$col] = new ActiveForm(new $relOptions['model'](), $colPath[2]); |
81
|
|
|
} |
82
|
|
|
$inputs[$col]->parent = $this; |
83
|
|
|
|
84
|
|
|
$inputs[$col]->parent = $this; |
85
|
|
|
} elseif (!empty($modelName::$cols[$col])) { |
86
|
|
|
$inputs[$col] = $modelName::$cols[$col]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
return $inputs; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function checkRequest($params = [], $ajax = false) { |
94
|
|
View Code Duplication |
if (!$this->checkAccess()) { |
|
|
|
|
95
|
|
|
$this->drawError('you not have access to "' . $this->modelName . '" manager with name: "' . $this->formName . '"'); |
96
|
|
|
return []; |
97
|
|
|
} |
98
|
|
|
$successId = 0; |
99
|
|
|
$modelName = $this->model; |
100
|
|
|
if (!empty($_POST[$this->requestFormName][$this->modelName]) || !empty($_FILES[$this->requestFormName]['tmp_name'][$this->modelName])) { |
101
|
|
|
$request = !empty($_POST[$this->requestFormName][$this->modelName]) ? $_POST[$this->requestFormName][$this->modelName] : []; |
102
|
|
|
if ($this->model) { |
103
|
|
|
if (!empty($this->form['handler']) && empty($_GET['notSave'])) { |
104
|
|
|
|
105
|
|
|
$modelName::{$this->form['handler']}($request); |
106
|
|
|
$text = 'Новый элемент был успешно добавлен'; |
107
|
|
|
\Msg::add($text, 'success'); |
108
|
|
|
\Msg::show(); |
109
|
|
|
} else { |
110
|
|
|
$presets = !empty($this->form['preset']) ? $this->form['preset'] : []; |
111
|
|
View Code Duplication |
if (!empty($this->form['userGroupPreset'][\Users\User::$cur->group_id])) { |
|
|
|
|
112
|
|
|
$presets = array_merge($presets, $this->form['userGroupPreset'][\Users\User::$cur->group_id]); |
113
|
|
|
} |
114
|
|
|
$afterSave = []; |
115
|
|
|
$error = false; |
116
|
|
|
foreach ($this->inputs as $col => $param) { |
117
|
|
|
if (!empty($presets[$col])) { |
118
|
|
|
continue; |
119
|
|
|
} |
120
|
|
|
if (is_object($param)) { |
121
|
|
|
$afterSave[$col] = $param; |
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
View Code Duplication |
if (!empty($this->form['userGroupReadonly'][\Users\User::$cur->group_id]) && in_array($col, $this->form['userGroupReadonly'][\Users\User::$cur->group_id])) { |
|
|
|
|
125
|
|
|
continue; |
126
|
|
|
} |
127
|
|
|
$type = ucfirst($param['type']); |
128
|
|
View Code Duplication |
if ($type == 'DynamicType') { |
|
|
|
|
129
|
|
|
switch ($param['typeSource']) { |
130
|
|
|
case 'selfMethod': |
131
|
|
|
$type = $this->model->{$param['selfMethod']}(); |
132
|
|
|
break; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
$inputClassName = '\Ui\ActiveForm\Input\\' . $type; |
136
|
|
|
$input = new $inputClassName(); |
137
|
|
|
$input->activeForm = $this; |
138
|
|
|
$input->activeFormParams = $params; |
139
|
|
|
$input->modelName = $this->modelName; |
140
|
|
|
$input->colName = $col; |
141
|
|
|
$input->colParams = $param; |
142
|
|
|
try { |
143
|
|
|
$input->validate($request); |
144
|
|
|
$input->parseRequest($request); |
145
|
|
|
} catch (\Exception $exc) { |
146
|
|
|
\Msg::add($exc->getMessage(), 'danger'); |
147
|
|
|
$error = true; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
if (!$error && empty($_GET['notSave'])) { |
151
|
|
|
foreach ($presets as $col => $preset) { |
152
|
|
|
if (!empty($preset['value'])) { |
153
|
|
|
$this->model->$col = $preset['value']; |
154
|
|
|
} elseif (!empty($preset['userCol'])) { |
155
|
|
|
if (strpos($preset['userCol'], ':')) { |
156
|
|
|
$rel = substr($preset['userCol'], 0, strpos($preset['userCol'], ':')); |
157
|
|
|
$param = substr($preset['userCol'], strpos($preset['userCol'], ':') + 1); |
158
|
|
|
$this->model->$col = \Users\User::$cur->$rel->$param; |
159
|
|
|
} else { |
160
|
|
|
$this->model->$col = \Users\User::$cur->{$preset['userCol']}; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
if (!$this->parent) { |
165
|
|
|
if (!empty($this->form['successText'])) { |
166
|
|
|
$text = $this->form['successText']; |
167
|
|
|
} else { |
168
|
|
|
$text = $this->model->pk() ? 'Изменения были успешно сохранены' : 'Новый элемент был успешно добавлен'; |
169
|
|
|
} |
170
|
|
|
\Msg::add($text, 'success'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->model->save(!empty($params['dataManagerParams']) ? $params['dataManagerParams'] : []); |
174
|
|
|
foreach ($afterSave as $col => $form) { |
175
|
|
|
if (strpos($col, 'form:') === 0) { |
176
|
|
|
$colPath = explode(':', $col); |
177
|
|
|
if (!$this->model->{$colPath[1]}) { |
178
|
|
|
$relOptions = $modelName::getRelation($colPath[1]); |
179
|
|
View Code Duplication |
if (!isset($this->model->_params[$modelName::index()])) { |
|
|
|
|
180
|
|
|
$this->model->_params[$modelName::index()] = 0; |
181
|
|
|
} |
182
|
|
|
$relOptions['model']::fixPrefix($relOptions['col']); |
183
|
|
|
$form->model->{$relOptions['col']} = $this->model->_params[$modelName::index()]; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
$form->checkRequest(); |
187
|
|
|
} |
188
|
|
|
if ($ajax) { |
189
|
|
|
\Msg::show(); |
190
|
|
|
} elseif (!empty($_GET['redirectUrl'])) { |
191
|
|
|
\Tools::redirect($_GET['redirectUrl'] . (!empty($_GET['dataManagerHash']) ? '#' . $_GET['dataManagerHash'] : '')); |
192
|
|
|
} |
193
|
|
|
$successId = $this->model->pk(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
if (!is_array($params) && is_callable($params)) { |
198
|
|
|
$params($request); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
return $successId; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function draw($params = [], $ajax = false) { |
205
|
|
View Code Duplication |
if (!$this->checkAccess()) { |
|
|
|
|
206
|
|
|
$this->drawError('you not have access to "' . $this->modelName . '" form with name: "' . $this->formName . '"'); |
207
|
|
|
return []; |
208
|
|
|
} |
209
|
|
|
$form = new Form(!empty($this->form['formOptions']) ? $this->form['formOptions'] : []); |
210
|
|
|
\App::$cur->view->widget('Ui\ActiveForm', ['form' => $form, 'activeForm' => $this, 'ajax' => $ajax, 'params' => $params]); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function drawCol($colName, $options, $form, $params = []) { |
214
|
|
|
if (is_object($options)) { |
215
|
|
|
$options->draw(); |
216
|
|
|
} else { |
217
|
|
|
$type = ucfirst($options['type']); |
218
|
|
View Code Duplication |
if ($type == 'DynamicType') { |
|
|
|
|
219
|
|
|
switch ($options['typeSource']) { |
220
|
|
|
case 'selfMethod': |
221
|
|
|
$type = $this->model->{$options['selfMethod']}(); |
222
|
|
|
break; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
$inputClassName = '\Ui\ActiveForm\Input\\' . $type; |
226
|
|
|
$input = new $inputClassName(); |
227
|
|
|
$input->form = $form; |
228
|
|
|
$input->activeForm = $this; |
229
|
|
|
$input->activeFormParams = $params; |
230
|
|
|
$input->modelName = $this->modelName; |
231
|
|
|
$input->colName = $colName; |
232
|
|
|
$input->colParams = $options; |
233
|
|
|
$input->options = !empty($options['options']) ? $options['options'] : []; |
234
|
|
|
$input->draw(); |
235
|
|
|
} |
236
|
|
|
return true; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public static function getOptionsList($inputParams, $params = [], $modelName = '', $aditionalInputNamePrefix = 'aditional', $options = []) { |
240
|
|
|
$values = []; |
241
|
|
|
switch ($inputParams['source']) { |
242
|
|
|
case 'model': |
243
|
|
|
$values = $inputParams['model']::getList(['forSelect' => true]); |
244
|
|
|
break; |
245
|
|
|
case 'array': |
246
|
|
|
$values = $inputParams['sourceArray']; |
247
|
|
|
break; |
248
|
|
|
case 'method': |
249
|
|
|
if (!empty($inputParams['params'])) { |
250
|
|
|
$values = call_user_func_array([\App::$cur->$inputParams['module'], $inputParams['method']], $inputParams['params']); |
251
|
|
|
} else { |
252
|
|
|
$values = \App::$cur->$inputParams['module']->$inputParams['method'](); |
253
|
|
|
} |
254
|
|
|
break; |
255
|
|
|
case 'relation': |
256
|
|
|
if (!$modelName) { |
257
|
|
|
return []; |
258
|
|
|
} |
259
|
|
|
$relation = $modelName::getRelation($inputParams['relation']); |
260
|
|
View Code Duplication |
if (!empty($params['dataManagerParams']['appType'])) { |
|
|
|
|
261
|
|
|
$options['appType'] = $params['dataManagerParams']['appType']; |
262
|
|
|
} |
263
|
|
|
$items = []; |
264
|
|
|
if (class_exists($relation['model'])) { |
265
|
|
|
$filters = $relation['model']::managerFilters(); |
266
|
|
|
if (!empty($filters['getRows']['where'])) { |
267
|
|
|
$options['where'][] = $filters['getRows']['where']; |
268
|
|
|
} |
269
|
|
|
if (!empty($relation['where'])) { |
270
|
|
|
$options['where'][] = $relation['where']; |
271
|
|
|
} |
272
|
|
|
if (!empty($relation['order'])) { |
273
|
|
|
$options['order'] = $relation['order']; |
274
|
|
|
} |
275
|
|
|
if (!empty($inputParams['itemName'])) { |
276
|
|
|
$options['itemName'] = $inputParams['itemName']; |
277
|
|
|
} |
278
|
|
|
$items = $relation['model']::getList($options); |
279
|
|
|
} |
280
|
|
|
if (!empty($params['noEmptyValue'])) { |
281
|
|
|
$values = []; |
282
|
|
|
} else { |
283
|
|
|
$values = [0 => 'Не задано']; |
284
|
|
|
} |
285
|
|
|
foreach ($items as $key => $item) { |
286
|
|
|
if (!empty($inputParams['showCol'])) { |
287
|
|
|
if (is_array($inputParams['showCol'])) { |
288
|
|
|
switch ($inputParams['showCol']['type']) { |
289
|
|
|
case 'staticMethod': |
290
|
|
|
$values[$key] = $inputParams['showCol']['class']::{$inputParams['showCol']['method']}($item); |
291
|
|
|
break; |
292
|
|
|
} |
293
|
|
|
} else { |
294
|
|
|
$values[$key] = $item->$inputParams['showCol']; |
295
|
|
|
} |
296
|
|
|
} else { |
297
|
|
|
$values[$key] = $item->name(); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
break; |
301
|
|
|
} |
302
|
|
|
foreach ($values as $key => $value) { |
303
|
|
|
if (is_array($value) && !empty($value['input']) && empty($value['input']['noprefix'])) { |
304
|
|
|
$values[$key]['input']['name'] = $aditionalInputNamePrefix . "[{$value['input']['name']}]"; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
return $values; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Draw error message |
312
|
|
|
* |
313
|
|
|
* @param string $errorText |
314
|
|
|
*/ |
315
|
|
|
public function drawError($errorText) { |
316
|
|
|
echo $errorText; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Check access cur user to form with name in param and $model |
321
|
|
|
* |
322
|
|
|
* @return boolean |
323
|
|
|
*/ |
324
|
|
|
public function checkAccess() { |
325
|
|
|
if (empty($this->form)) { |
326
|
|
|
$this->drawError('"' . $this->modelName . '" form with name: "' . $this->formName . '" not found'); |
327
|
|
|
return false; |
328
|
|
|
} |
329
|
|
|
if (\App::$cur->Access && !\App::$cur->Access->checkAccess($this)) { |
330
|
|
|
return false; |
331
|
|
|
} |
332
|
|
|
if (!empty($this->form['options']['access']['apps']) && !in_array(\App::$cur->name, $this->form['options']['access']['apps'])) { |
333
|
|
|
return false; |
334
|
|
|
} |
335
|
|
View Code Duplication |
if (!empty($this->form['options']['access']['groups']) && in_array(\Users\User::$cur->group_id, $this->form['options']['access']['groups'])) { |
|
|
|
|
336
|
|
|
return true; |
337
|
|
|
} |
338
|
|
|
if ($this->model && !empty($this->form['options']['access']['self']) && \Users\User::$cur->id == $this->model->user_id) { |
339
|
|
|
return true; |
340
|
|
|
} |
341
|
|
|
if ($this->formName == 'manager' && !\Users\User::$cur->isAdmin()) { |
342
|
|
|
return false; |
343
|
|
|
} |
344
|
|
|
return true; |
345
|
|
|
} |
346
|
|
|
} |
347
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.