22
33namespace SolutionForest \InspireCms \Base \Filament \Concerns ;
44
5- use Filament \Notifications \Notification ;
65use Filament \Resources \Pages \CreateRecord ;
7- use Filament \Support \Exceptions \Halt ;
86use Illuminate \Database \Eloquent \Model ;
9- use Illuminate \Support \Facades \ Blade ;
7+ use Illuminate \Support \Arr ;
108use Pboivin \FilamentPeek \Pages \Concerns \HasBuilderPreview ;
119use Pboivin \FilamentPeek \Pages \Concerns \HasPreviewModal ;
12- use Pboivin \FilamentPeek \Support \Html ;
13- use SolutionForest \InspireCms \InspireCmsConfig ;
14- use SolutionForest \InspireCms \Models \Contracts \Content ;
10+ use SolutionForest \InspireCms \Factories \PreviewFactory ;
1511
1612trait ContentPreviewEditorTrait
1713{
@@ -20,29 +16,24 @@ trait ContentPreviewEditorTrait
2016
2117 protected function getBuilderPreviewView (string $ builderName ): ?string
2218 {
23- $ template = filled ($ this ->data ['template_id ' ] ?? null ) ? InspireCmsConfig::getTemplateModelClass ()::find ($ this ->data ['template_id ' ]) : null ;
24- $ template ??= $ this ->getDocumentType ()?->getDefaultTemplate();
25-
26- $ templateContent = $ template ?->getContent();
27-
28- if (! $ template || blank ($ templateContent )) {
29- Notification::make ()
30- ->title (__ ('inspirecms::notification.template_not_found.title ' ))
31- ->body (__ ('inspirecms::notification.template_not_found.body ' ))
32- ->danger ()
33- ->seconds (60 )
34- ->send ();
35-
36- throw new Halt ;
37- }
38-
39- return $ templateContent ;
19+ return 'handle by previewFactory ' ;
4020 }
4121
4222 public static function renderBuilderPreview (string $ view , array $ data ): string
4323 {
44- return Html::injectPreviewModalStyle (
45- Blade::render ($ view , $ data )
24+ $ extraData = Arr::except ($ data , [
25+ 'documentType ' ,
26+ 'template ' ,
27+ 'recordData ' ,
28+ 'propertyData ' ,
29+ ]);
30+ return PreviewFactory::create ()->renderContentPreview (
31+ documentType: $ data ['documentType ' ] ?? null ,
32+ template: $ data ['template ' ] ?? null ,
33+ content: $ data ['recordData ' ] ?? [],
34+ propertyData: $ data ['propertyData ' ] ?? [],
35+ locale: $ data ['locale ' ] ?? null ,
36+ data: $ extraData ,
4637 );
4738 }
4839
@@ -55,49 +46,30 @@ public static function getBuilderEditorSchema(string $builderName): \Filament\Fo
5546
5647 public function mutateInitialBuilderEditorData (string $ builderName , array $ editorData ): array
5748 {
58- $ contentModel = $ this ->getModel ();
59- $ editorData ['contentModel ' ] = $ contentModel ;
60-
61- $ documentType = $ this ->getDocumentType ();
62- $ editorData ['documentType ' ] = $ documentType instanceof Model ? $ documentType ->getKey () : $ documentType ;
49+ $ editorData ['recordData ' ] = Arr::except ($ this ->data , [
50+ 'propertyData ' ,
51+ ]);
52+ $ editorData ['propertyData ' ] = $ this ->data ['propertyData ' ] ?? [];
6353
6454 if ($ this instanceof CreateRecord) {
65- $ editorData ['operation ' ] = 'create ' ;
66- $ editorData ['contentData ' ] = $ this -> data ;
67- $ editorData [ ' contentData ' ][ ' children ' ] = [];
55+ $ editorData ['editorOperation ' ] = 'create ' ;
56+ $ editorData ['recordData ' ][ ' children ' ] = [] ;
57+
6858 } else {
69- $ editorData ['operation ' ] = 'edit ' ;
59+ $ editorData ['editorOperation ' ] = 'edit ' ;
7060
7161 $ content = $ this ->getRecord ();
72- $ editorData ['contentData ' ] = $ this ->data ;
73- $ editorData ['contentData ' ]['children ' ] = $ content ->children ()->pluck ($ content ->getKeyName ())->toArray ();
74- $ editorData ['contentKey ' ] = $ content ->getKey ();
75-
62+ $ editorData ['recordData ' ]['children ' ] = $ content ->children ()->pluck ($ content ->getKeyName ())->toArray ();
7663 }
7764
65+ $ editorData ['documentType ' ] = ($ dt = $ this ->getDocumentType ()) && $ dt instanceof Model ? $ dt ->getKey () : ($ dt ?? null ); // primary key or model record
66+ $ editorData ['template ' ] = $ this ->data ['template_id ' ] ?? null ;
67+
7868 return $ editorData ;
7969 }
8070
8171 public static function mutateBuilderPreviewData (string $ builderName , array $ editorData , array $ previewData ): array
8272 {
83- $ contentModel = $ editorData ['contentModel ' ];
84-
85- if (! in_array (Content::class, class_implements ($ contentModel ))) {
86- throw new \Exception ('Model must implement ' . Content::class);
87- }
88-
89- $ documentType = InspireCmsConfig::getDocumentTypeModelClass ()::find ($ editorData ['documentType ' ]);
90-
91- $ contentDto = $ contentModel ::toPreviewDto (
92- record: $ editorData ['contentData ' ],
93- propertyData: $ editorData ['propertyData ' ] ?? [],
94- locale: $ editorData ['activeLocale ' ],
95- documentType: $ documentType ,
96- );
97-
98- $ previewData ['content ' ] = $ contentDto ;
99- $ previewData ['locale ' ] = $ editorData ['activeLocale ' ] ?? app ()->getLocale ();
100-
101- return $ previewData ;
73+ return array_merge ($ previewData , $ editorData );
10274 }
10375}
0 commit comments