Files
EIS/mod_pdf_tree/helper.php
Thomas Spohr 33fa9c8f94 Letzte Version
2025-08-13 08:35:35 +02:00

112 lines
3.4 KiB
PHP

<?php
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
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
$grouped = [];
foreach ($rows as $row) {
$pid = $row['parent_id'] === null ? null : (int) $row['parent_id'];
$grouped[$pid][] = $row;
}
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'];
$title = htmlspecialchars($item['title'] ?: $item['name']);
$path = self::convertToRelativeUrl($item['path'] ?? '');
// Erweiterung .pdf im Titel entfernen (nur Anzeige, nicht Link)
$displayTitle = preg_replace('/\.pdf$/i', '', $title);
if ($isFolder) {
$fileCount = self::countFilesRecursive($items, $item['id']);
$html .= '<li class="folder">';
$html .= '<span class="toggle">▶</span>';
$html .= '<span class="folder-icon">📁</span> ' . $displayTitle . ' <small>(' . $fileCount . ')</small>';
$html .= self::renderTree($items, $item['id']);
$html .= '</li>';
} else {
$html .= '<li class="file">';
$html .= '<span class="file-icon">📄</span><a class="file-link" href="' . htmlspecialchars($path) . '" title="' . $title . '">' . $displayTitle . '</a>';
$html .= '</li>';
}
}
$html .= '</ul>';
return $html;
}
/**
* Konvertiert absoluten Serverpfad in URL
*/
private static function convertToRelativeUrl(string $fullPath): string
{
// Absoluter Serverpfad zum Joomla-Root
$webRoot = '/var/www/joomla';
$webBase = ''; // ggf. '/unterordner', wenn Joomla in Subdir
if (str_starts_with($fullPath, $webRoot)) {
$relativePath = str_replace($webRoot, '', $fullPath);
// Leerzeichen und Sonderzeichen escapen
$relativePath = implode('/', array_map('rawurlencode', explode('/', $relativePath)));
return rtrim(\JUri::root(), '/') . $webBase . $relativePath;
}
return $fullPath; // Fallback
}
/**
* 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;
}
}