File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Controllers/NotificationController.php
Back
<?php /** * NotificationController * Handles viewing notification centers, mark-as-read transitions, deletions, and live polling AJAX */ require_once __DIR__ . '/../Core/Controller.php'; require_once __DIR__ . '/../Services/NotificationService.php'; require_once __DIR__ . '/../Helpers/Auth.php'; require_once __DIR__ . '/../Helpers/Session.php'; require_once __DIR__ . '/../Helpers/Url.php'; require_once __DIR__ . '/../Helpers/Security.php'; class NotificationController extends Controller { private NotificationService $notificationService; public function __construct() { $this->notificationService = new NotificationService(); } /** * GET /account/notifications * Customer & General Notification Center */ public function index(): void { $userId = Auth::id(); $filters = [ 'is_read' => $_GET['is_read'] ?? 'all', 'type' => $_GET['type'] ?? 'all', 'role_target' => $_GET['role_target'] ?? 'all' ]; $page = max(1, (int)($_GET['page'] ?? 1)); $data = $this->notificationService->getUserNotifications($userId, $filters, $page, 20); $this->render('account/notifications', [ 'notifications' => $data['notifications'], 'totalCount' => $data['total_count'], 'unreadCount' => $data['unread_count'], 'currentPage' => $data['page'], 'totalPages' => $data['total_pages'], 'filters' => $filters, 'activeTab' => 'notifications', 'pageTitle' => 'การแจ้งเตือน - CARGOO' ], 'account'); } /** * GET /seller/notifications * Seller Center Notification Hub */ public function sellerIndex(): void { $userId = Auth::id(); $filters = [ 'is_read' => $_GET['is_read'] ?? 'all', 'type' => $_GET['type'] ?? 'all', 'role_target' => 'seller' ]; $page = max(1, (int)($_GET['page'] ?? 1)); $data = $this->notificationService->getUserNotifications($userId, $filters, $page, 20); $this->render('seller/notifications/index', [ 'notifications' => $data['notifications'], 'totalCount' => $data['total_count'], 'unreadCount' => $data['unread_count'], 'currentPage' => $data['page'], 'totalPages' => $data['total_pages'], 'filters' => $filters, 'activeNav' => 'notifications', 'pageTitle' => 'การแจ้งเตือนร้านค้า - Seller Center' ], 'seller'); } /** * GET /account/notifications/{id} * View full notification details */ public function show(int $id): void { $userId = Auth::id(); $notif = (new Notification())->fetchOne( "SELECT * FROM `notifications` WHERE `id` = :id AND `user_id` = :uid LIMIT 1", [':id' => $id, ':uid' => $userId] ); if (!$notif) { if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { $this->json(['success' => false, 'message' => 'ไม่พบการแจ้งเตือน'], 404); return; } Session::setFlash('danger', 'ไม่พบการแจ้งเตือน'); Url::redirect(url('/account/notifications')); return; } // Mark as read when viewed $this->notificationService->markAsRead($id, $userId); $notif['is_read'] = 1; if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { $unreadCount = $this->notificationService->getUnreadCount($userId); $this->json([ 'success' => true, 'notification' => $notif, 'unread_count' => $unreadCount ]); return; } if (!empty($notif['action_url'])) { Url::redirect(url($notif['action_url'])); return; } Url::redirect(url('/account/notifications')); } /** * GET /account/notifications/{id}/read * Mark single notification as read and redirect to target URL */ public function readAndRedirect(int $id): void { $userId = Auth::id(); $this->notificationService->markAsRead($id, $userId); $notif = (new Notification())->fetchOne( "SELECT * FROM `notifications` WHERE `id` = :id AND `user_id` = :uid LIMIT 1", [':id' => $id, ':uid' => $userId] ); $redirectUrl = !empty($notif['action_url']) ? url($notif['action_url']) : url('/account/notifications'); Url::redirect($redirectUrl); } /** * POST /account/notifications/mark-all-read * POST /api/v1/notifications/mark-all-read */ public function markAllRead(): void { $userId = Auth::id(); $roleTarget = $_POST['role_target'] ?? $_GET['role_target'] ?? null; if ($roleTarget === 'all' || empty($roleTarget)) { $roleTarget = null; } $this->notificationService->markAllAsRead($userId, $roleTarget); if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { $this->json(['success' => true, 'unread_count' => 0]); return; } Session::setFlash('success', 'ทำเครื่องหมายอ่านแล้วทั้งหมดเรียบร้อยแล้ว'); $redirectUrl = ($roleTarget === 'seller') ? url('/seller/notifications') : url('/account/notifications'); Url::redirect($redirectUrl); } /** * POST /api/v1/notifications/{id}/read */ public function markSingleReadAjax(int $id): void { $userId = Auth::id(); $ok = $this->notificationService->markAsRead($id, $userId); $unreadCount = $this->notificationService->getUnreadCount($userId); $this->json(['success' => $ok, 'unread_count' => $unreadCount]); } /** * POST /account/notifications/{id}/delete */ public function delete(int $id): void { $userId = Auth::id(); $this->notificationService->deleteNotification($id, $userId); if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') { $this->json(['success' => true]); return; } Session::setFlash('success', 'ลบการแจ้งเตือนเรียบร้อยแล้ว'); $referrer = $_SERVER['HTTP_REFERER'] ?? url('/account/notifications'); Url::redirect($referrer); } /** * GET /api/v1/notifications/unread-count * AJAX endpoint for navbar badge */ public function unreadCountAjax(): void { if (!Auth::check()) { $this->json(['count' => 0]); return; } $userId = Auth::id(); $roleTarget = $_GET['role'] ?? null; $count = $this->notificationService->getUnreadCount($userId, $roleTarget); $this->json(['success' => true, 'count' => $count]); } /** * GET /api/v1/notifications/latest * AJAX endpoint for notification preview dropdown */ public function latestAjax(): void { if (!Auth::check()) { $this->json(['notifications' => []]); return; } $userId = Auth::id(); $roleTarget = $_GET['role'] ?? null; $items = $this->notificationService->getLatestPreview($userId, 5, $roleTarget); $this->json(['success' => true, 'notifications' => $items]); } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.24 |
proxy
|
phpinfo
|
Settings