author can be edited and added in news moderation page

This commit is contained in:
2026-04-24 17:41:59 +02:00
parent 125c255115
commit fa984e7391
2 changed files with 23 additions and 10 deletions

View File

@@ -354,7 +354,7 @@ $counts['total'] = count($all_contributions);
<?= nl2br(htmlspecialchars($news['content'])) ?> <?= nl2br(htmlspecialchars($news['content'])) ?>
</div> </div>
<div class="action-buttons"> <div class="action-buttons">
<button class="btn btn-edit" onclick="editNews(<?= $news['news_id'] ?>, '<?= htmlspecialchars(addslashes($news['title']), ENT_QUOTES) ?>', '<?= htmlspecialchars(addslashes($news['content']), ENT_QUOTES) ?>')"> <button class="btn btn-edit" onclick="editNews(<?= $news['news_id'] ?>, '<?= htmlspecialchars(addslashes($news['title']), ENT_QUOTES) ?>', '<?= htmlspecialchars(addslashes($news['content']), ENT_QUOTES) ?>', '<?= htmlspecialchars(addslashes($news['author_name']), ENT_QUOTES) ?>')">
<i class="fa-solid fa-pen"></i> Bearbeiten <i class="fa-solid fa-pen"></i> Bearbeiten
</button> </button>
<button class="btn btn-delete" onclick="deleteNews(<?= $news['news_id'] ?>)"> <button class="btn btn-delete" onclick="deleteNews(<?= $news['news_id'] ?>)">
@@ -710,10 +710,14 @@ $counts['total'] = count($all_contributions);
'<label style="display:block;font-weight:600;font-size:1.15rem;margin-bottom:4px;">Titel</label>' + '<label style="display:block;font-weight:600;font-size:1.15rem;margin-bottom:4px;">Titel</label>' +
'<input id="swal-news-title" class="swal2-input" style="margin:0;width:100%;" placeholder="Titel der Neuigkeit">' + '<input id="swal-news-title" class="swal2-input" style="margin:0;width:100%;" placeholder="Titel der Neuigkeit">' +
'</div>' + '</div>' +
'<div>' + '<div style="margin-bottom:12px;">' +
'<label style="display:block;font-weight:600;font-size:1.15rem;margin-bottom:4px;">Inhalt</label>' + '<label style="display:block;font-weight:600;font-size:1.15rem;margin-bottom:4px;">Inhalt</label>' +
'<textarea id="swal-news-content" class="swal2-textarea" style="margin:0;width:100%;" placeholder="Neuigkeit verfassen..."></textarea>' + '<textarea id="swal-news-content" class="swal2-textarea" style="margin:0;width:100%;" placeholder="Neuigkeit verfassen..."></textarea>' +
'</div>' + '</div>' +
'<div>' +
'<label style="display:block;font-weight:600;font-size:1.15rem;margin-bottom:4px;">Autor</label>' +
'<input id="swal-news-author" class="swal2-input" style="margin:0;width:100%;" value="Stadtverwaltung">' +
'</div>' +
'</div>', '</div>',
showCancelButton: true, showCancelButton: true,
confirmButtonText: 'Veröffentlichen', confirmButtonText: 'Veröffentlichen',
@@ -722,11 +726,12 @@ $counts['total'] = count($all_contributions);
preConfirm: function () { preConfirm: function () {
const title = document.getElementById('swal-news-title').value.trim(); const title = document.getElementById('swal-news-title').value.trim();
const content = document.getElementById('swal-news-content').value.trim(); const content = document.getElementById('swal-news-content').value.trim();
const author = document.getElementById('swal-news-author').value.trim() || 'Stadtverwaltung';
if (!title || !content) { if (!title || !content) {
Swal.showValidationMessage('Titel und Inhalt sind Pflichtfelder.'); Swal.showValidationMessage('Titel und Inhalt sind Pflichtfelder.');
return false; return false;
} }
return { title: title, content: content }; return { title: title, content: content, author_name: author };
} }
}).then(function (result) { }).then(function (result) {
if (!result.isConfirmed) return; if (!result.isConfirmed) return;
@@ -736,6 +741,7 @@ $counts['total'] = count($all_contributions);
formData.append('municipality_id', MUNICIPALITY_ID); formData.append('municipality_id', MUNICIPALITY_ID);
formData.append('title', result.value.title); formData.append('title', result.value.title);
formData.append('content', result.value.content); formData.append('content', result.value.content);
formData.append('author_name', result.value.author_name);
fetch(API_URL, { method: 'POST', body: formData }) fetch(API_URL, { method: 'POST', body: formData })
.then(function (r) { return r.json(); }) .then(function (r) { return r.json(); })
@@ -754,7 +760,7 @@ $counts['total'] = count($all_contributions);
// ============================================================= // =============================================================
// Edit News Article // Edit News Article
// ============================================================= // =============================================================
function editNews(newsId, currentTitle, currentContent) { function editNews(newsId, currentTitle, currentContent, currentAuthor) {
Swal.fire({ Swal.fire({
title: 'Neuigkeit bearbeiten', title: 'Neuigkeit bearbeiten',
html: html:
@@ -763,10 +769,14 @@ $counts['total'] = count($all_contributions);
'<label style="display:block;font-weight:600;font-size:1.15rem;margin-bottom:4px;">Titel</label>' + '<label style="display:block;font-weight:600;font-size:1.15rem;margin-bottom:4px;">Titel</label>' +
'<input id="swal-news-title" class="swal2-input" style="margin:0;width:100%;" value="' + currentTitle + '">' + '<input id="swal-news-title" class="swal2-input" style="margin:0;width:100%;" value="' + currentTitle + '">' +
'</div>' + '</div>' +
'<div>' + '<div style="margin-bottom:12px;">' +
'<label style="display:block;font-weight:600;font-size:1.15rem;margin-bottom:4px;">Inhalt</label>' + '<label style="display:block;font-weight:600;font-size:1.15rem;margin-bottom:4px;">Inhalt</label>' +
'<textarea id="swal-news-content" class="swal2-textarea" style="margin:0;width:100%;">' + currentContent + '</textarea>' + '<textarea id="swal-news-content" class="swal2-textarea" style="margin:0;width:100%;">' + currentContent + '</textarea>' +
'</div>' + '</div>' +
'<div>' +
'<label style="display:block;font-weight:600;font-size:1.15rem;margin-bottom:4px;">Autor</label>' +
'<input id="swal-news-author" class="swal2-input" style="margin:0;width:100%;" value="' + currentAuthor + '">' +
'</div>' +
'</div>', '</div>',
showCancelButton: true, showCancelButton: true,
confirmButtonText: 'Speichern', confirmButtonText: 'Speichern',
@@ -775,7 +785,8 @@ $counts['total'] = count($all_contributions);
preConfirm: function () { preConfirm: function () {
return { return {
title: document.getElementById('swal-news-title').value.trim(), title: document.getElementById('swal-news-title').value.trim(),
content: document.getElementById('swal-news-content').value.trim() content: document.getElementById('swal-news-content').value.trim(),
author_name: document.getElementById('swal-news-author').value.trim() || 'Stadtverwaltung'
}; };
} }
}).then(function (result) { }).then(function (result) {
@@ -786,6 +797,7 @@ $counts['total'] = count($all_contributions);
formData.append('news_id', newsId); formData.append('news_id', newsId);
formData.append('title', result.value.title); formData.append('title', result.value.title);
formData.append('content', result.value.content); formData.append('content', result.value.content);
formData.append('author_name', result.value.author_name);
fetch(API_URL, { method: 'POST', body: formData }) fetch(API_URL, { method: 'POST', body: formData })
.then(function (r) { return r.json(); }) .then(function (r) { return r.json(); })

View File

@@ -382,13 +382,14 @@ function handle_create_news($input) {
try { try {
$stmt = $pdo->prepare(" $stmt = $pdo->prepare("
INSERT INTO news (municipality_id, title, content) INSERT INTO news (municipality_id, title, content, author_name)
VALUES (:mid, :title, :content) VALUES (:mid, :title, :content, :author)
"); ");
$stmt->execute([ $stmt->execute([
':mid' => $input['municipality_id'], ':mid' => $input['municipality_id'],
':title' => $input['title'], ':title' => $input['title'],
':content' => $input['content'] ':content' => $input['content'],
':author' => $input['author_name'] ?? 'Stadtverwaltung'
]); ]);
json_response(['message' => 'News created successfully.', 'news_id' => (int) $pdo->lastInsertId()], 201); json_response(['message' => 'News created successfully.', 'news_id' => (int) $pdo->lastInsertId()], 201);
} catch (PDOException $e) { } catch (PDOException $e) {
@@ -411,7 +412,7 @@ function handle_update_news($input) {
$set = []; $set = [];
$params = [':id' => $input['news_id']]; $params = [':id' => $input['news_id']];
foreach (['title', 'content'] as $field) { foreach (['title', 'content', 'author_name'] as $field) {
if (isset($input[$field]) && $input[$field] !== '') { if (isset($input[$field]) && $input[$field] !== '') {
$set[] = "$field = :$field"; $set[] = "$field = :$field";
$params[":$field"] = $input[$field]; $params[":$field"] = $input[$field];