66use Illuminate \Support \Str ;
77use SolutionForest \InspireCms \Exports \ExportResult ;
88use SolutionForest \InspireCms \Helpers \FileHelper ;
9+ use SolutionForest \InspireCms \Helpers \TemplateHelper ;
910use SolutionForest \InspireCms \ImportData \Entities as ImportDataEntities ;
11+ use SolutionForest \InspireCms \Models \Contracts \Content ;
1012use SolutionForest \InspireCms \Models \Contracts \DocumentType ;
1113use SolutionForest \InspireCms \Models \Contracts \Export ;
1214use SolutionForest \InspireCms \Models \Contracts \FieldGroup ;
@@ -101,34 +103,44 @@ protected function ensureTempFolderForExport(string $folderPrefix, array $subFol
101103 return array_merge ([$ folderName ], $ tmpFolderData );
102104 }
103105
106+ /**
107+ * @return string | array
108+ */
104109 protected function generateImportFileName (Model $ record )
105110 {
106111 switch (true ) {
112+
107113 case $ record instanceof DocumentType:
114+ case $ record instanceof Content:
108115 return $ record ->slug . '.json ' ;
109116
110117 case $ record instanceof FieldGroup:
111118 return Str::replace ('_ ' , '- ' , $ record ->name ) . '.json ' ;
112119
113- // case $record instanceof Template:
114- // if (is_array($record->content)) {
115- // $themes = array_keys($record->content);
116- // } else {
117- // $themes = [inspirecms_templates()->getCurrentTheme()];
118- // }
119- // return collect($themes)
120- // ->filter()
121- // ->unique()
122- // ->map(fn ($theme) => $record->slug . '/' . "$theme.blade.php")
123- // ->toArray();
120+ case $ record instanceof Template:
121+ if (is_array ($ record ->content )) {
122+ $ themes = array_keys ($ record ->content );
123+ } else {
124+ $ themes = [inspirecms_templates ()->getCurrentTheme ()];
125+ }
126+
127+ return collect ($ themes )
128+ ->filter ()
129+ ->unique ()
130+ ->mapWithKeys (fn ($ theme ) => [$ theme => $ record ->slug . '/ ' . TemplateHelper::ensureViewFileNameForTemplate ($ theme )])
131+ ->toArray ();
124132 }
125133
126134 return $ record ->getKey () . '.json ' ;
127135 }
128136
137+ /**
138+ * @return array|bool|string
139+ */
129140 protected function prepareImportContentFromModel (Model $ record )
130141 {
131142 switch (true ) {
143+
132144 case $ record instanceof DocumentType:
133145 $ array = ImportDataEntities \DocumentType::fromRecord ($ record )->toExportArray ();
134146
@@ -145,6 +157,11 @@ protected function prepareImportContentFromModel(Model $record)
145157 // $themeContent = [inspirecms_templates()->getCurrentTheme() => $themeContent];
146158 // }
147159 // return $themeContent;
160+
161+ case $ record instanceof Content:
162+ $ array = ImportDataEntities \Content::fromRecord ($ record )->toExportArray ();
163+
164+ return json_encode ($ array , JSON_PRETTY_PRINT );
148165 }
149166
150167 return '' ;
@@ -182,15 +199,39 @@ protected function handleExportCompletion(string $folderName, $processingErrors)
182199 protected function processRecordForImportUsed (Model $ record , $ fs , ?string $ dir , array &$ errors )
183200 {
184201 try {
185- $ content = $ this -> prepareImportContentFromModel ( $ record );
202+
186203 $ filename = $ this ->generateImportFileName ($ record );
187- $ path = $ dir . '/ ' . $ filename ;
188204
189- $ fs ->put ($ path , $ content );
205+ if ($ record instanceof Template && is_array ($ filename )) {
206+
207+ foreach ($ filename as $ theme => $ templateFilePath ) {
208+
209+ $ templateContent = $ record ->getContent ($ theme );
210+
211+ $ path = $ dir . '/ ' . trim ($ templateFilePath , '/ ' );
212+
213+ $ fs ->put ($ path , $ templateContent );
214+ }
215+
216+ } elseif (! is_string ($ filename )) {
217+ $ errors [] = [
218+ 'record ' => $ record ->getKey (),
219+ 'model ' => get_class ($ record ),
220+ 'message ' => 'Invalid filename ' ,
221+ ];
222+ } else {
223+
224+ $ content = $ this ->prepareImportContentFromModel ($ record );
225+
226+ $ path = $ dir . '/ ' . $ filename ;
227+ $ fs ->put ($ path , $ content );
228+
229+ }
190230
191231 } catch (\Throwable $ th ) {
192232 $ errors [] = [
193233 'record ' => $ record ->getKey (),
234+ 'model ' => get_class ($ record ),
194235 'message ' => $ th ->getMessage (),
195236 ];
196237 }
0 commit comments