false, 'error' => 'Datenbankverbindung fehlgeschlagen']); exit(); } mysqli_set_charset($conn, "utf8"); // Parameter aus JavaScript (AnchorID, relationType, relationID) $idAnchor = isset($_POST['AnchorID']) ? intval($_POST['AnchorID']) : 0; if ($idAnchor === 0) { $idAnchor = isset($_POST['IDAnchor']) ? intval($_POST['IDAnchor']) : 0; } $idEntry = isset($_POST['relationID']) ? intval($_POST['relationID']) : 0; if ($idEntry === 0) { $idEntry = isset($_POST['IDEntry']) ? intval($_POST['IDEntry']) : 0; } $relationtype = isset($_POST['relationType']) ? trim($_POST['relationType']) : ''; if (empty($relationtype)) { $relationtype = isset($_POST['Relationtype']) ? trim($_POST['Relationtype']) : ''; } if ($idAnchor === 0 || $idEntry === 0) { echo json_encode(['success' => false, 'error' => 'IDAnchor und IDEntry müssen angegeben werden']); exit(); } if (empty($relationtype)) { echo json_encode(['success' => false, 'error' => 'Relationtype muss angegeben werden']); exit(); } // Erlaubte Relationstypen $allowedTypes = ['BT', 'NT', 'RT', 'USE', 'UF']; if (!in_array($relationtype, $allowedTypes)) { echo json_encode(['success' => false, 'error' => 'Ungültiger Relationstyp']); exit(); } // Prüfen ob Relation bereits existiert $checkSql = "SELECT ID FROM Linking WHERE IDAnchor = $idAnchor AND IDEntry = $idEntry AND Relationtype = '" . mysqli_real_escape_string($conn, $relationtype) . "'"; $checkResult = mysqli_query($conn, $checkSql); if ($checkResult && mysqli_num_rows($checkResult) > 0) { echo json_encode(['success' => false, 'error' => 'Diese Relation existiert bereits']); mysqli_close($conn); exit(); } // Relation einfügen $insertSql = "INSERT INTO Linking (IDAnchor, IDEntry, Relationtype) VALUES ($idAnchor, $idEntry, '" . mysqli_real_escape_string($conn, $relationtype) . "')"; if (mysqli_query($conn, $insertSql)) { $newId = mysqli_insert_id($conn); // Reziproke Relation erstellen (BT <-> NT, RT <-> RT) $reciprocalType = null; switch ($relationtype) { case 'BT': $reciprocalType = 'NT'; break; case 'NT': $reciprocalType = 'BT'; break; case 'RT': $reciprocalType = 'RT'; break; case 'USE': $reciprocalType = 'UF'; break; case 'UF': $reciprocalType = 'USE'; break; } if ($reciprocalType) { // Prüfen ob reziproke Relation schon existiert $checkRecSql = "SELECT ID FROM Linking WHERE IDAnchor = $idEntry AND IDEntry = $idAnchor AND Relationtype = '$reciprocalType'"; $checkRecResult = mysqli_query($conn, $checkRecSql); if (!$checkRecResult || mysqli_num_rows($checkRecResult) === 0) { $insertRecSql = "INSERT INTO Linking (IDAnchor, IDEntry, Relationtype) VALUES ($idEntry, $idAnchor, '$reciprocalType')"; mysqli_query($conn, $insertRecSql); } } echo json_encode([ 'success' => true, 'message' => 'Relation erfolgreich erstellt', 'id' => $newId ]); } else { echo json_encode(['success' => false, 'error' => 'Fehler beim Speichern: ' . mysqli_error($conn)]); } mysqli_close($conn); ?>