|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Active form input dynamic list |
|
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\ActiveForm\Input; |
|
13
|
|
|
|
|
14
|
|
|
class DynamicList extends \Ui\ActiveForm\Input { |
|
15
|
|
|
|
|
16
|
|
|
public function draw() { |
|
17
|
|
|
$inputName = $this->colName(); |
|
18
|
|
|
$inputLabel = $this->colLabel(); |
|
19
|
|
|
$inputOptions = [ |
|
20
|
|
|
'activeForm' => $this->activeForm, |
|
21
|
|
|
'cols' => $this->getCols(), |
|
22
|
|
|
'values' => $this->value(), |
|
23
|
|
|
'source' => !empty($this->colParams['source']) ? $this->colParams['source'] : 'relation', |
|
24
|
|
|
'modelPk' => $this->activeForm->model->pk() |
|
25
|
|
|
]; |
|
26
|
|
|
$this->form->input('dynamicList', $inputName, $inputLabel, $inputOptions); |
|
27
|
|
|
return true; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function parseRequest($request) { |
|
31
|
|
|
$modelName = $this->modelName; |
|
32
|
|
|
switch ($this->colParams['source']) { |
|
33
|
|
|
case'options': |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
break; |
|
36
|
|
|
default: |
|
37
|
|
|
$rels = []; |
|
38
|
|
|
$relation = $modelName::getRelation($this->colParams['relation']); |
|
39
|
|
|
if (!empty($request[$this->colName]) && $this->activeForm->model->pk()) { |
|
40
|
|
|
switch ($relation['type']) { |
|
41
|
|
|
case 'relModel': |
|
42
|
|
|
foreach ($request[$this->colName] as $row) { |
|
43
|
|
|
$rels[$row['relItem']] = true; |
|
44
|
|
|
} |
|
45
|
|
|
$relModels = $relation['relModel']::getList(['where' => [$modelName::index(), $this->activeForm->model->pk()], 'key' => $relation['model']::index()]); |
|
46
|
|
|
foreach ($relModels as $model) { |
|
47
|
|
|
if (empty($rels[$model->{$relation['model']::index()}])) { |
|
48
|
|
|
$model->delete(); |
|
49
|
|
|
} else { |
|
50
|
|
|
unset($rels[$model->{$relation['model']::index()}]); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
foreach ($rels as $relId => $trash) { |
|
54
|
|
|
$model = new $relation['relModel']([ |
|
55
|
|
|
$modelName::index() => $this->activeForm->model->pk(), |
|
56
|
|
|
$relation['model']::index() => $relId |
|
57
|
|
|
]); |
|
58
|
|
|
$model->save(); |
|
59
|
|
|
} |
|
60
|
|
|
break; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function value() { |
|
67
|
|
|
$values = []; |
|
68
|
|
|
switch ($this->colParams['source']) { |
|
69
|
|
|
case'options': |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
break; |
|
72
|
|
|
default: |
|
73
|
|
|
if ($this->activeForm->model) { |
|
74
|
|
|
$items = $this->activeForm->model->{$this->colParams['relation']}(['array' => true]); |
|
75
|
|
|
foreach ($items as $key => $item) { |
|
76
|
|
|
$values[] = ['relItem' => $key]; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $values; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getCols() { |
|
85
|
|
|
$modelName = $this->modelName; |
|
86
|
|
|
switch ($this->colParams['source']) { |
|
87
|
|
|
case'options': |
|
|
|
|
|
|
88
|
|
|
foreach ($this->colParams['options']['inputs'] as $colName => $col) { |
|
89
|
|
|
$inputClassName = '\Ui\ActiveForm\Input\\' . ucfirst($col['type']); |
|
90
|
|
|
$input = new $inputClassName(); |
|
91
|
|
|
$input->form = $this->form; |
|
92
|
|
|
$input->activeForm = $this->activeForm; |
|
93
|
|
|
$input->activeFormParams = $this->activeFormParams; |
|
94
|
|
|
$input->modelName = $this->modelName; |
|
95
|
|
|
$input->colName = "[{$this->colName}][{$colName}][]"; |
|
96
|
|
|
$input->colParams = $col; |
|
97
|
|
|
$input->options = !empty($col['options']) ? $col['options'] : []; |
|
98
|
|
|
$cols[] = ['input' => $input, 'col' => $col]; |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
break; |
|
101
|
|
|
default: |
|
102
|
|
|
$relation = $modelName::getRelation($this->colParams['relation']); |
|
103
|
|
|
$cols = []; |
|
104
|
|
|
switch ($relation['type']) { |
|
105
|
|
|
case 'relModel': |
|
106
|
|
|
$cols['relItem'] = [ |
|
107
|
|
|
'col' => [ |
|
108
|
|
|
'label' => $relation['model']::objectName(), |
|
109
|
|
|
'type' => 'select', |
|
110
|
|
|
'options' => [ |
|
111
|
|
|
'values' => $relation['model']::getList(['forSelect' => true]) |
|
112
|
|
|
] |
|
113
|
|
|
] |
|
114
|
|
|
]; |
|
115
|
|
|
break; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
return $cols; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
As per the PSR-2 coding standard, there must be a space after the
casekeyword, instead of the test immediately following it.To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.