getQuery(true)
->select('*')
->from($db->quoteName('#__eis_documents'))
->order($db->quoteName('ordering') . ' ASC');
$rows = $db->setQuery($query)->loadAssocList();
// Nach parent_id gruppieren und alphabetisch sortieren
$grouped = [];
foreach ($rows as $row) {
$pid = $row['parent_id'] === null ? null : (int) $row['parent_id'];
$grouped[$pid][] = $row;
}
// Sortieren nach name (case-insensitive)
foreach ($grouped as &$group) {
usort($group, fn($a, $b) => strcasecmp($a['name'], $b['name']));
}
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'];
$rawName = $item['title'] ?: $item['name'];
$fileId = (int) $item['id'];
// Dateiendung .pdf entfernen und escapen
$displayName = preg_replace('/\.pdf$/i', '', $rawName);
$displayName = htmlspecialchars($displayName, ENT_QUOTES, 'UTF-8');
if ($isFolder) {
$fileCount = self::countFilesRecursive($items, $item['id']);
// WICHTIG: Toggle-Element einfügen – dein JS hängt hier dran
$html .= '- ';
$html .= '▶ ';
$html .= '📁 ' . $displayName . ' (' . (int) $fileCount . ')';
// Kindknoten
$html .= self::renderTree($items, $item['id']);
$html .= '
';
} else {
$link = Route::_('index.php?option=com_eis&task=download.download&id=' . $fileId);
// Tooltip + (neu) inline-Anzeige der Dateigröße, falls der Pfad existiert
$tooltip = '';
$sizeStr = '';
if (!empty($item['path']) && is_file($item['path'])) {
$size = @filesize($item['path']);
if ($size !== false) {
$formatted = self::formatFileSize((int) $size);
$tooltip = ' title="Größe: ' . $formatted . '"';
// Sichtbar neben dem Dateinamen anzeigen
//$sizeStr = ' (' . $formatted . ')';
}
}
$html .= '- ';
//$html .= '📄 ' . $displayName . '' . $sizeStr;
$html .= '📄 ' . $displayName . '';
$html .= '
';
}
}
$html .= '
';
return $html;
}
/**
* 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;
}
/**
* Formatierte Dateigröße für Tooltip
*/
private static function formatFileSize(int $bytes): string
{
if ($bytes >= 1073741824) {
return number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
return number_format($bytes / 1048576, 1) . ' MB';
} elseif ($bytes >= 1024) {
return number_format($bytes / 1024, 0) . ' KB';
} else {
return $bytes . ' B';
}
}
}