22
33namespace Photobooth \Utility ;
44
5- use Photobooth \Utility \PathUtility ;
65use Photobooth \Service \LanguageService ;
76
87class CollageLayoutScanner
@@ -29,15 +28,15 @@ public static function scanLayouts(): array
2928
3029 // Initialize the main group key in $layoutFiles early
3130 $ layoutFiles [$ mainGroupKey ] = [];
32-
31+
3332 // Ensure the base directory exists, create if it's a 'private' one and missing
3433 if (!is_dir ($ absoluteBaseDir )) {
35- if ($ mainGroupKey === 'private ' ) {
34+ if ($ mainGroupKey === 'private ' ) {
3635 try {
3736 mkdir ($ absoluteBaseDir , 0777 , true );
3837 } catch (\Exception $ e ) {
3938 error_log ('CollageLayoutScanner: Failed to create base directory: ' . $ absoluteBaseDir . ' - ' . $ e ->getMessage ());
40- continue ;
39+ continue ;
4140 }
4241 } else {
4342 continue ; // Skip if 'template' base dir doesn't exist (expected to be present)
@@ -52,18 +51,18 @@ public static function scanLayouts(): array
5251
5352 // Ensure the subdirectory exists, create if it's in a 'private' context and missing
5453 if (!is_dir ($ subDirPath )) {
55- if ($ mainGroupKey === 'private ' ) {
54+ if ($ mainGroupKey === 'private ' ) {
5655 try {
5756 mkdir ($ subDirPath , 0777 , true );
5857 } catch (\Exception $ e ) {
5958 error_log ('CollageLayoutScanner: Failed to create subdirectory: ' . $ subDirPath . ' - ' . $ e ->getMessage ());
60- continue ;
59+ continue ;
6160 }
6261 } else {
6362 continue ; // Skip if 'template' subdir doesn't exist (expected to be present)
6463 }
6564 }
66-
65+
6766 // If directory exists (or was created), scan it
6867 // Pass the mainGroupKey AND the subGroupName to build the nested structure
6968 self ::scanDirectory ($ subDirPath , $ layoutFiles [$ mainGroupKey ], $ subGroupName , $ mainGroupKey );
@@ -84,6 +83,10 @@ public static function scanLayouts(): array
8483 private static function scanDirectory (string $ directory , array &$ layoutFiles , string $ subGroupKey , string $ mainGroupKey ): void
8584 {
8685 $ files = glob ($ directory . DIRECTORY_SEPARATOR . '*.json ' );
86+ if ($ files === false ) {
87+ return ;
88+ }
89+
8790 foreach ($ files as $ filePath ) {
8891 $ fileContent = file_get_contents ($ filePath );
8992 if ($ fileContent === false ) {
@@ -97,8 +100,8 @@ private static function scanDirectory(string $directory, array &$layoutFiles, st
97100 continue ;
98101 }
99102
100- $ layoutId = basename ($ filePath , '.json ' );
101-
103+ $ layoutId = basename ($ filePath , '.json ' );
104+
102105 $ layoutName = $ layoutConfig ['name ' ] ?? $ layoutId ;
103106
104107 $ refFilePath = $ mainGroupKey . '/collage/ ' . $ subGroupKey . '/ ' . $ layoutId ;
@@ -108,9 +111,9 @@ private static function scanDirectory(string $directory, array &$layoutFiles, st
108111 $ layoutFiles [$ subGroupKey ][$ layoutId ] = [
109112 'id ' => $ layoutId ,
110113 'name ' => $ layoutName ,
111- 'description ' => $ layoutConfig ['description ' ] ?? '' ,
114+ 'description ' => $ layoutConfig ['description ' ] ?? '' ,
112115 'author ' => $ layoutConfig ['author ' ] ?? 'Unknown ' ,
113- 'ref_file_path ' => $ refFilePath ,
116+ 'ref_file_path ' => $ refFilePath ,
114117 'aspect_ratio ' => $ layoutConfig ['aspect_ratio ' ] ?? '' ,
115118 'width ' => $ layoutConfig ['width ' ] ?? '' ,
116119 'height ' => $ layoutConfig ['height ' ] ?? '' ,
@@ -164,7 +167,7 @@ private static function groupAndTranslateLayouts(array $rawLayoutFiles): array
164167 }
165168 }
166169 }
167-
170+
168171 return $ groupedLayouts ;
169172 }
170173
@@ -179,6 +182,10 @@ public static function getLayoutData(string $logicalReferencePath): ?array
179182 {
180183 $ AbsFilePath = self ::getCollageConfigPath ($ logicalReferencePath );
181184
185+ if ($ AbsFilePath === null ) {
186+ return null ;
187+ }
188+
182189 $ fileContent = file_get_contents ($ AbsFilePath );
183190
184191 if ($ fileContent === false ) {
@@ -192,17 +199,16 @@ public static function getLayoutData(string $logicalReferencePath): ?array
192199 return [];
193200 }
194201
195- $ layoutId = basename ($ AbsFilePath , '.json ' );
196-
197- $ layoutName = $ layoutConfig ['name ' ] ?? $ layoutId ;
202+ $ layoutId = basename ($ AbsFilePath , '.json ' );
203+
204+ $ layoutName = $ layoutConfig ['name ' ] ?? $ layoutId ;
198205
199-
200206 $ layoutData = [
201207 'id ' => $ layoutId ,
202208 'name ' => $ layoutName ,
203- 'ref_file_path ' => $ logicalReferencePath ,
209+ 'ref_file_path ' => $ logicalReferencePath ,
204210 ];
205-
211+
206212 $ layoutData = array_merge ($ layoutConfig , $ layoutData );
207213
208214 return $ layoutData ;
@@ -223,7 +229,7 @@ public static function getLayoutSelectOptionsHtml(?string $currentSelectedPath =
223229
224230 foreach ($ designes as $ mainGroupTitle => $ subGroups ) {
225231 $ optionsHtml .= '<optgroup label=" ' . htmlspecialchars ($ mainGroupTitle , ENT_QUOTES ) . '"> ' ;
226-
232+
227233 // Sort subgroups by their translated titles to ensure consistent order
228234 ksort ($ subGroups );
229235
@@ -234,13 +240,13 @@ public static function getLayoutSelectOptionsHtml(?string $currentSelectedPath =
234240 }
235241
236242 // Sort the layouts within the subgroup by their name
237- uasort ($ layouts , function ($ a , $ b ) {
243+ uasort ($ layouts , function ($ a , $ b ) {
238244 return strcmp ($ a ['name ' ] ?? $ a ['id ' ], $ b ['name ' ] ?? $ b ['id ' ]);
239245 });
240246
241247 foreach ($ layouts as $ layoutId => $ layoutData ) {
242248 $ selected = ($ layoutData ['ref_file_path ' ] === $ currentSelectedPath ) ? ' selected="selected" ' : '' ;
243-
249+
244250 $ displayName = htmlspecialchars ($ layoutData ['name ' ] ?? $ layoutData ['id ' ] ?? '' , ENT_QUOTES );
245251
246252 $ optionsHtml .= '<option value=" ' . htmlspecialchars ($ layoutData ['ref_file_path ' ], ENT_QUOTES ) . '" ' . $ selected . '> ' ;
@@ -265,14 +271,14 @@ public static function getCollageConfigPath(string $logicalReferencePath): ?stri
265271 {
266272 // Add the .json extension
267273 $ fullPathWithExtension = $ logicalReferencePath . '.json ' ;
268-
274+
269275 // Let PathUtility build the absolute path
270276 $ absolutePath = PathUtility::getAbsolutePath ($ fullPathWithExtension );
271277
272278 if (file_exists ($ absolutePath )) {
273279 return $ absolutePath ;
274280 }
275-
281+
276282 // Log, falls die Datei nicht gefunden wird, hilfreich für Debugging
277283 error_log ('DEBUG: CollageLayoutScanner::getCollageConfigPath - Layout JSON file not found at: ' . $ absolutePath );
278284 return null ;
0 commit comments