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()]); }