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 = self::convertToRelativeUrl($item['path'] ?? '');
// Erweiterung .pdf im Titel entfernen (nur Anzeige, nicht Link)
$displayTitle = preg_replace('/\.pdf$/i', '', $title);
if ($isFolder) {
$fileCount = self::countFilesRecursive($items, $item['id']);
$html .= '- ';
$html .= '▶';
$html .= '📁 ' . $displayTitle . ' (' . $fileCount . ')';
$html .= self::renderTree($items, $item['id']);
$html .= '
';
} else {
$html .= '- ';
$html .= '📄' . $displayTitle . '';
$html .= '
';
}
}
$html .= '
';
return $html;
}
/**
* Konvertiert absoluten Serverpfad in URL
*/
private static function convertToRelativeUrl(string $fullPath): string
{
// Absoluter Serverpfad zum Joomla-Root
$webRoot = '/var/www/joomla';
$webBase = ''; // ggf. '/unterordner', wenn Joomla in Subdir
if (str_starts_with($fullPath, $webRoot)) {
$relativePath = str_replace($webRoot, '', $fullPath);
// Leerzeichen und Sonderzeichen escapen
$relativePath = implode('/', array_map('rawurlencode', explode('/', $relativePath)));
return rtrim(\JUri::root(), '/') . $webBase . $relativePath;
}
return $fullPath; // Fallback
}
/**
* 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;
}
}