diff --git a/public/assets/js/admin-notifications.js b/public/assets/js/admin-notifications.js index 7d24e1d..35db5b1 100644 --- a/public/assets/js/admin-notifications.js +++ b/public/assets/js/admin-notifications.js @@ -38,3 +38,23 @@ function sendNotification() { btn.innerHTML = ' Send'; }); } + +function confirmDelete(id, title) { + if (!confirm('Delete notification "' + title + '"? This action cannot be undone.')) return; + + const csrf = document.querySelector('meta[name="csrf-token"]')?.content || ''; + fetch('/admin/notifications/' + id + '/delete', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'X-CSRF-Token': csrf, + }, + body: '_csrf_token=' + encodeURIComponent(csrf), + }) + .then(r => r.json()) + .then(data => { + showToast(data.success ? 'success' : 'error', data.message); + if (data.success) location.reload(); + }) + .catch(() => showToast('error', 'Failed to delete notification')); +} diff --git a/routes/web.php b/routes/web.php index 245b034..0d5f359 100755 --- a/routes/web.php +++ b/routes/web.php @@ -115,6 +115,7 @@ $router->group(['prefix' => 'admin', 'middleware' => [AuthMiddleware::class, Rol $router->post('/app-version', [AdminController::class, 'appVersion'], [CSRFMiddleware::class]); $router->get('/notifications', [AdminController::class, 'notifications']); $router->post('/notifications/send', [AdminController::class, 'sendNotification'], [CSRFMiddleware::class]); + $router->post('/notifications/:id/delete', [AdminController::class, 'deleteNotification'], [CSRFMiddleware::class]); $router->get('/policies', [AdminController::class, 'policies']); $router->post('/policies', [AdminController::class, 'savePolicies'], [CSRFMiddleware::class]); diff --git a/src/Controllers/AdminController.php b/src/Controllers/AdminController.php index 97e7988..036edc2 100755 --- a/src/Controllers/AdminController.php +++ b/src/Controllers/AdminController.php @@ -295,6 +295,26 @@ class AdminController ]); } + public function deleteNotification(int $id): void + { + $this->requireSuperAdmin(); + + $notificationModel = new Notification(); + $note = $notificationModel->findById($id); + + if (!$note) { + $this->view->json(['success' => false, 'message' => 'Notification not found.'], 404); + } + + $notificationModel->delete($id); + + $this->auditService->log('notification_deleted', 'notification', $id, [ + 'title' => $note['title'], + ]); + + $this->view->json(['success' => true, 'message' => 'Notification deleted.']); + } + public function sendNotification(): void { $this->requireSuperAdmin(); diff --git a/src/Models/Notification.php b/src/Models/Notification.php index b7993f9..87821c6 100644 --- a/src/Models/Notification.php +++ b/src/Models/Notification.php @@ -143,4 +143,10 @@ class Notification $result = $this->db->fetch("SELECT COUNT(*) as total FROM users WHERE status = 'active'"); return (int) ($result['total'] ?? 0); } + + public function delete(int $id): void + { + $this->db->delete('notifications', 'id = ?', [$id]); + $this->db->delete('notification_reads', 'notification_id = ?', [$id]); + } } diff --git a/views/admin/notifications.php b/views/admin/notifications.php index 313575e..eb9b659 100644 --- a/views/admin/notifications.php +++ b/views/admin/notifications.php @@ -29,12 +29,13 @@ $data = $notifications['data'] ?? []; Message Target Read by + Actions - No notifications sent yet. + No notifications sent yet. @@ -65,6 +66,13 @@ $data = $notifications['data'] ?? []; + + +