fixed main field

This commit is contained in:
2026-05-14 10:00:47 +02:00
parent 574ddbbd32
commit 6460454201
2 changed files with 28 additions and 21 deletions
+15 -13
View File
@@ -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();