diff --git a/config/media-picker.php b/config/media-picker.php index badb72f..2ca9fc2 100644 --- a/config/media-picker.php +++ b/config/media-picker.php @@ -16,6 +16,7 @@ 'is_tenant_aware' => true, 'tenant_ownership_relationship_name' => 'tenant', + // 'tenant_model' => \App\Models\Tenant::class, 'model' => \Vormkracht10\MediaPicker\Models\Media::class, diff --git a/src/MediaPickerServiceProvider.php b/src/MediaPickerServiceProvider.php index 287565a..949a9c1 100644 --- a/src/MediaPickerServiceProvider.php +++ b/src/MediaPickerServiceProvider.php @@ -2,19 +2,22 @@ namespace Vormkracht10\MediaPicker; -use Filament\Support\Assets\AlpineComponent; -use Filament\Support\Assets\Asset; -use Filament\Support\Assets\Css; use Filament\Support\Assets\Js; -use Filament\Support\Facades\FilamentAsset; -use Filament\Support\Facades\FilamentIcon; +use Filament\Support\Assets\Css; +use Filament\Support\Assets\Asset; use Illuminate\Filesystem\Filesystem; -use Livewire\Features\SupportTesting\Testable; -use Spatie\LaravelPackageTools\Commands\InstallCommand; +use Illuminate\Support\Facades\Config; use Spatie\LaravelPackageTools\Package; +use Filament\Support\Facades\FilamentIcon; +use Vormkracht10\MediaPicker\Models\Media; +use Filament\Support\Facades\FilamentAsset; +use Filament\Support\Assets\AlpineComponent; +use Livewire\Features\SupportTesting\Testable; +use Vormkracht10\MediaPicker\MediaPickerPlugin; use Spatie\LaravelPackageTools\PackageServiceProvider; -use Vormkracht10\MediaPicker\Commands\MediaPickerCommand; use Vormkracht10\MediaPicker\Testing\TestsMediaPicker; +use Spatie\LaravelPackageTools\Commands\InstallCommand; +use Vormkracht10\MediaPicker\Commands\MediaPickerCommand; class MediaPickerServiceProvider extends PackageServiceProvider { @@ -55,6 +58,15 @@ public function configurePackage(Package $package): void public function packageRegistered(): void {} + public function boot(): void + { + // Allow setting tenant relationship and model + MediaPickerPlugin::configureTenantUsing(function ($tenantRelationship, $tenantModel) { + Config::set('media-picker.tenant_relationship', $tenantRelationship); + Config::set('media-picker.tenant_model', $tenantModel); + }); + } + public function packageBooted(): void { // Asset Registration diff --git a/src/Models/Media.php b/src/Models/Media.php index 7836555..42fec6d 100644 --- a/src/Models/Media.php +++ b/src/Models/Media.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Concerns\HasUlids; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Support\Facades\Config; class Media extends Model { @@ -21,4 +22,41 @@ class Media extends Model 'size' => 'integer', 'public' => 'boolean', ]; + + public function getRouteKeyName() + { + return 'ulid'; + } + + protected static function booted() + { + static::creating(function ($model) { + $tenantRelationship = Config::get('media-picker.tenant_relationship'); + $tenantModel = Config::get('media-picker.tenant_model'); + + if ($tenantRelationship && class_exists($tenantModel)) { + $currentTenant = app($tenantModel)->current(); + + if ($currentTenant) { + $model->{$tenantRelationship . '_id'} = $currentTenant->id; + } + } + }); + } + + // Dynamic tenant relationship method + public function tenant() + { + $tenantRelationship = Config::get('media-picker.tenant_relationship'); + $tenantModel = Config::get('media-picker.tenant_model'); + + if ($tenantRelationship && class_exists($tenantModel)) { + return $this->belongsTo( + $tenantModel, + $tenantRelationship . '_id' + ); + } + + return null; + } }