false, 'error' => 'Unauthorized']); exit; } if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['success' => false, 'error' => 'Method not allowed']); exit; } $kind = ($_POST['kind'] ?? 'custom') === 'fixed' ? 'fixed' : 'custom'; $mappingId = intval($_POST['mapping_id'] ?? 0); $sourceValue = (string) ($_POST['source_value'] ?? ''); $datadbIds = []; if (isset($_POST['datadb_ids'])) { $decoded = json_decode($_POST['datadb_ids'], true); if (is_array($decoded)) { $datadbIds = $decoded; } } if ($mappingId <= 0 || trim($sourceValue) === '') { http_response_code(422); echo json_encode(['success' => false, 'error' => 'Missing required parameters']); exit; } try { $pdo = DBHandlerSelect::getInstance()->getConnection(); if ($kind === 'fixed') { $stmt = $pdo->prepare("SELECT fixed_field_key FROM template_fixed_mapping WHERE id = ?"); $stmt->execute([$mappingId]); $fixedKey = (string) ($stmt->fetchColumn() ?: ''); if ($fixedKey === '') { http_response_code(422); echo json_encode(['success' => false, 'error' => 'Invalid fixed field']); exit; } $cleared = !empty($datadbIds) ? binding_apply_to_datadb($pdo, $fixedKey, null, $datadbIds) : 0; } else { $cleared = !empty($datadbIds) ? binding_apply_to_details($pdo, $mappingId, '', $datadbIds) : 0; } binding_delete($pdo, $mappingId, $sourceValue, $kind); echo json_encode(['success' => true, 'cleared_rows' => $cleared]); } catch (Exception $e) { http_response_code(500); echo json_encode(['success' => false, 'error' => $e->getMessage()]); }