|
Server : LiteSpeed System : Linux terra.hostitbro.com 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64 User : outerorb ( 1091) PHP Version : 8.1.34 Disable Function : mail Directory : /home2/outerorb/emp.outerorbittech.in/admin/api/ | |
|
Path: /home2/outerorb/emp.outerorbittech.in/admin/api/notifications.php
Size: 3.4 KB
Permissions: 0666
<?php
require __DIR__ . '/../includes/helpers.php';
require_admin();
header('Content-Type: application/json');
$action = isset($_REQUEST['action']) ? sanitize_text($_REQUEST['action']) : '';
$admin = current_admin();
if (!$admin) {
http_response_code(401);
echo json_encode(['error' => 'Unauthorized']);
exit;
}
try {
switch ($action) {
case 'list':
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 10;
$unreadOnly = isset($_GET['unread']) && $_GET['unread'] === '1';
$notifications = get_user_notifications($admin['id'], $limit, $unreadOnly);
echo json_encode([
'success' => true,
'notifications' => $notifications
]);
break;
case 'unread-count':
$count = get_unread_notification_count($admin['id']);
echo json_encode([
'success' => true,
'count' => $count
]);
break;
case 'mark-read':
$requestData = json_decode(file_get_contents('php://input'), true);
$notificationId = (int) ($requestData['notification_id'] ?? 0);
if (!$notificationId) {
throw new Exception('Invalid notification ID');
}
// Verify the notification belongs to the current admin
$stmt = db()->prepare('SELECT id FROM notifications WHERE id = ? AND user_id = ?');
$stmt->execute([$notificationId, $admin['id']]);
if (!$stmt->fetch()) {
throw new Exception('Notification not found');
}
mark_notification_as_read($notificationId);
echo json_encode([
'success' => true,
'message' => 'Notification marked as read'
]);
break;
case 'mark-all-read':
mark_all_notifications_as_read($admin['id']);
echo json_encode([
'success' => true,
'message' => 'All notifications marked as read'
]);
break;
case 'delete':
$requestData = json_decode(file_get_contents('php://input'), true);
$notificationId = (int) ($requestData['notification_id'] ?? 0);
if (!$notificationId) {
throw new Exception('Invalid notification ID');
}
// Verify the notification belongs to the current admin
$stmt = db()->prepare('SELECT id FROM notifications WHERE id = ? AND user_id = ?');
$stmt->execute([$notificationId, $admin['id']]);
if (!$stmt->fetch()) {
throw new Exception('Notification not found');
}
delete_notification($notificationId);
echo json_encode([
'success' => true,
'message' => 'Notification deleted'
]);
break;
default:
http_response_code(400);
echo json_encode([
'error' => 'Invalid action'
]);
break;
}
} catch (Exception $e) {
http_response_code(400);
echo json_encode([
'error' => $e->getMessage()
]);
}