Mit neuen Manfest

This commit is contained in:
Thomas Spohr
2025-08-26 20:28:35 +02:00
parent dda6869030
commit 103fea5c48
20 changed files with 559 additions and 304 deletions

Binary file not shown.

View File

@@ -4,43 +4,55 @@ namespace EIS\Component\EIS\Administrator\Controller;
\defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;
use EIS\Component\EIS\Administrator\Helper\SettingsHelper;
class ConfigController extends BaseController
{
public function save(): void
{
$app = Factory::getApplication();
$input = $app->getInput();
$db = Factory::getDbo();
// Eingabe
$pdfPath = $input->getString('pdf_path', '');
// Existiert ein Eintrag?
$query = $db->getQuery(true)
->select('COUNT(*)')
->from($db->quoteName('#__eis_settings'));
$db->setQuery($query);
$exists = (int) $db->loadResult() > 0;
if ($exists) {
// Update
$query = $db->getQuery(true)
->update($db->quoteName('#__eis_settings'))
->set($db->quoteName('pdf_path') . ' = ' . $db->quote($pdfPath));
} else {
// Insert
$query = $db->getQuery(true)
->insert($db->quoteName('#__eis_settings'))
->columns([$db->quoteName('pdf_path')])
->values($db->quote($pdfPath));
// CSRF
if (!Session::checkToken('post')) {
throw new \RuntimeException(Text::_('JINVALID_TOKEN'), 403);
}
$db->setQuery($query)->execute();
$app = Factory::getApplication();
$input = $app->getInput();
$app->enqueueMessage('Pfad gespeichert: ' . $pdfPath, 'message');
// Feldname aus dem Config-Template
$path = (string) $input->post->get('document_root', '', 'string');
$path = trim($path);
// Minimal-Validierung & Normalisierung
// (Hier kein striktes is_readable(), damit man den Pfad auch erst konfigurieren kann,
// wenn das Verzeichnis später angelegt/umgehängt wird. Warnen ist aber hilfreich.)
if ($path === '') {
$app->enqueueMessage(Text::_('COM_EIS_MSG_INVALID_PATH') ?: 'Bitte einen gültigen Pfad angeben.', 'warning');
$this->setRedirect(Route::_('index.php?option=com_eis&view=config', false));
return;
}
// Doppelte Slashes und trailing Slash bereinigen
$path = preg_replace('#/+#', '/', $path);
// Lass den Root-Slash stehen, entferne sonst trailing Slash
if ($path !== '/' && str_ends_with($path, '/')) {
$path = rtrim($path, '/');
}
// Optional: Hinweise zur Existenz/Lesbarkeit (nur Hinweis, kein Hard-Error)
if (!is_dir($path)) {
$app->enqueueMessage(Text::sprintf('COM_EIS_MSG_PATH_NOT_EXISTS', $path) ?: "Hinweis: Verzeichnis existiert nicht: {$path}", 'notice');
} elseif (!is_readable($path)) {
$app->enqueueMessage(Text::sprintf('COM_EIS_MSG_PATH_NOT_READABLE', $path) ?: "Hinweis: Verzeichnis nicht lesbar: {$path}", 'notice');
}
// Persistieren
SettingsHelper::setSetting('document_root', $path);
$app->enqueueMessage(Text::_('COM_EIS_MSG_SAVED_SUCCESS') ?: 'Einstellungen wurden gespeichert.', 'message');
$this->setRedirect(Route::_('index.php?option=com_eis&view=config', false));
}
}

View File

@@ -5,8 +5,12 @@ namespace EIS\Component\EIS\Administrator\Controller;
\defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;
use Joomla\Database\DatabaseDriver;
use EIS\Component\EIS\Administrator\Helper\SettingsHelper;
class DisplayController extends BaseController
{
@@ -17,36 +21,39 @@ class DisplayController extends BaseController
*/
public function scan(): void
{
$db = Factory::getDbo();
// CSRF prüfen (Form hat Token)
if (!Session::checkToken('post')) {
throw new \RuntimeException(Text::_('JINVALID_TOKEN'), 403);
}
// Pfad aus Tabelle laden
$query = $db->getQuery(true)
->select($db->quoteName('pdf_path'))
->from($db->quoteName('#__eis_settings'))
->order('id ASC')
->setLimit(1);
$app = Factory::getApplication();
/** @var DatabaseDriver $db */
$db = Factory::getDbo();
$db->setQuery($query);
$path = $db->loadResult();
// Pfad aus Settings laden (Key/Value); Default /var/www/pdf
$path = SettingsHelper::getSetting('document_root', '/var/www/pdf');
if (!$path || !is_dir($path)) {
Factory::getApplication()->enqueueMessage('Pfad ungültig oder nicht gesetzt: ' . $path, 'error');
$this->setRedirect('index.php?option=com_eis&view=main');
$app->enqueueMessage(Text::sprintf('COM_EIS_MSG_PATH_NOT_EXISTS', $path) ?: ('Pfad ungültig oder nicht gesetzt: ' . $path), 'error');
$this->setRedirect(Route::_('index.php?option=com_eis&view=main', false));
return;
}
// Verzeichnis rekursiv scannen
$data = $this->scanFolder($path);
// Alte Einträge löschen
// Alte Einträge löschen (Hinweis: Dann sind ALLE eingefügten „neu“)
$db->truncateTable('#__eis_documents');
// In Datenbank speichern
$this->saveToDb($data, null, $db);
// In Datenbank speichern und neue IDs sammeln
$newIds = $this->saveToDb($data, null, $db);
// Neue IDs im UserState für die View -> virtueller Ordner "Neue Dokumente"
$app->setUserState('com_eis.new_ids', $newIds);
// Erfolgsmeldung
Factory::getApplication()->enqueueMessage('PDF-Struktur erfolgreich gespeichert.', 'message');
$this->setRedirect('index.php?option=com_eis&view=main');
$app->enqueueMessage(Text::_('COM_EIS_MSG_SCAN_DONE') ?: 'PDF-Struktur erfolgreich gespeichert.', 'message');
$this->setRedirect(Route::_('index.php?option=com_eis&view=main', false));
}
/**
@@ -59,7 +66,12 @@ class DisplayController extends BaseController
return $result;
}
foreach (scandir($dir) as $file) {
$entries = @scandir($dir);
if ($entries === false) {
return $result;
}
foreach ($entries as $file) {
if ($file === '.' || $file === '..') {
continue;
}
@@ -78,18 +90,26 @@ class DisplayController extends BaseController
}
}
// Alphabetisch stabil sortieren (Ordner/Dateien je Ebene)
usort($result, static function ($a, $b) {
return strcasecmp((string)$a['name'], (string)$b['name']);
});
return $result;
}
/**
* Struktur rekursiv in die Datenbank schreiben
* @return int[] Insert-IDs aller neu eingefügten Dateien (für „Neue Dokumente“)
*/
private function saveToDb(array $items, ?int $parentId, DatabaseDriver $db): void
private function saveToDb(array $items, ?int $parentId, DatabaseDriver $db): array
{
$insertedFileIds = [];
foreach ($items as $item) {
$name = $db->quote($item['name']);
$path = $db->quote($item['path'] ?? ''); // Leerer String statt NULL
$parent = $parentId !== null ? (int) $parentId : 'NULL';
$path = $db->quote($item['path'] ?? ''); // leer statt NULL
$parent = $parentId !== null ? (int)$parentId : 'NULL';
$isFolder = isset($item['children']) ? 1 : 0;
$query = $db->getQuery(true)
@@ -97,45 +117,55 @@ class DisplayController extends BaseController
->columns(['name', 'path', 'parent_id', 'is_folder'])
->values("$name, $path, $parent, $isFolder");
$db->setQuery($query);
$db->execute();
$db->setQuery($query)->execute();
$insertedId = $db->insertid();
$insertedId = (int)$db->insertid();
if ($isFolder && !empty($item['children'])) {
$this->saveToDb($item['children'], $insertedId, $db);
// Kinder speichern
$childIds = $this->saveToDb($item['children'], $insertedId, $db);
// Nur Datei-IDs sammeln
$insertedFileIds = array_merge($insertedFileIds, $childIds);
} else {
// Datei -> ID merken
$insertedFileIds[] = $insertedId;
}
}
return $insertedFileIds;
}
public function rename(): void
{
// CSRF prüfen
\Joomla\CMS\Session\Session::checkToken() or jexit(\JText::_('JINVALID_TOKEN'));
if (!Session::checkToken('post')) {
throw new \RuntimeException(Text::_('JINVALID_TOKEN'), 403);
}
$app = \Joomla\CMS\Factory::getApplication();
$id = (int) $app->input->get('id');
$title = trim((string) $app->input->get('title', '', 'STRING'));
$app = Factory::getApplication();
$id = (int) $app->input->post->get('id', 0);
$title = trim((string) $app->input->post->get('title', '', 'string'));
if ($id <= 0) {
$app->enqueueMessage('Ungültige ID', 'error');
$this->setRedirect('index.php?option=com_eis&view=main');
$app->enqueueMessage(Text::_('JERROR_AN_ERROR_HAS_OCCURRED') ?: 'Ungültige ID', 'error');
$this->setRedirect(Route::_('index.php?option=com_eis&view=main', false));
return;
}
/** @var \Joomla\Database\DatabaseDriver $db */
$db = \Joomla\CMS\Factory::getDbo();
/** @var DatabaseDriver $db */
$db = Factory::getDbo();
$query = $db->getQuery(true)
->update($db->quoteName('#__eis_documents'))
->set($db->quoteName('title') . ' = ' . ($title === '' ? 'NULL' : $db->quote($title)))
->where($db->quoteName('id') . ' = ' . (int) $id);
->where($db->quoteName('id') . ' = ' . (int)$id);
try {
$db->setQuery($query)->execute();
$app->enqueueMessage('Anzeigename gespeichert', 'message');
$app->enqueueMessage(Text::_('COM_EIS_MSG_SAVED_SUCCESS') ?: 'Anzeigename gespeichert', 'message');
} catch (\Throwable $e) {
$app->enqueueMessage('Fehler beim Speichern: ' . $e->getMessage(), 'error');
$app->enqueueMessage((Text::_('COM_EIS_MSG_SAVED_ERROR') ?: 'Fehler beim Speichern: ') . $e->getMessage(), 'error');
}
$this->setRedirect('index.php?option=com_eis&view=main');
$this->setRedirect(Route::_('index.php?option=com_eis&view=main', false));
}
}

View File

@@ -0,0 +1,45 @@
<?php
namespace EIS\Component\EIS\Administrator\Helper;
\defined('_JEXEC') or die;
use Joomla\CMS\Factory;
final class SettingsHelper
{
public static function getSetting(string $param, $default = '')
{
$db = Factory::getDbo();
$q = $db->getQuery(true)
->select($db->quoteName('value'))
->from($db->quoteName('#__eis_settings'))
->where($db->quoteName('param') . ' = ' . $db->quote($param));
$db->setQuery($q);
$val = $db->loadResult();
return ($val !== null && $val !== '') ? $val : $default;
}
public static function setSetting(string $param, $value): void
{
$db = Factory::getDbo();
// Basis-INSERT per Query-Builder zusammenstellen …
$qb = $db->getQuery(true)
->insert($db->quoteName('#__eis_settings'))
->columns([$db->quoteName('param'), $db->quoteName('value'), $db->quoteName('modified')])
->values(
$db->quote($param) . ', ' .
$db->quote((string) $value) . ', ' .
$db->quote(date('Y-m-d H:i:s'))
);
// … und dann als String um "ON DUPLICATE KEY UPDATE" erweitern.
$sql = (string) $qb
. ' ON DUPLICATE KEY UPDATE '
. $db->quoteName('value') . ' = VALUES(' . $db->quoteName('value') . '), '
. $db->quoteName('modified') . ' = VALUES(' . $db->quoteName('modified') . ')';
$db->setQuery($sql)->execute();
}
}

View File

@@ -5,6 +5,7 @@ namespace EIS\Component\EIS\Administrator\Helper;
\defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\Database\DatabaseDriver;
class TreeHelper
@@ -30,12 +31,46 @@ class TreeHelper
// alphabetisch nach name
foreach ($grouped as &$group) {
usort($group, fn($a, $b) => strcasecmp($a['name'], $b['name']));
usort($group, static fn($a, $b) => strcasecmp((string)$a['name'], (string)$b['name']));
}
return $grouped;
}
/**
* Kombiniert: Virtueller Ordner "Neue Dokumente" (falls $newIds vorhanden) + normaler Baum
*/
public static function renderTreeWithNew(array $items, array $newIds = []): string
{
$html = '';
// 1) Virtueller Ordner "Neue Dokumente"
$newFiles = self::collectFilesByIds($items, $newIds);
if (!empty($newFiles)) {
$label = Text::_('COM_EIS_VIRTUAL_NEW') ?: 'Neue Dokumente';
$html .= '<ul class="pdf-tree">';
$html .= '<li class="folder" data-id="new" data-title="' . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . '" data-name="' . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . '" data-count="' . count($newFiles) . '">';
$html .= '<span class="toggle" role="button" aria-label="Ordner umschalten" tabindex="0">▼</span> ';
$html .= '<span class="folder-label">🆕 ' . htmlspecialchars($label, ENT_QUOTES, 'UTF-8') . ' <small class="count">(' . (int) count($newFiles) . ')</small></span>';
// Kinder sichtbar
$html .= '<ul style="display:block">';
foreach ($newFiles as $row) {
$html .= self::renderSingleFileLi($row);
}
$html .= '</ul>';
$html .= '</li>';
$html .= '</ul>';
}
// 2) Normaler Baum
$html .= self::renderTree($items, null);
return $html;
}
/** Baum rendern (nutzt title als alternativen Anzeigenamen) */
public static function renderTree(array $items, ?int $parentId = null): string
{
@@ -46,39 +81,27 @@ class TreeHelper
$html = '<ul class="pdf-tree">';
foreach ($items[$parentId] as $item) {
$isFolder = (bool) $item['is_folder'];
$rawName = $item['title'] ?: $item['name'];
$display = preg_replace('/\.pdf$/i', '', (string) $rawName);
$display = htmlspecialchars($display, ENT_QUOTES, 'UTF-8');
$id = (int) $item['id'];
$isFolder = !empty($item['is_folder']);
$rawName = (string)($item['title'] ?? '') !== '' ? (string)$item['title'] : (string)$item['name'];
$display = preg_replace('/\.pdf$/i', '', $rawName);
$display = htmlspecialchars((string)$display, ENT_QUOTES, 'UTF-8');
$id = (int)$item['id'];
if ($isFolder) {
$fileCount = self::countFilesRecursive($items, $id);
$html .= '<li class="folder"'
. ' data-id="' . $id . '"'
. ' data-id="' . $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">📁 ' . $display . ' <small class="count">(' . (int)$fileCount . ')</small></span>';
$html .= self::renderTree($items, $id);
$html .= '</li>';
. ' data-count="' . (int)$fileCount . '"'
. '>'
. '<span class="toggle" role="button" aria-label="Ordner umschalten" tabindex="0">▶</span> '
. '<span class="folder-label">📁 ' . $display . ' <small class="count">(' . (int)$fileCount . ')</small></span>'
. self::renderTree($items, $id)
. '</li>';
} else {
$sizeStr = '';
if (!empty($item['path']) && is_file($item['path'])) {
$size = @filesize($item['path']);
if ($size !== false) {
$sizeStr = ' <small class="meta">(' . self::formatFileSize((int) $size) . ')</small>';
}
}
$html .= '<li class="file"'
. ' data-id="' . $id . '"'
. ' data-title="' . htmlspecialchars((string)($item['title'] ?? ''), ENT_QUOTES, 'UTF-8') . '"'
. ' data-name="' . htmlspecialchars((string)$item['name'], ENT_QUOTES, 'UTF-8') . '"'
. '>';
$html .= '<span class="file-label">📄 ' . $display . $sizeStr . '</span>';
$html .= '</li>';
$html .= self::renderSingleFileLi($item, $display);
}
}
@@ -86,6 +109,64 @@ class TreeHelper
return $html;
}
/** Einzelnes <li> für Datei (wird auch vom virtuellen Ordner genutzt) */
private static function renderSingleFileLi(array $item, ?string $displayAlreadyEscaped = null): string
{
$rawName = (string)($item['title'] ?? '') !== '' ? (string)$item['title'] : (string)$item['name'];
$display = $displayAlreadyEscaped ?? htmlspecialchars(preg_replace('/\.pdf$/i', '', $rawName), ENT_QUOTES, 'UTF-8');
$id = (int)$item['id'];
// Dateigröße ermitteln (optional)
$sizeStr = '';
$sizeBytes = '';
if (!empty($item['path']) && is_file((string)$item['path'])) {
$bytes = @filesize((string)$item['path']); // @ unterdrückt Warnungen bei Zugriffsproblemen
if ($bytes !== false) {
$sizeBytes = (string)(int)$bytes;
$sizeStr = self::formatFileSize((int)$bytes);
}
}
$html = '<li class="file"'
. ' data-id="' . $id . '"'
. ' data-title="' . htmlspecialchars((string)($item['title'] ?? ''), ENT_QUOTES, 'UTF-8') . '"'
. ' data-name="' . htmlspecialchars((string)$item['name'], ENT_QUOTES, 'UTF-8') . '"'
. ' data-size="' . htmlspecialchars($sizeStr, ENT_QUOTES, 'UTF-8') . '"'
. ' data-bytes="' . htmlspecialchars($sizeBytes, ENT_QUOTES, 'UTF-8') . '"'
. '>'
. '<span class="file-label">📄 ' . $display
. ($sizeStr !== '' ? ' <small class="meta">(' . $sizeStr . ')</small>' : '')
. '</span>'
. '</li>';
return $html;
}
/** Dateien zu gegebenen IDs einsammeln (nur is_folder = 0) */
private static function collectFilesByIds(array $items, array $ids): array
{
if (empty($ids)) {
return [];
}
$ids = array_map('intval', $ids);
// Flat-Index aller Rows
$idx = [];
foreach ($items as $group) {
foreach ($group as $row) {
$idx[(int)$row['id']] = $row;
}
}
$out = [];
foreach ($ids as $id) {
if (isset($idx[$id]) && empty($idx[$id]['is_folder'])) {
$out[] = $idx[$id];
}
}
return $out;
}
private static function countFilesRecursive(array $items, int $parentId): int
{
$count = 0;
@@ -93,7 +174,7 @@ class TreeHelper
return 0;
}
foreach ($items[$parentId] as $item) {
if ((bool) $item['is_folder']) {
if (!empty($item['is_folder'])) {
$count += self::countFilesRecursive($items, (int)$item['id']);
} else {
$count++;

View File

@@ -4,29 +4,20 @@ namespace EIS\Component\EIS\Administrator\View\Config;
\defined('_JEXEC') or die;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Factory;
use EIS\Component\EIS\Administrator\Helper\SettingsHelper;
class HtmlView extends BaseHtmlView
{
protected $form;
protected $item;
protected $pdfPath = '';
public function display($tpl = null): void
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from($db->quoteName('#__eis_settings'))
->setLimit(1);
$db->setQuery($query);
$this->item = $db->loadAssoc();
$this->pdfPath = SettingsHelper::getSetting('document_root', '/var/www/pdf');
parent::display($tpl);
}
public function getItem()
public function getPdfPath(): string
{
return $this->item;
return (string)$this->pdfPath;
}
}

View File

@@ -1,20 +1,41 @@
<?php
namespace EIS\Component\EIS\Administrator\View\Main;
\defined('_JEXEC') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use EIS\Component\EIS\Administrator\Helper\TreeHelper;
class HtmlView extends BaseHtmlView
{
/** @var string HTML des Baums (inkl. virtuellem Ordner „Neue Dokumente“, falls vorhanden) */
protected $treeHtml = '';
public function display($tpl = null): void
{
// Baumdaten laden und fürs Template bereitstellen
$items = TreeHelper::getItems();
$this->treeHtml = TreeHelper::renderTree($items);
$app = Factory::getApplication();
// Daten für den Baum holen
$items = TreeHelper::getItems();
// Neu hinzugekommene IDs aus dem letzten Scan (können leer sein)
$newIds = (array) $app->getUserState('com_eis.new_ids', []);
// Baum rendern (virtueller Ordner „Neue Dokumente“ + normaler Baum)
$this->treeHtml = TreeHelper::renderTreeWithNew($items, $newIds);
// Template rendern
parent::display($tpl);
// Optional: nur einmal anzeigen -> State leeren
$app->setUserState('com_eis.new_ids', []);
}
/** Ermöglicht im Template: $this->treeHtml */
public function getTreeHtml(): string
{
return (string) $this->treeHtml;
}
}