Finanzmanager/src/Controllers/BaseController.php
fds2610-bot a077e03bee Fix #40: Kategorie im Kontoblatt ohne Neuladen speichern
Das Ändern von Kategorie oder Klasse im Kontoblatt lud bisher die ganze
Seite neu, um die Zeile zu aktualisieren – dabei sprang der Bildschirm
nach ganz oben. kontoblatt.js schickt die Auswahl jetzt per fetch im
Hintergrund; Kategorie, Klasse und das Symbol der Bargeld-Abgleichspalte
aktualisieren sich an Ort und Stelle, die Bildlaufposition bleibt stehen.
Ohne JavaScript bleibt es bei der bisherigen Weiterleitung.
2026-08-19 14:57:09 +02:00

99 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace Finanzmanager\Controllers;
use Finanzmanager\Auth;
use Finanzmanager\View;
abstract class BaseController
{
protected View $view;
public function __construct()
{
$this->view = new View();
}
/** Seiten hinter der Anmeldung rufen das zuerst auf. */
protected function verlangeAnmeldung(): void
{
Auth::verlange();
}
protected function render(string $template, array $daten = []): void
{
$daten['csrfToken'] = Auth::csrfToken();
$daten['benutzer'] = Auth::benutzer();
$this->view->render($template, $daten);
}
protected function post(string $feld, string $standard = ''): string
{
$wert = $_POST[$feld] ?? $standard;
return is_string($wert) ? trim($wert) : $standard;
}
/**
* Mehrfachauswahl aus dem Formular (Ankreuzfelder mit Namen wie „konten[]“).
*
* @return list<int>
*/
protected function postIds(string $feld): array
{
$werte = $_POST[$feld] ?? [];
if (!is_array($werte)) {
return [];
}
return array_values(array_map('intval', array_filter($werte, 'is_scalar')));
}
/**
* Mehrfachauswahl mit Textwerten (Ankreuzfelder wie „konten[]“).
*
* @return list<string>
*/
protected function postTexte(string $feld): array
{
$werte = $_POST[$feld] ?? [];
if (!is_array($werte)) {
return [];
}
$texte = [];
foreach ($werte as $wert) {
if (is_string($wert) && trim($wert) !== '') {
$texte[] = trim($wert);
}
}
return $texte;
}
protected function query(string $feld, string $standard = ''): string
{
$wert = $_GET[$feld] ?? $standard;
return is_string($wert) ? trim($wert) : $standard;
}
/**
* Ob die Anfrage per JavaScript im Hintergrund geschickt wurde, statt als
* gewöhnliche Formularübermittlung. Nur dann erwartet der Aufrufer JSON
* statt einer Weiterleitung – ohne JavaScript bleibt der bisherige Weg
* über Location-Header und Flash-Meldung der einzige.
*/
protected function istAjax(): bool
{
return ($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '') === 'XMLHttpRequest';
}
/** @param array<string,mixed> $daten */
protected function json(array $daten): void
{
header('Content-Type: application/json; charset=utf-8');
echo json_encode($daten, JSON_UNESCAPED_UNICODE);
exit;
}
}