1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibraryPro\Http\Livewire; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\MessageBag; |
7
|
|
|
use Illuminate\Support\ViewErrorBag; |
8
|
|
|
use Livewire\Component; |
9
|
|
|
use Spatie\MediaLibraryPro\Dto\ViewMediaItem; |
10
|
|
|
|
11
|
|
|
class LivewireMediaLibraryComponent extends Component |
12
|
|
|
{ |
13
|
|
|
public $name; |
14
|
|
|
public $multiple; |
15
|
|
|
public $sortable; |
16
|
|
|
public $editableName; |
17
|
|
|
public $rules; |
18
|
|
|
public $maxItems = null; |
19
|
|
|
public $media; |
20
|
|
|
public $view; |
21
|
|
|
public $listView; |
22
|
|
|
public $itemView; |
23
|
|
|
public $propertiesView; |
24
|
|
|
public $fieldsView; |
25
|
|
|
public $listErrorMessage = null; |
26
|
|
|
protected $validationErrors = null; |
27
|
|
|
|
28
|
|
|
public function mount( |
29
|
|
|
string $name, |
30
|
|
|
bool $multiple, |
31
|
|
|
bool $sortable = true, |
32
|
|
|
bool $editableName = true, |
33
|
|
|
string $rules = '', |
34
|
|
|
?int $maxItems = null, |
35
|
|
|
array $media = [], |
36
|
|
|
string $view = null, |
37
|
|
|
string $listView = null, |
38
|
|
|
string $itemView = null, |
39
|
|
|
string $propertiesView = null, |
40
|
|
|
string $fieldsView = null |
41
|
|
|
) { |
42
|
|
|
$this->name = $name; |
43
|
|
|
$this->multiple = $multiple; |
44
|
|
|
$this->sortable = $sortable; |
45
|
|
|
$this->editableName = $editableName; |
46
|
|
|
|
47
|
|
|
$this->rules = $rules; |
48
|
|
|
$this->maxItems = $maxItems; |
49
|
|
|
|
50
|
|
|
$this->media = $media; |
51
|
|
|
|
52
|
|
|
$this->view = empty($view) ? 'media-library::livewire.media-library' : $view; |
53
|
|
|
$this->listView = $listView; |
54
|
|
|
$this->itemView = $itemView; |
55
|
|
|
$this->propertiesView = $propertiesView; |
56
|
|
|
$this->fieldsView = $fieldsView; |
57
|
|
|
|
58
|
|
|
$this->listErrorMessage = $this->determineListErrorMessage(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function getListeners() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
"{$this->name}:fileAdded" => 'onFileAdded', |
65
|
|
|
"{$this->name}:uploadError" => 'onUploadError', |
66
|
|
|
"{$this->name}:showListErrorMessage" => 'onShowListErrorMessage', |
67
|
|
|
"{$this->name}:clearMedia" => 'onClearMedia', |
68
|
|
|
"{$this->name}:mediaComponentValidationErrors" => 'onMediaComponentValidationErrors', |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function onFileAdded(array $newMediaItem): void |
73
|
|
|
{ |
74
|
|
|
if (! $this->allowsUpload($newMediaItem)) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (isset($this->media[$newMediaItem['oldUuid']])) { |
79
|
|
|
$existingMedia = $this->media[$newMediaItem['oldUuid']]; |
80
|
|
|
$newMediaItem['order'] = $existingMedia['order']; |
81
|
|
|
|
82
|
|
|
unset($this->media[$newMediaItem['oldUuid']]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$this->media[$newMediaItem['uuid']] = $newMediaItem; |
86
|
|
|
|
87
|
|
|
$this->media = collect($this->media)->sortBy('order')->toArray(); |
88
|
|
|
|
89
|
|
|
$this->emit("{$this->name}:mediaChanged", $this->name, $this->media); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function remove(string $uuid): void |
93
|
|
|
{ |
94
|
|
|
$this->media = collect($this->media) |
95
|
|
|
->reject(fn (array $mediaItem) => $mediaItem['uuid'] === $uuid) |
96
|
|
|
->toArray(); |
97
|
|
|
|
98
|
|
|
$this->emit("{$this->name}:mediaChanged", $this->name, $this->media); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function allowsUpload(array $mediaItem): bool |
102
|
|
|
{ |
103
|
|
|
if ($this->isReplacing($mediaItem)) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->allowsUploads(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function allowsUploads(): bool |
111
|
|
|
{ |
112
|
|
|
if (is_null($this->maxItems)) { |
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return (is_countable($this->media) ? count($this->media) : 0) < $this->maxItems; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function isReplacing(array $newMediaItem): bool |
120
|
|
|
{ |
121
|
|
|
if (! $newMediaItem['oldUuid']) { |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return collect($this->media) |
126
|
|
|
->contains(fn (array $existingMediaItem): bool => $existingMediaItem['uuid'] === $newMediaItem['oldUuid']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function hideError(string $uuid) |
130
|
|
|
{ |
131
|
|
|
if (! isset($this->media[$uuid])) { |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->media[$uuid]['hideError'] = true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function determineListErrorMessage(MessageBag $viewErrorBag = null): ?string |
139
|
|
|
{ |
140
|
|
|
if ($viewErrorBag) { |
141
|
|
|
return $viewErrorBag->first($this->name); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$errors = session()->get('errors'); |
145
|
|
|
|
146
|
|
|
if (! $errors instanceof ViewErrorBag) { |
147
|
|
|
return null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $errors->first($this->name); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function clearListErrorMessage() |
154
|
|
|
{ |
155
|
|
|
$this->listErrorMessage = null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function onUploadError(string $uuid, string $uploadError) |
159
|
|
|
{ |
160
|
|
|
if (! isset($this->media[$uuid])) { |
161
|
|
|
return; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->media[$uuid]['uploadError'] = $uploadError; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function onShowListErrorMessage(string $message) |
168
|
|
|
{ |
169
|
|
|
$this->listErrorMessage = $message; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function onMediaComponentValidationErrors(string $componentName, array $validationErrors) |
173
|
|
|
{ |
174
|
|
|
if ($componentName !== $this->name) { |
175
|
|
|
return; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$messageBag = new MessageBag($validationErrors); |
179
|
|
|
|
180
|
|
|
if ($messageBag->has($this->name)) { |
181
|
|
|
$this->onShowListErrorMessage($messageBag->first($this->name)); |
182
|
|
|
} else { |
183
|
|
|
$this->listErrorMessage = null; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$this->validationErrors = $messageBag; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function onClearMedia(string $componentName): void |
190
|
|
|
{ |
191
|
|
|
if ($componentName !== $this->name) { |
192
|
|
|
return; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$this->media = []; |
196
|
|
|
$this->listErrorMessage = ''; |
197
|
|
|
|
198
|
|
|
$this->emit("{$this->name}:mediaChanged", $this->name, $this->media); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function setMediaProperty(string $uuid, string $attributeName, $value) |
202
|
|
|
{ |
203
|
|
|
$this->media[$uuid][$attributeName] = $value; |
204
|
|
|
|
205
|
|
|
$this->emit("{$this->name}:mediaChanged", $this->name, $this->media); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function setCustomProperty(string $uuid, string $customPropertyName, $value) |
209
|
|
|
{ |
210
|
|
|
Arr::set($this->media, "{$uuid}.custom_properties.{$customPropertyName}", $value); |
211
|
|
|
|
212
|
|
|
$this->emit("{$this->name}:mediaChanged", $this->name, $this->media); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function setNewOrder(array $newOrder) |
216
|
|
|
{ |
217
|
|
|
foreach ($newOrder as $newOrderItem) { |
218
|
|
|
Arr::set($this->media, "{$newOrderItem['uuid']}.order", $newOrderItem['order']); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$this->media = collect($this->media) |
222
|
|
|
->sortBy('order') |
223
|
|
|
->toArray(); |
224
|
|
|
|
225
|
|
|
$this->emit("{$this->name}:mediaChanged", $this->name, $this->media); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function render() |
229
|
|
|
{ |
230
|
|
|
return view($this->view, [ |
231
|
|
|
'errors' => $this->validationErrors, |
232
|
|
|
'sortedMedia' => collect($this->media) |
233
|
|
|
->map(fn (array $mediaItem) => new ViewMediaItem($this->name, $mediaItem)) |
234
|
|
|
->sortBy('order') |
235
|
|
|
->values(), |
236
|
|
|
]); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|