33namespace Statikbe \FilamentFlexibleContentBlockPages \Resources \MenuResource \Pages ;
44
55use Filament \Actions \LocaleSwitcher ;
6- use Filament \Infolists \Components \IconEntry ;
7- use Filament \Panel ;
8- use Filament \Resources \Concerns \Translatable ;
9- use Filament \Resources \Pages \PageRegistration ;
10- use Illuminate \Routing \Route ;
11- use Illuminate \Support \Facades \Route as RouteFacade ;
12- use Kalnoy \Nestedset \QueryBuilder ;
6+ use Illuminate \Database \Eloquent \Model ;
7+ use SolutionForest \FilamentTree \Concern \TreeRecords \Translatable ;
8+ use SolutionForest \FilamentTree \Actions \Action ;
9+ use SolutionForest \FilamentTree \Actions \DeleteAction ;
10+ use SolutionForest \FilamentTree \Actions \EditAction ;
11+ use SolutionForest \FilamentTree \Pages \TreePage ;
1312use Statikbe \FilamentFlexibleContentBlockPages \Facades \FilamentFlexibleContentBlockPages ;
1413use Statikbe \FilamentFlexibleContentBlockPages \Filament \Form \Forms \MenuItemForm ;
14+ use Statikbe \FilamentFlexibleContentBlockPages \Models \MenuItem ;
1515use Statikbe \FilamentFlexibleContentBlockPages \Resources \MenuResource ;
16- use Studio15 \FilamentTree \Components \TreePage ;
1716
1817class ManageMenuItems extends TreePage
1918{
@@ -23,43 +22,23 @@ class ManageMenuItems extends TreePage
2322
2423 public mixed $ record ;
2524
25+ protected static int $ maxDepth = 3 ;
26+
2627 public function mount (): void
2728 {
28- parent ::mount ();
29-
3029 $ menuModelClass = MenuResource::getModel ();
3130 $ recordId = request ()->route ('record ' );
3231 $ this ->record = app ($ menuModelClass )->findOrFail ($ recordId );
3332 }
3433
35- /**
36- * Copied from Resource/Page to support routing in resources.
37- * {@inheritDoc}
38- */
39- public static function route (string $ path ): PageRegistration
40- {
41- return new PageRegistration (
42- page: static ::class,
43- route: fn (Panel $ panel ): Route => RouteFacade::get ($ path , static ::class)
44- ->middleware (static ::getRouteMiddleware ($ panel ))
45- ->withoutMiddleware (static ::getWithoutRouteMiddleware ($ panel )),
46- );
47- }
48-
49- public static function getModel (): string |QueryBuilder
34+ public function getModel (): string
5035 {
5136 return FilamentFlexibleContentBlockPages::config ()->getMenuItemModel ()::class;
5237 }
5338
54- protected function getViewData (): array
39+ public function getTranslatableLocales (): array
5540 {
56- $ query = static ::getModel ()::scoped (['menu_id ' => $ this ->record ->id ])
57- ->with ('linkable ' )
58- ->defaultOrder ();
59-
60- return [
61- 'tree ' => $ query ->get ()->toTree (),
62- ];
41+ return static ::getResource ()::getTranslatableLocales ();
6342 }
6443
6544 public function getTitle (): string
@@ -75,47 +54,67 @@ public function getBreadcrumb(): string
7554 return flexiblePagesTrans ('menu_items.manage.breadcrumb ' );
7655 }
7756
78- public static function getCreateForm (): array
79- {
80- return MenuItemForm::getSchema ();
81- }
82-
83- public static function getEditForm (): array
57+ protected function getHeaderActions (): array
8458 {
85- return MenuItemForm::getSchema ();
59+ return [
60+ LocaleSwitcher::make (),
61+ ];
8662 }
8763
88- public static function getInfolistColumns (): array
64+ protected function getTreeActions (): array
8965 {
9066 return [
91- IconEntry::make ('is_visible ' )
92- ->label ('' )
93- ->icon (fn (bool $ state ): string => $ state ? 'heroicon-o-eye ' : 'heroicon-o-eye-slash ' )
94- ->color (fn (bool $ state ): string => $ state ? 'gray ' : 'warning ' )
95- ->tooltip (fn (bool $ state ): ?string => $ state ? null : flexiblePagesTrans ('menu_items.status.hidden ' ))
96- ->hidden (fn (bool $ state ): bool => $ state )
97- ->size ('sm ' ),
67+ Action::make ('create ' )
68+ ->mountUsing (
69+ fn ($ arguments , $ form , $ model ) => $ form ->fill ([
70+ 'menu_id ' => $ this ->record ->id ,
71+ 'parent_id ' => $ arguments ['parent_id ' ] ?? -1 ,
72+ 'is_visible ' => true ,
73+ 'target ' => '_self ' ,
74+ ])
75+ )
76+ ->action (function (array $ data ): void {
77+ $ data ['menu_id ' ] = $ this ->record ->id ;
78+ static ::getModel ()::create ($ data );
79+ }),
80+ EditAction::make ()
81+ ->mountUsing (
82+ fn ($ arguments , $ form , $ model ) => $ form ->fill ([
83+ ...$ model ->toArray (),
84+ 'menu_id ' => $ this ->record ->id ,
85+ ])
86+ ),
87+ DeleteAction::make (),
9888 ];
9989 }
10090
101- protected function getHeaderActions (): array
91+ protected function getTreeRecords ()
10292 {
103- return [
104- LocaleSwitcher::make (),
105- ];
93+ return static ::getModel ()::where ('menu_id ' , $ this ->record ->id )
94+ ->with ('linkable ' )
95+ ->orderBy ('order ' )
96+ ->get ();
10697 }
10798
108- protected function mutateFormDataBeforeCreate ( array $ data ): array
99+ public function getTreeRecordTitle (? Model $ record = null ): string
109100 {
110- $ data ['menu_id ' ] = $ this ->record ->id ;
101+ /** @var MenuItem $record */
102+ if (! $ record ) {
103+ return '' ;
104+ }
111105
112- return $ data ;
106+ $ locale = $ this ->getActiveLocale ();
107+ return $ record ->getDisplayLabel ($ locale );
113108 }
114109
115- protected function mutateFormDataBeforeSave ( array $ data ): array
110+ public function getTreeRecordIcon (? \ Illuminate \ Database \ Eloquent \ Model $ record = null ): ? string
116111 {
117- $ data ['menu_id ' ] = $ this ->record ->id ;
112+ //TODO
113+ return parent ::getTreeRecordIcon ($ record );
114+ }
118115
119- return $ data ;
116+ protected function getFormSchema (): array
117+ {
118+ return MenuItemForm::getSchema ();
120119 }
121120}
0 commit comments