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((string)$a['name'], (string)$b['name']));
}
return $grouped;
}
/** Liest param/value aus #__eis_settings (Key/Value) */
private static function getSetting(string $param, $default = '')
{
$db = Factory::getDbo();
try {
$q = $db->getQuery(true)
->select($db->quoteName('value'))
->from($db->quoteName('#__eis_settings'))
->where($db->quoteName('param') . ' = ' . $db->quote($param))
->setLimit(1);
$db->setQuery($q);
$val = $db->loadResult();
return ($val !== null && $val !== '') ? $val : $default;
} catch (\Throwable $e) {
return $default;
}
}
/** Holt die zuletzt gespeicherten neuen Datei-IDs (vom Backend-Scan) */
private static function getLastNewIds(): array
{
$json = (string) self::getSetting('last_new_ids', '[]');
$arr = json_decode($json, true);
if (!is_array($arr)) return [];
return array_values(array_unique(array_map('intval', $arr)));
}
/** Baut einen flachen Index id => row über alle Items */
private static function buildFlatIndex(array $grouped): array
{
$idx = [];
foreach ($grouped as $group) {
foreach ($group as $row) {
$idx[(int) $row['id']] = $row;
}
}
return $idx;
}
/**
* Hauptfunktion zum Rendern des Baums (klassisch, ohne „Neue Dokumente“)
* - Nutzt title als alternativen Anzeigenamen (Fallback name)
* - Fügt für Ordner eine klickbare Label-Zeile hinzu
* - Liefert data-* Attribute für spätere Inline-Edits / Preview
*/
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', '', (string) $rawName);
$displayName = htmlspecialchars($displayName, ENT_QUOTES, 'UTF-8');
if ($isFolder) {
$fileCount = self::countFilesRecursive($items, (int)$item['id']);
// Ordner-- inkl. Data-Attribute + große Tap-Ziele (toggle + folder-label)
$html .= '
- ';
$html .= '▶ ';
$html .= '📁 ' . $displayName . ' (' . (int) $fileCount . ')';
// Kindknoten
$html .= self::renderTree($items, (int)$item['id']);
$html .= '
';
} else {
$html .= self::renderSingleFileLi($item, $displayName);
}
}
$html .= '
';
return $html;
}
/**
* NEU: Rendert (falls vorhanden) zuerst einen virtuellen Ordner „Neue Dokumente“,
* danach den normalen Baum.
*/
public static function renderTreeWithNew(array $items): string
{
$html = '';
// 1) Virtueller Ordner „Neue Dokumente“
$newIds = self::getLastNewIds();
if (!empty($newIds)) {
$idx = self::buildFlatIndex($items);
$files = [];
foreach ($newIds as $id) {
if (!isset($idx[$id])) continue;
$row = $idx[$id];
if (!empty($row['is_folder'])) continue; // nur Dateien
$files[] = $row;
}
if (!empty($files)) {
$label = 'Neue Dokumente'; // optional via Sprachschlüssel ersetzen
$html .= '';
$html .= '- ';
$html .= '▼ ';
$html .= '🆕 ' . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . ' (' . (int) count($files) . ')';
// Kinder direkt sichtbar
$html .= '
';
foreach ($files as $fileRow) {
$html .= self::renderSingleFileLi($fileRow);
}
$html .= '
';
$html .= ' ';
$html .= '
';
}
}
// 2) Normaler Baum
$html .= self::renderTree($items, null);
return $html;
}
/** Einzelnes für Datei (wiederverwendbar) */
private static function renderSingleFileLi(array $item, ?string $displayNameEscaped = null): string
{
$fileId = (int) $item['id'];
$rawName = $item['title'] ?: $item['name'];
$display = $displayNameEscaped ?? htmlspecialchars(preg_replace('/\.pdf$/i', '', (string)$rawName), ENT_QUOTES, 'UTF-8');
// Tooltip + (optional) inline-Anzeige der Dateigröße
$tooltip = '';
$sizeStr = '';
if (!empty($item['path']) && is_file((string)$item['path'])) {
$bytes = @filesize((string)$item['path']);
if ($bytes !== false) {
$formatted = self::formatFileSize((int)$bytes);
$tooltip = ' title="Größe: ' . $formatted . '"';
// $sizeStr = ' (' . $formatted . ')';
}
}
$link = Route::_('index.php?option=com_eis&task=download.download&id=' . $fileId);
$html = '';
$html .= '📄 '
. $display . '' . $sizeStr;
$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, (int) $item['id']);
} else {
$count++;
}
}
return $count;
}
/**
* Formatierte Dateigröße für Tooltip / Anzeige
*/
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';
}
}
}