3
3
namespace Statikbe \FilamentFlexibleContentBlockPages \Resources \MenuResource \Pages ;
4
4
5
5
use 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 ;
13
12
use Statikbe \FilamentFlexibleContentBlockPages \Facades \FilamentFlexibleContentBlockPages ;
14
13
use Statikbe \FilamentFlexibleContentBlockPages \Filament \Form \Forms \MenuItemForm ;
14
+ use Statikbe \FilamentFlexibleContentBlockPages \Models \MenuItem ;
15
15
use Statikbe \FilamentFlexibleContentBlockPages \Resources \MenuResource ;
16
- use Studio15 \FilamentTree \Components \TreePage ;
17
16
18
17
class ManageMenuItems extends TreePage
19
18
{
@@ -23,43 +22,23 @@ class ManageMenuItems extends TreePage
23
22
24
23
public mixed $ record ;
25
24
25
+ protected static int $ maxDepth = 3 ;
26
+
26
27
public function mount (): void
27
28
{
28
- parent ::mount ();
29
-
30
29
$ menuModelClass = MenuResource::getModel ();
31
30
$ recordId = request ()->route ('record ' );
32
31
$ this ->record = app ($ menuModelClass )->findOrFail ($ recordId );
33
32
}
34
33
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
50
35
{
51
36
return FilamentFlexibleContentBlockPages::config ()->getMenuItemModel ()::class;
52
37
}
53
38
54
- protected function getViewData (): array
39
+ public function getTranslatableLocales (): array
55
40
{
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 ();
63
42
}
64
43
65
44
public function getTitle (): string
@@ -75,47 +54,67 @@ public function getBreadcrumb(): string
75
54
return flexiblePagesTrans ('menu_items.manage.breadcrumb ' );
76
55
}
77
56
78
- public static function getCreateForm (): array
79
- {
80
- return MenuItemForm::getSchema ();
81
- }
82
-
83
- public static function getEditForm (): array
57
+ protected function getHeaderActions (): array
84
58
{
85
- return MenuItemForm::getSchema ();
59
+ return [
60
+ LocaleSwitcher::make (),
61
+ ];
86
62
}
87
63
88
- public static function getInfolistColumns (): array
64
+ protected function getTreeActions (): array
89
65
{
90
66
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 (),
98
88
];
99
89
}
100
90
101
- protected function getHeaderActions (): array
91
+ protected function getTreeRecords ()
102
92
{
103
- return [
104
- LocaleSwitcher::make (),
105
- ];
93
+ return static ::getModel ()::where ('menu_id ' , $ this ->record ->id )
94
+ ->with ('linkable ' )
95
+ ->orderBy ('order ' )
96
+ ->get ();
106
97
}
107
98
108
- protected function mutateFormDataBeforeCreate ( array $ data ): array
99
+ public function getTreeRecordTitle (? Model $ record = null ): string
109
100
{
110
- $ data ['menu_id ' ] = $ this ->record ->id ;
101
+ /** @var MenuItem $record */
102
+ if (! $ record ) {
103
+ return '' ;
104
+ }
111
105
112
- return $ data ;
106
+ $ locale = $ this ->getActiveLocale ();
107
+ return $ record ->getDisplayLabel ($ locale );
113
108
}
114
109
115
- protected function mutateFormDataBeforeSave ( array $ data ): array
110
+ public function getTreeRecordIcon (? \ Illuminate \ Database \ Eloquent \ Model $ record = null ): ? string
116
111
{
117
- $ data ['menu_id ' ] = $ this ->record ->id ;
112
+ //TODO
113
+ return parent ::getTreeRecordIcon ($ record );
114
+ }
118
115
119
- return $ data ;
116
+ protected function getFormSchema (): array
117
+ {
118
+ return MenuItemForm::getSchema ();
120
119
}
121
120
}
0 commit comments