87 lines
2.3 KiB
PHP
87 lines
2.3 KiB
PHP
<?php
|
|
/**
|
|
* deleteRelation.php - Löscht eine Relation
|
|
*
|
|
* Parameter (POST):
|
|
* - IDAnchor: ID des Ausgangseintrags
|
|
* - LinkingID: ID der Relation in der Linking-Tabelle
|
|
*/
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
include "db_connection.php";
|
|
|
|
$conn = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
|
|
if (mysqli_connect_errno()) {
|
|
echo json_encode(['success' => false, 'error' => 'Datenbankverbindung fehlgeschlagen']);
|
|
exit();
|
|
}
|
|
|
|
mysqli_set_charset($conn, "utf8");
|
|
|
|
$idAnchor = isset($_POST['IDAnchor']) ? intval($_POST['IDAnchor']) : 0;
|
|
$linkingId = isset($_POST['LinkingID']) ? intval($_POST['LinkingID']) : 0;
|
|
|
|
if ($linkingId === 0) {
|
|
echo json_encode(['success' => false, 'error' => 'LinkingID muss angegeben werden']);
|
|
exit();
|
|
}
|
|
|
|
// Zuerst Relation lesen für reziproke Löschung
|
|
$getSql = "SELECT IDAnchor, IDEntry, Relationtype FROM Linking WHERE ID = $linkingId";
|
|
$getResult = mysqli_query($conn, $getSql);
|
|
|
|
if (!$getResult || mysqli_num_rows($getResult) === 0) {
|
|
echo json_encode(['success' => false, 'error' => 'Relation nicht gefunden']);
|
|
mysqli_close($conn);
|
|
exit();
|
|
}
|
|
|
|
$relation = mysqli_fetch_assoc($getResult);
|
|
$targetId = $relation['IDEntry'];
|
|
$relationType = $relation['Relationtype'];
|
|
|
|
// Reziproke Relation bestimmen
|
|
$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;
|
|
}
|
|
|
|
// Relation löschen
|
|
$deleteSql = "DELETE FROM Linking WHERE ID = $linkingId";
|
|
|
|
if (mysqli_query($conn, $deleteSql)) {
|
|
$affectedRows = mysqli_affected_rows($conn);
|
|
|
|
// Reziproke Relation ebenfalls löschen
|
|
if ($reciprocalType) {
|
|
$deleteRecSql = "DELETE FROM Linking WHERE IDAnchor = $targetId AND IDEntry = " . $relation['IDAnchor'] . " AND Relationtype = '$reciprocalType'";
|
|
mysqli_query($conn, $deleteRecSql);
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Relation erfolgreich gelöscht',
|
|
'affected' => $affectedRows
|
|
]);
|
|
} else {
|
|
echo json_encode(['success' => false, 'error' => 'Fehler beim Löschen: ' . mysqli_error($conn)]);
|
|
}
|
|
|
|
mysqli_close($conn);
|
|
?>
|