1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Uploaders\Support\Traits; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD; |
6
|
|
|
use Backpack\CRUD\app\Library\Uploaders\Support\Interfaces\UploaderInterface; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Support\Facades\Storage; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
|
12
|
|
|
trait HandleRepeatableUploads |
13
|
|
|
{ |
14
|
|
|
public bool $handleRepeatableFiles = false; |
15
|
|
|
|
16
|
|
|
public ?string $repeatableContainerName = null; |
17
|
|
|
|
18
|
|
|
/******************************* |
19
|
|
|
* Setters - fluently configure the uploader |
20
|
|
|
*******************************/ |
21
|
|
|
public function repeats(string $repeatableContainerName): self |
22
|
|
|
{ |
23
|
|
|
$this->handleRepeatableFiles = true; |
24
|
|
|
|
25
|
|
|
$this->repeatableContainerName = $repeatableContainerName; |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/******************************* |
31
|
|
|
* Getters |
32
|
|
|
*******************************/ |
33
|
|
|
public function getRepeatableContainerName(): ?string |
34
|
|
|
{ |
35
|
|
|
return $this->repeatableContainerName; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/******************************* |
39
|
|
|
* Default implementation methods |
40
|
|
|
*******************************/ |
41
|
|
|
protected function uploadRepeatableFiles($values, $previousValues, $entry = null) |
42
|
|
|
{ |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function handleRepeatableFiles(Model $entry): Model |
46
|
|
|
{ |
47
|
|
|
$values = collect(CRUD::getRequest()->get($this->getRepeatableContainerName())); |
|
|
|
|
48
|
|
|
$files = collect(CRUD::getRequest()->file($this->getRepeatableContainerName())); |
49
|
|
|
$value = $this->mergeValuesRecursive($values, $files); |
50
|
|
|
|
51
|
|
|
if ($this->isRelationship) { |
52
|
|
|
return $this->uploadRelationshipFiles($entry, $value); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$entry->{$this->getRepeatableContainerName()} = json_encode($this->processRepeatableUploads($entry, $value)); |
|
|
|
|
56
|
|
|
|
57
|
|
|
return $entry; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function uploadRelationshipFiles(Model $entry, mixed $value): Model |
61
|
|
|
{ |
62
|
|
|
$modelCount = CRUD::get('uploaded_'.$this->getRepeatableContainerName().'_count'); |
|
|
|
|
63
|
|
|
$value = $value->slice($modelCount, 1)->toArray(); |
64
|
|
|
|
65
|
|
|
foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
66
|
|
|
if (array_key_exists($modelCount, $value) && isset($value[$modelCount][$uploader->getName()])) { |
67
|
|
|
$entry->{$uploader->getName()} = $uploader->uploadFiles($entry, $value[$modelCount][$uploader->getName()]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $entry; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function processRepeatableUploads(Model $entry, Collection $values): Collection |
75
|
|
|
{ |
76
|
|
|
foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $uploader) { |
77
|
|
|
$uploadedValues = $uploader->uploadRepeatableFiles($values->pluck($uploader->getName())->toArray(), $this->getPreviousRepeatableValues($entry, $uploader)); |
78
|
|
|
|
79
|
|
|
$values = $values->map(function ($item, $key) use ($uploadedValues, $uploader) { |
80
|
|
|
$item[$uploader->getName()] = $uploadedValues[$key] ?? null; |
81
|
|
|
|
82
|
|
|
return $item; |
83
|
|
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $values; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function retrieveRepeatableFiles(Model $entry): Model |
90
|
|
|
{ |
91
|
|
|
if ($this->isRelationship) { |
92
|
|
|
return $this->retrieveRepeatableRelationFiles($entry); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$repeatableUploaders = app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()); |
96
|
|
|
|
97
|
|
|
$values = $entry->{$this->getRepeatableContainerName()}; |
98
|
|
|
$values = is_string($values) ? json_decode($values, true) : $values; |
99
|
|
|
$values = array_map(function ($item) use ($repeatableUploaders) { |
100
|
|
|
foreach ($repeatableUploaders as $upload) { |
101
|
|
|
$item[$upload->getName()] = $this->getValuesWithPathStripped($item, $upload); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $item; |
105
|
|
|
}, $values); |
106
|
|
|
|
107
|
|
|
$entry->{$this->getRepeatableContainerName()} = $values; |
108
|
|
|
|
109
|
|
|
return $entry; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function retrieveRepeatableRelationFiles(Model $entry) |
113
|
|
|
{ |
114
|
|
|
switch($this->getRepeatableRelationType()) { |
115
|
|
|
case 'BelongsToMany': |
116
|
|
|
$pivotClass = app('crud')->getModel()->{$this->getUploaderSubfield()['baseEntity']}()->getPivotClass(); |
117
|
|
|
$pivotFieldName = 'pivot_'.$this->getName(); |
|
|
|
|
118
|
|
|
$connectedEntry = new $pivotClass([$this->getName() => $entry->$pivotFieldName]); |
119
|
|
|
$entry->{$pivotFieldName} = $this->retrieveFiles($connectedEntry)->{$this->getName()}; |
|
|
|
|
120
|
|
|
|
121
|
|
|
break; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $entry; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function getRepeatableRelationType() |
128
|
|
|
{ |
129
|
|
|
return $this->getUploaderField()->getAttributes()['relation_type']; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function getUploaderField() |
133
|
|
|
{ |
134
|
|
|
return app('crud')->field($this->getRepeatableContainerName()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function getUploaderSubfield() |
138
|
|
|
{ |
139
|
|
|
return collect($this->getUploaderFieldSubfields())->where('name', '===', $this->getName())->first(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function getUploaderFieldSubfields() |
143
|
|
|
{ |
144
|
|
|
return $this->getUploaderField()->getAttributes()['subfields']; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
private function deleteRepeatableFiles(Model $entry): void |
148
|
|
|
{ |
149
|
|
|
if ($this->isRelationship) { |
150
|
|
|
switch($this->getRepeatableRelationType()) { |
151
|
|
|
case 'BelongsToMany': |
152
|
|
|
// handle belongs to many deletion |
153
|
|
|
return; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$this->deleteFiles($entry); |
|
|
|
|
157
|
|
|
|
158
|
|
|
return; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$repeatableValues = collect($entry->{$this->getName()}); |
162
|
|
|
foreach (app('UploadersRepository')->getRepeatableUploadersFor($this->getRepeatableContainerName()) as $upload) { |
163
|
|
|
if (! $upload->shouldDeleteFiles()) { |
164
|
|
|
continue; |
165
|
|
|
} |
166
|
|
|
$values = $repeatableValues->pluck($upload->getName())->toArray(); |
167
|
|
|
foreach ($values as $value) { |
168
|
|
|
if (! $value) { |
169
|
|
|
continue; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (is_array($value)) { |
173
|
|
|
foreach ($value as $subvalue) { |
174
|
|
|
Storage::disk($upload->getDisk())->delete($upload->getPath().$subvalue); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
continue; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
Storage::disk($upload->getDisk())->delete($upload->getPath().$value); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
/******************************* |
185
|
|
|
* Helper methods |
186
|
|
|
*******************************/ |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Given two multidimensional arrays/collections, merge them recursively. |
190
|
|
|
*/ |
191
|
|
|
protected function mergeValuesRecursive(array|Collection $array1, array|Collection $array2): array|Collection |
192
|
|
|
{ |
193
|
|
|
$merged = $array1; |
194
|
|
|
foreach ($array2 as $key => &$value) { |
195
|
|
|
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
196
|
|
|
$merged[$key] = $this->mergeValuesRecursive($merged[$key], $value); |
197
|
|
|
} else { |
198
|
|
|
$merged[$key] = $value; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $merged; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Repeatable items send `_order_` parameter in the request. |
207
|
|
|
* This holds the order of the items in the repeatable container. |
208
|
|
|
*/ |
209
|
|
|
protected function getFileOrderFromRequest(): array |
210
|
|
|
{ |
211
|
|
|
$items = CRUD::getRequest()->input('_order_'.$this->getRepeatableContainerName()) ?? []; |
212
|
|
|
|
213
|
|
|
array_walk($items, function (&$key, $value) { |
214
|
|
|
$requestValue = $key[$this->getName()] ?? null; |
215
|
|
|
$key = $this->handleMultipleFiles ? (is_string($requestValue) ? explode(',', $requestValue) : $requestValue) : $requestValue; |
216
|
|
|
}); |
217
|
|
|
|
218
|
|
|
return $items; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
private function getPreviousRepeatableValues(Model $entry, UploaderInterface $uploader): array |
222
|
|
|
{ |
223
|
|
|
$previousValues = json_decode($entry->getOriginal($uploader->getRepeatableContainerName()), true); |
|
|
|
|
224
|
|
|
|
225
|
|
|
if (! empty($previousValues)) { |
226
|
|
|
$previousValues = array_column($previousValues, $uploader->getName()); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $previousValues ?? []; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
private function getValuesWithPathStripped(array|string|null $item, UploaderInterface $upload) |
233
|
|
|
{ |
234
|
|
|
$uploadedValues = $item[$upload->getName()] ?? null; |
235
|
|
|
if (is_array($uploadedValues)) { |
236
|
|
|
return array_map(function ($value) use ($upload) { |
237
|
|
|
return Str::after($value, $upload->getPath()); |
238
|
|
|
}, $uploadedValues); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return isset($uploadedValues) ? Str::after($uploadedValues, $upload->getPath()) : null; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|