fixes and new notification backend service
This commit is contained in:
1 parent
1ad59a9881
commit
133b1d57ed
5 files changed
+322
-1
No files matched your search
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
session_set_cookie_params([
|
||||
'lifetime' => 0,
|
||||
'path' => '/',
|
||||
'domain' => '',
|
||||
'secure' => false,
|
||||
'httponly' => true,
|
||||
'samesite' => 'Lax'
|
||||
]);
|
||||
session_start();
|
||||
|
||||
header('Content-Type: application/json');
|
||||
error_reporting(E_ALL);
|
||||
ini_set('display_errors', '0');
|
||||
|
||||
if (empty($_SESSION['user_logged_in'])) {
|
||||
http_response_code(401);
|
||||
echo json_encode(['error' => 'Nicht eingeloggt']);
|
||||
exit;
|
||||
}
|
||||
|
||||
if (empty($_SESSION['is_admin'])) {
|
||||
http_response_code(403);
|
||||
echo json_encode(['error' => 'Keine Berechtigung']);
|
||||
exit;
|
||||
}
|
||||
|
||||
include_once(__DIR__ . '/../../../controller/DatabaseController.php');
|
||||
include_once(__DIR__ . '/../../../controller/NotifyController.php');
|
||||
|
||||
$daten = json_decode(file_get_contents('php://input'), true) ?? [];
|
||||
$action = $daten['action'] ?? '';
|
||||
|
||||
try {
|
||||
$notificationCtl = new Notification();
|
||||
|
||||
switch ($action) {
|
||||
|
||||
case 'get_entries': {
|
||||
$entries = $notificationCtl->getAllMessages();
|
||||
|
||||
foreach ($entries as &$entry) {
|
||||
$entry['GESEHEN_ANZAHL'] = $notificationCtl->countSeenBy((int)$entry['ID']);
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true, 'entries' => array_values($entries)]);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'add_entry': {
|
||||
$titel = trim($daten['titel'] ?? '');
|
||||
$nachricht = trim($daten['nachricht'] ?? '');
|
||||
|
||||
if ($titel === '' || $nachricht === '') {
|
||||
throw new InvalidArgumentException('Titel und Nachricht erforderlich');
|
||||
}
|
||||
|
||||
$notificationCtl->createMessage($titel, $nachricht);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'edit_entry': {
|
||||
$id = $daten['id'] ?? null;
|
||||
$titel = trim($daten['titel'] ?? '');
|
||||
$nachricht = trim($daten['nachricht'] ?? '');
|
||||
|
||||
if (!is_numeric($id)) {
|
||||
throw new InvalidArgumentException('Ungültige ID');
|
||||
}
|
||||
if ($titel === '' || $nachricht === '') {
|
||||
throw new InvalidArgumentException('Titel und Nachricht erforderlich');
|
||||
}
|
||||
|
||||
$notificationCtl->updateMessage((int)$id, $titel, $nachricht);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'delete_entry': {
|
||||
$id = $daten['id'] ?? null;
|
||||
|
||||
if (!is_numeric($id)) {
|
||||
throw new InvalidArgumentException('Ungültige ID');
|
||||
}
|
||||
|
||||
$notificationCtl->deleteMessage((int)$id);
|
||||
|
||||
echo json_encode(['success' => true]);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
throw new InvalidArgumentException("Unbekannte Aktion: $action");
|
||||
}
|
||||
} catch (InvalidArgumentException $e) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => $e->getMessage()]);
|
||||
} catch (Throwable $e) {
|
||||
http_response_code(500);
|
||||
error_log($e->getMessage());
|
||||
echo json_encode(['error' => 'Serverfehler']);
|
||||
}
|
||||
@@ -129,4 +129,49 @@ class Notification
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function updateMessage(int $id, string $titel, string $nachricht): bool
|
||||
{
|
||||
try {
|
||||
DatabaseUpdate(
|
||||
$this->table,
|
||||
"TITEL = ?, NACHRICHT = ?",
|
||||
"ID = ?",
|
||||
$titel,
|
||||
$nachricht,
|
||||
$id
|
||||
);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Löscht eine Nachricht komplett, inkl. aller "Gesehen"-Einträge dazu.
|
||||
*/
|
||||
public function deleteMessage(int $id): bool
|
||||
{
|
||||
try {
|
||||
DatabaseDelete($this->seenTable, "NACHRICHTEN_ID = ?", $id);
|
||||
DatabaseDelete($this->table, "ID = ?", $id);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Anzahl Nutzer, die eine bestimmte Nachricht bereits bestätigt haben.
|
||||
*/
|
||||
public function countSeenBy(int $id): int
|
||||
{
|
||||
$rows = DatabaseShow(
|
||||
$this->seenTable,
|
||||
"ID",
|
||||
"NACHRICHTEN_ID = ?",
|
||||
$id
|
||||
);
|
||||
return count($rows);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
const BENACHRICHTIGUNGEN_HANDLER_URL = '/api/js/handler/BenachrichtigungenAjaxHandler';
|
||||
|
||||
async function benachrichtigungenAjaxRequest(payload) {
|
||||
try {
|
||||
const response = await fetch(BENACHRICHTIGUNGEN_HANDLER_URL, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
const data = await parseJsonResponse(response);
|
||||
|
||||
if (data.error) {
|
||||
throw new Error(data.error);
|
||||
}
|
||||
return data;
|
||||
|
||||
} catch (error) {
|
||||
console.error('Benachrichtigungen-Ajax-Fehler:', error.message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function getNotificationEntries() {
|
||||
return benachrichtigungenAjaxRequest({ action: 'get_entries' });
|
||||
}
|
||||
|
||||
function addNotificationEntry(titel, nachricht) {
|
||||
return benachrichtigungenAjaxRequest({ action: 'add_entry', titel, nachricht });
|
||||
}
|
||||
|
||||
function editNotificationEntry(id, titel, nachricht) {
|
||||
return benachrichtigungenAjaxRequest({ action: 'edit_entry', id, titel, nachricht });
|
||||
}
|
||||
|
||||
function deleteNotificationEntry(id) {
|
||||
return benachrichtigungenAjaxRequest({ action: 'delete_entry', id });
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
const NOTIFICATION_HANDLER_URL = '/api/js/handler/NotificationAjaxHandler.php';
|
||||
const NOTIFICATION_HANDLER_URL = '/api/js/handler/NotificationAjaxHandler';
|
||||
|
||||
async function notificationAjaxRequest(payload) {
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
include_once(__DIR__ . '/../controller/NotifyController.php');
|
||||
?>
|
||||
|
||||
<div class="arbeitsberichte-wrapper">
|
||||
<div class="vergabe-form">
|
||||
<h3>Neue Nachricht anlegen</h3>
|
||||
<form id="addNotificationForm" class="vergabe-grid">
|
||||
|
||||
<div class="form-group">
|
||||
<label>Titel</label>
|
||||
<input type="text" name="titel" id="in-titel" required>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label>Nachricht</label>
|
||||
<input type="text" name="nachricht" id="in-nachricht" required>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn-submit-vergabe">💾 Speichern</button>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="table-container" style="margin-top: 30px;">
|
||||
<h3>Nachrichten Verwaltung</h3>
|
||||
<div id="tableWrapper">
|
||||
<p>Lade Daten...</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="/js/AjaxMethods.js"></script>
|
||||
<script src="/js/ajaxApi/BenachrichtigungenAjaxMethods.js"></script>
|
||||
<script src="/js/AjaxRenderTables.js"></script>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const tableWrapper = document.getElementById('tableWrapper');
|
||||
|
||||
function renderNotificationsTabelle(entries) {
|
||||
if (!entries || entries.length === 0) {
|
||||
tableWrapper.innerHTML = '<p>Keine Nachrichten vorhanden.</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
let html = `
|
||||
<table class="db-table mobile-responsive-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Titel</th><th>Nachricht</th><th>Gesehen von</th>
|
||||
<th style="width: 220px;">Aktion</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
`;
|
||||
for (const e of entries) {
|
||||
html += `
|
||||
<tr data-id="${escapeHtml(e.ID)}">
|
||||
<td data-label="Titel">
|
||||
<input type="text" class="edit-titel" value="${escapeHtml(e.TITEL)}">
|
||||
</td>
|
||||
<td data-label="Nachricht">
|
||||
<input type="text" class="edit-nachricht" value="${escapeHtml(e.NACHRICHT)}">
|
||||
</td>
|
||||
<td data-label="Gesehen von">${escapeHtml(e.GESEHEN_ANZAHL)} Nutzer</td>
|
||||
<td data-label="Aktion">
|
||||
<div style="display:flex; flex-direction:column; gap:6px; align-items:stretch;">
|
||||
<button type="button" class="btn-edit" data-id="${escapeHtml(e.ID)}">Speichern</button>
|
||||
<button type="button" class="btn-delete" data-id="${escapeHtml(e.ID)}">Löschen</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>`;
|
||||
}
|
||||
html += `</tbody></table>`;
|
||||
tableWrapper.innerHTML = html;
|
||||
|
||||
tableWrapper.querySelectorAll('.btn-edit').forEach(btn => {
|
||||
btn.addEventListener('click', async () => {
|
||||
const row = tableWrapper.querySelector(`tr[data-id="${btn.dataset.id}"]`);
|
||||
const titel = row.querySelector('.edit-titel').value.trim();
|
||||
const nachricht = row.querySelector('.edit-nachricht').value.trim();
|
||||
|
||||
if (!titel || !nachricht) return;
|
||||
|
||||
try {
|
||||
await editNotificationEntry(btn.dataset.id, titel, nachricht);
|
||||
await ladeListe();
|
||||
} catch (err) {
|
||||
alert('Fehler beim Speichern: ' + err.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
tableWrapper.querySelectorAll('.btn-delete').forEach(btn => {
|
||||
btn.addEventListener('click', async () => {
|
||||
if (!confirm('Möchtest du diese Nachricht wirklich löschen? Dies entfernt auch alle "Gesehen"-Markierungen dazu.')) return;
|
||||
try {
|
||||
await deleteNotificationEntry(btn.dataset.id);
|
||||
await ladeListe();
|
||||
} catch (err) {
|
||||
alert('Fehler beim Löschen: ' + err.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function ladeListe() {
|
||||
try {
|
||||
const data = await getNotificationEntries();
|
||||
renderNotificationsTabelle(data.entries);
|
||||
} catch (err) {
|
||||
tableWrapper.innerHTML = `<p>❌ ${escapeHtml(err.message)}</p>`;
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('addNotificationForm').addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
const titel = document.getElementById('in-titel').value.trim();
|
||||
const nachricht = document.getElementById('in-nachricht').value.trim();
|
||||
|
||||
if (!titel || !nachricht) return;
|
||||
|
||||
try {
|
||||
await addNotificationEntry(titel, nachricht);
|
||||
e.target.reset();
|
||||
await ladeListe();
|
||||
} catch (err) {
|
||||
alert('Fehler beim Speichern: ' + err.message);
|
||||
}
|
||||
});
|
||||
|
||||
ladeListe();
|
||||
});
|
||||
</script>
|
||||
Reference in new issue
Block a user