77use Tapp \FilamentLibrary \Models \LibraryItem ;
88use Filament \Actions \CreateAction ;
99use Filament \Actions \Action ;
10+ use Filament \Forms \Components \TextInput ;
11+ use Filament \Forms \Components \FileUpload ;
1012
1113class ListLibraryItems extends ListRecords
1214{
@@ -18,9 +20,9 @@ class ListLibraryItems extends ListRecords
1820 public function mount (): void
1921 {
2022 parent ::mount ();
21-
23+
2224 $ this ->parentId = request ()->get ('parent ' );
23-
25+
2426 if ($ this ->parentId ) {
2527 $ this ->parentFolder = LibraryItem::find ($ this ->parentId );
2628 }
@@ -43,27 +45,70 @@ protected function getHeaderActions(): array
4345 ->color ('gray ' );
4446 }
4547
46- // Add "Create" action
47- $ actions [] = CreateAction::make ()
48- ->url (fn (): string =>
49- $ this ->parentId
50- ? static ::getResource ()::getUrl ('create ' , ['parent ' => $ this ->parentId ])
51- : static ::getResource ()::getUrl ('create ' )
52- );
48+ // Add "Create Folder" modal action
49+ $ actions [] = Action::make ('create_folder ' )
50+ ->label ('Create Folder ' )
51+ ->icon ('heroicon-o-folder-plus ' )
52+ ->color ('success ' )
53+ ->form ([
54+ TextInput::make ('name ' )
55+ ->label ('Folder Name ' )
56+ ->required ()
57+ ->maxLength (255 )
58+ ->placeholder ('Enter folder name ' ),
59+ ])
60+ ->action (function (array $ data ): void {
61+ LibraryItem::create ([
62+ 'name ' => $ data ['name ' ],
63+ 'type ' => 'folder ' ,
64+ 'parent_id ' => $ this ->parentId ,
65+ 'created_by ' => auth ()->user ()?->id,
66+ ]);
67+
68+ $ this ->redirect (static ::getResource ()::getUrl ('index ' , $ this ->parentId ? ['parent ' => $ this ->parentId ] : []));
69+ });
70+
71+ // Add "Upload File" modal action
72+ $ actions [] = Action::make ('upload_file ' )
73+ ->label ('Upload File ' )
74+ ->icon ('heroicon-o-document-plus ' )
75+ ->color ('primary ' )
76+ ->form ([
77+ FileUpload::make ('file ' )
78+ ->label ('Upload File ' )
79+ ->required ()
80+ ->acceptedFileTypes (['* ' ])
81+ ->maxSize (10240 ) // 10MB
82+ ->disk ('public ' )
83+ ->directory ('library-files ' )
84+ ->visibility ('private ' ),
85+ ])
86+ ->action (function (array $ data ): void {
87+ $ file = $ data ['file ' ];
88+
89+ LibraryItem::create ([
90+ 'name ' => $ file ->getClientOriginalName (),
91+ 'type ' => 'file ' ,
92+ 'parent_id ' => $ this ->parentId ,
93+ 'created_by ' => auth ()->user ()?->id,
94+ ]);
95+
96+ $ this ->redirect (static ::getResource ()::getUrl ('index ' , $ this ->parentId ? ['parent ' => $ this ->parentId ] : []));
97+ });
5398
5499 return $ actions ;
55100 }
56101
57102 protected function getTableQuery (): \Illuminate \Database \Eloquent \Builder
58103 {
59104 $ query = parent ::getTableQuery ();
60-
105+
61106 if ($ this ->parentId ) {
62107 $ query ->where ('parent_id ' , $ this ->parentId );
63108 } else {
64109 $ query ->whereNull ('parent_id ' );
65110 }
66-
111+
67112 return $ query ;
68113 }
69114
@@ -72,7 +117,7 @@ public function getTitle(): string
72117 if ($ this ->parentFolder ) {
73118 return $ this ->parentFolder ->name ;
74119 }
75-
120+
76121 return 'All Folders ' ;
77122 }
78123
@@ -81,21 +126,21 @@ public function getBreadcrumbs(): array
81126 $ breadcrumbs = [
82127 static ::getResource ()::getUrl () => 'All Folders ' ,
83128 ];
84-
129+
85130 if ($ this ->parentFolder ) {
86131 $ current = $ this ->parentFolder ;
87132 $ path = [];
88-
133+
89134 while ($ current ) {
90135 array_unshift ($ path , $ current );
91136 $ current = $ current ->parent ;
92137 }
93-
138+
94139 foreach ($ path as $ folder ) {
95140 $ breadcrumbs [static ::getResource ()::getUrl ('index ' , ['parent ' => $ folder ->id ])] = $ folder ->name ;
96141 }
97142 }
98-
143+
99144 return $ breadcrumbs ;
100145 }
101146}
0 commit comments