Skip to content

Commit a0f8027

Browse files
committed
fix: corrected weird behaviour that causes loss of configurations
1 parent 4e952e9 commit a0f8027

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

phpmyfaq/admin/assets/src/configuration/configuration.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@ export const handleSaveConfiguration = async (): Promise<void> => {
9595
const form = document.getElementById('configuration-list') as HTMLFormElement;
9696
const formData = new FormData(form);
9797

98+
// Collect all configuration field names that are currently in the form
99+
const availableFields: string[] = [];
100+
const inputs = form.querySelectorAll('input, select, textarea');
101+
inputs.forEach((input: Element): void => {
102+
const name = (input as HTMLInputElement).name;
103+
if (name && name.startsWith('edit[')) {
104+
// Extract the config key from a name like "edit[main.language]"
105+
const match = name.match(/edit\[([^\]]+)\]/);
106+
if (match) {
107+
availableFields.push(match[1]);
108+
}
109+
}
110+
});
111+
112+
// Add the available fields list to the form data
113+
formData.append('availableFields', JSON.stringify(availableFields));
114+
98115
const response = (await saveConfiguration(formData)) as unknown as Response;
99116

100117
if (typeof response.success === 'string') {

phpmyfaq/src/phpMyFAQ/Controller/Administration/Api/ConfigurationTabController.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
/**
64
* The Admin Configuration Tab Controller
75
*
@@ -17,6 +15,8 @@
1715
* @since 2023-10-30
1816
*/
1917

18+
declare(strict_types=1);
19+
2020
namespace phpMyFAQ\Controller\Administration\Api;
2121

2222
use 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

Comments
 (0)