Sindbad~EG File Manager
<?php
/**
* Admin Chat List API
* Returns list of all chat conversations for admin
*/
require_once '../config/config.php';
header('Content-Type: application/json');
// Check if user is logged in and has admin access
if (!isLoggedIn()) {
echo json_encode(['success' => false, 'message' => 'Unauthorized']);
exit;
}
$userAccessLevel = $_SESSION['access_level'] ?? '';
if (!in_array($userAccessLevel, ['superuser', 'admin', 'regional_admin', 'district_admin'])) {
echo json_encode(['success' => false, 'message' => 'Access denied']);
exit;
}
try {
$db = Database::getInstance()->getConnection();
// Get all conversations with last message and unread count
$sql = "
SELECT
c.id,
c.visitor_name as name,
c.visitor_email,
c.created_at,
c.updated_at,
(SELECT COUNT(*) FROM chat_messages
WHERE conversation_id = c.id
AND sender_type != 'admin'
AND is_read = 0) as unread_count,
(SELECT message_text FROM chat_messages
WHERE conversation_id = c.id
ORDER BY created_at DESC LIMIT 1) as last_message,
(SELECT created_at FROM chat_messages
WHERE conversation_id = c.id
ORDER BY created_at DESC LIMIT 1) as last_message_time,
(SELECT sender_type FROM chat_messages
WHERE conversation_id = c.id
ORDER BY created_at DESC LIMIT 1) as last_sender_type
FROM conversations c
ORDER BY
(SELECT MAX(created_at) FROM chat_messages WHERE conversation_id = c.id) DESC,
c.updated_at DESC
LIMIT 100
";
$stmt = $db->query($sql);
$conversations = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Format the data
$formatted = array_map(function($conv) {
return [
'id' => (int)$conv['id'],
'name' => $conv['name'] ?: 'Anonymous',
'email' => $conv['visitor_email'],
'type' => 'Member',
'unread_count' => (int)$conv['unread_count'],
'last_message' => $conv['last_message'],
'last_message_time' => $conv['last_message_time'] ?: $conv['created_at'],
'last_sender_type' => $conv['last_sender_type'],
'created_at' => $conv['created_at']
];
}, $conversations);
echo json_encode([
'success' => true,
'conversations' => $formatted,
'total' => count($formatted)
]);
} catch (Exception $e) {
error_log("Admin chat list error: " . $e->getMessage());
echo json_encode([
'success' => false,
'message' => 'Failed to load conversations'
]);
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists