asdlkif
This commit is contained in:
@@ -1,175 +1,701 @@
|
||||
<div class="container">
|
||||
<h1>Messenger</h1>
|
||||
<div class="box">
|
||||
<h1>Messenger</h1>
|
||||
<?php $this->renderFeedbackMessages(); ?>
|
||||
|
||||
<div class="row">
|
||||
<!-- Conversations List -->
|
||||
<div class="col-md-4">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
Conversations
|
||||
<?php if ($this->unread_count > 0): ?>
|
||||
<span class="badge pull-right"><?= $this->unread_count ?></span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="panel-body" style="padding: 0; max-height: 500px; overflow-y: auto;">
|
||||
<div class="list-group">
|
||||
<?php if (empty($this->conversations)): ?>
|
||||
<div class="list-group-item">
|
||||
<em>No conversations yet</em>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<?php foreach ($this->conversations as $conv): ?>
|
||||
<a href="<?= Config::get('URL') ?>message/conversation/<?= $conv->other_user_id ?>"
|
||||
class="list-group-item <?= $conv->unread_count > 0 ? 'active' : '' ?>">
|
||||
<h5 class="list-group-item-heading">
|
||||
<?= htmlspecialchars($conv->user_name) ?>
|
||||
<?php if ($conv->unread_count > 0): ?>
|
||||
<span class="badge"><?= $conv->unread_count ?></span>
|
||||
<?php endif; ?>
|
||||
</h5>
|
||||
<p class="list-group-item-text">
|
||||
<small><?= date('M j, Y', strtotime($conv->last_message_time)) ?></small>
|
||||
</p>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Add user ID for JavaScript -->
|
||||
<meta name="user-id" content="<?= Session::get('user_id') ?>">
|
||||
|
||||
<script src="<?= Config::get('URL') ?>public/js/messaging.js"></script>
|
||||
|
||||
<!-- New Message -->
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">New Message</div>
|
||||
<div class="panel-body">
|
||||
<form action="<?= Config::get('URL') ?>message/send" method="post">
|
||||
<div class="form-group">
|
||||
<label for="receiver_id">To:</label>
|
||||
<select name="receiver_id" id="receiver_id" class="form-control" required>
|
||||
<option value="">Select user</option>
|
||||
<?php foreach ($this->all_users as $user): ?>
|
||||
<option value="<?= $user->user_id ?>"><?= htmlspecialchars($user->user_name) ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<div style="display: flex; gap: 20px; margin-top: 20px;">
|
||||
<!-- Left Sidebar -->
|
||||
<div style="width: 300px;" class="messaging-sidebar">
|
||||
<div style="margin-bottom: 20px; padding: 15px; background: #f5f5f5; border: 1px solid #ddd;">
|
||||
<h3 style="margin: 0 0 15px 0;">Channels</h3>
|
||||
<div onclick="loadGlobalChat()" style="display: block; padding: 10px; margin-bottom: 8px; background: white; border: 1px solid #ddd; text-decoration: none; color: #333; cursor: pointer; transition: background-color 0.2s;" onmouseover="this.style.backgroundColor='#f0f0f0'" onmouseout="this.style.backgroundColor='white'" id="global-chat-link">
|
||||
<div style="display: flex; align-items: center; gap: 8px;">
|
||||
<span style="color: #2196F3;">#</span>
|
||||
<strong>Global Chat</strong>
|
||||
</div>
|
||||
<small style="color: #666; font-size: 11px;">Public chatroom</small>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="subject">Subject:</label>
|
||||
<input type="text" name="subject" id="subject" class="form-control" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="message">Message:</label>
|
||||
<textarea name="message" id="message" class="form-control" rows="3" required></textarea>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Send Message</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Send to Group -->
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">Send to Group</div>
|
||||
<div class="panel-body">
|
||||
<form action="<?= Config::get('URL') ?>message/sendgroup" method="post">
|
||||
<div class="form-group">
|
||||
<label for="group_type">Group:</label>
|
||||
<select name="group_type" id="group_type" class="form-control" required>
|
||||
<option value="">Select group</option>
|
||||
<option value="all_users">All Users</option>
|
||||
<option value="admins">Administrators</option>
|
||||
<option value="moderators">Moderators</option>
|
||||
</select>
|
||||
<div style="margin-bottom: 20px; padding: 15px; background: #f5f5f5; border: 1px solid #ddd;">
|
||||
<h3 style="margin: 0 0 15px 0;">Direct Messages</h3>
|
||||
<?php if (empty($this->conversations)): ?>
|
||||
<p style="text-align: center; padding: 20px; background: white; border: 1px solid #ddd;">No conversations yet</p>
|
||||
<?php else: ?>
|
||||
<?php foreach ($this->conversations as $conv): ?>
|
||||
<div onclick="loadConversation(<?= $conv->other_user_id ?>, '<?= htmlspecialchars(addslashes($conv->user_name)) ?>')"
|
||||
style="display: block; padding: 10px; margin-bottom: 8px; background: white; border: 1px solid #ddd; text-decoration: none; color: #333; cursor: pointer; transition: background-color 0.2s;"
|
||||
onmouseover="this.style.backgroundColor='#f0f0f0'"
|
||||
onmouseout="this.style.backgroundColor='white'"
|
||||
id="conversation-<?= $conv->other_user_id ?>">
|
||||
<div style="display: flex; justify-content: space-between; align-items: center;">
|
||||
<div style="display: flex; align-items: center; gap: 8px;">
|
||||
<span style="color: #4CAF50;">@</span>
|
||||
<strong><?= htmlspecialchars($conv->user_name) ?></strong>
|
||||
</div>
|
||||
<?php if ($conv->unread_count > 0): ?>
|
||||
<span style="background: #f44336; color: white; padding: 2px 6px; border-radius: 10px; font-size: 11px;">
|
||||
<?= $conv->unread_count ?>
|
||||
</span>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<small style="color: #666; font-size: 11px;"><?= date('M j, H:i', strtotime($conv->last_message_time)) ?></small>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="group_subject">Subject:</label>
|
||||
<input type="text" name="subject" id="group_subject" class="form-control" required>
|
||||
|
||||
<div style="padding: 15px; background: #f5f5f5; border: 1px solid #ddd;">
|
||||
<h3 style="margin: 0 0 15px 0;">New Message</h3>
|
||||
<form action="<?= Config::get('URL') ?>message/send" method="post" id="message-form">
|
||||
<select name="receiver_id" style="width: 100%; padding: 8px; margin-bottom: 8px; border: 1px solid #ddd;" required>
|
||||
<option value="">Select user</option>
|
||||
<?php foreach ($this->all_users as $user): ?>
|
||||
<option value="<?= $user->user_id ?>"><?= htmlspecialchars($user->user_name) ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
<input type="text" name="subject" placeholder="Subject" style="width: 100%; padding: 8px; margin-bottom: 8px; border: 1px solid #ddd; max-width: 250px;" required>
|
||||
<textarea name="message" placeholder="Message" style="width: 100%; padding: 8px; margin-bottom: 8px; border: 1px solid #ddd; height: 60px; resize: vertical; max-width: 250px;" required></textarea>
|
||||
<button type="submit" class="button">Send Message</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Main Chat Area -->
|
||||
<div style="flex: 1; border: 1px solid #ddd; background: white;" class="chat-main-area">
|
||||
<div id="chat-content" style="height: 700px; min-height: 700px; display: flex; flex-direction: column;">
|
||||
<!-- Default state -->
|
||||
<div id="default-state" style="flex: 1; display: flex; align-items: center; justify-content: center; background: #f9f9f9;">
|
||||
<div style="text-align: center; padding: 60px 20px; color: #666;">
|
||||
<h3 style="margin: 0 0 10px 0;">Welcome to Messenger</h3>
|
||||
<p>Select Global Chat or a conversation to start messaging</p>
|
||||
<small style="color: #999;">Click on any channel or conversation in the sidebar</small>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Chat interface (hidden by default) -->
|
||||
<div id="chat-interface" style="display: none; flex: 1; flex-direction: column;">
|
||||
<!-- Chat header -->
|
||||
<div id="chat-header" style="padding: 15px; border-bottom: 1px solid #ddd; background: #f5f5f5; display: flex; justify-content: space-between; align-items: center;">
|
||||
<div id="chat-title"></div>
|
||||
<div id="chat-actions"></div>
|
||||
</div>
|
||||
|
||||
<!-- Messages container -->
|
||||
<div id="messages-container" style="flex: 1; padding: 20px; overflow-y: auto;">
|
||||
<!-- Messages will be loaded here -->
|
||||
</div>
|
||||
|
||||
<!-- Message input -->
|
||||
<div id="message-input-area" style="padding: 15px; border-top: 1px solid #ddd; background: #f5f5f5;">
|
||||
<!-- Message input will be loaded here -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="group_message">Message:</label>
|
||||
<textarea name="message" id="group_message" class="form-control" rows="3" required></textarea>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-warning">Send to Group</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Chat Area -->
|
||||
<div class="col-md-8">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
Chat Area
|
||||
<span class="pull-right">Select a conversation to start messaging</span>
|
||||
</div>
|
||||
<div class="panel-body" style="height: 500px; background-color: #f5f5f5; text-align: center; padding-top: 200px;">
|
||||
<em>Select a conversation from the left to view messages</em>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.message-container {
|
||||
height: 400px;
|
||||
overflow-y: auto;
|
||||
background-color: #f5f5f5;
|
||||
padding: 15px;
|
||||
border-radius: 5px;
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
.messaging-sidebar {
|
||||
height: fit-content;
|
||||
}
|
||||
|
||||
.message {
|
||||
margin-bottom: 10px;
|
||||
clear: both;
|
||||
}
|
||||
.chat-main-area {
|
||||
height: auto;
|
||||
}
|
||||
|
||||
.message.sent {
|
||||
text-align: right;
|
||||
}
|
||||
#messages-container {
|
||||
background: #fafafa;
|
||||
scroll-behavior: smooth;
|
||||
}
|
||||
|
||||
.message.received {
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.message-bubble {
|
||||
.message-bubble {
|
||||
display: inline-block;
|
||||
max-width: 70%;
|
||||
padding: 10px 15px;
|
||||
border-radius: 18px;
|
||||
position: relative;
|
||||
}
|
||||
margin-bottom: 8px;
|
||||
word-wrap: break-word;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.message.sent .message-bubble {
|
||||
background-color: #007bff;
|
||||
.message-bubble.sent {
|
||||
background: #2196F3;
|
||||
color: white;
|
||||
float: right;
|
||||
}
|
||||
border-bottom-right-radius: 4px;
|
||||
}
|
||||
|
||||
.message.received .message-bubble {
|
||||
background-color: #e5e5ea;
|
||||
color: black;
|
||||
float: left;
|
||||
}
|
||||
.message-bubble.received {
|
||||
background: white;
|
||||
color: #333;
|
||||
border: 1px solid #e0e0e0;
|
||||
border-bottom-left-radius: 4px;
|
||||
}
|
||||
|
||||
.message-time {
|
||||
font-size: 0.8em;
|
||||
color: #666;
|
||||
margin-top: 5px;
|
||||
clear: both;
|
||||
}
|
||||
.message-time {
|
||||
font-size: 11px;
|
||||
color: #999;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.badge {
|
||||
background-color: #d9534f;
|
||||
}
|
||||
.message-row {
|
||||
margin-bottom: 15px;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.list-group-item.active {
|
||||
background-color: #d9edf7;
|
||||
border-color: #bce8f1;
|
||||
color: #31708f;
|
||||
}
|
||||
.message-row.sent {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.list-group-item.active .badge {
|
||||
background-color: #d9534f;
|
||||
}
|
||||
.message-row.received {
|
||||
justify-content: flex-start;
|
||||
}
|
||||
|
||||
.global-message {
|
||||
margin-bottom: 20px;
|
||||
padding: 15px;
|
||||
background: white;
|
||||
border-radius: 12px;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
.global-message-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.global-message-bubble {
|
||||
color: #333;
|
||||
line-height: 1.5;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.chat-main-area {
|
||||
height: 500px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
|
||||
.message-bubble {
|
||||
max-width: 85%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
let currentChatType = null;
|
||||
let currentChatId = null;
|
||||
let messagePollingInterval = null;
|
||||
|
||||
// Function to scroll messages container to bottom
|
||||
function scrollToBottom() {
|
||||
const container = document.getElementById('messages-container');
|
||||
if (container) {
|
||||
// Small timeout to ensure content is rendered
|
||||
setTimeout(() => {
|
||||
container.scrollTop = container.scrollHeight;
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle DOM ready event
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
// Check URL hash for auto-loading chats
|
||||
const hash = window.location.hash.substring(1);
|
||||
|
||||
if (hash === 'load-global') {
|
||||
// Load global chat
|
||||
setTimeout(() => loadGlobalChat(), 100);
|
||||
} else if (hash.startsWith('load-conversation-')) {
|
||||
// Load conversation
|
||||
const userId = hash.split('-')[2];
|
||||
if (userId) {
|
||||
setTimeout(() => {
|
||||
// Get user name from the conversation list
|
||||
const conversationEl = document.querySelector('#conversation-' + userId);
|
||||
if (conversationEl) {
|
||||
const userName = conversationEl.querySelector('strong').textContent;
|
||||
loadConversation(userId, userName);
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function loadGlobalChat() {
|
||||
currentChatType = 'global';
|
||||
currentChatId = 'global';
|
||||
|
||||
// Hide default state, show chat interface
|
||||
document.getElementById('default-state').style.display = 'none';
|
||||
document.getElementById('chat-interface').style.display = 'flex';
|
||||
|
||||
// Update header
|
||||
document.getElementById('chat-title').innerHTML = `
|
||||
<div style="display: flex; align-items: center; gap: 8px;">
|
||||
<span style="color: #2196F3; font-size: 18px;">#</span>
|
||||
<strong>Global Chat</strong>
|
||||
</div>
|
||||
<small style="color: #666;">Public chatroom</small>
|
||||
`;
|
||||
|
||||
document.getElementById('chat-actions').innerHTML = `
|
||||
<span style="font-size: 12px; color: #666;">
|
||||
<i class="fa fa-users"></i> Public
|
||||
</span>
|
||||
`;
|
||||
|
||||
// Highlight active chat
|
||||
highlightActiveChat('global');
|
||||
|
||||
// Load messages
|
||||
loadGlobalMessages();
|
||||
|
||||
// Scroll to bottom after loading
|
||||
setTimeout(() => {
|
||||
scrollToBottom();
|
||||
}, 300);
|
||||
|
||||
// Setup message input
|
||||
setupGlobalMessageInput();
|
||||
|
||||
// Start polling for new messages
|
||||
startMessagePolling();
|
||||
}
|
||||
|
||||
function loadConversation(userId, userName) {
|
||||
currentChatType = 'conversation';
|
||||
currentChatId = userId;
|
||||
|
||||
// Hide default state, show chat interface
|
||||
document.getElementById('default-state').style.display = 'none';
|
||||
document.getElementById('chat-interface').style.display = 'flex';
|
||||
|
||||
// Update header
|
||||
document.getElementById('chat-title').innerHTML = `
|
||||
<div style="display: flex; align-items: center; gap: 8px;">
|
||||
<span style="color: #4CAF50; font-size: 18px;">@</span>
|
||||
<strong>${userName}</strong>
|
||||
</div>
|
||||
<small style="color: #666;">Direct message</small>
|
||||
`;
|
||||
|
||||
document.getElementById('chat-actions').innerHTML = `
|
||||
<span style="font-size: 12px; color: #666;">
|
||||
<i class="fa fa-user"></i> Private
|
||||
</span>
|
||||
`;
|
||||
|
||||
// Highlight active chat
|
||||
highlightActiveChat(userId);
|
||||
|
||||
// Load messages
|
||||
loadConversationMessages(userId);
|
||||
|
||||
// Scroll to bottom after loading
|
||||
setTimeout(() => {
|
||||
scrollToBottom();
|
||||
}, 300);
|
||||
|
||||
// Setup message input
|
||||
setupConversationMessageInput(userId);
|
||||
|
||||
// Start polling for new messages
|
||||
startMessagePolling();
|
||||
}
|
||||
|
||||
function highlightActiveChat(chatId) {
|
||||
// Remove active class from all chats
|
||||
document.querySelectorAll('[id^="conversation-"], [id="global-chat-link"]').forEach(el => {
|
||||
el.style.backgroundColor = 'white';
|
||||
el.style.borderColor = '#ddd';
|
||||
});
|
||||
|
||||
// Add active class to current chat
|
||||
const activeEl = chatId === 'global' ?
|
||||
document.getElementById('global-chat-link') :
|
||||
document.getElementById('conversation-' + chatId);
|
||||
|
||||
if (activeEl) {
|
||||
activeEl.style.backgroundColor = '#e8f4fd';
|
||||
activeEl.style.borderColor = '#2196F3';
|
||||
}
|
||||
}
|
||||
|
||||
function loadGlobalMessages() {
|
||||
const container = document.getElementById('messages-container');
|
||||
container.innerHTML = '<div style="text-align: center; padding: 40px; color: #666;">Loading messages...</div>';
|
||||
|
||||
fetch('<?= Config::get('URL') ?>message/getGlobalMessages')
|
||||
.then(response => {
|
||||
console.log('Global messages response status:', response.status);
|
||||
console.log('Global messages response headers:', response.headers);
|
||||
|
||||
if (!response.ok) {
|
||||
return response.text().then(text => {
|
||||
console.error('Server returned non-JSON response for global messages:', text);
|
||||
throw new Error(`HTTP error! status: ${response.status}, response: ${text.substring(0, 200)}`);
|
||||
});
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success && data.messages) {
|
||||
displayGlobalMessages(data.messages);
|
||||
} else {
|
||||
container.innerHTML = '<div style="text-align: center; padding: 40px; color: #f44336;">Error loading messages: ' + (data.message || 'Unknown error') + '</div>';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error loading global messages:', error);
|
||||
container.innerHTML = '<div style="text-align: center; padding: 40px; color: #f44336;">Network error: ' + error.message + '</div>';
|
||||
});
|
||||
}
|
||||
|
||||
function loadConversationMessages(userId) {
|
||||
const container = document.getElementById('messages-container');
|
||||
container.innerHTML = '<div style="text-align: center; padding: 40px; color: #666;">Loading messages...</div>';
|
||||
|
||||
fetch(`<?= Config::get('URL') ?>message/getConversationMessages/${userId}`)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success && data.messages) {
|
||||
displayConversationMessages(data.messages);
|
||||
} else {
|
||||
container.innerHTML = '<div style="text-align: center; padding: 40px; color: #f44336;">Error loading messages</div>';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error loading conversation messages:', error);
|
||||
container.innerHTML = '<div style="text-align: center; padding: 40px; color: #f44336;">Network error</div>';
|
||||
});
|
||||
}
|
||||
|
||||
function displayGlobalMessages(messages) {
|
||||
const container = document.getElementById('messages-container');
|
||||
|
||||
if (messages.length === 0) {
|
||||
container.innerHTML = `
|
||||
<div style="text-align: center; padding: 60px 20px;">
|
||||
<h4 style="color: #666;">No messages yet</h4>
|
||||
<p style="color: #999;">Start the conversation!</p>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
container.innerHTML = messages.map(message => {
|
||||
const timeString = new Date(message.created_at).toLocaleString('en-US', {
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
|
||||
const isOwn = message.sender_id == document.querySelector('meta[name="user-id"]').content;
|
||||
|
||||
return `
|
||||
<div class="global-message">
|
||||
<div class="global-message-header">
|
||||
<div style="display: flex; align-items: center; gap: 8px;">
|
||||
<strong style="color: ${isOwn ? '#2196F3' : '#333'}">
|
||||
${escapeHtml(message.sender_name)}
|
||||
</strong>
|
||||
${isOwn ? '<span style="font-size: 10px; background: #2196F3; color: white; padding: 2px 4px; border-radius: 3px;">You</span>' : ''}
|
||||
</div>
|
||||
<span class="message-time">${timeString}</span>
|
||||
</div>
|
||||
<div class="global-message-bubble">
|
||||
${escapeHtml(message.message)}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}).join('');
|
||||
|
||||
// Scroll to bottom
|
||||
container.scrollTop = container.scrollHeight;
|
||||
}
|
||||
|
||||
function displayConversationMessages(messages) {
|
||||
const container = document.getElementById('messages-container');
|
||||
const currentUserId = document.querySelector('meta[name="user-id"]').content;
|
||||
|
||||
if (messages.length === 0) {
|
||||
container.innerHTML = `
|
||||
<div style="text-align: center; padding: 60px 20px;">
|
||||
<h4 style="color: #666;">No messages yet</h4>
|
||||
<p style="color: #999;">Send a message to start the conversation!</p>
|
||||
</div>
|
||||
`;
|
||||
return;
|
||||
}
|
||||
|
||||
container.innerHTML = messages.map(message => {
|
||||
const isSent = message.sender_id == currentUserId;
|
||||
const timeString = new Date(message.created_at).toLocaleString('en-US', {
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
});
|
||||
|
||||
return `
|
||||
<div class="message-row ${isSent ? 'sent' : 'received'}">
|
||||
<div>
|
||||
<div class="message-bubble ${isSent ? 'sent' : 'received'}">
|
||||
${escapeHtml(message.message)}
|
||||
</div>
|
||||
<div class="message-time" style="text-align: ${isSent ? 'right' : 'left'}">
|
||||
${timeString}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}).join('');
|
||||
|
||||
// Scroll to bottom
|
||||
container.scrollTop = container.scrollHeight;
|
||||
}
|
||||
|
||||
function setupGlobalMessageInput() {
|
||||
const inputArea = document.getElementById('message-input-area');
|
||||
inputArea.innerHTML = `
|
||||
<form id="global-message-form" onsubmit="sendGlobalMessage(event)">
|
||||
<div style="display: flex; gap: 10px;">
|
||||
<input type="text"
|
||||
name="message"
|
||||
placeholder="Type your global message..."
|
||||
style="flex: 1; padding: 12px; border: 1px solid #ddd; border-radius: 25px;"
|
||||
required>
|
||||
<button type="submit" class="button" style="border-radius: 25px; padding: 12px 20px;">Send</button>
|
||||
</div>
|
||||
</form>
|
||||
`;
|
||||
}
|
||||
|
||||
function setupConversationMessageInput(userId) {
|
||||
const inputArea = document.getElementById('message-input-area');
|
||||
inputArea.innerHTML = `
|
||||
<form id="conversation-message-form" onsubmit="sendConversationMessage(event, ${userId})">
|
||||
<div style="display: flex; gap: 10px;">
|
||||
<input type="text"
|
||||
name="message"
|
||||
placeholder="Type your message..."
|
||||
style="flex: 1; padding: 12px; border: 1px solid #ddd; border-radius: 25px;"
|
||||
required>
|
||||
<button type="submit" class="button" style="border-radius: 25px; padding: 12px 20px;">Send</button>
|
||||
</div>
|
||||
</form>
|
||||
`;
|
||||
}
|
||||
|
||||
function sendGlobalMessage(event) {
|
||||
event.preventDefault();
|
||||
const form = event.target;
|
||||
const message = form.message.value;
|
||||
const submitButton = form.querySelector('button[type="submit"]');
|
||||
const originalText = submitButton.textContent;
|
||||
|
||||
submitButton.textContent = 'Sending...';
|
||||
submitButton.disabled = true;
|
||||
|
||||
fetch('<?= Config::get('URL') ?>message/sendToGlobal', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
},
|
||||
body: 'message=' + encodeURIComponent(message)
|
||||
})
|
||||
.then(response => {
|
||||
console.log('Global message response status:', response.status);
|
||||
console.log('Global message response headers:', response.headers);
|
||||
|
||||
if (!response.ok) {
|
||||
return response.text().then(text => {
|
||||
console.error('Server returned non-JSON response for global message:', text);
|
||||
throw new Error(`HTTP error! status: ${response.status}, response: ${text.substring(0, 200)}`);
|
||||
});
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
form.reset();
|
||||
loadGlobalMessages(); // Refresh messages
|
||||
// Scroll to bottom after sending
|
||||
setTimeout(() => {
|
||||
scrollToBottom();
|
||||
}, 500);
|
||||
} else {
|
||||
alert('Failed to send message: ' + (data.message || 'Unknown error'));
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error sending global message:', error);
|
||||
alert('Network error. Please try again. (' + error.message + ')');
|
||||
})
|
||||
.finally(() => {
|
||||
submitButton.textContent = originalText;
|
||||
submitButton.disabled = false;
|
||||
});
|
||||
}
|
||||
|
||||
function sendConversationMessage(event, userId) {
|
||||
event.preventDefault();
|
||||
const form = event.target;
|
||||
const message = form.message.value;
|
||||
const submitButton = form.querySelector('button[type="submit"]');
|
||||
const originalText = submitButton.textContent;
|
||||
|
||||
submitButton.textContent = 'Sending...';
|
||||
submitButton.disabled = true;
|
||||
|
||||
fetch('<?= Config::get('URL') ?>message/reply', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
},
|
||||
body: 'receiver_id=' + userId + '&message=' + encodeURIComponent(message)
|
||||
})
|
||||
.then(response => {
|
||||
console.log('Response status:', response.status);
|
||||
console.log('Response headers:', response.headers);
|
||||
|
||||
if (!response.ok) {
|
||||
return response.text().then(text => {
|
||||
console.error('Server returned non-JSON response:', text);
|
||||
throw new Error(`HTTP error! status: ${response.status}, response: ${text.substring(0, 200)}`);
|
||||
});
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
form.reset();
|
||||
loadConversationMessages(userId); // Refresh messages
|
||||
// Scroll to bottom after sending
|
||||
setTimeout(() => {
|
||||
scrollToBottom();
|
||||
}, 500);
|
||||
} else {
|
||||
alert('Failed to send message: ' + (data.message || 'Unknown error'));
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error sending conversation message:', error);
|
||||
alert('Network error. Please try again. (' + error.message + ')');
|
||||
})
|
||||
.finally(() => {
|
||||
submitButton.textContent = originalText;
|
||||
submitButton.disabled = false;
|
||||
});
|
||||
}
|
||||
|
||||
function startMessagePolling() {
|
||||
// Clear existing polling
|
||||
if (messagePollingInterval) {
|
||||
clearInterval(messagePollingInterval);
|
||||
}
|
||||
|
||||
// Start new polling
|
||||
messagePollingInterval = setInterval(() => {
|
||||
if (currentChatType === 'global') {
|
||||
loadGlobalMessages();
|
||||
} else if (currentChatType === 'conversation') {
|
||||
loadConversationMessages(currentChatId);
|
||||
}
|
||||
}, 3000); // Poll every 3 seconds
|
||||
}
|
||||
|
||||
function escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
// Handle new message form submission
|
||||
document.getElementById('message-form').addEventListener('submit', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
const formData = new FormData(this);
|
||||
const submitButton = this.querySelector('button[type="submit"]');
|
||||
const originalText = submitButton.textContent;
|
||||
|
||||
submitButton.textContent = 'Sending...';
|
||||
submitButton.disabled = true;
|
||||
|
||||
fetch(this.action, {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
this.reset();
|
||||
showFeedback('Message sent successfully!', 'success');
|
||||
// Could refresh conversation list here
|
||||
} else {
|
||||
showFeedback(data.message || 'Failed to send message', 'error');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error sending message:', error);
|
||||
showFeedback('Network error. Please try again.', 'error');
|
||||
})
|
||||
.finally(() => {
|
||||
submitButton.textContent = originalText;
|
||||
submitButton.disabled = false;
|
||||
});
|
||||
});
|
||||
|
||||
function showFeedback(message, type) {
|
||||
const feedback = document.createElement('div');
|
||||
feedback.className = `feedback-${type}`;
|
||||
feedback.textContent = message;
|
||||
feedback.style.cssText = `
|
||||
position: fixed;
|
||||
top: 20px;
|
||||
right: 20px;
|
||||
padding: 15px 20px;
|
||||
background: ${type === 'success' ? '#4caf50' : '#f44336'};
|
||||
color: white;
|
||||
border-radius: 4px;
|
||||
z-index: 1000;
|
||||
box-shadow: 0 2px 10px rgba(0,0,0,0.2);
|
||||
animation: slideIn 0.3s ease;
|
||||
`;
|
||||
|
||||
document.body.appendChild(feedback);
|
||||
|
||||
setTimeout(() => {
|
||||
feedback.style.animation = 'slideOut 0.3s ease';
|
||||
setTimeout(() => feedback.remove(), 300);
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
// Add CSS for animations
|
||||
const animationStyle = document.createElement('style');
|
||||
animationStyle.textContent = `
|
||||
@keyframes slideIn {
|
||||
from { transform: translateX(100%); opacity: 0; }
|
||||
to { transform: translateX(0); opacity: 1; }
|
||||
}
|
||||
|
||||
@keyframes slideOut {
|
||||
from { transform: translateX(0); opacity: 1; }
|
||||
to { transform: translateX(100%); opacity: 0; }
|
||||
}
|
||||
`;
|
||||
document.head.appendChild(animationStyle);
|
||||
</script>
|
||||
|
||||
<?php $this->render('_templates/footer'); ?>
|
||||
|
||||
Reference in New Issue
Block a user