11<?php
22
3- declare (strict_types=1 );
4-
53/**
64 * The Admin Configuration Tab Controller
75 *
1715 * @since 2023-10-30
1816 */
1917
18+ declare (strict_types=1 );
19+
2020namespace phpMyFAQ \Controller \Administration \Api ;
2121
2222use phpMyFAQ \Administration \Helper ;
@@ -93,6 +93,7 @@ public function save(Request $request): JsonResponse
9393
9494 $ csrfToken = $ request ->request ->get (key: 'pmf-csrf-token ' );
9595 $ configurationData = $ request ->getPayload ()->all (key: 'edit ' );
96+ $ availableFieldsJson = $ request ->request ->get (key: 'availableFields ' );
9697
9798 $ oldConfigurationData = $ this ->configuration ->getAll ();
9899
@@ -103,6 +104,15 @@ public function save(Request $request): JsonResponse
103104 return $ this ->json (['error ' => Translation::get (key: 'msgNoPermission ' )], Response::HTTP_UNAUTHORIZED );
104105 }
105106
107+ // Parse the list of available fields from the form
108+ $ availableFields = [];
109+ if ($ availableFieldsJson ) {
110+ $ availableFields = json_decode ($ availableFieldsJson , true );
111+ if (!is_array ($ availableFields )) {
112+ $ availableFields = [];
113+ }
114+ }
115+
106116 // Set the new values
107117 $ newConfigValues = [];
108118 $ escapeValues = [
@@ -152,10 +162,24 @@ public function save(Request $request): JsonResponse
152162 }
153163 }
154164
165+ // Only process fields that were available in the current form
166+ // For checkboxes: if field is available but not in configurationData, set to false
167+ // For other fields: keep original value if not in configurationData
168+ if (!empty ($ availableFields )) {
169+ foreach ($ availableFields as $ fieldKey ) {
170+ if (!array_key_exists ($ fieldKey , $ newConfigValues )) {
171+ // Field was in the form but not submitted (unchecked checkbox)
172+ if (isset ($ oldConfigurationData [$ fieldKey ]) && $ oldConfigurationData [$ fieldKey ] === 'true ' ) {
173+ $ newConfigValues [$ fieldKey ] = 'false ' ;
174+ }
175+ }
176+ }
177+ }
178+
179+ // Keep all values that were not in the available fields (from other tabs)
155180 foreach ($ oldConfigurationData as $ key => $ value ) {
156- $ newValueExists = array_key_exists ($ key , $ newConfigValues );
157- if (!$ newValueExists ) {
158- $ newConfigValues [$ key ] = $ value === 'true ' ? 'false ' : $ value ;
181+ if (!array_key_exists ($ key , $ newConfigValues )) {
182+ $ newConfigValues [$ key ] = $ value ;
159183 }
160184 }
161185
0 commit comments