diff --git a/public/userarea/mapping_template_xls_scheme2.php b/public/userarea/mapping_template_xls_scheme2.php index 544aed2..dd845a9 100644 --- a/public/userarea/mapping_template_xls_scheme2.php +++ b/public/userarea/mapping_template_xls_scheme2.php @@ -1569,9 +1569,14 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; const mappingId = checkbox.dataset.mappingId; const value = checkbox.checked ? 1 : 0; - const checkedMainFields = document.querySelectorAll('.main-field-checkbox:checked'); + // Count only the other Main fields already checked in this table + const otherCheckedMainFields = Array.from( + document.querySelectorAll('#schemaFieldsBody .main-field-checkbox') + ).filter(cb => cb !== checkbox && cb.checked); - if (checkedMainFields.length > 2) { + // If I am checking this one, I can have max 2 total: + // this checkbox + max 1 other already checked + if (checkbox.checked && otherCheckedMainFields.length >= 2) { checkbox.checked = false; alert('Puoi selezionare al massimo 2 campi Main.'); return; @@ -1593,7 +1598,7 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; if (!data.success) { console.error("❌ Error updating main_field:", data.message); - document.querySelectorAll('.main-field-checkbox').forEach(cb => { + document.querySelectorAll('#schemaFieldsBody .main-field-checkbox').forEach(cb => { cb.checked = cb.dataset.originalChecked === 'true'; }); @@ -1601,14 +1606,14 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; return; } - document.querySelectorAll('.main-field-checkbox').forEach(cb => { + document.querySelectorAll('#schemaFieldsBody .main-field-checkbox').forEach(cb => { cb.dataset.originalChecked = cb.checked ? 'true' : 'false'; }); }) .catch(error => { console.error("❌ Fetch error:", error); - document.querySelectorAll('.main-field-checkbox').forEach(cb => { + document.querySelectorAll('#schemaFieldsBody .main-field-checkbox').forEach(cb => { cb.checked = cb.dataset.originalChecked === 'true'; }); @@ -1661,9 +1666,9 @@ $apiSampleJson = $template['api_sample_json'] ?? ''; }); - // Salva lo stato originale dei checkbox al caricamento - document.querySelectorAll('.main-field-checkbox').forEach(cb => { - cb.dataset.originalChecked = cb.checked; + // Save original Main checkbox state + document.querySelectorAll('#schemaFieldsBody .main-field-checkbox').forEach(cb => { + cb.dataset.originalChecked = cb.checked ? 'true' : 'false'; }); // AUTO VALUE select change -> save auto_value diff --git a/public/userarea/update_main_field.php b/public/userarea/update_main_field.php index 127d6d2..302c665 100644 --- a/public/userarea/update_main_field.php +++ b/public/userarea/update_main_field.php @@ -7,13 +7,15 @@ include('include/headscript.php'); header('Content-Type: application/json'); -// Read JSON payload $data = json_decode(file_get_contents('php://input'), true); $template_id = intval($data['template_id'] ?? 0); $mapping_id = intval($data['mapping_id'] ?? 0); $value = intval($data['value'] ?? 0); +// IMPORTANT: main_field may be ENUM('0','1'), so use string values +$mainValue = ($value === 1) ? '1' : '0'; + if ($template_id <= 0 || $mapping_id <= 0) { echo json_encode(['success' => false, 'message' => 'Invalid IDs']); exit; @@ -25,13 +27,12 @@ $pdo = $db->getConnection(); try { $pdo->beginTransaction(); - // If user is trying to activate a Main field, check current number of active Main fields - if ($value === 1) { + if ($mainValue === '1') { $stmt = $pdo->prepare(" - SELECT COUNT(*) - FROM template_mapping - WHERE template_id = ? - AND main_field = 1 + SELECT COUNT(*) + FROM template_mapping + WHERE template_id = ? + AND main_field = '1' AND id <> ? "); $stmt->execute([$template_id, $mapping_id]); @@ -39,22 +40,23 @@ try { if ($currentMainCount >= 2) { $pdo->rollBack(); + echo json_encode([ 'success' => false, - 'message' => 'Maximum 2 Main fields allowed' + 'message' => 'Maximum 2 Main fields allowed', + 'currentMainCount' => $currentMainCount ]); exit; } } - // Update only this mapping $stmt = $pdo->prepare(" - UPDATE template_mapping - SET main_field = ? - WHERE id = ? + UPDATE template_mapping + SET main_field = ? + WHERE id = ? AND template_id = ? "); - $stmt->execute([$value, $mapping_id, $template_id]); + $stmt->execute([$mainValue, $mapping_id, $template_id]); $pdo->commit();