@@ -273,78 +273,86 @@ public function actionSaveCustomizeSourcesModalSettings(): Response
273273 if ($ multiPage ) {
274274 $ sourcePages = $ this ->request ->getBodyParam ('sourcePages ' , []);
275275 $ pageSettings = $ this ->request ->getBodyParam ('pageSettings ' , []);
276+ $ sourcePageIndexes = [];
276277 }
277278
278279 // Normalize to the way it's stored in the DB
279- foreach ($ sourceOrder as $ source ) {
280- if (isset ($ source ['key ' ])) {
281- $ type = match (true ) {
282- str_starts_with ($ source ['key ' ], 'custom: ' ) => ElementSources::TYPE_CUSTOM ,
283- str_starts_with ($ source ['key ' ], 'heading: ' ) => ElementSources::TYPE_HEADING ,
284- default => ElementSources::TYPE_NATIVE ,
285- };
286-
287- $ isCustom = $ type === ElementSources::TYPE_CUSTOM ;
288- $ sourceConfig = [
289- 'type ' => $ type ,
290- 'key ' => $ source ['key ' ],
291- ];
292-
293- if (isset ($ sourcePages [$ source ['key ' ]])) {
294- $ sourceConfig ['page ' ] = $ sourcePages [$ source ['key ' ]];
280+ foreach ($ sourceOrder as $ key ) {
281+ $ type = match (true ) {
282+ str_starts_with ($ key , 'custom: ' ) => ElementSources::TYPE_CUSTOM ,
283+ str_starts_with ($ key , 'heading: ' ) => ElementSources::TYPE_HEADING ,
284+ default => ElementSources::TYPE_NATIVE ,
285+ };
286+
287+ $ isCustom = $ type === ElementSources::TYPE_CUSTOM ;
288+ $ sourceConfig = [
289+ 'type ' => $ type ,
290+ 'key ' => $ key ,
291+ ];
292+
293+ if (isset ($ sourcePages [$ key ])) {
294+ $ sourceConfig ['page ' ] = $ sourcePages [$ key ];
295+ }
296+
297+ // Were new settings posted?
298+ if (isset ($ sourceSettings [$ key ])) {
299+ $ postedSettings = $ sourceSettings [$ key ];
300+
301+ if ($ type !== ElementSources::TYPE_HEADING ) {
302+ $ sourceConfig ['tableAttributes ' ] = array_values (array_filter ($ postedSettings ['tableAttributes ' ] ?? [])) ?: '- ' ;
295303 }
296304
297- // Were new settings posted?
298- if ( isset ( $ sourceSettings [ $ source [ ' key ' ]])) {
299- $ postedSettings = $ sourceSettings [ $ source [ ' key ' ]];
305+ if ( isset ( $ postedSettings [ ' defaultSort ' ])) {
306+ $ sourceConfig [ ' defaultSort ' ] = $ postedSettings [ ' defaultSort ' ];
307+ }
300308
301- if ($ type !== ElementSources:: TYPE_HEADING ) {
302- $ sourceConfig ['tableAttributes ' ] = array_values ( array_filter ( $ postedSettings ['tableAttributes ' ] ?? [])) ?: ' - ' ;
303- }
309+ if (isset ( $ postedSettings [ ' defaultViewMode ' ]) ) {
310+ $ sourceConfig ['defaultViewMode ' ] = $ postedSettings ['defaultViewMode ' ] ;
311+ }
304312
305- if (isset ($ postedSettings ['defaultSort ' ])) {
306- $ sourceConfig ['defaultSort ' ] = $ postedSettings ['defaultSort ' ];
307- }
313+ if ($ isCustom ) {
314+ $ sourceConfig += [
315+ 'label ' => $ postedSettings ['label ' ],
316+ 'condition ' => $ conditionsService ->createCondition ($ postedSettings ['condition ' ])->getConfig (),
317+ ];
308318
309- if (isset ($ postedSettings ['defaultViewMode ' ])) {
310- $ sourceConfig ['defaultViewMode ' ] = $ postedSettings ['defaultViewMode ' ] ;
319+ if (isset ($ postedSettings ['sites ' ]) && $ postedSettings [ ' sites ' ] !== ' * ' ) {
320+ $ sourceConfig ['sites ' ] = is_array ( $ postedSettings ['sites ' ]) ? $ postedSettings [ ' sites ' ] : false ;
311321 }
312322
313- if ($ isCustom ) {
314- $ sourceConfig += [
315- 'label ' => $ postedSettings ['label ' ],
316- 'condition ' => $ conditionsService ->createCondition ($ postedSettings ['condition ' ])->getConfig (),
317- ];
318-
319- if (isset ($ postedSettings ['sites ' ]) && $ postedSettings ['sites ' ] !== '* ' ) {
320- $ sourceConfig ['sites ' ] = is_array ($ postedSettings ['sites ' ]) ? $ postedSettings ['sites ' ] : false ;
321- }
322-
323- if (isset ($ postedSettings ['userGroups ' ]) && $ postedSettings ['userGroups ' ] !== '* ' ) {
324- $ sourceConfig ['userGroups ' ] = is_array ($ postedSettings ['userGroups ' ]) ? $ postedSettings ['userGroups ' ] : false ;
325- }
326- } elseif ($ type === ElementSources::TYPE_HEADING ) {
327- $ sourceConfig ['heading ' ] = $ postedSettings ['heading ' ];
328- } elseif (isset ($ postedSettings ['enabled ' ])) {
329- $ sourceConfig ['disabled ' ] = !$ postedSettings ['enabled ' ];
330- if ($ sourceConfig ['disabled ' ]) {
331- $ disabledSourceKeys [] = $ source ['key ' ];
332- }
323+ if (isset ($ postedSettings ['userGroups ' ]) && $ postedSettings ['userGroups ' ] !== '* ' ) {
324+ $ sourceConfig ['userGroups ' ] = is_array ($ postedSettings ['userGroups ' ]) ? $ postedSettings ['userGroups ' ] : false ;
333325 }
334- } elseif (isset ($ oldSourceConfigs [$ source ['key ' ]])) {
335- $ sourceConfig += $ oldSourceConfigs [$ source ['key ' ]];
336- if (!empty ($ sourceConfig ['disabled ' ])) {
337- $ disabledSourceKeys [] = $ source ['key ' ];
326+ } elseif ($ type === ElementSources::TYPE_HEADING ) {
327+ $ sourceConfig ['heading ' ] = $ postedSettings ['heading ' ];
328+ } elseif (isset ($ postedSettings ['enabled ' ])) {
329+ $ sourceConfig ['disabled ' ] = !$ postedSettings ['enabled ' ];
330+ if ($ sourceConfig ['disabled ' ]) {
331+ $ disabledSourceKeys [] = $ key ;
338332 }
339- } elseif ($ isCustom ) {
340- // Ignore it
341- continue ;
342333 }
334+ } elseif (isset ($ oldSourceConfigs [$ key ])) {
335+ $ sourceConfig += $ oldSourceConfigs [$ key ];
336+ if (!empty ($ sourceConfig ['disabled ' ])) {
337+ $ disabledSourceKeys [] = $ key ;
338+ }
339+ } elseif ($ isCustom ) {
340+ // Ignore it
341+ continue ;
342+ }
343+
344+ $ newSourceConfigs [] = $ sourceConfig ;
343345
344- $ newSourceConfigs [] = $ sourceConfig ;
346+ if ($ multiPage ) {
347+ $ sourcePageIndexes [] = array_search ($ sourceConfig ['page ' ], array_keys ($ pageSettings ));
345348 }
346349 }
347350
351+ if ($ multiPage ) {
352+ /** @phpstan-ignore-next-line */
353+ array_multisort ($ sourcePageIndexes , SORT_NUMERIC , range (1 , count ($ newSourceConfigs )), SORT_NUMERIC , $ newSourceConfigs );
354+ }
355+
348356 $ projectConfig ->set (sprintf ('%s.%s ' , ProjectConfig::PATH_ELEMENT_SOURCES , $ elementType ), $ newSourceConfigs );
349357
350358 if ($ multiPage ) {
0 commit comments