erste Lauffähige Fassung
This commit is contained in:
99
mod_pdf_tree/helper.php
Normal file
99
mod_pdf_tree/helper.php
Normal file
@@ -0,0 +1,99 @@
|
||||
|
||||
<?php
|
||||
// Direktzugriff verhindern
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
// Hilfsklasse zur URI-Verarbeitung einbinden
|
||||
use Joomla\CMS\Uri\Uri;
|
||||
|
||||
/**
|
||||
* Modul-Hilfsklasse: Baumstruktur aus JSON-Datei aufbereiten
|
||||
*/
|
||||
class ModEisAnzeigeHelper
|
||||
{
|
||||
/**
|
||||
* JSON-Datei einlesen und als Array zurückgeben
|
||||
* @param string $jsonPath Pfad relativ zum Joomla-Root
|
||||
* @return array Strukturierte Baumdaten oder leeres Array
|
||||
*/
|
||||
public static function getItems(string $jsonPath): array
|
||||
{
|
||||
$file = JPATH_ROOT . DIRECTORY_SEPARATOR . ltrim($jsonPath, '/');
|
||||
if (!file_exists($file)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$json = file_get_contents($file);
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE || !is_array($data)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Baumstruktur als HTML generieren (rekursiv)
|
||||
* @param array $items Eingelesene JSON-Daten
|
||||
* @return string HTML-Ausgabe
|
||||
*/
|
||||
public static function renderTree(array $items): string
|
||||
{
|
||||
$html = '<ul class="pdf-tree">';
|
||||
|
||||
foreach ($items as $node) {
|
||||
$name = htmlspecialchars($node['name'], ENT_QUOTES, 'UTF-8');
|
||||
|
||||
// Ordner: hat Kinder
|
||||
if (isset($node['children']) && is_array($node['children'])) {
|
||||
$count = self::countFiles($node['children']);
|
||||
|
||||
$html .= '<li class="folder">'
|
||||
. '<span class="toggle">▶</span>'
|
||||
. '<span class="folder-icon"></span>'
|
||||
. '<span class="folder-name">'
|
||||
. $name . ' (' . $count . ')'
|
||||
. '</span>';
|
||||
|
||||
// Rekursiver Aufruf
|
||||
$html .= self::renderTree($node['children']);
|
||||
$html .= '</li>';
|
||||
} else {
|
||||
// Datei
|
||||
$path = $node['path'] ?? '';
|
||||
$relative = str_replace(JPATH_ROOT, '', $path);
|
||||
$url = Uri::root(true) . '/' . ltrim(str_replace(DIRECTORY_SEPARATOR, '/', $relative), '/');
|
||||
|
||||
$html .= '<li class="file">'
|
||||
. '<span class="file-icon"></span>'
|
||||
. '<a href="' . $url . '" class="file-link" download>'
|
||||
. $name . '</a>'
|
||||
. '</li>';
|
||||
}
|
||||
}
|
||||
|
||||
$html .= '</ul>';
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dateien im aktuellen Knotenbaum zählen
|
||||
* @param array $items Unterknoten
|
||||
* @return int Anzahl Dateien
|
||||
*/
|
||||
private static function countFiles(array $items): int
|
||||
{
|
||||
$count = 0;
|
||||
|
||||
foreach ($items as $node) {
|
||||
if (isset($node['children']) && is_array($node['children'])) {
|
||||
$count += self::countFiles($node['children']);
|
||||
} else {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
||||
return $count;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user