Update updateArticle-controller.php
This commit is contained in:
@@ -7,78 +7,13 @@ require_once '../model/LocalArticleManager.php';
|
|||||||
require_once '../model/ArticleManager.php';
|
require_once '../model/ArticleManager.php';
|
||||||
require_once '../model/Article.php';
|
require_once '../model/Article.php';
|
||||||
require_once '../validator/article-validator.php';
|
require_once '../validator/article-validator.php';
|
||||||
|
require_once '../../includes/article-block-helper.php';
|
||||||
|
|
||||||
if (!isset($_SESSION["user_email"])) {
|
if (!isset($_SESSION["user_email"])) {
|
||||||
header("Location: index.php?pfad=login");
|
header("Location: index.php?pfad=login");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Baut die Blockliste aus den POST-Daten (blocks[i][type], blocks[i][text],
|
|
||||||
* blocks[i][existing_image]) und ggf. hochgeladenen Dateien (blocks[i][image])
|
|
||||||
* zusammen. Läuft bei JEDEM Submit (Zwischen-Schritt "Block hinzufügen/löschen"
|
|
||||||
* UND finales Speichern), damit neu ausgewählte Bilder in jedem Fall persistiert
|
|
||||||
* werden, bevor PHP die temporäre Upload-Datei nach Request-Ende verwirft.
|
|
||||||
*
|
|
||||||
* @param array $postBlocks $_POST['blocks'] ?? []
|
|
||||||
* @param array $fileBlocks $_FILES['blocks'] ?? []
|
|
||||||
* @param string $uploadDir absoluter Pfad zum uploads-Verzeichnis (mit trailing slash)
|
|
||||||
* @return array Liste von ['type' => 'text'|'image', 'value' => string]
|
|
||||||
*/
|
|
||||||
function rebuildBlocksFromPost(array $postBlocks, array $fileBlocks, string $uploadDir): array {
|
|
||||||
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
|
|
||||||
|
|
||||||
$keys = array_keys($postBlocks);
|
|
||||||
if (isset($fileBlocks['name']) && is_array($fileBlocks['name'])) {
|
|
||||||
$keys = array_unique(array_merge($keys, array_keys($fileBlocks['name'])));
|
|
||||||
}
|
|
||||||
sort($keys, SORT_NUMERIC);
|
|
||||||
|
|
||||||
$blocks = [];
|
|
||||||
|
|
||||||
foreach ($keys as $key) {
|
|
||||||
$type = $postBlocks[$key]['type'] ?? null;
|
|
||||||
|
|
||||||
if ($type === 'text') {
|
|
||||||
$blocks[] = [
|
|
||||||
'type' => 'text',
|
|
||||||
'value' => $postBlocks[$key]['text'] ?? '',
|
|
||||||
];
|
|
||||||
} elseif ($type === 'image') {
|
|
||||||
// Vorbelegung: bereits vorhandenes Server-Bild (falls Datei nicht ersetzt wird)
|
|
||||||
$value = $postBlocks[$key]['existing_image'] ?? '';
|
|
||||||
|
|
||||||
$hasUpload = isset($fileBlocks['error'][$key]['image'])
|
|
||||||
&& $fileBlocks['error'][$key]['image'] === UPLOAD_ERR_OK;
|
|
||||||
|
|
||||||
if ($hasUpload) {
|
|
||||||
$tmpName = $fileBlocks['tmp_name'][$key]['image'];
|
|
||||||
$originalName = $fileBlocks['name'][$key]['image'];
|
|
||||||
$extension = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
|
|
||||||
if (!in_array($extension, $allowedExtensions, true)) {
|
|
||||||
$extension = 'jpg';
|
|
||||||
}
|
|
||||||
|
|
||||||
$fileName = 'img_' . uniqid() . '.' . $extension;
|
|
||||||
$destination = $uploadDir . $fileName;
|
|
||||||
|
|
||||||
if (move_uploaded_file($tmpName, $destination)) {
|
|
||||||
$value = 'uploads/' . $fileName;
|
|
||||||
}
|
|
||||||
// Bei Fehler: alter Wert (falls vorhanden) bleibt erhalten, Block wird nicht verworfen
|
|
||||||
}
|
|
||||||
|
|
||||||
$blocks[] = [
|
|
||||||
'type' => 'image',
|
|
||||||
'value' => $value,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
// unbekannter/fehlender type -> Block wird ignoriert
|
|
||||||
}
|
|
||||||
|
|
||||||
return $blocks;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
||||||
|
|
||||||
if (isset($_GET["id"]) && !empty($_GET["id"])) {
|
if (isset($_GET["id"]) && !empty($_GET["id"])) {
|
||||||
@@ -118,7 +53,9 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
$_SESSION["old_content"] = json_encode($blocks, JSON_UNESCAPED_UNICODE);
|
$_SESSION["old_content"] = json_encode($blocks, JSON_UNESCAPED_UNICODE);
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Zwischenspeichern
|
// Zwischen-Schritt: Block hinzufügen oder entfernen (kein echtes Speichern).
|
||||||
|
// Wird bei aktivem JavaScript per preventDefault() abgefangen und lokal im
|
||||||
|
// DOM erledigt (js/editor.js) – ohne JS läuft dieser Server-Roundtrip.
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
if (isset($_POST['editor_action']) && $_POST['editor_action'] !== '') {
|
if (isset($_POST['editor_action']) && $_POST['editor_action'] !== '') {
|
||||||
$action = $_POST['editor_action'];
|
$action = $_POST['editor_action'];
|
||||||
@@ -191,7 +128,12 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
// ----------------- Verwaiste Bilder aufräumen -----------------
|
// ----------------- Verwaiste Bilder aufräumen -----------------
|
||||||
// Bilder, die im alten (gespeicherten) Content vorkamen, im neuen aber nicht
|
// Bilder, die im alten (gespeicherten) Content vorkamen, im neuen aber nicht
|
||||||
// mehr referenziert werden, wurden vom Nutzer entfernt oder ersetzt -> löschen.
|
// mehr referenziert werden, wurden vom Nutzer entfernt oder ersetzt -> löschen.
|
||||||
// TODO: Bilder, die innerhalb derselben Bearbeitungs-Sitzung neu hochgeladen und noch vor dem finalen Speichern wieder entfernt wurden, werden hierüber nicht erfasst (sie tauchten nie im alten Content auf) und bleiben als Datei liegen. Für eine vollständige Bereinigung würde sich ein
|
// Hinweis/TODO: Bilder, die innerhalb derselben Bearbeitungs-Sitzung neu
|
||||||
|
// hochgeladen und noch vor dem finalen Speichern wieder entfernt wurden,
|
||||||
|
// werden hierüber nicht erfasst (sie tauchten nie im alten Content auf) und
|
||||||
|
// bleiben als Datei liegen. Für eine vollständige Bereinigung würde sich ein
|
||||||
|
// regelmäßiger Cleanup-Job anbieten, der verwaiste Dateien im uploads/-Ordner
|
||||||
|
// mit den in der Datenbank referenzierten Pfaden abgleicht.
|
||||||
$oldBlocks = json_decode($article->getContent(), true);
|
$oldBlocks = json_decode($article->getContent(), true);
|
||||||
$oldImagePaths = [];
|
$oldImagePaths = [];
|
||||||
if (is_array($oldBlocks)) {
|
if (is_array($oldBlocks)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user