1 | <?php |
||
15 | class FileBind |
||
16 | { |
||
17 | 1 | public function bind($model, $attribute, $files) |
|
39 | |||
40 | private function save($model, $attribute, $insertData, $updateData, $resultFiles) |
||
41 | { |
||
42 | if (count($insertData['rows'])) { |
||
43 | $this->insertRelations($model, $attribute, $insertData['rows'], $insertData['columns']); |
||
44 | } |
||
45 | if (count($updateData['rows'])) { |
||
46 | $this->updateRelations($model, $attribute, $updateData['rows']); |
||
47 | } |
||
48 | if (count($resultFiles)) { |
||
49 | $this->updateFiles($model, $attribute, $resultFiles); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | 1 | private function newFiles($model, $attribute, $fileIds) |
|
63 | |||
64 | private function prepareInsertRelations($model, $attribute, $newFiles) |
||
65 | { |
||
66 | $ownerId = $model->getPrimaryKey(); |
||
67 | $relation = $model->fileRelation($attribute); |
||
68 | $uploadedFiles = $model->fileState($attribute); |
||
69 | $handlerExtraFields = $model->fileOption($attribute, 'extraFields'); |
||
70 | |||
71 | $files = []; |
||
72 | $rows = []; |
||
73 | $extraFields = []; |
||
74 | foreach ($uploadedFiles as $fileId) { |
||
75 | if (isset($newFiles[$fileId])) { |
||
76 | $file = $newFiles[$fileId]; |
||
77 | $row = [$ownerId, $fileId]; |
||
78 | if ($handlerExtraFields) { |
||
79 | $fields = [ |
||
80 | key($relation->via->link) => $ownerId, |
||
81 | current($relation->link) => $fileId, |
||
82 | ]; |
||
83 | $extraFields = $handlerExtraFields($file, $fields); |
||
84 | $row = array_merge($row, array_values($extraFields)); |
||
85 | } |
||
86 | $rows[] = $row; |
||
87 | $files[$file->getPrimaryKey()] = $file; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | $columns = [key($relation->via->link), current($relation->link)]; |
||
92 | $columns = array_merge($columns, array_keys($extraFields)); |
||
93 | |||
94 | return ['rows' => $rows, 'files' => $files, 'columns' => $columns]; |
||
95 | } |
||
96 | |||
97 | private function prepareUpdateRelations($model, $attribute, $newFiles, $currentRelations) |
||
98 | { |
||
99 | $relation = $model->fileRelation($attribute); |
||
100 | $handlerExtraFields = $model->fileOption($attribute, 'extraFields'); |
||
101 | |||
102 | $files = []; |
||
103 | $rows = []; |
||
104 | foreach ($currentRelations as $fields) { |
||
105 | if (isset($newFiles[$fields[current($relation->link)]])) { |
||
106 | $file = $newFiles[$fields[current($relation->link)]]; |
||
107 | if ($handlerExtraFields) { |
||
108 | $extraFields = $handlerExtraFields($file, $fields); |
||
109 | $fieldChanged = (bool)count(array_diff_assoc($extraFields, $fields)); |
||
110 | if ($fieldChanged) { |
||
111 | $rows[$file->getPrimaryKey()] = $extraFields; |
||
112 | } |
||
113 | } |
||
114 | $files[$file->getPrimaryKey()] = $file; |
||
115 | } |
||
116 | } |
||
117 | return ['rows' => $rows, 'files' => $files]; |
||
118 | } |
||
119 | |||
120 | private function insertRelations($model, $attribute, $rows, $columns) |
||
127 | |||
128 | private function updateRelations($model, $attribute, $rows) |
||
129 | { |
||
130 | $relation = $model->fileRelation($attribute); |
||
131 | $ownerId = $model->getPrimaryKey(); |
||
132 | $db = Yii::$app->getDb()->createCommand(); |
||
133 | |||
134 | foreach ($rows as $fileId => $row) { |
||
135 | $db->update($relation->via->from[0], $row, [ |
||
136 | key($relation->via->link) => $ownerId, |
||
137 | current($relation->link) => $fileId |
||
138 | ])->execute(); |
||
139 | } |
||
140 | } |
||
141 | |||
142 | private function updateFiles($model, $attribute, $files) |
||
143 | { |
||
144 | $handlerUpdateFile = $model->fileOption($attribute, 'updateFile'); |
||
145 | $relation = $model->fileOption($attribute, 'relation'); |
||
146 | $isMultiple = $model->fileOption($attribute, 'multiple'); |
||
147 | |||
148 | $relationValue = []; |
||
149 | foreach ($files as $file) { |
||
150 | $relationValue[] = $file; |
||
151 | if ($handlerUpdateFile) { |
||
152 | $fileUpd = $handlerUpdateFile($file); |
||
153 | $dirtyAttributes = $fileUpd->getDirtyAttributes(); |
||
154 | if (count($dirtyAttributes)) { |
||
155 | $fileUpd->updateAttributes($dirtyAttributes); |
||
156 | } |
||
157 | } |
||
158 | } |
||
159 | |||
160 | if (!$isMultiple) { |
||
161 | $relationValue = array_shift($relationValue); |
||
162 | } |
||
163 | $model->populateRelation($relation, $relationValue); |
||
164 | } |
||
165 | |||
166 | 1 | public function delete($model, $attribute, $files) |
|
167 | { |
||
168 | 1 | $relation = $model->fileRelation($attribute); |
|
169 | 1 | $storage = $model->fileStorage($attribute); |
|
170 | 1 | $presets = array_keys($model->fileOption($attribute, 'preset', [])); |
|
171 | 1 | $handlerTemplatePath = $model->fileOption($attribute, 'templatePath'); |
|
172 | |||
173 | 1 | $db = Yii::$app->getDb()->createCommand(); |
|
174 | 1 | foreach ($files as $file) { |
|
175 | foreach ($presets as $preset) { |
||
176 | $thumbPath = $model->thumbPath($attribute, $preset, $file); |
||
177 | $filePath = str_replace($storage->path, '', $thumbPath); |
||
178 | if ($storage->has($filePath)) { |
||
179 | $storage->delete($filePath); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | if ($file->delete()) { |
||
184 | $db->delete($relation->via->from[0], [ |
||
185 | current($relation->link) => $file->getPrimaryKey() |
||
186 | ])->execute(); |
||
187 | $filePath = $handlerTemplatePath($file); |
||
188 | if ($storage->has($filePath)) { |
||
189 | $storage->delete($filePath); |
||
190 | } |
||
191 | } |
||
192 | 1 | } |
|
193 | 1 | } |
|
194 | |||
195 | public function relations($model, $attribute) |
||
205 | |||
206 | 1 | public function files($model, $attribute) |
|
214 | |||
215 | 1 | public function file($model, $attribute) |
|
220 | } |
||
221 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: