124 lines
3.7 KiB
PHP
124 lines
3.7 KiB
PHP
<?php
|
|
defined('_JEXEC') or die;
|
|
|
|
use Joomla\CMS\Factory;
|
|
use Joomla\CMS\Router\Route;
|
|
use Joomla\Database\DatabaseDriver;
|
|
|
|
class ModEisAnzeigeHelper
|
|
{
|
|
/**
|
|
* Holt alle PDF-Elemente aus der Datenbank
|
|
*/
|
|
public static function getItems(): array
|
|
{
|
|
/** @var DatabaseDriver $db */
|
|
$db = Factory::getDbo();
|
|
|
|
$query = $db->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 = '<ul class="pdf-tree">';
|
|
|
|
foreach ($items[$parentId] as $item) {
|
|
$isFolder = (bool) $item['is_folder'];
|
|
$rawName = $item['title'] ?: $item['name'];
|
|
$fileId = (int) $item['id'];
|
|
|
|
// Dateiendung .pdf entfernen
|
|
$displayName = preg_replace('/\.pdf$/i', '', $rawName);
|
|
$displayName = htmlspecialchars($displayName);
|
|
|
|
if ($isFolder) {
|
|
$fileCount = self::countFilesRecursive($items, $item['id']);
|
|
$html .= '<li class="folder">';
|
|
$html .= '📁 ' . $displayName . ' <small>(' . $fileCount . ')</small>';
|
|
$html .= self::renderTree($items, $item['id']);
|
|
$html .= '</li>';
|
|
} else {
|
|
$link = Route::_('index.php?option=com_eis&task=download.download&id=' . $fileId);
|
|
|
|
// Tooltip mit Dateigröße
|
|
$tooltip = '';
|
|
if (!empty($item['path']) && file_exists($item['path'])) {
|
|
$size = filesize($item['path']);
|
|
$tooltip = ' title="Größe: ' . self::formatFileSize($size) . '"';
|
|
}
|
|
|
|
$html .= '<li class="file">';
|
|
$html .= '📄 <a class="file-link" href="' . $link . '"' . $tooltip . '>' . $displayName . '</a>';
|
|
$html .= '</li>';
|
|
}
|
|
}
|
|
|
|
$html .= '</ul>';
|
|
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';
|
|
}
|
|
}
|
|
}
|