Compare commits

...

13 Commits

Author SHA1 Message Date
caroline.slt 09f8b11f36 Loader 2026-07-19 21:35:23 +02:00
caroline.slt cc4cebdb45 . 2026-07-19 21:16:54 +02:00
niklas.ortmann 96f38ce72e Merge pull request 'Suche nutzt nun Tags' (#59) from suchergebnisseTags into dev
Reviewed-on: #59
2026-07-19 16:29:40 +02:00
niklas.ortmann b81f0c9e5c Update DatabaseArticleManager.php 2026-07-19 16:28:29 +02:00
niklas.ortmann f8aed4283e Update dataSources.local.xml 2026-07-19 16:28:27 +02:00
niklas.ortmann afd90086d8 Update profile.php 2026-07-19 14:20:43 +02:00
niklas.ortmann a7a54f877c Merge pull request 'Beitragseditor funktioniert mit und ohne JS (+Bilder löschen)' (#57) from BeitragseditorOhneJS into dev
Reviewed-on: #57
2026-07-19 13:45:33 +02:00
NOrtmann1 9b9f71d21c Update DatabaseInitializer.php 2026-07-19 13:44:27 +02:00
NOrtmann1 4b84f0ab09 Update updateArticle-controller.php 2026-07-19 13:30:39 +02:00
NOrtmann1 8c8b148bf4 Update updateArticle-controller.php 2026-07-19 13:15:23 +02:00
NOrtmann1 6bf335ce46 Update createArticle.php 2026-07-19 13:15:20 +02:00
NOrtmann1 144a1015f1 Create article-block-helper.php 2026-07-19 13:15:17 +02:00
NOrtmann1 c0a94b2eee Update createArticle-controller.php 2026-07-19 13:15:14 +02:00
16 changed files with 435 additions and 226 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="dataSourceStorageLocal" created-in="IU-261.24374.151"> <component name="dataSourceStorageLocal" created-in="IU-253.32098.101">
<data-source name="articles" uuid="315cb5c9-2b0f-435b-b602-59823b160908"> <data-source name="articles" uuid="315cb5c9-2b0f-435b-b602-59823b160908">
<database-info product="SQLite" version="3.51.1" jdbc-version="4.2" driver-name="SQLite JDBC" driver-version="3.51.1.0" dbms="SQLITE" exact-version="3.51.1" exact-driver-version="3.51"> <database-info product="SQLite" version="3.51.1" jdbc-version="4.2" driver-name="SQLite JDBC" driver-version="3.51.1.0" dbms="SQLITE" exact-version="3.51.1" exact-driver-version="3.51">
<identifier-quote-string>&quot;</identifier-quote-string> <identifier-quote-string>&quot;</identifier-quote-string>
+58 -20
View File
@@ -4,12 +4,28 @@ if (!isset($_SESSION["user"])) {
header("Location: index.php?pfad=login"); header("Location: index.php?pfad=login");
exit(); exit();
} }
// -------------------------------------------------------------------------
// Aktuelle Blockliste ermitteln: Session-Fallback (nach Validierungsfehler
// oder Zwischen-Submit "add_block"/"delete_block") oder leeres Array (Start).
// -------------------------------------------------------------------------
$rawContent = '[]';
if (isset($_SESSION['old_content']) && !empty($_SESSION['old_content'])) {
$rawContent = $_SESSION['old_content'];
unset($_SESSION['old_content']);
}
$blocks = json_decode($rawContent, true);
if (!is_array($blocks)) {
$blocks = [];
}
$blocks = array_values($blocks); // sequentielle Indizes 0..n-1 sicherstellen
?> ?>
<!-- <!--
Seite: Beitrag erstellen Seite: Beitrag erstellen
Inhalt: Formular für die Erstellung eines neuen Beitrags Inhalt: Formular für die Erstellung eines neuen Beitrags
--> -->
<form method="post" action="php/controller/createArticle-controller.php" id="editor-form" class="article-editor-scope.editor-container article-editor-scope editor-container"> <form method="post" action="php/controller/createArticle-controller.php" id="editor-form" enctype="multipart/form-data" class="article-editor-scope.editor-container article-editor-scope editor-container">
<main class="editor-main"> <main class="editor-main">
<?php include_once "includes/alertMessages.php"?> <?php include_once "includes/alertMessages.php"?>
@@ -18,27 +34,49 @@ if (!isset($_SESSION["user"])) {
value="<?php echo htmlspecialchars($_SESSION['old_title'] ?? ''); unset($_SESSION['old_title']); ?>" value="<?php echo htmlspecialchars($_SESSION['old_title'] ?? ''); unset($_SESSION['old_title']); ?>"
placeholder="Titel hier eingeben" required> placeholder="Titel hier eingeben" required>
<!-- Hier werden die dynamischen divs via JavaScript eingefügt --> <!--
<div id="block-container"></div> Content-Blöcke: werden serverseitig als echte, benannte Formularfelder gerendert
(blocks[i][type], blocks[i][text] bzw. blocks[i][image]). Dadurch funktioniert das
Hinzufügen/Entfernen von Blöcken und der Bild-Upload auch ganz ohne JavaScript über
einen normalen Formular-Submit. Ist JavaScript aktiv, fängt js/editor.js diese
Submits ab und erledigt dieselbe Änderung lokal im DOM, ohne den Server zu belasten.
-->
<div id="block-container">
<?php foreach ($blocks as $i => $block): ?>
<?php
$blockType = $block['type'] ?? '';
if ($blockType !== 'text' && $blockType !== 'image') {
continue; // unbekannter/kaputter Block wird übersprungen
}
?>
<div class="editor-block article-editor-scope" data-index="<?php echo (int)$i; ?>">
<input type="hidden" name="blocks[<?php echo (int)$i; ?>][type]" value="<?php echo htmlspecialchars($blockType); ?>">
<!-- Plus-Button und das Pop-up-Menü --> <?php if ($blockType === 'text'): ?>
<div id="add-block-control" class="article-editor-scope add-block-control"> <textarea name="blocks[<?php echo (int)$i; ?>][text]"
<button type="button" id="plus-button" class="article-editor-scope plus-button">+</button> placeholder="Schreibe deinen Textblock..."><?php echo htmlspecialchars($block['value'] ?? ''); ?></textarea>
<div id="block-popup" class="article-editor-scope block-popup hidden"> <?php else: /* image */ ?>
<button type="button" data-type="text">Textblock</button> <?php if (!empty($block['value'])): ?>
<button type="button" data-type="image">Bild einfügen</button> <img src="<?php echo htmlspecialchars($block['value']); ?>"
</div> class="block-image-preview"
style="max-width:200px;display:block;margin-top:10px;">
<input type="hidden" name="blocks[<?php echo (int)$i; ?>][existing_image]" value="<?php echo htmlspecialchars($block['value']); ?>">
<?php endif; ?>
<input type="file" name="blocks[<?php echo (int)$i; ?>][image]" accept="image/*">
<?php endif; ?>
<button type="submit" name="editor_action" value="delete_block:<?php echo (int)$i; ?>" class="delete-block-btn">✕</button>
</div>
<?php endforeach; ?>
</div> </div>
<!-- Unsichtbares Textfeld, das die JSON-Daten hält und an den Controller postet --> <div id="add-block-control" class="article-editor-scope add-block-control">
<textarea id="content" name="content" style="display:none;"><?php <button type="button" id="plus-button" class="article-editor-scope plus-button">+</button>
if (isset($_SESSION['old_content']) && !empty($_SESSION['old_content'])){ <div id="block-popup" class="article-editor-scope block-popup">
echo htmlspecialchars($_SESSION['old_content']); <button type="submit" name="editor_action" value="add_text" data-type="text">Textblock</button>
unset($_SESSION['old_content']); <button type="submit" name="editor_action" value="add_image" data-type="image">Bild einfügen</button>
} else { </div>
echo '[]'; // Standardmäßig ein leeres JSON-Array </div>
}
?></textarea>
</main> </main>
<!-- Seitenleiste --> <!-- Seitenleiste -->
@@ -268,4 +306,4 @@ if (!isset($_SESSION["user"])) {
</aside> </aside>
</form> </form>
+1 -1
View File
@@ -84,7 +84,7 @@ $isEditMode = (isset($_GET["edit"]) && $_GET["edit"] === "1") || !empty($error);
<form action="php/controller/deleteAccount-controller.php" method="POST"> <form action="php/controller/deleteAccount-controller.php" method="POST">
<button type="submit" <button type="submit"
class="button" class="button"
onclick="return confirm('Möchtest du deinen Account wirklich unwiderruflich löschen?');"> onclick="return confirm('Möchtest du deinen Account und alle deine Beiträge wirklich unwiderruflich löschen?');">
Account löschen Account löschen
</button> </button>
</form> </form>
+11
View File
@@ -137,6 +137,17 @@ $categories = [
<p class="s-res-meta"><span id="s-res-result-count"><?php echo $totalResultsCount; ?></span> Treffer für Ihre Suchanfrage "<?php echo htmlspecialchars($query); ?>"</p> <p class="s-res-meta"><span id="s-res-result-count"><?php echo $totalResultsCount; ?></span> Treffer für Ihre Suchanfrage "<?php echo htmlspecialchars($query); ?>"</p>
</div> </div>
<p
id="results-loading"
class="results-loading"
role="status"
aria-live="polite"
hidden
>
<span class="results-spinner" aria-hidden="true"></span>
Suchergebnisse werden geladen …
</p>
<!-- Ergebnisliste --> <!-- Ergebnisliste -->
<div class="s-res-list"> <div class="s-res-list">
<?php if (!empty($results)): ?> <?php if (!empty($results)): ?>
+12
View File
@@ -457,6 +457,18 @@ if ($replyAuthor === null) {
<button type="submit" class="button"> <button type="submit" class="button">
Kommentar senden Kommentar senden
</button> </button>
<p
id="comment-loading"
class="comment-loading"
role="status"
aria-live="polite"
hidden
>
<span class="comment-spinner" aria-hidden="true"></span>
Kommentar wird gesendet …
</p>
</form> </form>
<?php else: ?> <?php else: ?>
<div class="comment-login-hint"> <div class="comment-login-hint">
+11
View File
@@ -113,6 +113,17 @@ $resultCount = count($results);
</p> </p>
</div> </div>
<p
id="results-loading"
class="results-loading"
role="status"
aria-live="polite"
hidden
>
<span class="results-spinner" aria-hidden="true"></span>
Kategoriebeiträge werden geladen …
</p>
<!-- Ergebnisliste --> <!-- Ergebnisliste -->
<div class="s-res-list"> <div class="s-res-list">
<?php if (!empty($results)): ?> <?php if (!empty($results)): ?>
+30
View File
@@ -225,4 +225,34 @@ h1 {
text-align: center; text-align: center;
text-decoration: none; text-decoration: none;
box-sizing: border-box; box-sizing: border-box;
}
.comment-loading {
display: flex;
align-items: center;
gap: 8px;
margin-top: 10px;
}
.comment-loading[hidden] {
display: none;
}
.comment-spinner {
width: 16px;
height: 16px;
border: 2px solid #cbd5e1;
border-top-color: #1f2937;
border-radius: 50%;
animation: comment-spinner-rotation 0.8s linear infinite;
}
@keyframes comment-spinner-rotation {
to {
transform: rotate(360deg);
}
}
#comment-form button[type="submit"]:disabled {
cursor: wait;
opacity: 0.65;
} }
+29
View File
@@ -271,4 +271,33 @@ CSS für die Suchergebnis-Seite
align-items: flex-start; align-items: flex-start;
} }
} }
/* Ladeanzeige beim Nachladen von Such- und Kategorieergebnissen */
.results-loading {
display: flex;
align-items: center;
gap: 8px;
margin: 15px 0;
color: #4a5568;
font-size: 0.95rem;
}
.results-loading[hidden] {
display: none;
}
.results-spinner {
width: 16px;
height: 16px;
flex-shrink: 0;
border: 2px solid #cbd5e1;
border-top-color: #3182ce;
border-radius: 50%;
animation: results-spinner-rotation 0.8s linear infinite;
}
@keyframes results-spinner-rotation {
to {
transform: rotate(360deg);
}
}
+69
View File
@@ -0,0 +1,69 @@
<?php
/**
* 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/Veröffentlichen), damit neu ausgewählte Bilder in jedem
* Fall persistiert werden, bevor PHP die temporäre Upload-Datei nach
* Request-Ende verwirft.
*
* Wird sowohl vom createArticle- als auch vom updateArticle-Controller genutzt.
*
* @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;
}
+38 -1
View File
@@ -11,7 +11,19 @@ document.addEventListener("DOMContentLoaded", function () {
const parentCommentInput = document.getElementById("parent-comment-id"); const parentCommentInput = document.getElementById("parent-comment-id");
const replyInfo = document.getElementById("reply-info"); const replyInfo = document.getElementById("reply-info");
if (!form || !commentsList || !commentContent || !parentCommentInput) { const submitButton = form
? form.querySelector('button[type="submit"]')
: null;
const loadingMessage = document.getElementById("comment-loading");
if (
!form
|| !commentsList
|| !commentContent
|| !parentCommentInput
|| !submitButton
) {
return; return;
} }
@@ -53,6 +65,23 @@ document.addEventListener("DOMContentLoaded", function () {
form.addEventListener("submit", function (event) { form.addEventListener("submit", function (event) {
event.preventDefault(); event.preventDefault();
/*
* Verhindert einen erneuten Submit, während der vorherige
* Kommentar noch gespeichert wird.
*/
if (submitButton.disabled) {
return;
}
const originalButtonText = submitButton.textContent;
submitButton.disabled = true;
submitButton.textContent = "Wird gesendet …";
if (loadingMessage) {
loadingMessage.hidden = false;
}
const formData = new FormData(form); const formData = new FormData(form);
const parentCommentId = parentCommentInput.value; const parentCommentId = parentCommentInput.value;
@@ -269,6 +298,14 @@ document.addEventListener("DOMContentLoaded", function () {
.catch(function (error) { .catch(function (error) {
console.error(error); console.error(error);
alert("Kommentar konnte nicht gesendet werden."); alert("Kommentar konnte nicht gesendet werden.");
})
.finally(function () {
submitButton.disabled = false;
submitButton.textContent = originalButtonText;
if (loadingMessage) {
loadingMessage.hidden = true;
}
}); });
}); });
+23 -4
View File
@@ -25,6 +25,7 @@
let listContainer, sortRadios, categorySelect, limitSelect; let listContainer, sortRadios, categorySelect, limitSelect;
let prevBtn, nextBtn, numbersContainer, jsNav, resultCountEl; let prevBtn, nextBtn, numbersContainer, jsNav, resultCountEl;
let loadingElement;
function init() { function init() {
listContainer = document.querySelector('.s-res-list'); listContainer = document.querySelector('.s-res-list');
@@ -38,23 +39,41 @@
numbersContainer = document.getElementById('dynamic-page-numbers'); numbersContainer = document.getElementById('dynamic-page-numbers');
jsNav = document.getElementById('js-page-navigation'); jsNav = document.getElementById('js-page-navigation');
resultCountEl = document.getElementById('s-res-result-count'); resultCountEl = document.getElementById('s-res-result-count');
loadingElement = document.getElementById('results-loading');
const checkedRadio = document.querySelector('.sort-radio:checked'); const checkedRadio = document.querySelector('.sort-radio:checked');
state.sort = checkedRadio ? checkedRadio.value : 'alphabet'; state.sort = checkedRadio ? checkedRadio.value : 'alphabet';
state.category = categorySelect ? categorySelect.value : 'all'; state.category = categorySelect ? categorySelect.value : 'all';
state.itemsPerPage = limitSelect ? (parseInt(limitSelect.value, 10) || 10) : 10; state.itemsPerPage = limitSelect ? (parseInt(limitSelect.value, 10) || 10) : 10;
if (loadingElement) {
loadingElement.hidden = false;
}
fetchFullDataset() fetchFullDataset()
.then(function (data) { .then(function (data) {
state.allItems = data.results || []; state.allItems = data.results || [];
attachEvents(); attachEvents();
render(); render();
if (jsNav) jsNav.style.display = 'flex';
if (jsNav) {
jsNav.style.display = 'flex';
}
}) })
.catch(function (err) { .catch(function (err) {
// JSON-Endpoint nicht erreichbar: die serverseitig gerenderte /*
// (No-JS-)Ansicht bleibt sichtbar und funktioniert weiter. * Falls das Nachladen nicht funktioniert, bleibt die bereits
console.error('Suchergebnisse konnten nicht nachgeladen werden:', err); * serverseitig ausgegebene Ergebnisliste sichtbar.
*/
console.error(
'Suchergebnisse konnten nicht nachgeladen werden:',
err
);
})
.finally(function () {
if (loadingElement) {
loadingElement.hidden = true;
}
}); });
} }
+23 -4
View File
@@ -21,6 +21,7 @@
let listContainer, sortRadios, searchInput, limitSelect; let listContainer, sortRadios, searchInput, limitSelect;
let prevBtn, nextBtn, numbersContainer, jsNav, resultCountEl; let prevBtn, nextBtn, numbersContainer, jsNav, resultCountEl;
let loadingElement;
let searchDebounceTimer; let searchDebounceTimer;
function init() { function init() {
@@ -35,23 +36,41 @@
numbersContainer = document.getElementById('dynamic-page-numbers'); numbersContainer = document.getElementById('dynamic-page-numbers');
jsNav = document.getElementById('js-page-navigation'); jsNav = document.getElementById('js-page-navigation');
resultCountEl = document.getElementById('s-res-result-count'); resultCountEl = document.getElementById('s-res-result-count');
loadingElement = document.getElementById('results-loading');
const checkedRadio = document.querySelector('.sort-radio:checked'); const checkedRadio = document.querySelector('.sort-radio:checked');
state.sort = checkedRadio ? checkedRadio.value : 'alphabet'; state.sort = checkedRadio ? checkedRadio.value : 'alphabet';
state.query = searchInput ? searchInput.value : ''; state.query = searchInput ? searchInput.value : '';
state.itemsPerPage = limitSelect ? (parseInt(limitSelect.value, 10) || 10) : 10; state.itemsPerPage = limitSelect ? (parseInt(limitSelect.value, 10) || 10) : 10;
if (loadingElement) {
loadingElement.hidden = false;
}
fetchFullDataset() fetchFullDataset()
.then(function (data) { .then(function (data) {
state.allItems = data.results || []; state.allItems = data.results || [];
attachEvents(); attachEvents();
render(); render();
if (jsNav) jsNav.style.display = 'flex';
if (jsNav) {
jsNav.style.display = 'flex';
}
}) })
.catch(function (err) { .catch(function (err) {
// JSON-Endpoint nicht erreichbar: die serverseitig gerenderte /*
// (No-JS-)Ansicht bleibt sichtbar und funktioniert weiter. * Falls das Nachladen nicht funktioniert, bleibt die bereits
console.error('Kategorie-Beiträge konnten nicht nachgeladen werden:', err); * serverseitig ausgegebene Kategorieansicht sichtbar.
*/
console.error(
'Kategorie-Beiträge konnten nicht nachgeladen werden:',
err
);
})
.finally(function () {
if (loadingElement) {
loadingElement.hidden = true;
}
}); });
} }
+100 -114
View File
@@ -5,134 +5,120 @@ if (session_status() === PHP_SESSION_NONE) {
require_once '../model/LocalArticleManager.php'; require_once '../model/LocalArticleManager.php';
require_once '../model/ArticleManager.php'; require_once '../model/ArticleManager.php';
require_once '../validator/article-validator.php'; require_once '../validator/article-validator.php';
require_once '../../includes/article-block-helper.php';
if (!isset($_SESSION["user"])) { if (!isset($_SESSION["user"])) {
header("Location: index.php?pfad=login"); header("Location: index.php?pfad=login");
exit(); exit();
} }
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$_SESSION["old_title"] = $_POST["title"] ?? '';
$_SESSION["old_content"] = $_POST["content"] ?? '';
$_SESSION["old_category"] = $_POST["category"] ?? '';
$_SESSION["old_tags"] = $_POST["tags"] ?? '';
if(!isset($_POST["title"]) ||!isset($_POST["content"]) || !isset($_POST["category"])){ if ($_SERVER["REQUEST_METHOD"] === "POST") {
$_SESSION["message"] = "missing_parameters";
$uploadDir = __DIR__ . '/../../uploads/';
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
// Formularzustand (Titel/Tags/Kategorie/Blöcke) immer sichern, damit er nach einem
// Redirect (PRG-Pattern oder Validierungsfehler) wieder angezeigt werden kann.
$_SESSION["old_title"] = $_POST["title"] ?? '';
$_SESSION["old_category"] = $_POST["category"] ?? '';
$_SESSION["old_tags"] = $_POST["tags"] ?? '';
$blocks = rebuildBlocksFromPost($_POST['blocks'] ?? [], $_FILES['blocks'] ?? [], $uploadDir);
$_SESSION["old_content"] = json_encode($blocks, JSON_UNESCAPED_UNICODE);
// ---------------------------------------------------------------------
// Zwischenspeichern
// ---------------------------------------------------------------------
if (isset($_POST['editor_action']) && $_POST['editor_action'] !== '') {
$action = $_POST['editor_action'];
if ($action === 'add_text') {
$blocks[] = ['type' => 'text', 'value' => ''];
} elseif ($action === 'add_image') {
$blocks[] = ['type' => 'image', 'value' => ''];
} elseif (str_starts_with($action, 'delete_block:')) {
$deleteIndex = (int) substr($action, strlen('delete_block:'));
unset($blocks[$deleteIndex]);
$blocks = array_values($blocks);
}
$_SESSION["old_content"] = json_encode($blocks, JSON_UNESCAPED_UNICODE);
header("location: ../../index.php?pfad=createArticle");
exit();
}
// ---------------------------------------------------------------------
// Echtes Veröffentlichen
// ---------------------------------------------------------------------
if (!isset($_POST["title"]) || !isset($_POST["category"])) {
$_SESSION["message"] = "missing_parameters";
header("location: ../../index.php?pfad=createArticle");
exit();
} else {
$title = $_POST["title"];
$content = json_encode($blocks, JSON_UNESCAPED_UNICODE);
$author = $_SESSION["user_email"];
$category = $_POST["category"];
$tags = $_POST['tags'] ?? '';
// -------------------------------- Validierung der Daten: -------------------------
if (!articleTitleValidator($title)) {
$_SESSION["message"] = "invalid_title";
header("location: ../../index.php?pfad=createArticle");
exit();
}
if (!articleContentValidator($content)) {
$_SESSION["message"] = "invalid_content";
header("location: ../../index.php?pfad=createArticle");
exit();
}
if (!articleCategoryValidator($category)) {
$_SESSION["message"] = "invalid_category";
header("location: ../../index.php?pfad=createArticle");
exit();
}
if (!articleTagValidator($tags)) {
$_SESSION["message"] = "invalid_tags";
header("location: ../../index.php?pfad=createArticle"); header("location: ../../index.php?pfad=createArticle");
exit(); exit();
} else { } else {
$title = $_POST["title"]; $cleanedTags = [];
$content = $_POST["content"]; $rawTags = explode(',', $tags);
$author = $_SESSION["user_email"]; foreach ($rawTags as $rawTag) {
$category = $_POST["category"]; // Leerzeichen am Anfang/Ende des einzelnen Tags entfernen:
$tags = $_POST['tags'] ?? ''; $tag = trim($rawTag);
$cleanedTags[] = $tag;
// -------------------------------- Validierung der Daten: -------------------------
if (!articleTitleValidator($title)) {
$_SESSION["message"] = "invalid_title";
header("location: ../../index.php?pfad=createArticle");
exit();
} }
// Duplikate entfernen:
$cleanedTags = array_unique($cleanedTags);
$cleanedTags = implode(',', $cleanedTags);
}
if (!articleContentValidator($content)) { // ----------------- Übertragung der validierten Daten in ArticleManager: ---------------------------
$_SESSION["message"] = "invalid_content"; try {
header("location: ../../index.php?pfad=createArticle"); $articleManager = ArticleManager::getInstance();
exit(); // $content enthält bereits die finalen "uploads/..."-Pfade (kein Base64 mehr),
} // da rebuildBlocksFromPost() Datei-Uploads sofort verarbeitet.
$articleManager->addArticle($title, $content, $author, $category, $cleanedTags);
if (!articleCategoryValidator($category)) { // Formulardaten nach erfolgreichem Erstellen aus der Session löschen
$_SESSION["message"] = "invalid_category"; unset($_SESSION["old_title"], $_SESSION["old_content"], $_SESSION["old_category"], $_SESSION["old_tags"]);
header("location: ../../index.php?pfad=createArticle");
exit();
}
if (!articleTagValidator($tags)) { } catch (\Throwable $e) {
$_SESSION["message"] = "invalid_tags"; $_SESSION["message"] = "internal_error";
header("location: ../../index.php?pfad=createArticle"); header("location: ../../index.php?pfad=createArticle");
exit();
} else {
$cleanedTags = [];
$rawTags = explode(',', $tags);
foreach ($rawTags as $rawTag) {
// Leerzeichen am Anfang/Ende des einzelnen Tags entfernen:
$tag = trim($rawTag);
$cleanedTags[] = $tag;
}
// Duplikate entfernen:
$cleanedTags = array_unique($cleanedTags);
$cleanedTags = implode(',', $cleanedTags);
}
// ----------------- Base64-Bilder verarbeiten und auf Server speichern -----------------
$blocks = json_decode($content, true);
$uploadDir = __DIR__ . '/../../uploads/';
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
if (is_array($blocks)) {
foreach ($blocks as &$block) {
// sicherstellen, dass 'type' und 'value' existieren:
if (isset($block['type']) && isset($block['value']) && $block['type'] === 'image' && str_starts_with($block['value'], 'data:image/')) {
// Base64-String zerlegen
$parts = explode(',', $block['value']);
// falls der String korrupt ist und kein Komma hat
if (count($parts) < 2) {
continue;
}
$metadata = $parts[0];
$base64Data = $parts[1];
// Dateiendung ermitteln
preg_match('/data:image\/(?<extension>.*?);/', $metadata, $matches);
$extension = $matches['extension'] ?? 'jpg';
if ($extension === 'jpeg') {
$extension = 'jpg';
}
// Eindeutigen Dateinamen generieren
$fileName = 'img_' . uniqid() . '.' . $extension;
$filePath = $uploadDir . $fileName;
// Datei im /uploads speichern:
if (file_put_contents($filePath, base64_decode($base64Data)) !== false) {
// temporären Base64-String durch den echten Pfad ersetzen
$block['value'] = 'uploads/' . $fileName;
} else {
$_SESSION["message"] = "image_upload_error";
header("location: ../../index.php?pfad=createArticle");
exit();
}
}
}
unset($block);
}
// Aktualisiertes Array wieder in JSON konvertieren
$finalContent = json_encode($blocks, JSON_UNESCAPED_UNICODE);
// ----------------- Übertragung der validierten Daten in ArticleManager: ---------------------------
try {
$articleManager = ArticleManager::getInstance();
$articleManager->addArticle($title, $content, $author, $category, $cleanedTags);
// Formulardaten nach erfolgreichem Erstellen aus der Session löschen
unset($_SESSION["old_title"], $_SESSION["old_content"], $_SESSION["old_category"], $_SESSION["old_tags"]);
} catch (\Throwable $e){
$_SESSION["message"] = "internal_error";
header("location: ../../index.php?pfad=createArticle");
exit();
}
$_SESSION["message"] = "new_article";
// Weiterleitung zur Homepage
header("location: ../../index.php");
exit(); exit();
} }
$_SESSION["message"] = "new_article";
// Weiterleitung zur Homepage
header("location: ../../index.php");
exit();
} }
}
?> ?>
+12 -70
View File
@@ -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"])) {
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'];
@@ -134,7 +71,7 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
} }
$_SESSION["old_content"] = json_encode($blocks, JSON_UNESCAPED_UNICODE); $_SESSION["old_content"] = json_encode($blocks, JSON_UNESCAPED_UNICODE);
header("location: ../../content/updateArticle.php?id=$id"); header("location: ../../index.php?pfad=updateArticle&id=$id");
exit(); exit();
} }
@@ -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)) {
+10 -9
View File
@@ -310,21 +310,20 @@ class DatabaseArticleManager implements ArticleManagerDAO {
try { try {
$db = $this->getConnection(); $db = $this->getConnection();
$sql = "SELECT id, title, content, author, category, tags, created $sql = "SELECT id, title, content, author, category, tags, created
FROM articles FROM articles
WHERE title LIKE :keyword WHERE title LIKE :keyword
OR content LIKE :keyword OR content LIKE :keyword
OR tags LIKE :keyword"; OR tags LIKE :keyword;";
$command = $db->prepare($sql); $command = $db->prepare($sql);
if (!$command) { if (!$command) {
throw new InternalServerErrorException("internal_error"); throw new InternalServerErrorException("internal_error");
} }
// Wildcards für die Suche hinzufügen // Wildcards für die SQL-Suche hinzufügen
$searchParam = '%' . $cleankeyword . '%'; $searchParam = '%' . $cleankeyword . '%';
$success = $command->execute([ $success = $command->execute([
":keyword" => $searchParam ":keyword" => $searchParam
]); ]);
@@ -337,10 +336,11 @@ class DatabaseArticleManager implements ArticleManagerDAO {
$filteredArticles = []; $filteredArticles = [];
foreach ($rows as $row) { foreach ($rows as $row) {
$likes = $this->getLikesForArticle(intval($row['id'])); $articleId = intval($row['id']);
$likes = $this->getLikesForArticle($articleId);
$filteredArticles[] = new Article( $filteredArticles[] = new Article(
intval($row['id']), $articleId,
$row['title'] ?? '', $row['title'] ?? '',
$row['content'] ?? '', $row['content'] ?? '',
$row['author'] ?? '', $row['author'] ?? '',
@@ -358,6 +358,7 @@ class DatabaseArticleManager implements ArticleManagerDAO {
} }
} }
/** /**
* Holt alle User-IDs, die einen bestimmten Beitrag geliked haben. * Holt alle User-IDs, die einen bestimmten Beitrag geliked haben.
* *
+7 -2
View File
@@ -234,13 +234,18 @@ class DatabaseInitializer {
// Bestimmt per Zufall einen Autor aus dem Pool der gültigen E-Mails // Bestimmt per Zufall einen Autor aus dem Pool der gültigen E-Mails
$randomAuthor = $availableEmails[array_rand($availableEmails)]; $randomAuthor = $availableEmails[array_rand($availableEmails)];
// Text in (blocks[i][type]/[text]/[image])-Format umwandeln:
$content = json_encode([
['type' => 'text', 'value' => $article[1]]
], JSON_UNESCAPED_UNICODE);
$articleInsertStmt->execute([ $articleInsertStmt->execute([
':title' => $article[0], ':title' => $article[0],
':content' => $article[1], ':content' => $content,
':author' => $randomAuthor, ':author' => $randomAuthor,
':category' => $article[2], ':category' => $article[2],
':tags' => $article[3] ':tags' => $article[3]
]); ]);
} }
} }
} }