2 main
This commit is contained in:
@@ -3,14 +3,16 @@ ini_set('display_errors', 1);
|
||||
ini_set('display_startup_errors', 1);
|
||||
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);
|
||||
|
||||
$template_id = intval($data['template_id']);
|
||||
$mapping_id = intval($data['mapping_id']);
|
||||
$value = intval($data['value']);
|
||||
$template_id = intval($data['template_id'] ?? 0);
|
||||
$mapping_id = intval($data['mapping_id'] ?? 0);
|
||||
$value = intval($data['value'] ?? 0);
|
||||
|
||||
if ($template_id <= 0 || $mapping_id <= 0) {
|
||||
echo json_encode(['success' => false, 'message' => 'Invalid IDs']);
|
||||
@@ -23,19 +25,47 @@ $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) {
|
||||
// Setta tutti main_field a 0 per questo template
|
||||
$stmt = $pdo->prepare("UPDATE template_mapping SET main_field = 0 WHERE template_id = ?");
|
||||
$stmt->execute([$template_id]);
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT COUNT(*)
|
||||
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
|
||||
$stmt = $pdo->prepare("UPDATE template_mapping SET main_field = ? WHERE id = ? AND template_id = ?");
|
||||
// Update only this mapping
|
||||
$stmt = $pdo->prepare("
|
||||
UPDATE template_mapping
|
||||
SET main_field = ?
|
||||
WHERE id = ?
|
||||
AND template_id = ?
|
||||
");
|
||||
$stmt->execute([$value, $mapping_id, $template_id]);
|
||||
|
||||
$pdo->commit();
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
} catch (Exception $e) {
|
||||
$pdo->rollBack();
|
||||
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
|
||||
if ($pdo->inTransaction()) {
|
||||
$pdo->rollBack();
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user