Skip to content

Commit 04aa35b

Browse files
committed
wip media collection
1 parent eed49ec commit 04aa35b

File tree

4 files changed

+123
-31
lines changed

4 files changed

+123
-31
lines changed

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

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
namespace Moox\Media\Http\Livewire;
44

55
use Exception;
6-
use Filament\Forms\Components\FileUpload;
7-
use Filament\Forms\Components\Select;
8-
use Filament\Forms\Concerns\InteractsWithForms;
9-
use Filament\Forms\Contracts\HasForms;
10-
use Filament\Notifications\Notification;
11-
use Filament\Schemas\Schema;
12-
use Illuminate\Database\Eloquent\Model;
136
use Livewire\Component;
14-
use Livewire\WithFileUploads;
7+
use Filament\Schemas\Schema;
158
use Livewire\WithPagination;
169
use Moox\Media\Models\Media;
10+
use Livewire\WithFileUploads;
11+
use Filament\Forms\Components\Select;
12+
use Filament\Forms\Contracts\HasForms;
1713
use Moox\Media\Models\MediaCollection;
14+
use Illuminate\Database\Eloquent\Model;
15+
use Filament\Notifications\Notification;
16+
use Filament\Forms\Components\FileUpload;
17+
use Moox\Localization\Models\Localization;
18+
use Filament\Forms\Concerns\InteractsWithForms;
1819
use Spatie\MediaLibrary\MediaCollections\FileAdderFactory;
1920

2021
/** @property \Filament\Schemas\Schema $form */
@@ -111,11 +112,36 @@ public function form(Schema $schema): Schema
111112
{
112113
$collection = Select::make('media_collection_id')
113114
->label(__('media::fields.collection'))
114-
->options(fn () => MediaCollection::whereHas('translations', function ($query) {
115-
$query->where('locale', app()->getLocale());
116-
})->get()->pluck('name', 'id')->filter()->toArray())
115+
->options(function () {
116+
$currentLang = app()->getLocale();
117+
118+
$defaultLocale = null;
119+
if (class_exists(Localization::class)) {
120+
$localization = Localization::where('is_default', true)
121+
->where('is_active_admin', true)
122+
->with('language')
123+
->first();
124+
125+
if ($localization && $localization->language) {
126+
$defaultLocale = $localization->locale_variant ?: $localization->language->alpha2;
127+
}
128+
}
129+
130+
return MediaCollection::with('translations')
131+
->get()
132+
->mapWithKeys(function ($item) use ($currentLang, $defaultLocale) {
133+
$name =
134+
$item->translate($currentLang)?->name
135+
?? ($defaultLocale ? $item->translate($defaultLocale)?->name : null)
136+
?? $item->translations->first()?->name
137+
?? ('ID: ' . $item->id);
138+
139+
return [$item->id => $name];
140+
})
141+
->toArray();
142+
})
117143
->searchable()
118-
->default(MediaCollection::first()->id)
144+
->default(MediaCollection::first()?->id)
119145
->required()
120146
->live();
121147

@@ -471,9 +497,32 @@ public function render()
471497
->orderBy('created_at', 'desc')
472498
->paginate(18);
473499

474-
$collectionOptions = MediaCollection::whereHas('translations', function ($query) {
475-
$query->where('locale', app()->getLocale());
476-
})->get()->pluck('name', 'id')->filter()->toArray();
500+
$currentLang = app()->getLocale();
501+
502+
$defaultLocale = null;
503+
if (class_exists(\Moox\Localization\Models\Localization::class)) {
504+
$localization = \Moox\Localization\Models\Localization::where('is_default', true)
505+
->where('is_active_admin', true)
506+
->with('language')
507+
->first();
508+
509+
if ($localization && $localization->language) {
510+
$defaultLocale = $localization->locale_variant ?: $localization->language->alpha2;
511+
}
512+
}
513+
514+
$collectionOptions = MediaCollection::with('translations')
515+
->get()
516+
->mapWithKeys(function ($item) use ($currentLang, $defaultLocale) {
517+
$name =
518+
$item->translate($currentLang)?->name
519+
?? ($defaultLocale ? $item->translate($defaultLocale)?->name : null)
520+
?? $item->translations->first()?->name
521+
?? ('ID: ' . $item->id);
522+
523+
return [$item->id => $name];
524+
})
525+
->toArray();
477526

478527
$uploaderOptions = [];
479528
$uploaderTypes = Media::query()

packages/media/src/MediaServiceProvider.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44

55
namespace Moox\Media;
66

7-
use Filament\Support\Facades\FilamentView;
8-
use Filament\Tables\View\TablesRenderHook;
9-
use Illuminate\Support\Facades\Blade;
10-
use Illuminate\Support\Facades\Gate;
117
use Livewire\Livewire;
12-
use Moox\Media\Console\Commands\InstallCommand;
13-
use Moox\Media\Http\Livewire\MediaPickerModal;
148
use Moox\Media\Models\Media;
9+
use Illuminate\Support\Facades\Gate;
1510
use Moox\Media\Policies\MediaPolicy;
16-
use Moox\Media\Resources\MediaCollectionResource\Pages\ListMediaCollections;
17-
use Moox\Media\Resources\MediaResource\Pages\ListMedia;
11+
use Illuminate\Support\Facades\Blade;
12+
use Illuminate\Support\Facades\Schema;
13+
use Moox\Media\Models\MediaCollection;
1814
use Spatie\LaravelPackageTools\Package;
15+
use Filament\Support\Facades\FilamentView;
16+
use Filament\Tables\View\TablesRenderHook;
17+
use Moox\Media\Http\Livewire\MediaPickerModal;
18+
use Moox\Media\Console\Commands\InstallCommand;
1919
use Spatie\LaravelPackageTools\PackageServiceProvider;
20+
use Moox\Media\Resources\MediaResource\Pages\ListMedia;
21+
use Moox\Media\Resources\MediaCollectionResource\Pages\ListMediaCollections;
2022

2123
class MediaServiceProvider extends PackageServiceProvider
2224
{
@@ -52,5 +54,19 @@ public function boot()
5254
fn (): string => Blade::render('@include("localization::lang-selector")'),
5355
scopes: ListMediaCollections::class
5456
);
57+
58+
$this->app->booted(function () {
59+
if (app()->runningInConsole()) {
60+
return;
61+
}
62+
63+
try {
64+
if (Schema::hasTable('media_collections')) {
65+
MediaCollection::ensureUncategorizedExists();
66+
}
67+
} catch (\Exception $e) {
68+
// Silently ignore - table might not exist yet
69+
}
70+
});
5571
}
5672
}

packages/media/src/Models/MediaCollection.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Moox\Media\Models;
44

5-
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
6-
use Astrotomic\Translatable\Translatable;
75
use Illuminate\Database\Eloquent\Model;
6+
use Astrotomic\Translatable\Translatable;
7+
use Moox\Localization\Models\Localization;
88
use Illuminate\Database\Eloquent\Relations\HasMany;
9+
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
910

1011
class MediaCollection extends Model implements TranslatableContract
1112
{
@@ -50,9 +51,37 @@ public static function ensureUncategorizedExists()
5051
return;
5152
}
5253

53-
static::create([
54-
'name' => __('media::fields.uncategorized'),
55-
'description' => __('media::fields.uncategorized_description'),
56-
]);
54+
$defaultLocale = null;
55+
if (class_exists(Localization::class)) {
56+
$localization = Localization::where('is_default', true)
57+
->where('is_active_admin', true)
58+
->with('language')
59+
->first();
60+
61+
if ($localization && $localization->language) {
62+
$defaultLocale = $localization->locale_variant ?: $localization->language->alpha2;
63+
}
64+
}
65+
66+
$locale = $defaultLocale ?: config('app.locale');
67+
68+
$collection = new static();
69+
$translation = $collection->translateOrNew($locale);
70+
71+
$previousLocale = app()->getLocale();
72+
app()->setLocale($locale);
73+
74+
$translation->name = __('media::fields.uncategorized');
75+
$translation->description = __('media::fields.uncategorized_description');
76+
77+
if ($translation->name === 'media::fields.uncategorized') {
78+
app()->setLocale('en');
79+
$translation->name = __('media::fields.uncategorized');
80+
$translation->description = __('media::fields.uncategorized_description');
81+
}
82+
83+
app()->setLocale($previousLocale);
84+
85+
$collection->save();
5786
}
5887
}

packages/media/src/Resources/MediaCollectionResource/Pages/ListMediaCollections.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public function mount(): void
2727
$this->lang = request()->get('lang', $this->getDefaultLocale());
2828

2929
app()->setLocale($this->lang);
30-
31-
MediaCollection::ensureUncategorizedExists();
3230
}
3331

3432
protected function getDefaultLocale(): string

0 commit comments

Comments
 (0)