Files
EIS/mod_pdf_tree/helper.php
2025-07-31 12:22:10 +02:00

109 lines
3.1 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 = htmlspecialchars($item['path'] ?? '');
$path = htmlspecialchars(self::convertToRelativeUrl($item['path'] ?? ''));
if ($isFolder) {
$fileCount = self::countFilesRecursive($items, $item['id']);
$html .= '<li class="folder">';
$html .= '<span class="toggle">▶</span>';
$html .= '<span class="folder-icon"></span>' . $title . ' <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="' . $path . '">' . $title . '</a>';
$html .= '</li>';
}
}
$html .= '</ul>';
return $html;
}
/**
* Konvertiert absoluten Serverpfad in URL
*/
private static function convertToRelativeUrl(string $fullPath): string
{
// <== HIER den absoluten Pfad zu deinem Webroot anpassen
$webRoot = '/var/www/vhosts/ts-it24.net/stbv.ts-it24.net';
$webBase = ''; // ggf. '/subdir' falls Joomla in Unterordner
if (str_starts_with($fullPath, $webRoot)) {
return $webBase . str_replace($webRoot, '', $fullPath);
}
return $fullPath; // Fallback: Original verwenden
}
/**
* 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;
}
}