Files
EIS/mod_pdf_tree/helper.php
Thomas Spohr fc1de065c9 Mit Formular
2025-08-20 14:08:20 +02:00

149 lines
5.4 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
* - Nutzt title als alternativen Anzeigenamen (Fallback name)
* - Fügt für Ordner eine klickbare Label-Zeile hinzu (bessere Tap-Ziele auf iPad)
* - 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 = '<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 und escapen
$displayName = preg_replace('/\.pdf$/i', '', (string) $rawName);
$displayName = htmlspecialchars($displayName, ENT_QUOTES, 'UTF-8');
if ($isFolder) {
$fileCount = self::countFilesRecursive($items, $item['id']);
// Ordner-<li> inkl. Data-Attribute + große Tap-Ziele (toggle + folder-label)
$html .= '<li class="folder"'
. ' data-id="' . (int) $item['id'] . '"'
. ' data-title="' . htmlspecialchars((string)($item['title'] ?? ''), ENT_QUOTES, 'UTF-8') . '"'
. ' data-name="' . htmlspecialchars((string)$item['name'], ENT_QUOTES, 'UTF-8') . '"'
. '>';
$html .= '<span class="toggle" role="button" aria-label="Ordner umschalten" tabindex="0">▶</span> ';
$html .= '<span class="folder-label">📁 ' . $displayName . ' <small class="count">(' . (int) $fileCount . ')</small></span>';
// Kindknoten
$html .= self::renderTree($items, $item['id']);
$html .= '</li>';
} else {
$link = Route::_('index.php?option=com_eis&task=download.download&id=' . $fileId);
// Tooltip + (optional) inline-Anzeige der Dateigröße
$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 . '"';
// Wenn du die Größe auch sichtbar möchtest, Zeile darunter einkommentieren:
// $sizeStr = ' <small class="meta">(' . $formatted . ')</small>';
}
}
// Datei-<li> inkl. Data-Attribute + data-filename am Link (für Preview-Anzeige)
$html .= '<li class="file"'
. ' data-id="' . $fileId . '"'
. ' data-title="' . htmlspecialchars((string)($item['title'] ?? ''), ENT_QUOTES, 'UTF-8') . '"'
. ' data-name="' . htmlspecialchars((string)$item['name'], ENT_QUOTES, 'UTF-8') . '"'
. '>';
$html .= '📄 <a class="file-link" href="' . $link . '"' . $tooltip
. ' data-filename="' . $displayName . '">'
. $displayName . '</a>' . $sizeStr;
$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, (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';
}
}
}