Thesaurus/ajax/getTreeData.php
2026-02-23 16:11:35 +01:00

76 lines
2.0 KiB
PHP

<?php
/**
* getTreeData.php - Liefert Klassifikationsdaten im jsTree-Format
*
* Nutzt die bestehende Treeview-Tabelle
*
* Parameter:
* - id: Parent-ID (# oder 0 für Root-Ebene)
*
* Rückgabe: JSON-Array im jsTree-Format
*/
header('Content-Type: application/json; charset=utf-8');
// Datenbankverbindung
include "db_connection.php";
$conn = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
if (mysqli_connect_errno()) {
echo json_encode([]);
exit();
}
mysqli_set_charset($conn, "utf8");
// Parameter auslesen
$parentId = isset($_GET['id']) ? $_GET['id'] : '#';
// Root-Ebene: parent_id ist NULL oder 0
if ($parentId === '#' || $parentId === '0' || $parentId === 0) {
$sql = "SELECT id, name, text, link as href, parent_id
FROM Treeview
WHERE parent_id IS NULL OR parent_id = 0 OR parent_id = ''
ORDER BY text";
} else {
// Unterebene: Kinder des angegebenen Parents
$parentId = intval($parentId);
$sql = "SELECT id, name, text, link as href, parent_id
FROM Treeview
WHERE parent_id = $parentId
ORDER BY text";
}
$res = mysqli_query($conn, $sql);
if (!$res) {
echo json_encode([]);
exit();
}
$result = [];
while ($row = mysqli_fetch_assoc($res)) {
// Prüfen ob dieser Knoten Kinder hat
$childCheckSql = "SELECT COUNT(*) as cnt FROM Treeview WHERE parent_id = " . intval($row['id']);
$childRes = mysqli_query($conn, $childCheckSql);
$childRow = mysqli_fetch_assoc($childRes);
$hasChildren = ($childRow['cnt'] > 0);
$result[] = [
'id' => strval($row['id']),
'text' => $row['text'] ? $row['text'] : $row['name'],
'children' => $hasChildren,
'data' => [
'name' => $row['name'],
'href' => $row['href']
],
'icon' => $hasChildren ? 'fas fa-folder text-warning' : 'fas fa-file text-secondary'
];
}
mysqli_close($conn);
echo json_encode($result, JSON_UNESCAPED_UNICODE);
?>