getQuery(true)
->select('*')
->from($db->quoteName('#__eis_documents'))
->order($db->quoteName('ordering') . ' ASC');
$rows = $db->setQuery($query)->loadAssocList();
// Nach parent_id gruppieren
$grouped = [];
foreach ($rows as $row) {
$pid = $row['parent_id'] === null ? null : (int) $row['parent_id'];
$grouped[$pid][] = $row;
}
return $grouped;
}
/**
* Hauptfunktion zum Rendern des Baums
*/
public static function renderTree(array $items, int $parentId = null): string
{
if (!isset($items[$parentId])) {
return '';
}
$html = '
';
foreach ($items[$parentId] as $item) {
$isFolder = (bool) $item['is_folder'];
$title = htmlspecialchars($item['title'] ?: $item['name']);
// $path = htmlspecialchars($item['path'] ?? '');
$path = htmlspecialchars(self::convertToRelativeUrl($item['path'] ?? ''));
if ($isFolder) {
$fileCount = self::countFilesRecursive($items, $item['id']);
$html .= '- ';
$html .= '▶';
$html .= '' . $title . ' (' . $fileCount . ')';
$html .= self::renderTree($items, $item['id']);
$html .= '
';
} else {
$html .= '- ';
$html .= '' . $title . '';
$html .= '
';
}
}
$html .= '
';
return $html;
}
/**
* Konvertiert absoluten Serverpfad in URL
*/
private static function convertToRelativeUrl(string $fullPath): string
{
// <== HIER den absoluten Pfad zu deinem Webroot anpassen
$webRoot = '/var/www/vhosts/ts-it24.net/stbv.ts-it24.net';
$webBase = ''; // ggf. '/subdir' falls Joomla in Unterordner
if (str_starts_with($fullPath, $webRoot)) {
return $webBase . str_replace($webRoot, '', $fullPath);
}
return $fullPath; // Fallback: Original verwenden
}
/**
* Zählt alle PDF-Dateien unterhalb eines Ordners rekursiv
*/
private static function countFilesRecursive(array $items, int $parentId): int
{
$count = 0;
if (!isset($items[$parentId])) {
return 0;
}
foreach ($items[$parentId] as $item) {
if ((bool)$item['is_folder']) {
$count += self::countFilesRecursive($items, $item['id']);
} else {
$count++;
}
}
return $count;
}
}