[ Root System Explorer ]
Location:
Root
/
home
/
u456045770
/
domains
/
srmeshop.in
/
public_html
/
admin
/
chat
+ Folder
+ File
Upload
Editing: chats.php
<?php // chat.php - UPDATED VERSION to handle admin and client chats require_once '../config.php'; class Chat extends DBConnection { private $settings; public function __construct() { global $_settings; $this->settings = $_settings; parent::__construct(); ini_set('display_errors', 0); } public function __destruct() { parent::__destruct(); } public function get_messages() { // Check if logged in $client_id = $this->settings->userdata('id'); if (!$client_id || $client_id <= 0) { return json_encode(['error' => 'Not logged in']); } $client_id = (int)$client_id; $conversation_id = $_GET['conversation_id'] ?? ''; $conversation_type = $_GET['conversation_type'] ?? 'client'; if (empty($conversation_id)) { return json_encode(['error' => 'Invalid conversation ID']); } try { // Determine conversation type and receiver ID $receiver_id = null; if ($conversation_type === 'admin') { // For admin chats, the conversation_id is like "admin_hotel_123" or "admin_pkg_456" $receiver_id = 'admin'; // Admin is always the receiver } else { // For client chats, conversation_id is the client ID $receiver_id = (int)$conversation_id; } // Get messages - handle both admin and client chats if ($conversation_type === 'admin') { // For admin chats, we need to identify the specific admin conversation // conversation_id format: admin_[type]_[id] $parts = explode('_', $conversation_id); if (count($parts) >= 3) { $product_type = $parts[1]; // hotel, pkg, vehicle $product_id = (int)$parts[2]; $stmt = $this->conn->prepare(" SELECT m.*, CASE WHEN m.sender_id = 'admin' THEN 'Admin' ELSE CONCAT(c.firstname, ' ', c.lastname) END as sender_name FROM messages m LEFT JOIN clients c ON m.sender_id = c.id AND m.sender_id != 'admin' WHERE m.product_type = ? AND m.product_id = ? AND (m.sender_id = ? OR m.receiver_id = ?) ORDER BY m.created_at ASC LIMIT 100 "); $stmt->bind_param("siss", $product_type, $product_id, $client_id, $client_id); } else { return json_encode(['error' => 'Invalid admin conversation ID']); } } else { // For client-to-client chats - FIXED HERE! // Need to know which product we're chatting about // We should pass product_type and product_id as parameters $product_type = $_GET['product_type'] ?? 'vehicle'; // Default to 'vehicle' for vehicles page $product_id = $_GET['product_id'] ?? 0; $stmt = $this->conn->prepare(" SELECT m.*, CONCAT(c.firstname, ' ', c.lastname) as sender_name FROM messages m LEFT JOIN clients c ON m.sender_id = c.id WHERE ((m.sender_id = ? AND m.receiver_id = ?) OR (m.sender_id = ? AND m.receiver_id = ?)) AND m.product_type = ? AND m.product_id = ? ORDER BY m.created_at ASC LIMIT 100 "); $stmt->bind_param("iiissi", $client_id, $receiver_id, $receiver_id, $client_id, $product_type, $product_id); } $stmt->execute(); $result = $stmt->get_result(); $messages = []; while ($m = $result->fetch_assoc()) { $is_me = ($m['sender_id'] == $client_id); $messages[] = [ 'id' => $m['id'], 'message_text' => htmlspecialchars($m['message_text'] ?? ''), 'created_at' => $m['created_at'] ?? date('Y-m-d H:i:s'), 'is_me' => $is_me, 'sender_name' => htmlspecialchars($m['sender_name'] ?? 'User'), 'is_read' => (bool)($m['is_read'] ?? false) ]; } // Mark messages as read (for client-to-client only) if ($conversation_type === 'client' && is_numeric($receiver_id)) { $upd = $this->conn->prepare(" UPDATE messages SET is_read = 1 WHERE sender_id = ? AND receiver_id = ? AND is_read = 0 AND product_type = ? AND product_id = ? "); $upd->bind_param("iiss", $receiver_id, $client_id, $product_type, $product_id); $upd->execute(); } return json_encode($messages); } catch (Exception $e) { return json_encode(['error' => 'Failed to load messages: ' . $e->getMessage()]); } } public function send_message() { // Check if logged in $client_id = $this->settings->userdata('id'); if (!$client_id || $client_id <= 0) { return json_encode(['error' => 'Not logged in']); } $client_id = (int)$client_id; $conversation_id = $_POST['conversation_id'] ?? ''; $conversation_type = $_POST['conversation_type'] ?? 'client'; $product_type = $_POST['product_type'] ?? 'vehicle'; // Add this $product_id = $_POST['product_id'] ?? 0; // Add this $msg = trim($_POST['message'] ?? ''); if (empty($conversation_id)) { return json_encode(['error' => 'Invalid conversation']); } if (empty($msg)) { return json_encode(['error' => 'Message cannot be empty']); } try { // Determine receiver and product info based on conversation type $receiver_id = null; if ($conversation_type === 'admin') { // Admin conversation: format is "admin_[product_type]_[product_id]" $parts = explode('_', $conversation_id); if (count($parts) >= 3) { $receiver_id = 'admin'; // Send to admin $product_type = $parts[1]; // hotel, pkg, vehicle $product_id = (int)$parts[2]; } else { return json_encode(['error' => 'Invalid admin conversation ID']); } } else { // Client conversation: conversation_id is the receiver's client ID $receiver_id = $conversation_id; // Check if receiver exists (for client-to-client) if (is_numeric($receiver_id)) { $check_stmt = $this->conn->prepare("SELECT id FROM clients WHERE id = ?"); $check_stmt->bind_param("i", $receiver_id); $check_stmt->execute(); $check_result = $check_stmt->get_result(); if ($check_result->num_rows == 0) { return json_encode(['error' => 'Receiver not found']); } } } // Insert message $stmt = $this->conn->prepare(" INSERT INTO messages (sender_id, receiver_id, product_type, product_id, message_text, is_read, created_at) VALUES (?, ?, ?, ?, ?, 0, NOW()) "); $clean_msg = htmlspecialchars($msg); $sender_id = $client_id; $stmt->bind_param("sssis", $sender_id, $receiver_id, $product_type, $product_id, $clean_msg); $stmt->execute(); return json_encode([ 'success' => true, 'message' => 'Message sent successfully', 'message_id' => $stmt->insert_id ]); } catch (Exception $e) { return json_encode(['error' => 'Failed to send message: ' . $e->getMessage()]); } } public function get_conversations() { // Check if logged in $client_id = $this->settings->userdata('id'); if (!$client_id || $client_id <= 0) { return json_encode(['error' => 'Not logged in']); } $client_id = (int)$client_id; try { // Get all conversations for this user $stmt = $this->conn->prepare(" SELECT CASE WHEN m.sender_id = ? THEN m.receiver_id ELSE m.sender_id END as other_user_id, CASE WHEN m.sender_id = 'admin' OR m.receiver_id = 'admin' THEN 'admin' ELSE 'client' END as user_type, MAX(m.created_at) as last_message_time, m.product_type, m.product_id FROM messages m WHERE m.sender_id = ? OR m.receiver_id = ? GROUP BY other_user_id, user_type, m.product_type, m.product_id ORDER BY last_message_time DESC "); $stmt->bind_param("iss", $client_id, $client_id, $client_id); $stmt->execute(); $result = $stmt->get_result(); $conversations = []; while ($row = $result->fetch_assoc()) { $conversations[] = [ 'conversation_id' => $row['other_user_id'], 'user_type' => $row['user_type'], 'last_message' => $row['last_message_time'], 'product_type' => $row['product_type'], 'product_id' => $row['product_id'] ]; } return json_encode($conversations); } catch (Exception $e) { return json_encode(['error' => 'Failed to get conversations']); } } public function index() { return json_encode(['error' => 'Invalid action']); } } // Set JSON header first header('Content-Type: application/json'); // Get action $action = $_GET['f'] ?? 'none'; $chat = new Chat(); switch ($action) { case 'get_messages': echo $chat->get_messages(); break; case 'send_message': echo $chat->send_message(); break; case 'get_conversations': echo $chat->get_conversations(); break; default: echo $chat->index(); break; } ?>
SAVE CHANGES
[ CANCEL ]
Name
Type
Actions
.. (Parent Directory)
📄 chat_actions(1).php
FILE
Ren
[EDIT]
DEL
📄 chat_actions.php
FILE
Ren
[EDIT]
DEL
📄 chats.php
FILE
Ren
[EDIT]
DEL
📄 messages(1).php
FILE
Ren
[EDIT]
DEL
📄 messages(2).php
FILE
Ren
[EDIT]
DEL
📄 messages.php
FILE
Ren
[EDIT]
DEL
📄 test.html
FILE
Ren
[EDIT]
DEL