33namespace Moox \Media \Http \Livewire ;
44
55use Filament \Forms \Components \FileUpload ;
6+ use Filament \Forms \Components \Select ;
67use Filament \Forms \Concerns \InteractsWithForms ;
78use Filament \Forms \Contracts \HasForms ;
89use Filament \Forms \Form ;
@@ -58,6 +59,10 @@ class MediaPickerModal extends Component implements HasForms
5859
5960 public string $ uploaderFilter = '' ;
6061
62+ public ?string $ collection_name = 'default ' ;
63+
64+ public ?string $ collectionFilter = '' ;
65+
6166 protected $ listeners = [
6267 'set-media-picker-model ' => 'setModel ' ,
6368 'mediaUploaded ' => 'refreshMedia ' ,
@@ -73,31 +78,45 @@ public function mount(?int $modelId = null, ?string $modelClass = null): void
7378
7479 $ this ->modelClass = $ this ->modelClass ? str_replace ('\\\\' , '\\' , $ this ->modelClass ) : null ;
7580
76- if ($ this ->modelClass && ! class_exists ($ this ->modelClass )) {
81+ if ($ this ->modelClass && !class_exists ($ this ->modelClass )) {
7782 throw new \Exception (__ ('media::fields.class_not_found ' , ['class ' => $ this ->modelClass ]));
7883 }
7984
8085 if ($ this ->modelId && $ this ->modelClass ) {
8186 $ this ->model = app ($ this ->modelClass )::find ($ this ->modelId );
8287 }
8388
84- if (! $ this ->modelId || ! $ this ->model ) {
89+ if (!$ this ->modelId || !$ this ->model ) {
8590 $ this ->modelId = 0 ;
8691 }
8792 }
8893
8994 public function form (Form $ form ): Form
9095 {
96+ $ collection = Select::make ('collection_name ' )
97+ ->label (__ ('media::fields.collection ' ))
98+ ->options (function () {
99+ return Media::query ()
100+ ->distinct ()
101+ ->pluck ('collection_name ' , 'collection_name ' )
102+ ->toArray ();
103+ })
104+ ->searchable ()
105+ ->required ()
106+ ->live ();
107+
91108 $ upload = FileUpload::make (__ ('files ' ))
92109 ->label (__ ('media::fields.upload ' ))
110+ ->live ()
93111 ->afterStateUpdated (function ($ state ) {
94- if (! $ state ) {
112+ if (!$ state ) {
95113 return ;
96114 }
97115
98116 $ processedFiles = session ('processed_files ' , []);
117+ $ collection = $ this ->collection_name ;
99118
100- if (! is_array ($ state )) {
119+ if (!is_array ($ state )) {
101120 $ fileHash = hash_file ('sha256 ' , $ state ->getRealPath ());
102121 $ fileName = $ state ->getClientOriginalName ();
103122
@@ -124,7 +143,7 @@ public function form(Form $form): Form
124143 $ model ->exists = true ;
125144
126145 $ fileAdder = app (FileAdderFactory::class)->create ($ model , $ state );
127- $ media = $ fileAdder ->preservingOriginal ()->toMediaCollection (' default ' );
146+ $ media = $ fileAdder ->preservingOriginal ()->toMediaCollection ($ collection );
128147
129148 $ title = pathinfo ($ state ->getClientOriginalName (), PATHINFO_FILENAME );
130149
@@ -181,7 +200,7 @@ public function form(Form $form): Form
181200 $ model ->exists = true ;
182201
183202 $ fileAdder = app (FileAdderFactory::class)->create ($ model , $ tempFile );
184- $ media = $ fileAdder ->preservingOriginal ()->toMediaCollection (' default ' );
203+ $ media = $ fileAdder ->preservingOriginal ()->toMediaCollection ($ collection );
185204
186205 $ title = pathinfo ($ tempFile ->getClientOriginalName (), PATHINFO_FILENAME );
187206
@@ -265,7 +284,7 @@ public function form(Form $form): Form
265284 $ upload ->visibility ($ this ->uploadConfig ['visibility ' ]);
266285 }
267286
268- return $ form ->schema ([$ upload ]);
287+ return $ form ->schema ([$ collection , $ upload ]);
269288 }
270289
271290 public function setModel (?int $ modelId , string $ modelClass ): void
@@ -289,7 +308,7 @@ public function toggleMediaSelection(int $mediaId)
289308 $ this ->selectedMediaIds [] = $ mediaId ;
290309 }
291310 } else {
292- if (! empty ($ this ->selectedMediaIds ) && $ this ->selectedMediaIds [0 ] === $ mediaId ) {
311+ if (!empty ($ this ->selectedMediaIds ) && $ this ->selectedMediaIds [0 ] === $ mediaId ) {
293312 $ this ->selectedMediaIds = [];
294313 } else {
295314 $ this ->selectedMediaIds = [$ mediaId ];
@@ -304,7 +323,7 @@ public function toggleMediaSelection(int $mediaId)
304323 if (isset ($ media ->uploader ->name )) {
305324 $ uploaderName = $ media ->uploader ->name ;
306325 } elseif (isset ($ media ->uploader ->first_name ) && isset ($ media ->uploader ->last_name )) {
307- $ uploaderName = $ media ->uploader ->first_name . ' ' . $ media ->uploader ->last_name ;
326+ $ uploaderName = $ media ->uploader ->first_name . ' ' . $ media ->uploader ->last_name ;
308327 }
309328 }
310329
@@ -349,7 +368,7 @@ public function applySelection()
349368 $ selectedMedia = Media::whereIn ('id ' , $ this ->selectedMediaIds )->get ();
350369
351370 if ($ selectedMedia ->isNotEmpty ()) {
352- if (! $ this ->multiple ) {
371+ if (!$ this ->multiple ) {
353372 $ media = $ selectedMedia ->first ();
354373 $ this ->dispatch ('mediaSelected ' , [
355374 'id ' => $ media ->id ,
@@ -359,7 +378,7 @@ public function applySelection()
359378 'name ' => $ media ->getAttribute ('name ' ),
360379 ]);
361380 } else {
362- $ selectedMediaData = $ selectedMedia ->map (fn ($ media ) => [
381+ $ selectedMediaData = $ selectedMedia ->map (fn ($ media ) => [
363382 'id ' => $ media ->id ,
364383 'url ' => $ media ->getUrl (),
365384 'file_name ' => $ media ->file_name ,
@@ -412,10 +431,12 @@ public function render()
412431 $ media = Media::query ()
413432 ->when ($ this ->searchQuery , function ($ query ) {
414433 $ query ->where (function ($ subQuery ) {
415- $ subQuery ->where ('file_name ' , 'like ' , '% ' .$ this ->searchQuery .'% ' )
416- ->orWhereRaw ('LOWER(JSON_EXTRACT(translations, "$.*.title")) LIKE ? ' , ['% ' .strtolower ($ this ->searchQuery ).'% ' ])
417- ->orWhereRaw ('LOWER(JSON_EXTRACT(translations, "$.*.description")) LIKE ? ' , ['% ' .strtolower ($ this ->searchQuery ).'% ' ])
418- ->orWhereRaw ('LOWER(JSON_EXTRACT(translations, "$.*.alt")) LIKE ? ' , ['% ' .strtolower ($ this ->searchQuery ).'% ' ]);
434+ $ subQuery ->where ('file_name ' , 'like ' , '% ' . $ this ->searchQuery . '% ' )
435+ ->orWhereHas ('translations ' , function ($ query ) {
436+ $ query ->where ('title ' , 'like ' , '% ' . $ this ->searchQuery . '% ' )
437+ ->orWhere ('description ' , 'like ' , '% ' . $ this ->searchQuery . '% ' )
438+ ->orWhere ('alt ' , 'like ' , '% ' . $ this ->searchQuery . '% ' );
439+ });
419440 });
420441 })
421442 ->when ($ this ->fileTypeFilter , function ($ query ) {
@@ -472,9 +493,17 @@ public function render()
472493 ->where ('uploader_id ' , $ parts [1 ]);
473494 }
474495 })
496+ ->when ($ this ->collectionFilter , function ($ query ) {
497+ $ query ->where ('collection_name ' , $ this ->collectionFilter );
498+ })
475499 ->orderBy ('created_at ' , 'desc ' )
476500 ->paginate (18 );
477501
502+ $ collectionOptions = Media::query ()
503+ ->distinct ()
504+ ->pluck ('collection_name ' )
505+ ->toArray ();
506+
478507 $ uploaderOptions = [];
479508 $ uploaderTypes = Media::query ()
480509 ->distinct ()
@@ -494,25 +523,25 @@ public function render()
494523 $ uploader = $ media ->uploader ;
495524 if ($ uploader && method_exists ($ uploader , 'getName ' )) {
496525 return [
497- 'id ' => $ media ->uploader_type . ':: ' . $ media ->uploader_id ,
526+ 'id ' => $ media ->uploader_type . ':: ' . $ media ->uploader_id ,
498527 'name ' => $ uploader ->getName (),
499528 ];
500529 }
501530 if ($ uploader && isset ($ uploader ->name )) {
502531 return [
503- 'id ' => $ media ->uploader_type . ':: ' . $ media ->uploader_id ,
532+ 'id ' => $ media ->uploader_type . ':: ' . $ media ->uploader_id ,
504533 'name ' => $ uploader ->name ,
505534 ];
506535 }
507536
508537 return null ;
509538 })
510539 ->filter ()
511- ->unique (fn (array $ item ): string => $ item ['id ' ])
540+ ->unique (fn (array $ item ): string => $ item ['id ' ])
512541 ->pluck ('name ' , 'id ' )
513542 ->toArray ();
514543
515- if (! empty ($ uploaders )) {
544+ if (!empty ($ uploaders )) {
516545 $ typeName = class_basename ($ type );
517546 $ uploaderOptions [$ typeName ] = $ uploaders ;
518547 }
@@ -521,6 +550,7 @@ public function render()
521550 return view ('media::livewire.media-picker-modal ' , [
522551 'mediaItems ' => $ media ,
523552 'uploaderOptions ' => $ uploaderOptions ,
553+ 'collectionOptions ' => $ collectionOptions ,
524554 ]);
525555 }
526556}
0 commit comments