@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | |
14 | 14 | class DynamicList extends \Ui\ActiveForm\Input { |
15 | 15 | |
16 | - public function draw() { |
|
16 | + public function draw() { |
|
17 | 17 | $inputName = $this->colName(); |
18 | 18 | $inputLabel = $this->colLabel(); |
19 | 19 | $inputOptions = [ |
@@ -25,119 +25,119 @@ discard block |
||
25 | 25 | ]; |
26 | 26 | $this->form->input('dynamicList', $inputName, $inputLabel, $inputOptions); |
27 | 27 | return true; |
28 | - } |
|
28 | + } |
|
29 | 29 | |
30 | - public function parseRequest($request) { |
|
30 | + public function parseRequest($request) { |
|
31 | 31 | $modelName = $this->modelName; |
32 | 32 | switch ($this->colParams['source']) { |
33 | - case'options': |
|
33 | + case'options': |
|
34 | 34 | |
35 | 35 | break; |
36 | - default: |
|
36 | + default: |
|
37 | 37 | $rels = []; |
38 | 38 | $relation = $modelName::getRelation($this->colParams['relation']); |
39 | 39 | if ($this->activeForm->model->pk()) { |
40 | - switch ($relation['type']) { |
|
40 | + switch ($relation['type']) { |
|
41 | 41 | case 'relModel': |
42 | 42 | foreach ($request[$this->colName] as $row) { |
43 | 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) { |
|
44 | + } |
|
45 | + $relModels = $relation['relModel']::getList(['where' => [$modelName::index(), $this->activeForm->model->pk()], 'key' => $relation['model']::index()]); |
|
46 | + foreach ($relModels as $model) { |
|
47 | 47 | if (empty($rels[$model->{$relation['model']::index()}])) { |
48 | - $model->delete(); |
|
48 | + $model->delete(); |
|
49 | 49 | } else { |
50 | - unset($rels[$model->{$relation['model']::index()}]); |
|
50 | + unset($rels[$model->{$relation['model']::index()}]); |
|
51 | + } |
|
51 | 52 | } |
52 | - } |
|
53 | - foreach ($rels as $relId => $trash) { |
|
53 | + foreach ($rels as $relId => $trash) { |
|
54 | 54 | $model = new $relation['relModel']([ |
55 | 55 | $modelName::index() => $this->activeForm->model->pk(), |
56 | 56 | $relation['model']::index() => $relId |
57 | 57 | ]); |
58 | 58 | $model->save(); |
59 | - } |
|
60 | - break; |
|
59 | + } |
|
60 | + break; |
|
61 | 61 | |
62 | 62 | case 'many': |
63 | 63 | $requestData = []; |
64 | - if (!empty($request[$this->colName])) { |
|
64 | + if (!empty($request[$this->colName])) { |
|
65 | 65 | foreach ($request[$this->colName] as $colName => $items) { |
66 | - foreach ($request[$this->colName][$colName] as $key => $data) { |
|
66 | + foreach ($request[$this->colName][$colName] as $key => $data) { |
|
67 | 67 | $requestData[$key][$colName] = $data; |
68 | - } |
|
68 | + } |
|
69 | + } |
|
69 | 70 | } |
70 | - } |
|
71 | - foreach ($requestData as $key => $row) { |
|
71 | + foreach ($requestData as $key => $row) { |
|
72 | 72 | if (!empty($row['id'])) { |
73 | - $rels[$row['id']] = $row; |
|
74 | - unset($requestData[$key]); |
|
73 | + $rels[$row['id']] = $row; |
|
74 | + unset($requestData[$key]); |
|
75 | 75 | } |
76 | - } |
|
77 | - $relModels = $this->activeForm->model->{$this->colParams['relation']}; |
|
78 | - foreach ($relModels as $model) { |
|
76 | + } |
|
77 | + $relModels = $this->activeForm->model->{$this->colParams['relation']}; |
|
78 | + foreach ($relModels as $model) { |
|
79 | 79 | if (empty($rels[$model->pk()])) { |
80 | - $model->delete(); |
|
80 | + $model->delete(); |
|
81 | 81 | } else { |
82 | - $model->setParams($rels[$model->pk()]); |
|
83 | - $model->save(); |
|
82 | + $model->setParams($rels[$model->pk()]); |
|
83 | + $model->save(); |
|
84 | + } |
|
84 | 85 | } |
85 | - } |
|
86 | - foreach ($requestData as $row) { |
|
86 | + foreach ($requestData as $row) { |
|
87 | 87 | $row[$relation['col']] = $this->activeForm->model->pk(); |
88 | 88 | $model = new $relation['model']($row); |
89 | 89 | $model->save(); |
90 | - } |
|
91 | - $this->activeForm->model->loadRelation($this->colParams['relation']); |
|
92 | - } |
|
90 | + } |
|
91 | + $this->activeForm->model->loadRelation($this->colParams['relation']); |
|
92 | + } |
|
93 | 93 | } |
94 | 94 | } |
95 | - } |
|
95 | + } |
|
96 | 96 | |
97 | - public function value() { |
|
97 | + public function value() { |
|
98 | 98 | $values = []; |
99 | 99 | switch ($this->colParams['source']) { |
100 | - case'options': |
|
100 | + case'options': |
|
101 | 101 | |
102 | 102 | break; |
103 | - default: |
|
103 | + default: |
|
104 | 104 | if ($this->activeForm->model) { |
105 | - $items = $this->activeForm->model->{$this->colParams['relation']}; |
|
106 | - foreach ($items as $key => $item) { |
|
105 | + $items = $this->activeForm->model->{$this->colParams['relation']}; |
|
106 | + foreach ($items as $key => $item) { |
|
107 | 107 | $value = ['id' => $item->id]; |
108 | 108 | foreach ($this->colParams['options']['cols'] as $colName) { |
109 | - $value[$colName] = $item->$colName; |
|
109 | + $value[$colName] = $item->$colName; |
|
110 | 110 | } |
111 | 111 | $values[] = $value; |
112 | - } |
|
112 | + } |
|
113 | 113 | } |
114 | 114 | } |
115 | 115 | |
116 | 116 | return $values; |
117 | - } |
|
117 | + } |
|
118 | 118 | |
119 | - public function getCols() { |
|
119 | + public function getCols() { |
|
120 | 120 | $modelName = $this->modelName; |
121 | 121 | $cols = []; |
122 | 122 | switch ($this->colParams['source']) { |
123 | - case'options': |
|
123 | + case'options': |
|
124 | 124 | foreach ($this->colParams['options']['inputs'] as $colName => $col) { |
125 | - $inputClassName = '\Ui\ActiveForm\Input\\' . ucfirst($col['type']); |
|
126 | - $input = new $inputClassName(); |
|
127 | - $input->form = $this->form; |
|
128 | - $input->activeForm = $this->activeForm; |
|
129 | - $input->activeFormParams = $this->activeFormParams; |
|
130 | - $input->modelName = $this->modelName; |
|
131 | - $input->colName = "[{$this->colName}]"; |
|
132 | - $input->colParams = $col; |
|
133 | - $input->options = !empty($col['options']) ? $col['options'] : []; |
|
134 | - $cols[$colName] = ['input' => $input, 'col' => $col]; |
|
125 | + $inputClassName = '\Ui\ActiveForm\Input\\' . ucfirst($col['type']); |
|
126 | + $input = new $inputClassName(); |
|
127 | + $input->form = $this->form; |
|
128 | + $input->activeForm = $this->activeForm; |
|
129 | + $input->activeFormParams = $this->activeFormParams; |
|
130 | + $input->modelName = $this->modelName; |
|
131 | + $input->colName = "[{$this->colName}]"; |
|
132 | + $input->colParams = $col; |
|
133 | + $input->options = !empty($col['options']) ? $col['options'] : []; |
|
134 | + $cols[$colName] = ['input' => $input, 'col' => $col]; |
|
135 | 135 | } |
136 | 136 | break; |
137 | - default: |
|
137 | + default: |
|
138 | 138 | $relation = $modelName::getRelation($this->colParams['relation']); |
139 | 139 | switch ($relation['type']) { |
140 | - case 'relModel': |
|
140 | + case 'relModel': |
|
141 | 141 | $cols['relItem'] = [ |
142 | 142 | 'col' => [ |
143 | 143 | 'label' => $relation['model']::objectName(), |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | ] |
149 | 149 | ]; |
150 | 150 | break; |
151 | - case 'many': |
|
151 | + case 'many': |
|
152 | 152 | $inputClassName = '\Ui\ActiveForm\Input\Hidden'; |
153 | 153 | $input = new $inputClassName(); |
154 | 154 | $input->form = $this->form; |
@@ -160,22 +160,22 @@ discard block |
||
160 | 160 | $input->options = []; |
161 | 161 | $cols['id'] = ['input' => $input, 'hidden' => true]; |
162 | 162 | foreach ($this->colParams['options']['cols'] as $colName) { |
163 | - $col = $relation['model']::getColInfo($colName); |
|
164 | - $inputClassName = '\Ui\ActiveForm\Input\\' . ucfirst($col['colParams']['type']); |
|
165 | - $input = new $inputClassName(); |
|
166 | - $input->form = $this->form; |
|
167 | - $input->activeForm = $this->activeForm; |
|
168 | - $input->activeFormParams = $this->activeFormParams; |
|
169 | - $input->modelName = $relation['model']; |
|
170 | - $input->colName = "[{$this->colName}]"; |
|
171 | - $input->colParams = $col; |
|
172 | - $input->options = !empty($col['options']) ? $col['options'] : []; |
|
173 | - $cols[$colName] = ['input' => $input, 'col' => $col]; |
|
163 | + $col = $relation['model']::getColInfo($colName); |
|
164 | + $inputClassName = '\Ui\ActiveForm\Input\\' . ucfirst($col['colParams']['type']); |
|
165 | + $input = new $inputClassName(); |
|
166 | + $input->form = $this->form; |
|
167 | + $input->activeForm = $this->activeForm; |
|
168 | + $input->activeFormParams = $this->activeFormParams; |
|
169 | + $input->modelName = $relation['model']; |
|
170 | + $input->colName = "[{$this->colName}]"; |
|
171 | + $input->colParams = $col; |
|
172 | + $input->options = !empty($col['options']) ? $col['options'] : []; |
|
173 | + $cols[$colName] = ['input' => $input, 'col' => $col]; |
|
174 | 174 | } |
175 | 175 | break; |
176 | 176 | } |
177 | 177 | } |
178 | 178 | return $cols; |
179 | - } |
|
179 | + } |
|
180 | 180 | |
181 | 181 | } |