trf_certest/public/userarea/update_visible_parts.php

58 lines
1.6 KiB
PHP

<?php
require_once 'include/headscript.php';
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
$template_id = isset($data['template_id']) ? (int)$data['template_id'] : 0;
$mapping_id = isset($data['mapping_id']) ? (int)$data['mapping_id'] : 0;
$value = isset($data['value']) ? (int)$data['value'] : null;
// Validate input
if ($template_id <= 0 || $mapping_id <= 0 || $value === null) {
echo json_encode(['success' => false, 'message' => 'Invalid input']);
exit;
}
// Normalize to 0/1
$value = ($value === 1) ? 1 : 0;
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
try {
$pdo->beginTransaction();
if ($value === 1) {
// 1) Force ONLY ONE row visible_parts per template:
// set all to 0 for this template
$stmtReset = $pdo->prepare("
UPDATE template_mapping
SET is_visible_parts = 0
WHERE template_id = ?
");
$stmtReset->execute([$template_id]);
}
// 2) Set requested mapping to 1 or 0
$stmtSet = $pdo->prepare("
UPDATE template_mapping
SET is_visible_parts = ?
WHERE id = ? AND template_id = ?
");
$stmtSet->execute([$value, $mapping_id, $template_id]);
$pdo->commit();
echo json_encode([
'success' => true,
'template_id' => $template_id,
'mapping_id' => $mapping_id,
'value' => $value
]);
} catch (Exception $e) {
if ($pdo->inTransaction()) $pdo->rollBack();
echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}