getQuery(true)
->select('*')
->from($db->quoteName('#__eis_documents'))
->order($db->quoteName('ordering') . ' ASC');
$rows = $db->setQuery($query)->loadAssocList();
$grouped = [];
foreach ($rows as $row) {
$pid = $row['parent_id'] === null ? null : (int) $row['parent_id'];
$grouped[$pid][] = $row;
}
// alphabetisch nach name
foreach ($grouped as &$group) {
usort($group, static fn($a, $b) => strcasecmp((string)$a['name'], (string)$b['name']));
}
return $grouped;
}
/**
* Kombiniert: Virtueller Ordner "Neue Dokumente" (falls $newIds vorhanden) + normaler Baum
*/
public static function renderTreeWithNew(array $items, array $newIds = []): string
{
$html = '';
// 1) Virtueller Ordner "Neue Dokumente"
$newFiles = self::collectFilesByIds($items, $newIds);
if (!empty($newFiles)) {
$label = Text::_('COM_EIS_VIRTUAL_NEW') ?: 'Neue Dokumente';
$html .= '
';
$html .= '- ';
$html .= '▼ ';
$html .= '🆕 ' . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . ' (' . (int) count($newFiles) . ')';
// Kinder sichtbar
$html .= '
';
foreach ($newFiles as $row) {
$html .= self::renderSingleFileLi($row);
}
$html .= '
';
$html .= ' ';
$html .= '
';
}
// 2) Normaler Baum
$html .= self::renderTree($items, null);
return $html;
}
/** Baum rendern (nutzt title als alternativen Anzeigenamen) */
public static function renderTree(array $items, ?int $parentId = null): string
{
if (!isset($items[$parentId])) {
return '';
}
$html = '';
foreach ($items[$parentId] as $item) {
$isFolder = !empty($item['is_folder']);
$rawName = (string)($item['title'] ?? '') !== '' ? (string)$item['title'] : (string)$item['name'];
$display = preg_replace('/\.pdf$/i', '', $rawName);
$display = htmlspecialchars((string)$display, ENT_QUOTES, 'UTF-8');
$id = (int)$item['id'];
if ($isFolder) {
$fileCount = self::countFilesRecursive($items, $id);
$html .= '- '
. '▶ '
. '📁 ' . $display . ' (' . (int)$fileCount . ')'
. self::renderTree($items, $id)
. '
';
} else {
$html .= self::renderSingleFileLi($item, $display);
}
}
$html .= '
';
return $html;
}
/** Einzelnes für Datei (wird auch vom virtuellen Ordner genutzt) */
private static function renderSingleFileLi(array $item, ?string $displayAlreadyEscaped = null): string
{
$rawName = (string)($item['title'] ?? '') !== '' ? (string)$item['title'] : (string)$item['name'];
$display = $displayAlreadyEscaped ?? htmlspecialchars(preg_replace('/\.pdf$/i', '', $rawName), ENT_QUOTES, 'UTF-8');
$id = (int)$item['id'];
// Dateigröße ermitteln (optional)
$sizeStr = '';
$sizeBytes = '';
if (!empty($item['path']) && is_file((string)$item['path'])) {
$bytes = @filesize((string)$item['path']); // @ unterdrückt Warnungen bei Zugriffsproblemen
if ($bytes !== false) {
$sizeBytes = (string)(int)$bytes;
$sizeStr = self::formatFileSize((int)$bytes);
}
}
$html = ''
. '📄 ' . $display
. ($sizeStr !== '' ? ' (' . $sizeStr . ')' : '')
. ''
. '';
return $html;
}
/** Dateien zu gegebenen IDs einsammeln (nur is_folder = 0) */
private static function collectFilesByIds(array $items, array $ids): array
{
if (empty($ids)) {
return [];
}
$ids = array_map('intval', $ids);
// Flat-Index aller Rows
$idx = [];
foreach ($items as $group) {
foreach ($group as $row) {
$idx[(int)$row['id']] = $row;
}
}
$out = [];
foreach ($ids as $id) {
if (isset($idx[$id]) && empty($idx[$id]['is_folder'])) {
$out[] = $idx[$id];
}
}
return $out;
}
private static function countFilesRecursive(array $items, int $parentId): int
{
$count = 0;
if (!isset($items[$parentId])) {
return 0;
}
foreach ($items[$parentId] as $item) {
if (!empty($item['is_folder'])) {
$count += self::countFilesRecursive($items, (int)$item['id']);
} else {
$count++;
}
}
return $count;
}
private static function formatFileSize(int $bytes): string
{
if ($bytes >= 1073741824) return number_format($bytes / 1073741824, 2) . ' GB';
if ($bytes >= 1048576) return number_format($bytes / 1048576, 1) . ' MB';
if ($bytes >= 1024) return number_format($bytes / 1024, 0) . ' KB';
return $bytes . ' B';
}
}