66use Filament \Forms \Form ;
77use Filament \Infolists ;
88use Filament \Infolists \Infolist ;
9+ use Filament \Notifications \Notification ;
910use Filament \Resources \Resource ;
1011use Filament \Tables ;
1112use Filament \Tables \Table ;
1213use Illuminate \Database \Eloquent \Builder ;
1314use Illuminate \Database \Eloquent \Model ;
15+ use Illuminate \Support \Arr ;
1416use Illuminate \Support \Str ;
17+ use SolutionForest \InspireCms \Exports \Exporters \BaseExporter ;
1518use SolutionForest \InspireCms \Filament \Clusters \Settings ;
16- use SolutionForest \InspireCms \Filament \Clusters \Settings \Resources \ExportResource \Pages ;
1719use SolutionForest \InspireCms \Filament \Concerns \ClusterSectionResourceTrait ;
1820use SolutionForest \InspireCms \Filament \Contracts \ClusterSectionResource ;
1921use SolutionForest \InspireCms \InspireCmsConfig ;
@@ -29,6 +31,8 @@ class ExportResource extends Resource implements ClusterSectionResource
2931
3032 protected static ?string $ cluster = Settings::class;
3133
34+ protected static bool $ shouldRegisterNavigation = false ;
35+
3236 public static function getPermissionPrefixes (): array
3337 {
3438 return [
@@ -109,28 +113,72 @@ public static function infolist(Infolist $infolist): Infolist
109113 Infolists \Components \TextEntry::make ('author.email ' )->inlineLabel ()->copyable (),
110114 ]),
111115
112- \SolutionForest \InspireCms \Filament \Infolists \Components \JsonEntry::make ('payload ' )
113- ->label (__ ('inspirecms::resources/import.payload.label ' ))
116+ Infolists \Components \Section::make ()
117+ ->heading ('Detail ' )
118+ ->collapsible ()
114119 ->columnSpanFull ()
115- ->darkTheme ('tomorrow_night_eighties ' ),
120+ ->columns (1 )
121+ ->schema ([
122+ Infolists \Components \TextEntry::make ('exporter ' )
123+ ->label (__ ('inspirecms::resources/export.exporter.label ' ))
124+ ->inlineLabel ()
125+ ->formatStateUsing (function ($ state ) {
126+ if (is_string ($ state ) && class_exists ($ state )) {
127+ return $ state ::getLabel ();
128+ }
129+ return $ state ;
130+ }),
131+
132+ \SolutionForest \InspireCms \Filament \Infolists \Components \JsonEntry::make ('payload ' )
133+ ->label (__ ('inspirecms::resources/export.message.label ' ))
134+ ->columnSpanFull ()
135+ ->darkTheme ('tomorrow_night_eighties ' )
136+ ->getStateUsing (function ($ record ) {
137+ $ payload = $ record ->payload ;
138+ return collect ($ payload )->except ('result ' )->all ();
139+ }),
140+
141+ \SolutionForest \InspireCms \Filament \Infolists \Components \JsonEntry::make ('payload.result ' )
142+ ->label (__ ('inspirecms::resources/export.result.label ' ))
143+ ->columnSpanFull ()
144+ ->darkTheme ('tomorrow_night_eighties ' ),
145+ ]),
116146 ]);
117147 }
118148
119149 public static function form (Form $ form ): Form
120150 {
151+ $ exporters = collect ([
152+ \SolutionForest \InspireCms \Exports \Exporters \DocumentTypeExporter::class,
153+ \SolutionForest \InspireCms \Exports \Exporters \FieldGroupExporter::class,
154+ \SolutionForest \InspireCms \Exports \Exporters \TemplateExporter::class,
155+ ])
156+ ->mapWithKeys (fn ($ exporter ) => [$ exporter => $ exporter ::getLabel ()])
157+ ->all ();
158+
121159 return $ form
160+ ->columns (1 )
122161 ->schema ([
123- Forms \Components \ToggleButtons::make ('exporter ' )
124- ->options (
125- collect ([
126- \SolutionForest \InspireCms \Exporters \DocumentTypeExporter::class,
127- \SolutionForest \InspireCms \Exporters \FieldGroupExporter::class,
128- \SolutionForest \InspireCms \Exporters \TemplateExporter::class,
129- ])
130- ->mapWithKeys (fn ($ exporter ) => [$ exporter => $ exporter ::getLabel ()])
131- ->all ()
132- )
133- ->required (),
162+ Forms \Components \Select::make ('exporter ' )
163+ ->options ($ exporters )
164+ ->required ()
165+ ->live (),
166+
167+ Forms \Components \Group::make ()
168+ ->statePath ('payload.args ' )
169+ ->dehydrated (true )
170+ ->schema (function (Forms \Get $ get ) {
171+
172+ $ exporter = $ get ('exporter ' );
173+
174+ if ($ exporter && is_string ($ exporter ) && class_exists ($ exporter ) && is_a ($ exporter , BaseExporter::class, true )) {
175+ $ fields = $ exporter ::getArgsFormFields ();
176+
177+ return $ fields ;
178+ }
179+
180+ return [];
181+ }),
134182 ]);
135183 }
136184
@@ -185,16 +233,44 @@ public static function table(Table $table): Table
185233 ->label (__ ('inspirecms::inspirecms.created_at ' ))
186234 ->sortable (),
187235 ])
236+ ->recordAction ('view ' )
237+ ->headerActions ([
238+ Tables \Actions \CreateAction::make ()
239+ ->createAnother (false )
240+ ->modalWidth ('lg ' )
241+ ->stickyModalHeader ()->stickyModalHeader ()
242+ ->slideOver ()
243+ ->form (fn (Form $ form ) => static ::form ($ form ))
244+ // todo: add translations
245+ ->label ('Add ' )
246+ ->modalSubmitActionLabel ('Export ' )
247+ ->successNotificationTitle ('Queued for export, please wait for the download link. ' )
248+ ->failureNotificationTitle ('Missing required data, failed to export. ' )
249+ ->failureNotification (fn (Notification $ notification ) => $ notification ->warning ())
250+ ->using (function (Tables \Actions \CreateAction $ action , array $ data , string $ model ) {
251+
252+ $ user = auth ()->user ();
253+ $ exporter = $ data ['exporter ' ] ?? null ;
254+
255+ if (! $ user || ! filled ($ exporter )) {
256+ $ action ->sendFailureNotification ();
257+ return $ action ->cancel ();
258+ }
259+ $ export = app ($ model , ['attributes ' => Arr::only ($ data , ['exporter ' , 'payload ' ])]);
260+ $ export ->author ()->associate ($ user );
261+ $ export ->save ();
262+
263+ return $ export ;
264+ }),
265+ ])
188266 ->actions ([
189267 Tables \Actions \ViewAction::make ()->iconButton ()->slideOver ()->authorize (null ),
190268 ]);
191269 }
192270
193271 public static function getPages (): array
194272 {
195- return [
196- 'index ' => Pages \ListExports::route ('/ ' ),
197- ];
273+ return [];
198274 }
199275
200276 public static function getModel (): string
0 commit comments