2 main
This commit is contained in:
@@ -1445,6 +1445,7 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
document.getElementById('updateSchemaButton').addEventListener('click', updateSchemaDetails);
|
document.getElementById('updateSchemaButton').addEventListener('click', updateSchemaDetails);
|
||||||
|
|
||||||
document.getElementById('schemaFieldsBody').addEventListener('change', function(event) {
|
document.getElementById('schemaFieldsBody').addEventListener('change', function(event) {
|
||||||
|
|
||||||
if (event.target.classList.contains('mapping-select')) {
|
if (event.target.classList.contains('mapping-select')) {
|
||||||
let tr = event.target.closest('tr');
|
let tr = event.target.closest('tr');
|
||||||
let mappingId = event.target.getAttribute('data-id');
|
let mappingId = event.target.getAttribute('data-id');
|
||||||
@@ -1560,19 +1561,22 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
if (sourceType === 'XLS') updateXlsDropdowns();
|
if (sourceType === 'XLS') updateXlsDropdowns();
|
||||||
if (sourceType === 'API') updateJsonDropdowns();
|
if (sourceType === 'API') updateJsonDropdowns();
|
||||||
|
|
||||||
} else if (event.target.classList.contains('main-field-checkbox')) {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.target.classList.contains('main-field-checkbox')) {
|
||||||
const checkbox = event.target;
|
const checkbox = event.target;
|
||||||
const mappingId = checkbox.dataset.mappingId;
|
const mappingId = checkbox.dataset.mappingId;
|
||||||
const value = checkbox.checked ? 1 : 0;
|
const value = checkbox.checked ? 1 : 0;
|
||||||
|
|
||||||
// Se checked, deseleziona tutti gli altri visivamente
|
const checkedMainFields = document.querySelectorAll('.main-field-checkbox:checked');
|
||||||
if (checkbox.checked) {
|
|
||||||
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
if (checkedMainFields.length > 2) {
|
||||||
if (cb !== checkbox) cb.checked = false;
|
checkbox.checked = false;
|
||||||
});
|
alert('Puoi selezionare al massimo 2 campi Main.');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Salva l'aggiornamento
|
|
||||||
fetch('update_main_field.php', {
|
fetch('update_main_field.php', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@@ -1583,36 +1587,43 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
mapping_id: mappingId,
|
mapping_id: mappingId,
|
||||||
value: value
|
value: value
|
||||||
})
|
})
|
||||||
}).then(response => response.json())
|
})
|
||||||
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
if (!data.success) {
|
if (!data.success) {
|
||||||
console.error("❌ Error updating main_field:", data.message);
|
console.error("❌ Error updating main_field:", data.message);
|
||||||
checkbox.checked = !checkbox.checked;
|
|
||||||
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
||||||
cb.checked = cb.dataset.originalChecked === 'true';
|
cb.checked = cb.dataset.originalChecked === 'true';
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
alert(data.message || 'Errore durante il salvataggio del campo Main.');
|
||||||
cb.dataset.originalChecked = cb.checked;
|
return;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
||||||
|
cb.dataset.originalChecked = cb.checked ? 'true' : 'false';
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error("❌ Fetch error:", error);
|
console.error("❌ Fetch error:", error);
|
||||||
checkbox.checked = !checkbox.checked;
|
|
||||||
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
||||||
cb.checked = cb.dataset.originalChecked === 'true';
|
cb.checked = cb.dataset.originalChecked === 'true';
|
||||||
});
|
});
|
||||||
|
|
||||||
|
alert('Errore di comunicazione durante il salvataggio del campo Main.');
|
||||||
});
|
});
|
||||||
} else if (event.target.classList.contains('visible-parts-checkbox')) {
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.target.classList.contains('visible-parts-checkbox')) {
|
||||||
const checkbox = event.target;
|
const checkbox = event.target;
|
||||||
const mappingId = checkbox.dataset.mappingId;
|
const mappingId = checkbox.dataset.mappingId;
|
||||||
const value = checkbox.checked ? 1 : 0;
|
const value = checkbox.checked ? 1 : 0;
|
||||||
|
|
||||||
// salva stato per rollback
|
|
||||||
const prevChecked = checkbox.checked;
|
const prevChecked = checkbox.checked;
|
||||||
|
|
||||||
// ✅ UI: se sto mettendo a 1, tolgo la spunta a tutti gli altri SUBITO
|
|
||||||
if (value === 1) {
|
if (value === 1) {
|
||||||
document.querySelectorAll('.visible-parts-checkbox').forEach(cb => {
|
document.querySelectorAll('.visible-parts-checkbox').forEach(cb => {
|
||||||
if (cb !== checkbox) cb.checked = false;
|
if (cb !== checkbox) cb.checked = false;
|
||||||
@@ -1634,26 +1645,22 @@ $apiSampleJson = $template['api_sample_json'] ?? '';
|
|||||||
.then(data => {
|
.then(data => {
|
||||||
if (!data.success) {
|
if (!data.success) {
|
||||||
console.error("❌ Error updating is_visible_parts:", data.message);
|
console.error("❌ Error updating is_visible_parts:", data.message);
|
||||||
|
|
||||||
// rollback UI
|
|
||||||
checkbox.checked = !prevChecked;
|
checkbox.checked = !prevChecked;
|
||||||
|
|
||||||
// se avevo tolto le spunte agli altri, ricarico per riallineare la UI al DB
|
|
||||||
// (semplice e safe)
|
|
||||||
location.reload();
|
location.reload();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error("❌ Fetch error:", error);
|
console.error("❌ Fetch error:", error);
|
||||||
|
|
||||||
// rollback UI
|
|
||||||
checkbox.checked = !prevChecked;
|
checkbox.checked = !prevChecked;
|
||||||
location.reload();
|
location.reload();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Salva lo stato originale dei checkbox al caricamento
|
// Salva lo stato originale dei checkbox al caricamento
|
||||||
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
document.querySelectorAll('.main-field-checkbox').forEach(cb => {
|
||||||
cb.dataset.originalChecked = cb.checked;
|
cb.dataset.originalChecked = cb.checked;
|
||||||
|
|||||||
@@ -3,14 +3,16 @@ ini_set('display_errors', 1);
|
|||||||
ini_set('display_startup_errors', 1);
|
ini_set('display_startup_errors', 1);
|
||||||
error_reporting(E_ALL);
|
error_reporting(E_ALL);
|
||||||
|
|
||||||
include('include/headscript.php'); // Assumi che questo includa la connessione DB
|
include('include/headscript.php');
|
||||||
|
|
||||||
// Recupera il payload JSON
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
// Read JSON payload
|
||||||
$data = json_decode(file_get_contents('php://input'), true);
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
|
||||||
$template_id = intval($data['template_id']);
|
$template_id = intval($data['template_id'] ?? 0);
|
||||||
$mapping_id = intval($data['mapping_id']);
|
$mapping_id = intval($data['mapping_id'] ?? 0);
|
||||||
$value = intval($data['value']);
|
$value = intval($data['value'] ?? 0);
|
||||||
|
|
||||||
if ($template_id <= 0 || $mapping_id <= 0) {
|
if ($template_id <= 0 || $mapping_id <= 0) {
|
||||||
echo json_encode(['success' => false, 'message' => 'Invalid IDs']);
|
echo json_encode(['success' => false, 'message' => 'Invalid IDs']);
|
||||||
@@ -23,19 +25,47 @@ $pdo = $db->getConnection();
|
|||||||
try {
|
try {
|
||||||
$pdo->beginTransaction();
|
$pdo->beginTransaction();
|
||||||
|
|
||||||
|
// If user is trying to activate a Main field, check current number of active Main fields
|
||||||
if ($value === 1) {
|
if ($value === 1) {
|
||||||
// Setta tutti main_field a 0 per questo template
|
$stmt = $pdo->prepare("
|
||||||
$stmt = $pdo->prepare("UPDATE template_mapping SET main_field = 0 WHERE template_id = ?");
|
SELECT COUNT(*)
|
||||||
$stmt->execute([$template_id]);
|
FROM template_mapping
|
||||||
|
WHERE template_id = ?
|
||||||
|
AND main_field = 1
|
||||||
|
AND id <> ?
|
||||||
|
");
|
||||||
|
$stmt->execute([$template_id, $mapping_id]);
|
||||||
|
$currentMainCount = (int)$stmt->fetchColumn();
|
||||||
|
|
||||||
|
if ($currentMainCount >= 2) {
|
||||||
|
$pdo->rollBack();
|
||||||
|
echo json_encode([
|
||||||
|
'success' => false,
|
||||||
|
'message' => 'Maximum 2 Main fields allowed'
|
||||||
|
]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setta il valore per questo mapping
|
// Update only this mapping
|
||||||
$stmt = $pdo->prepare("UPDATE template_mapping SET main_field = ? WHERE id = ? AND template_id = ?");
|
$stmt = $pdo->prepare("
|
||||||
|
UPDATE template_mapping
|
||||||
|
SET main_field = ?
|
||||||
|
WHERE id = ?
|
||||||
|
AND template_id = ?
|
||||||
|
");
|
||||||
$stmt->execute([$value, $mapping_id, $template_id]);
|
$stmt->execute([$value, $mapping_id, $template_id]);
|
||||||
|
|
||||||
$pdo->commit();
|
$pdo->commit();
|
||||||
|
|
||||||
echo json_encode(['success' => true]);
|
echo json_encode(['success' => true]);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
$pdo->rollBack();
|
if ($pdo->inTransaction()) {
|
||||||
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
$pdo->rollBack();
|
||||||
|
}
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
'success' => false,
|
||||||
|
'message' => $e->getMessage()
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user