400, 'message' => 'Bitte Text eingeben!']); exit(); } /** * Ermittelt die Eltern-Notation durch Abschneiden des letzten Segments. * * Beispiele: * "T 4.10" → "T 4" * "G 1.1.1" → "G 1.1" * "G 1.1" → "G 1" * "G 1" → "G" * "G" → null (Root-Ebene) */ function getParentNotation($notation) { $lastDot = strrpos($notation, '.'); if ($lastDot !== false) { return substr($notation, 0, $lastDot); } $lastSpace = strrpos($notation, ' '); if ($lastSpace !== false) { return substr($notation, 0, $lastSpace); } return null; // Root } /** * Sucht die Treeview-ID für eine gegebene Notation. * Die Notation steht am Anfang des name-Feldes, gefolgt von einem Leerzeichen * oder ist identisch mit dem name-Feld (bei Blatt-Knoten ohne Titel). * * @param mysqli $conn * @param string $notation z.B. "T 4" * @return int Treeview-ID oder 0 wenn nicht gefunden */ function findTreeviewIdByNotation($conn, $notation) { $escaped = mysqli_real_escape_string($conn, $notation); // Entweder exakter Match oder Notation + Leerzeichen am Anfang $sql = "SELECT id FROM Treeview WHERE name = '$escaped' OR name LIKE '$escaped %' ORDER BY id LIMIT 1"; $res = mysqli_query($conn, $sql); if ($res) { $row = mysqli_fetch_assoc($res); if ($row) return intval($row['id']); } return 0; } try { $crud = new CRUD(); // Normalisierten Descriptor erzeugen $desc = $crud->prepare_desc($term); $completetext = $term; // Dubletten-Prüfung (normalisiert) $existing = $crud->checkDuplicateByNormalizedText('Classification', $desc); if ($existing > 0) { echo json_encode(['status' => 409, 'message' => 'Eine Klassifikation mit diesem Text existiert bereits']); exit(); } // Neuen Eintrag in Anchor + Entry erstellen $result = $crud->insertNewTerm('Classification', $term, $desc, $detailType, $classification, $scopenote, $completetext); // --- Treeview-Eintrag anlegen --- $conn = mysqli_connect(HOST, USER, PASSWORD, DATABASE); if (!mysqli_connect_errno()) { mysqli_set_charset($conn, "utf8"); // Parent-ID ermitteln $parentId = 0; // Default: Root if (!empty($classification)) { $parentNotation = getParentNotation($classification); if ($parentNotation !== null) { $foundId = findTreeviewIdByNotation($conn, $parentNotation); if ($foundId > 0) { $parentId = $foundId; } // Hinweis: wenn Parent nicht gefunden → landet im Root (parent_id = 0) } } // name und text = vollständiger Term (Notation + Bezeichnung), link = '#' $nameEscaped = mysqli_real_escape_string($conn, $term); $insertSql = "INSERT INTO Treeview (name, text, link, parent_id) VALUES ('$nameEscaped', '$nameEscaped', '#', '$parentId')"; mysqli_query($conn, $insertSql); $treeviewId = mysqli_insert_id($conn); mysqli_close($conn); } echo json_encode([ 'status' => 200, 'message' => 'Klassifikation erfolgreich erstellt', 'Anchor' => $result['IDAnchor'], 'Entry' => $result['IDEntry'], 'Linking' => $result['IDLinking'], 'TreeviewID' => $treeviewId ?? 0 ]); } catch (Exception $e) { echo json_encode(['status' => 500, 'message' => $e->getMessage()]); } ?>