-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloadChat.php
More file actions
101 lines (85 loc) · 3.18 KB
/
loadChat.php
File metadata and controls
101 lines (85 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
header('Content-Type: application/json');
include 'db.php';
function logDebug($message, $data = null) {
error_log($message . ($data ? ": " . print_r($data, true) : ""));
}
function findUserByFullName($conn, $fullName) {
$fullName = trim(preg_replace('/\s+/', ' ', $fullName));
logDebug("Normalized full name", $fullName);
try {
$stmt = $conn->prepare("SELECT id FROM user_accounts WHERE CONCAT(firstName, ' ', lastName) = :fullName");
$stmt->bindParam(':fullName', $fullName);
$stmt->execute();
if ($stmt->rowCount() > 0) {
return $stmt->fetch(PDO::FETCH_ASSOC);
}
logDebug("Exact match not found, trying LIKE query");
$stmt = $conn->prepare("
SELECT id, firstName, lastName,
CONCAT(firstName, ' ', lastName) as full_name
FROM user_accounts
WHERE CONCAT(firstName, ' ', lastName) LIKE :fullNamePattern
");
$pattern = '%' . $fullName . '%';
$stmt->bindParam(':fullNamePattern', $pattern);
$stmt->execute();
if ($stmt->rowCount() > 0) {
return $stmt->fetch(PDO::FETCH_ASSOC);
}
logDebug("No user found for full name", $fullName);
return null;
} catch (PDOException $e) {
logDebug("Database error in findUserByFullName", $e->getMessage());
return null;
}
}
if (!isset($_POST['recipient'])) {
echo json_encode(['status' => 'error', 'message' => 'Recipient not specified']);
exit;
}
$recipient = $_POST['recipient'];
logDebug("Loading chat for recipient", $recipient);
try {
$user = findUserByFullName($conn, $recipient);
if (!$user) {
echo json_encode(['status' => 'error', 'message' => 'User not found']);
exit;
}
$query = "SELECT m.*,
CASE
WHEN m.is_admin = 1 THEN
(SELECT name FROM admin_accounts WHERE id = m.admin_id)
ELSE
CONCAT(u.firstName, ' ', u.lastName)
END as sender_name,
m.is_admin,
m.timestamp
FROM messages m
LEFT JOIN user_accounts u ON m.sender_id = u.id
WHERE m.sender_id = :user_id OR
(m.is_admin = 1 AND m.sender_id = :user_id2)
ORDER BY m.timestamp ASC";
$stmt = $conn->prepare($query);
$stmt->bindParam(':user_id', $user['id']);
$stmt->bindParam(':user_id2', $user['id']);
$stmt->execute();
$messages = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$messages[] = [
'id' => $row['id'],
'sender_id' => $row['sender_id'],
'admin_id' => $row['admin_id'],
'message' => $row['message'],
'is_admin' => (bool)$row['is_admin'],
'timestamp' => $row['timestamp'],
'sender_name' => $row['sender_name']
];
}
logDebug("Found messages", count($messages));
echo json_encode($messages);
} catch (PDOException $e) {
logDebug("Database error", $e->getMessage());
echo json_encode(['status' => 'error', 'message' => 'Database error: ' . $e->getMessage()]);
}
?>