124 lines
5.1 KiB
PHP
124 lines
5.1 KiB
PHP
<?php
|
|
/**
|
|
* saveSynonym.php - Speichert ein neues Synonym
|
|
*
|
|
* Parameter:
|
|
* - text1: Erster Begriff (bereits existierend)
|
|
* - text2: Zweiter Begriff (neues Synonym)
|
|
* - action: 'add' oder 'remove'
|
|
*/
|
|
|
|
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");
|
|
|
|
$text1 = isset($_POST['text1']) ? trim($_POST['text1']) : '';
|
|
$text2 = isset($_POST['text2']) ? trim($_POST['text2']) : '';
|
|
$action = isset($_POST['action']) ? $_POST['action'] : 'add';
|
|
|
|
if (empty($text1) || empty($text2)) {
|
|
echo json_encode(['success' => false, 'error' => 'Beide Begriffe müssen angegeben werden']);
|
|
exit();
|
|
}
|
|
|
|
// Descriptor erstellen (Kleinbuchstaben, Leerzeichen durch Unterstrich)
|
|
function prepare_desc($text) {
|
|
$desc = strtolower($text);
|
|
$desc = str_replace(' ', '_', $desc);
|
|
return $desc;
|
|
}
|
|
|
|
$desc1 = prepare_desc($text1);
|
|
$desc2 = prepare_desc($text2);
|
|
|
|
if ($action === 'add') {
|
|
// Prüfen ob text1 bereits eine IDLinking hat
|
|
$sql = "SELECT IDLinking FROM Synonyms WHERE Descriptor = '" . mysqli_real_escape_string($conn, $desc1) . "' LIMIT 1";
|
|
$res = mysqli_query($conn, $sql);
|
|
$idLinking = null;
|
|
|
|
if ($res && mysqli_num_rows($res) > 0) {
|
|
$row = mysqli_fetch_assoc($res);
|
|
$idLinking = $row['IDLinking'];
|
|
}
|
|
|
|
// Prüfen ob text2 bereits eine IDLinking hat
|
|
$sql2 = "SELECT IDLinking FROM Synonyms WHERE Descriptor = '" . mysqli_real_escape_string($conn, $desc2) . "' LIMIT 1";
|
|
$res2 = mysqli_query($conn, $sql2);
|
|
$idLinking2 = null;
|
|
|
|
if ($res2 && mysqli_num_rows($res2) > 0) {
|
|
$row2 = mysqli_fetch_assoc($res2);
|
|
$idLinking2 = $row2['IDLinking'];
|
|
}
|
|
|
|
// Fall 1: Beide haben noch keine IDLinking - neue Gruppe erstellen
|
|
if ($idLinking === null && $idLinking2 === null) {
|
|
// Höchste IDLinking finden und +1
|
|
$sqlMax = "SELECT MAX(IDLinking) as maxId FROM Synonyms";
|
|
$resMax = mysqli_query($conn, $sqlMax);
|
|
$rowMax = mysqli_fetch_assoc($resMax);
|
|
$newIdLinking = ($rowMax['maxId'] ?? 0) + 1;
|
|
|
|
// Beide Begriffe einfügen
|
|
$sqlInsert1 = "INSERT INTO Synonyms (IDLinking, Text, Descriptor) VALUES ($newIdLinking, '" . mysqli_real_escape_string($conn, $text1) . "', '" . mysqli_real_escape_string($conn, $desc1) . "')";
|
|
$sqlInsert2 = "INSERT INTO Synonyms (IDLinking, Text, Descriptor) VALUES ($newIdLinking, '" . mysqli_real_escape_string($conn, $text2) . "', '" . mysqli_real_escape_string($conn, $desc2) . "')";
|
|
|
|
mysqli_query($conn, $sqlInsert1);
|
|
mysqli_query($conn, $sqlInsert2);
|
|
|
|
echo json_encode(['success' => true, 'message' => 'Neue Synonym-Gruppe erstellt', 'idLinking' => $newIdLinking]);
|
|
}
|
|
// Fall 2: text1 hat bereits eine IDLinking - text2 zur Gruppe hinzufügen
|
|
else if ($idLinking !== null && $idLinking2 === null) {
|
|
$sqlInsert = "INSERT INTO Synonyms (IDLinking, Text, Descriptor) VALUES ($idLinking, '" . mysqli_real_escape_string($conn, $text2) . "', '" . mysqli_real_escape_string($conn, $desc2) . "')";
|
|
mysqli_query($conn, $sqlInsert);
|
|
|
|
echo json_encode(['success' => true, 'message' => 'Synonym zur bestehenden Gruppe hinzugefügt', 'idLinking' => $idLinking]);
|
|
}
|
|
// Fall 3: text2 hat bereits eine IDLinking - text1 zur Gruppe hinzufügen
|
|
else if ($idLinking === null && $idLinking2 !== null) {
|
|
$sqlInsert = "INSERT INTO Synonyms (IDLinking, Text, Descriptor) VALUES ($idLinking2, '" . mysqli_real_escape_string($conn, $text1) . "', '" . mysqli_real_escape_string($conn, $desc1) . "')";
|
|
mysqli_query($conn, $sqlInsert);
|
|
|
|
echo json_encode(['success' => true, 'message' => 'Begriff zur bestehenden Synonym-Gruppe hinzugefügt', 'idLinking' => $idLinking2]);
|
|
}
|
|
// Fall 4: Beide haben bereits IDLinking - Gruppen zusammenführen
|
|
else {
|
|
if ($idLinking == $idLinking2) {
|
|
echo json_encode(['success' => false, 'error' => 'Diese Begriffe sind bereits Synonyme']);
|
|
} else {
|
|
// Alle Einträge von idLinking2 auf idLinking umstellen
|
|
$sqlUpdate = "UPDATE Synonyms SET IDLinking = $idLinking WHERE IDLinking = $idLinking2";
|
|
mysqli_query($conn, $sqlUpdate);
|
|
|
|
echo json_encode(['success' => true, 'message' => 'Synonym-Gruppen zusammengeführt', 'idLinking' => $idLinking]);
|
|
}
|
|
}
|
|
}
|
|
else if ($action === 'remove') {
|
|
// Synonym entfernen (nur den einen Eintrag löschen)
|
|
$sqlDelete = "DELETE FROM Synonyms WHERE Descriptor = '" . mysqli_real_escape_string($conn, $desc2) . "'";
|
|
mysqli_query($conn, $sqlDelete);
|
|
|
|
if (mysqli_affected_rows($conn) > 0) {
|
|
echo json_encode(['success' => true, 'message' => 'Synonym entfernt']);
|
|
} else {
|
|
echo json_encode(['success' => false, 'error' => 'Synonym nicht gefunden']);
|
|
}
|
|
}
|
|
else {
|
|
echo json_encode(['success' => false, 'error' => 'Unbekannte Aktion']);
|
|
}
|
|
|
|
mysqli_close($conn);
|
|
?>
|