1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Uploads\Uploaders; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD; |
6
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Uploads\Interfaces\RepeatableUploaderInterface; |
7
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Uploads\Interfaces\UploaderInterface; |
8
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Uploads\Traits\HasCrudObjectType; |
9
|
|
|
use Backpack\CRUD\app\Library\CrudPanel\Uploads\Traits\HasName; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Support\Facades\Storage; |
12
|
|
|
|
13
|
|
|
class RepeatableUploader implements RepeatableUploaderInterface |
14
|
|
|
{ |
15
|
|
|
use HasCrudObjectType, HasName; |
16
|
|
|
|
17
|
|
|
public $repeatableUploads; |
18
|
|
|
|
19
|
|
|
public $isRelationship; |
20
|
|
|
|
21
|
|
|
public function __construct(array $crudObject) |
22
|
|
|
{ |
23
|
|
|
$this->name = $crudObject['name']; |
24
|
|
|
$this->crudObjectType = $crudObject['crudObjectType']; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function for(array $crudObject) |
28
|
|
|
{ |
29
|
|
|
if (isset($crudObject['relation_type']) && $crudObject['entity'] !== false) { |
30
|
|
|
return new RepeatableRelationship($crudObject); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return new static($crudObject); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function uploads(...$uploads): self |
37
|
|
|
{ |
38
|
|
|
foreach ($uploads as $upload) { |
39
|
|
|
if (! is_a($upload, UploaderInterface::class)) { |
40
|
|
|
throw new \Exception('Uploads must be an instance of UploaderInterface.'); |
41
|
|
|
} |
42
|
|
|
$this->repeatableUploads[] = $upload->repeats($this->name)->relationship($this->isRelationship ?? false); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->setupSubfieldsUploadSettings(); |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function save(Model $entry, $value = null) |
51
|
|
|
{ |
52
|
|
|
$values = collect(request()->get($this->name)); |
53
|
|
|
$files = collect(request()->file($this->name)); |
54
|
|
|
|
55
|
|
|
$value = $this->mergeValuesRecursive($values, $files); |
56
|
|
|
|
57
|
|
|
foreach ($this->repeatableUploads as $upload) { |
58
|
|
|
$value = $this->performSave($entry, $upload, $value); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function performSave(Model $entry, UploaderInterface $upload, $value, $row = null) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$uploadedValues = $upload->save($entry, $value->pluck($upload->getName())->toArray()); |
67
|
|
|
|
68
|
|
|
$value = $value->map(function ($item, $key) use ($upload, $uploadedValues) { |
69
|
|
|
$item[$upload->getName()] = $uploadedValues[$key] ?? null; |
70
|
|
|
|
71
|
|
|
return $item; |
72
|
|
|
}); |
73
|
|
|
|
74
|
|
|
return $value; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function setupSubfieldsUploadSettings() |
78
|
|
|
{ |
79
|
|
|
$crudField = CRUD::field($this->name); |
80
|
|
|
|
81
|
|
|
$subfields = collect($crudField->getAttributes()['subfields']); |
82
|
|
|
$subfields = $subfields->map(function ($item) { |
83
|
|
|
if (isset($item['withMedia']) || isset($item['withUploads'])) { |
84
|
|
|
/** @var UploaderInterface $uploader */ |
85
|
|
|
$uploader = array_filter($this->repeatableUploads, function ($item) { |
86
|
|
|
return $item->name !== $this->name; |
87
|
|
|
})[0]; |
88
|
|
|
$item['upload'] = true; |
89
|
|
|
$item['disk'] = $uploader->getDisk(); |
90
|
|
|
$item['prefix'] = $uploader->getPath(); |
91
|
|
|
if ($uploader->getTemporary()) { |
92
|
|
|
$item['temporary'] = $uploader->getTemporary(); |
93
|
|
|
$item['expiration'] = $uploader->getExpiration(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $item; |
98
|
|
|
})->toArray(); |
99
|
|
|
|
100
|
|
|
$crudField->subfields($subfields); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function processFileUpload(Model $entry) |
104
|
|
|
{ |
105
|
|
|
if (! $this->isRelationship) { |
106
|
|
|
$entry->{$this->name} = json_encode($this->save($entry)); |
107
|
|
|
} else { |
108
|
|
|
$entry = $this->save($entry); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $entry; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function retrieveUploadedFile(Model $entry) |
115
|
|
|
{ |
116
|
|
|
foreach ($this->repeatableUploads as $upload) { |
117
|
|
|
$entry = $this->retrieveFiles($entry, $upload); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $entry; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* The function called in the deleting event to delete the uploaded files upon entry deletion |
125
|
|
|
* |
126
|
|
|
* @param Model $entry |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function deleteUploadedFile(Model $entry) |
130
|
|
|
{ |
131
|
|
|
$repeatableValues = collect($entry->{$this->getName()}); |
132
|
|
|
foreach ($this->repeatableUploads as $upload) { |
133
|
|
|
if (! $upload->deleteWhenEntryIsDeleted) { |
134
|
|
|
continue; |
135
|
|
|
} |
136
|
|
|
$values = $repeatableValues->pluck($upload->getName())->toArray(); |
137
|
|
|
foreach ($values as $value) { |
138
|
|
|
if (! $value) { |
139
|
|
|
continue; |
140
|
|
|
} |
141
|
|
|
if (is_array($value)) { |
142
|
|
|
foreach ($value as $subvalue) { |
143
|
|
|
Storage::disk($upload->disk)->delete($upload->path.$subvalue); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
continue; |
147
|
|
|
} |
148
|
|
|
Storage::disk($upload->disk)->delete($upload->path.$value); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function retrieveFiles($entry, $upload) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
return $entry; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function mergeValuesRecursive($array1, $array2) |
159
|
|
|
{ |
160
|
|
|
$merged = $array1; |
161
|
|
|
foreach ($array2 as $key => &$value) { |
162
|
|
|
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) { |
163
|
|
|
$merged[$key] = $this->mergeValuesRecursive($merged[$key], $value); |
164
|
|
|
} else { |
165
|
|
|
$merged[$key] = $value; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return $merged; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.