Skip to content

Commit 3f17f96

Browse files
committed
w-i-p
1 parent c450919 commit 3f17f96

File tree

3 files changed

+64
-38
lines changed

3 files changed

+64
-38
lines changed

packages/media/resources/views/forms/components/media-picker.blade.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
@if ($this instanceof \Filament\Resources\Pages\EditRecord || $this instanceof \Filament\Resources\Pages\CreateRecord)
4242
<x-filament::button color="primary" size="sm" class="w-full flex items-center justify-center space-x-2"
4343
x-on:click="
44-
$dispatch('set-media-picker-model', {
45-
modelId: {{ $getRecord()?->id ?? 0 }},
46-
modelClass: '{{ $getRecord() ? addslashes($getRecord()::class) : addslashes($this->getResource()::getModel()) }}'
47-
});
48-
$dispatch('open-modal', { id: 'mediaPickerModal' });
49-
">
44+
$dispatch('set-media-picker-model', {
45+
modelId: {{ $getRecord()?->id ?? 0 }},
46+
modelClass: '{{ $getRecord() ? addslashes($getRecord()::class) : addslashes($this->getResource()::getModel()) }}'
47+
});
48+
$dispatch('open-modal', { id: 'mediaPickerModal' });
49+
">
5050
<span>Bild auswählen</span>
5151
</x-filament::button>
5252
@endif
@@ -71,7 +71,6 @@ class="relative group bg-white rounded-lg shadow hover:shadow-md transition-shad
7171
x-on:click="selectedMedia.splice(index, 1); initializeState();">
7272
</x-filament::button>
7373
</div>
74-
7574
@endif
7675
</div>
7776
</template>
@@ -84,6 +83,8 @@ class="border border-dashed border-gray-300 rounded-lg p-4 bg-gray-50 flex items
8483
</div>
8584

8685
<livewire:media-picker-modal id="media-picker-modal" :multiple="$field->isMultiple()"
87-
:upload-config="$field->getUploadConfig()" />
86+
:upload-config="$field->getUploadConfig()"
87+
:model-class="$this->getRecord() ? get_class($this->getRecord()) : $this->getResource()::getModel()"
88+
:model-id="$this->getRecord()?->id" />
8889
</div>
8990
</x-dynamic-component>

packages/media/resources/views/livewire/media-picker-modal.blade.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
</x-slot>
55

66
<div class="min-w-[1000px]">
7-
@if($modelId)
8-
<div class="mb-4">
9-
{{ $this->form }}
10-
</div>
11-
@endif
7+
<div class="mb-4">
8+
{{ $this->form }}
9+
</div>
1210

1311
<div class="mt-4">
1412
</div>
@@ -84,8 +82,8 @@ class="block w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 foc
8482

8583
<div wire:click="toggleMediaSelection({{ $item['id'] }})"
8684
class="relative rounded-lg shadow-md overflow-hidden bg-gray-100 hover:shadow-lg transition cursor-pointer
87-
{{ in_array($item['id'], $selectedMediaIds) ? 'ring-2 ring-blue-600' : 'border border-gray-200' }}
88-
{{ $selectedMediaMeta['id'] == $item['id'] ? 'ring-4 ring-blue-700 border-2 border-blue-700' : '' }}">
85+
{{ in_array($item['id'], $selectedMediaIds) ? 'ring-2 ring-blue-600' : 'border border-gray-200' }}
86+
{{ $selectedMediaMeta['id'] == $item['id'] ? 'ring-4 ring-blue-700 border-2 border-blue-700' : '' }}">
8987
@if ($fileData)
9088
<div class="flex flex-col justify-between items-center w-full h-32 bg-gray-200">
9189
<x-filament::icon icon="{{ $fileData['icon'] }}" class="w-16 h-16 text-gray-600" />

packages/media/src/Http/Livewire/MediaPickerModal.php

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
namespace Moox\Media\Http\Livewire;
44

5-
use Filament\Forms\Components\FileUpload;
6-
use Filament\Forms\Concerns\InteractsWithForms;
7-
use Filament\Forms\Contracts\HasForms;
8-
use Filament\Forms\Form;
95
use Livewire\Component;
10-
use Livewire\WithFileUploads;
6+
use Filament\Forms\Form;
117
use Livewire\WithPagination;
128
use Moox\Media\Models\Media;
9+
use Livewire\WithFileUploads;
10+
use Filament\Forms\Contracts\HasForms;
11+
use Illuminate\Database\Eloquent\Model;
12+
use Filament\Forms\Components\FileUpload;
13+
use Filament\Forms\Concerns\InteractsWithForms;
1314
use Spatie\MediaLibrary\MediaCollections\FileAdderFactory;
1415

1516
/** @property \Filament\Forms\Form $form */
@@ -23,6 +24,8 @@ class MediaPickerModal extends Component implements HasForms
2324

2425
public ?string $modelClass = null;
2526

27+
public ?Model $model = null;
28+
2629
public $media;
2730

2831
public array $selectedMediaIds = [];
@@ -52,28 +55,50 @@ class MediaPickerModal extends Component implements HasForms
5255

5356
public string $dateFilter = '';
5457

58+
5559
protected $listeners = [
5660
'set-media-picker-model' => 'setModel',
5761
'mediaUploaded' => 'refreshMedia',
5862
];
5963

60-
public function mount(): void
64+
public function mount(?int $modelId = null, ?string $modelClass = null): void
6165
{
6266
$this->files = [];
6367
$this->form->fill();
68+
69+
$this->modelClass = $modelClass;
70+
$this->modelId = $modelId;
71+
72+
if (!$this->modelClass) {
73+
$this->modelClass = Media::class;
74+
}
75+
76+
$this->modelClass = str_replace('\\\\', '\\', $this->modelClass);
77+
78+
if (!class_exists($this->modelClass)) {
79+
throw new \Exception("Die Klasse {$this->modelClass} existiert nicht.");
80+
}
81+
82+
if ($this->modelId) {
83+
$this->model = app($this->modelClass)::find($this->modelId);
84+
}
85+
86+
if (!$this->modelId || !$this->model) {
87+
$this->modelId = 0;
88+
}
6489
}
6590

6691
public function form(Form $form): Form
6792
{
6893
$upload = FileUpload::make('files')
6994
->afterStateUpdated(function ($state) {
70-
if (! $state) {
95+
if (!$state) {
7196
return;
7297
}
7398

7499
$processedFiles = session('processed_files', []);
75100

76-
if (! is_array($state)) {
101+
if (!is_array($state)) {
77102
$model = new Media;
78103
$model->exists = true;
79104

@@ -82,14 +107,15 @@ public function form(Form $form): Form
82107

83108
$title = pathinfo($state->getClientOriginalName(), PATHINFO_FILENAME);
84109

110+
$user = auth()->user();
85111
$media->title = $title;
86112
$media->alt = $title;
87-
$media->original_model_type = Media::class;
88-
$media->original_model_id = $media->id;
113+
$media->original_model_type = $this->modelClass;
114+
$media->original_model_id = $this->modelId ?: null;
89115
$media->model_id = $media->id;
90116
$media->model_type = Media::class;
91-
$media->uploader_type = get_class(auth()->user());
92-
$media->uploader_id = auth()->id();
117+
$media->uploader_type = $user ? get_class($user) : null;
118+
$media->uploader_id = $user?->id;
93119

94120
if (str_starts_with($media->mime_type, 'image/')) {
95121
[$width, $height] = getimagesize($media->getPath());
@@ -114,14 +140,15 @@ public function form(Form $form): Form
114140

115141
$title = pathinfo($tempFile->getClientOriginalName(), PATHINFO_FILENAME);
116142

143+
$user = auth()->user();
117144
$media->title = $title;
118145
$media->alt = $title;
119-
$media->original_model_type = Media::class;
120-
$media->original_model_id = $media->id;
146+
$media->original_model_type = $this->modelClass;
147+
$media->original_model_id = $this->modelId ?: null;
121148
$media->model_id = $media->id;
122149
$media->model_type = Media::class;
123-
$media->uploader_type = get_class(auth()->user());
124-
$media->uploader_id = auth()->id();
150+
$media->uploader_type = $user ? get_class($user) : null;
151+
$media->uploader_id = $user?->id;
125152

126153
if (str_starts_with($media->mime_type, 'image/')) {
127154
[$width, $height] = getimagesize($media->getPath());
@@ -215,7 +242,7 @@ public function toggleMediaSelection(int $mediaId)
215242
$this->selectedMediaIds[] = $mediaId;
216243
}
217244
} else {
218-
if (! empty($this->selectedMediaIds) && $this->selectedMediaIds[0] === $mediaId) {
245+
if (!empty($this->selectedMediaIds) && $this->selectedMediaIds[0] === $mediaId) {
219246
$this->selectedMediaIds = [];
220247
} else {
221248
$this->selectedMediaIds = [$mediaId];
@@ -254,15 +281,15 @@ public function applySelection()
254281
$selectedMedia = Media::whereIn('id', $this->selectedMediaIds)->get();
255282

256283
if ($selectedMedia->isNotEmpty()) {
257-
if (! $this->multiple) {
284+
if (!$this->multiple) {
258285
$media = $selectedMedia->first();
259286
$this->dispatch('mediaSelected', [
260287
'id' => $media->id,
261288
'url' => $media->getUrl(),
262289
'file_name' => $media->file_name,
263290
]);
264291
} else {
265-
$selectedMediaData = $selectedMedia->map(fn ($media) => [
292+
$selectedMediaData = $selectedMedia->map(fn($media) => [
266293
'id' => $media->id,
267294
'url' => $media->getUrl(),
268295
'file_name' => $media->file_name,
@@ -313,10 +340,10 @@ public function render()
313340
$media = Media::query()
314341
->when($this->searchQuery, function ($query) {
315342
$query->where(function ($subQuery) {
316-
$subQuery->where('file_name', 'like', '%'.$this->searchQuery.'%')
317-
->orWhere('title', 'like', '%'.$this->searchQuery.'%')
318-
->orWhere('description', 'like', '%'.$this->searchQuery.'%')
319-
->orWhere('alt', 'like', '%'.$this->searchQuery.'%');
343+
$subQuery->where('file_name', 'like', '%' . $this->searchQuery . '%')
344+
->orWhere('title', 'like', '%' . $this->searchQuery . '%')
345+
->orWhere('description', 'like', '%' . $this->searchQuery . '%')
346+
->orWhere('alt', 'like', '%' . $this->searchQuery . '%');
320347
});
321348
})
322349
->when($this->fileTypeFilter, function ($query) {

0 commit comments

Comments
 (0)