Basic message implementation
This commit is contained in:
20
application/_installation/04-create-table-messages.sql
Normal file
20
application/_installation/04-create-table-messages.sql
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS `messages` (
|
||||||
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
`sender_id` int(11) NOT NULL,
|
||||||
|
`receiver_id` int(11) DEFAULT NULL,
|
||||||
|
`group_type` enum('admins','moderators','all_users') DEFAULT NULL,
|
||||||
|
`subject` varchar(255) NOT NULL,
|
||||||
|
`message` text NOT NULL,
|
||||||
|
`is_read` tinyint(1) NOT NULL DEFAULT 0,
|
||||||
|
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
KEY `sender_id` (`sender_id`),
|
||||||
|
KEY `receiver_id` (`receiver_id`),
|
||||||
|
KEY `is_read` (`is_read`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
|
||||||
|
|
||||||
|
-- Foreign key constraints
|
||||||
|
ALTER TABLE `messages`
|
||||||
|
ADD CONSTRAINT `messages_ibfk_1` FOREIGN KEY (`sender_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE,
|
||||||
|
ADD CONSTRAINT `messages_ibfk_2` FOREIGN KEY (`receiver_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE;
|
||||||
212
application/controller/MessageController.php
Normal file
212
application/controller/MessageController.php
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class MessageController extends Controller
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
// Require login for all message features
|
||||||
|
Auth::checkAuthentication();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a message to a specific user via URL parameters
|
||||||
|
* URL format: message/send/{receiver_id}/{subject}/{message}
|
||||||
|
*/
|
||||||
|
public function send()
|
||||||
|
{
|
||||||
|
// Handle POST request
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$receiver_id = isset($_POST['receiver_id']) ? $_POST['receiver_id'] : null;
|
||||||
|
$subject = isset($_POST['subject']) ? $_POST['subject'] : 'No Subject';
|
||||||
|
$message = isset($_POST['message']) ? $_POST['message'] : null;
|
||||||
|
|
||||||
|
if (!$receiver_id || !$message) {
|
||||||
|
Session::add('feedback_negative', 'Receiver and message are required');
|
||||||
|
Redirect::to('message');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the message
|
||||||
|
$sender_id = Session::get('user_id');
|
||||||
|
$success = MessageModel::sendToUser($sender_id, $receiver_id, $subject, $message);
|
||||||
|
|
||||||
|
if ($success) {
|
||||||
|
Session::add('feedback_positive', 'Message sent successfully');
|
||||||
|
} else {
|
||||||
|
Session::add('feedback_negative', 'Failed to send message');
|
||||||
|
}
|
||||||
|
|
||||||
|
// If coming from conversation view, return there
|
||||||
|
if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'conversation') !== false) {
|
||||||
|
Redirect::to('message/conversation/' . $receiver_id);
|
||||||
|
} else {
|
||||||
|
Redirect::to('message');
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle GET request
|
||||||
|
$url_parts = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
|
||||||
|
$receiver_id = isset($url_parts[2]) ? $url_parts[2] : null;
|
||||||
|
$subject = isset($url_parts[3]) ? urldecode($url_parts[3]) : null;
|
||||||
|
$message = isset($url_parts[4]) ? urldecode($url_parts[4]) : null;
|
||||||
|
|
||||||
|
if (!$receiver_id || !$subject || !$message) {
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo json_encode(['success' => false, 'message' => 'Missing parameters. Use: message/send/{receiver_id}/{subject}/{message}']);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify receiver exists
|
||||||
|
$receiver = UserModel::getPublicProfileOfUser($receiver_id);
|
||||||
|
if (!$receiver) {
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo json_encode(['success' => false, 'message' => 'Receiver not found']);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the message
|
||||||
|
$sender_id = Session::get('user_id');
|
||||||
|
$success = MessageModel::sendToUser($sender_id, $receiver_id, $subject, $message);
|
||||||
|
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
if ($success) {
|
||||||
|
echo json_encode(['success' => true, 'message' => 'Message sent successfully']);
|
||||||
|
} else {
|
||||||
|
echo json_encode(['success' => false, 'message' => 'Failed to send message']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a message to a group via URL parameters
|
||||||
|
* URL format: message/sendgroup/{group_type}/{subject}/{message}
|
||||||
|
* group_type can be: admins, moderators, all_users
|
||||||
|
*/
|
||||||
|
public function sendgroup()
|
||||||
|
{
|
||||||
|
// Handle POST request
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$group_type = isset($_POST['group_type']) ? $_POST['group_type'] : null;
|
||||||
|
$subject = isset($_POST['subject']) ? $_POST['subject'] : 'No Subject';
|
||||||
|
$message = isset($_POST['message']) ? $_POST['message'] : null;
|
||||||
|
|
||||||
|
if (!$group_type || !$message) {
|
||||||
|
Session::add('feedback_negative', 'Group type and message are required');
|
||||||
|
Redirect::to('message');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate group type
|
||||||
|
if (!in_array($group_type, ['admins', 'moderators', 'all_users'])) {
|
||||||
|
Session::add('feedback_negative', 'Invalid group type');
|
||||||
|
Redirect::to('message');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the message
|
||||||
|
$sender_id = Session::get('user_id');
|
||||||
|
$success = MessageModel::sendToGroup($sender_id, $group_type, $subject, $message);
|
||||||
|
|
||||||
|
if ($success) {
|
||||||
|
Session::add('feedback_positive', 'Message sent to group successfully');
|
||||||
|
} else {
|
||||||
|
Session::add('feedback_negative', 'Failed to send message to group');
|
||||||
|
}
|
||||||
|
|
||||||
|
Redirect::to('message');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle GET request
|
||||||
|
$url_parts = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
|
||||||
|
$group_type = isset($url_parts[2]) ? $url_parts[2] : null;
|
||||||
|
$subject = isset($url_parts[3]) ? urldecode($url_parts[3]) : null;
|
||||||
|
$message = isset($url_parts[4]) ? urldecode($url_parts[4]) : null;
|
||||||
|
|
||||||
|
if (!$group_type || !$subject || !$message) {
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo json_encode(['success' => false, 'message' => 'Missing parameters. Use: message/sendgroup/{group_type}/{subject}/{message}']);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate group type
|
||||||
|
if (!in_array($group_type, ['admins', 'moderators', 'all_users'])) {
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo json_encode(['success' => false, 'message' => 'Invalid group type. Must be: admins, moderators, or all_users']);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send the message
|
||||||
|
$sender_id = Session::get('user_id');
|
||||||
|
$success = MessageModel::sendToGroup($sender_id, $group_type, $subject, $message);
|
||||||
|
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
if ($success) {
|
||||||
|
echo json_encode(['success' => true, 'message' => 'Message sent to group successfully']);
|
||||||
|
} else {
|
||||||
|
echo json_encode(['success' => false, 'message' => 'Failed to send message to group']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the messenger interface
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$user_id = Session::get('user_id');
|
||||||
|
|
||||||
|
// Get conversations and unread count
|
||||||
|
$conversations = MessageModel::getConversations($user_id);
|
||||||
|
$unread_count = MessageModel::getUnreadCount($user_id);
|
||||||
|
|
||||||
|
$this->View->render('message/index', array(
|
||||||
|
'conversations' => $conversations,
|
||||||
|
'unread_count' => $unread_count,
|
||||||
|
'all_users' => MessageModel::getAllUsers($user_id)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show conversation with a specific user
|
||||||
|
*/
|
||||||
|
public function conversation()
|
||||||
|
{
|
||||||
|
$user_id = Session::get('user_id');
|
||||||
|
$url_parts = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
|
||||||
|
$other_user_id = isset($url_parts[2]) ? $url_parts[2] : null;
|
||||||
|
|
||||||
|
if (!$other_user_id) {
|
||||||
|
Redirect::to('message');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get user info for the other person
|
||||||
|
$other_user = UserModel::getPublicProfileOfUser($other_user_id);
|
||||||
|
if (!$other_user) {
|
||||||
|
Redirect::to('message');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get messages
|
||||||
|
$messages = MessageModel::getMessagesWithUser($user_id, $other_user_id);
|
||||||
|
|
||||||
|
$this->View->render('message/conversation', array(
|
||||||
|
'messages' => $messages,
|
||||||
|
'other_user' => $other_user
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get unread count as JSON
|
||||||
|
*/
|
||||||
|
public function unreadcount()
|
||||||
|
{
|
||||||
|
$user_id = Session::get('user_id');
|
||||||
|
$unread_count = MessageModel::getUnreadCount($user_id);
|
||||||
|
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
echo json_encode(['unread_count' => $unread_count]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,8 +16,16 @@ class ProfileController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
|
$all_users = UserModel::getPublicProfilesOfAllUsers();
|
||||||
|
|
||||||
|
// Remove current user from the list
|
||||||
|
$current_user_id = Session::get('user_id');
|
||||||
|
if ($current_user_id && isset($all_users[$current_user_id])) {
|
||||||
|
unset($all_users[$current_user_id]);
|
||||||
|
}
|
||||||
|
|
||||||
$this->View->render('profile/index', array(
|
$this->View->render('profile/index', array(
|
||||||
'users' => UserModel::getPublicProfilesOfAllUsers())
|
'users' => $all_users)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
186
application/model/MessageModel.php
Normal file
186
application/model/MessageModel.php
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class MessageModel
|
||||||
|
{
|
||||||
|
public static function sendMessage($sender_id, $receiver_id, $group_type, $subject, $message)
|
||||||
|
{
|
||||||
|
$database = DatabaseFactory::getFactory()->getConnection();
|
||||||
|
|
||||||
|
$sql = "INSERT INTO messages (sender_id, receiver_id, group_type, subject, message)
|
||||||
|
VALUES (:sender_id, :receiver_id, :group_type, :subject, :message)";
|
||||||
|
$query = $database->prepare($sql);
|
||||||
|
return $query->execute(array(
|
||||||
|
':sender_id' => $sender_id,
|
||||||
|
':receiver_id' => $receiver_id,
|
||||||
|
':group_type' => $group_type,
|
||||||
|
':subject' => $subject,
|
||||||
|
':message' => $message
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function sendToUser($sender_id, $receiver_id, $subject, $message)
|
||||||
|
{
|
||||||
|
return self::sendMessage($sender_id, $receiver_id, null, $subject, $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function sendToGroup($sender_id, $group_type, $subject, $message)
|
||||||
|
{
|
||||||
|
return self::sendMessage($sender_id, null, $group_type, $subject, $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getUnreadCount($user_id)
|
||||||
|
{
|
||||||
|
$database = DatabaseFactory::getFactory()->getConnection();
|
||||||
|
|
||||||
|
$sql = "SELECT COUNT(*) as count FROM messages
|
||||||
|
WHERE receiver_id = :user_id AND is_read = 0";
|
||||||
|
$query = $database->prepare($sql);
|
||||||
|
$query->execute(array(':user_id' => $user_id));
|
||||||
|
$result = $query->fetch();
|
||||||
|
|
||||||
|
return $result->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getUnreadCountForUser($user_id, $sender_id)
|
||||||
|
{
|
||||||
|
$database = DatabaseFactory::getFactory()->getConnection();
|
||||||
|
|
||||||
|
$sql = "SELECT COUNT(*) as count FROM messages
|
||||||
|
WHERE receiver_id = :user_id AND sender_id = :sender_id AND is_read = 0";
|
||||||
|
$query = $database->prepare($sql);
|
||||||
|
$query->execute(array(
|
||||||
|
':user_id' => $user_id,
|
||||||
|
':sender_id' => $sender_id
|
||||||
|
));
|
||||||
|
$result = $query->fetch();
|
||||||
|
|
||||||
|
return $result->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getConversations($user_id)
|
||||||
|
{
|
||||||
|
$database = DatabaseFactory::getFactory()->getConnection();
|
||||||
|
|
||||||
|
$sql = "SELECT DISTINCT
|
||||||
|
CASE
|
||||||
|
WHEN sender_id = :user_id THEN receiver_id
|
||||||
|
ELSE sender_id
|
||||||
|
END as other_user_id,
|
||||||
|
u.user_name,
|
||||||
|
u.user_email,
|
||||||
|
MAX(m.created_at) as last_message_time,
|
||||||
|
(SELECT COUNT(*) FROM messages m2
|
||||||
|
WHERE ((m2.sender_id = :user_id AND m2.receiver_id = other_user_id) OR
|
||||||
|
(m2.receiver_id = :user_id AND m2.sender_id = other_user_id))
|
||||||
|
AND m2.is_read = 0 AND m2.receiver_id = :user_id) as unread_count
|
||||||
|
FROM messages m
|
||||||
|
JOIN users u ON (CASE
|
||||||
|
WHEN sender_id = :user_id THEN receiver_id
|
||||||
|
ELSE sender_id
|
||||||
|
END) = u.user_id
|
||||||
|
WHERE (sender_id = :user_id OR receiver_id = :user_id)
|
||||||
|
AND receiver_id IS NOT NULL
|
||||||
|
GROUP BY other_user_id
|
||||||
|
ORDER BY last_message_time DESC";
|
||||||
|
|
||||||
|
$query = $database->prepare($sql);
|
||||||
|
$query->execute(array(':user_id' => $user_id));
|
||||||
|
return $query->fetchAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getMessagesWithUser($user_id, $other_user_id)
|
||||||
|
{
|
||||||
|
$database = DatabaseFactory::getFactory()->getConnection();
|
||||||
|
|
||||||
|
$sql = "SELECT m.*, u_sender.user_name as sender_name,
|
||||||
|
CASE
|
||||||
|
WHEN m.sender_id = :user_id THEN 'sent'
|
||||||
|
ELSE 'received'
|
||||||
|
END as message_type
|
||||||
|
FROM messages m
|
||||||
|
JOIN users u_sender ON m.sender_id = u_sender.user_id
|
||||||
|
WHERE ((m.sender_id = :user_id AND m.receiver_id = :other_user_id) OR
|
||||||
|
(m.receiver_id = :user_id AND m.sender_id = :other_user_id))
|
||||||
|
AND m.group_type IS NULL
|
||||||
|
ORDER BY m.created_at ASC";
|
||||||
|
|
||||||
|
$query = $database->prepare($sql);
|
||||||
|
$query->execute(array(
|
||||||
|
':user_id' => $user_id,
|
||||||
|
':other_user_id' => $other_user_id
|
||||||
|
));
|
||||||
|
$messages = $query->fetchAll();
|
||||||
|
|
||||||
|
// Mark received messages as read
|
||||||
|
self::markAsRead($user_id, $other_user_id);
|
||||||
|
|
||||||
|
return $messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function markAsRead($user_id, $sender_id)
|
||||||
|
{
|
||||||
|
$database = DatabaseFactory::getFactory()->getConnection();
|
||||||
|
|
||||||
|
$sql = "UPDATE messages SET is_read = 1
|
||||||
|
WHERE receiver_id = :user_id AND sender_id = :sender_id AND is_read = 0";
|
||||||
|
$query = $database->prepare($sql);
|
||||||
|
return $query->execute(array(
|
||||||
|
':user_id' => $user_id,
|
||||||
|
':sender_id' => $sender_id
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getGroupMessages($user_id)
|
||||||
|
{
|
||||||
|
$database = DatabaseFactory::getFactory()->getConnection();
|
||||||
|
|
||||||
|
// Check user role to determine which group messages they should receive
|
||||||
|
$user_role = UserModel::getUserRole($user_id);
|
||||||
|
|
||||||
|
$where_conditions = [];
|
||||||
|
$params = [':user_id' => $user_id];
|
||||||
|
|
||||||
|
// All users receive all_users messages
|
||||||
|
$where_conditions[] = "group_type = 'all_users'";
|
||||||
|
|
||||||
|
// Admins receive admin messages
|
||||||
|
if ($user_role === 'admin') {
|
||||||
|
$where_conditions[] = "group_type = 'admins'";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Moderators receive moderator messages (and admin messages if not already covered)
|
||||||
|
if ($user_role === 'moderator' || $user_role === 'admin') {
|
||||||
|
$where_conditions[] = "group_type = 'moderators'";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($where_conditions)) {
|
||||||
|
return []; // User has no role that qualifies for group messages
|
||||||
|
}
|
||||||
|
|
||||||
|
$where_clause = "(" . implode(" OR ", $where_conditions) . ")";
|
||||||
|
|
||||||
|
$sql = "SELECT m.*, u.user_name as sender_name
|
||||||
|
FROM messages m
|
||||||
|
JOIN users u ON m.sender_id = u.user_id
|
||||||
|
WHERE $where_clause AND m.receiver_id IS NULL
|
||||||
|
ORDER BY m.created_at DESC";
|
||||||
|
|
||||||
|
$query = $database->prepare($sql);
|
||||||
|
$query->execute($params);
|
||||||
|
return $query->fetchAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getAllUsers($current_user_id)
|
||||||
|
{
|
||||||
|
$database = DatabaseFactory::getFactory()->getConnection();
|
||||||
|
|
||||||
|
$sql = "SELECT user_id, user_name, user_email
|
||||||
|
FROM users
|
||||||
|
WHERE user_id != :current_user_id
|
||||||
|
ORDER BY user_name ASC";
|
||||||
|
|
||||||
|
$query = $database->prepare($sql);
|
||||||
|
$query->execute(array(':current_user_id' => $current_user_id));
|
||||||
|
return $query->fetchAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -375,4 +375,31 @@ class UserModel
|
|||||||
// return one row (we only have one result or nothing)
|
// return one row (we only have one result or nothing)
|
||||||
return $query->fetch();
|
return $query->fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the user role type based on account type
|
||||||
|
*
|
||||||
|
* @param $user_id int user id
|
||||||
|
*
|
||||||
|
* @return string The user role (admin, moderator, user)
|
||||||
|
*/
|
||||||
|
public static function getUserRole($user_id)
|
||||||
|
{
|
||||||
|
$database = DatabaseFactory::getFactory()->getConnection();
|
||||||
|
|
||||||
|
$sql = "SELECT user_account_type FROM users WHERE user_id = :user_id LIMIT 1";
|
||||||
|
$query = $database->prepare($sql);
|
||||||
|
$query->execute(array(':user_id' => $user_id));
|
||||||
|
$result = $query->fetch();
|
||||||
|
|
||||||
|
// Map account type to role
|
||||||
|
switch ($result->user_account_type) {
|
||||||
|
case 7: // admin
|
||||||
|
return 'admin';
|
||||||
|
case 2: // moderator (example value, adjust according to your system)
|
||||||
|
return 'moderator';
|
||||||
|
default:
|
||||||
|
return 'user';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,17 @@
|
|||||||
<li <?php if (View::checkForActiveController($filename, "note")) { echo ' class="active" '; } ?> >
|
<li <?php if (View::checkForActiveController($filename, "note")) { echo ' class="active" '; } ?> >
|
||||||
<a href="<?php echo Config::get('URL'); ?>note/index">My Notes</a>
|
<a href="<?php echo Config::get('URL'); ?>note/index">My Notes</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li <?php if (View::checkForActiveController($filename, "message")) { echo ' class="active" '; } ?> >
|
||||||
|
<a href="<?php echo Config::get('URL'); ?>message/index">Messages</a>
|
||||||
|
<?php if (Session::userIsLoggedIn()) {
|
||||||
|
// Get unread message count
|
||||||
|
$user_id = Session::get('user_id');
|
||||||
|
$unread_count = MessageModel::getUnreadCount($user_id);
|
||||||
|
if ($unread_count > 0) {
|
||||||
|
echo '<span class="message-badge">' . $unread_count . '</span>';
|
||||||
|
}
|
||||||
|
} ?>
|
||||||
|
</li>
|
||||||
<?php } else { ?>
|
<?php } else { ?>
|
||||||
<!-- for not logged in users -->
|
<!-- for not logged in users -->
|
||||||
<li <?php if (View::checkForActiveControllerAndAction($filename, "login/index")) { echo ' class="active" '; } ?> >
|
<li <?php if (View::checkForActiveControllerAndAction($filename, "login/index")) { echo ' class="active" '; } ?> >
|
||||||
|
|||||||
117
application/view/message/conversation.php
Normal file
117
application/view/message/conversation.php
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
<?php $this->render('_templates/header'); ?>
|
||||||
|
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<!-- Back to Messenger -->
|
||||||
|
<div class="col-md-12">
|
||||||
|
<a href="<?= Config::get('URL') ?>message" class="btn btn-default">
|
||||||
|
<span class="glyphicon glyphicon-arrow-left"></span> Back to Messenger
|
||||||
|
</a>
|
||||||
|
<hr>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<!-- Chat Area -->
|
||||||
|
<div class="col-md-12">
|
||||||
|
<div class="panel panel-default">
|
||||||
|
<div class="panel-heading">
|
||||||
|
Conversation with <?= htmlspecialchars($this->other_user->user_name) ?>
|
||||||
|
</div>
|
||||||
|
<div class="panel-body message-container">
|
||||||
|
<?php if (empty($this->messages)): ?>
|
||||||
|
<div class="text-center">
|
||||||
|
<em>No messages yet. Start a conversation!</em>
|
||||||
|
</div>
|
||||||
|
<?php else: ?>
|
||||||
|
<?php foreach ($this->messages as $msg): ?>
|
||||||
|
<div class="message <?= $msg->message_type ?>">
|
||||||
|
<div class="message-bubble">
|
||||||
|
<?= htmlspecialchars($msg->message) ?>
|
||||||
|
</div>
|
||||||
|
<div class="message-time">
|
||||||
|
<?= date('M j, Y H:i', strtotime($msg->created_at)) ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Reply Form -->
|
||||||
|
<div class="panel-footer">
|
||||||
|
<form action="<?= Config::get('URL') ?>message/send" method="post" id="reply-form">
|
||||||
|
<input type="hidden" name="receiver_id" value="<?= $this->other_user->user_id ?>">
|
||||||
|
<div class="input-group">
|
||||||
|
<input type="text" name="message" class="form-control" placeholder="Type your message..." required>
|
||||||
|
<span class="input-group-btn">
|
||||||
|
<button type="submit" class="btn btn-primary">Send</button>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.message-container {
|
||||||
|
height: 400px;
|
||||||
|
overflow-y: auto;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
padding: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message {
|
||||||
|
margin-bottom: 15px;
|
||||||
|
clear: both;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message.sent {
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message.received {
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-bubble {
|
||||||
|
display: inline-block;
|
||||||
|
max-width: 70%;
|
||||||
|
padding: 10px 15px;
|
||||||
|
border-radius: 18px;
|
||||||
|
position: relative;
|
||||||
|
word-wrap: break-word;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message.sent .message-bubble {
|
||||||
|
background-color: #007bff;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message.received .message-bubble {
|
||||||
|
background-color: #e5e5ea;
|
||||||
|
color: black;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-time {
|
||||||
|
font-size: 0.8em;
|
||||||
|
color: #666;
|
||||||
|
margin-top: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Scroll to bottom of messages on load */
|
||||||
|
.message-container {
|
||||||
|
scroll-behavior: smooth;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
// Scroll to bottom of messages
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
const container = document.querySelector('.message-container');
|
||||||
|
container.scrollTop = container.scrollHeight;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<?php $this->render('_templates/footer'); ?>
|
||||||
177
application/view/message/index.php
Normal file
177
application/view/message/index.php
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
<?php $this->render('_templates/header'); ?>
|
||||||
|
|
||||||
|
<div class="container">
|
||||||
|
<h1>Messenger</h1>
|
||||||
|
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<!-- 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>
|
||||||
|
<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>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="group_subject">Subject:</label>
|
||||||
|
<input type="text" name="subject" id="group_subject" class="form-control" required>
|
||||||
|
</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;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
clear: both;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message.sent {
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message.received {
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-bubble {
|
||||||
|
display: inline-block;
|
||||||
|
max-width: 70%;
|
||||||
|
padding: 10px 15px;
|
||||||
|
border-radius: 18px;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message.sent .message-bubble {
|
||||||
|
background-color: #007bff;
|
||||||
|
color: white;
|
||||||
|
float: right;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message.received .message-bubble {
|
||||||
|
background-color: #e5e5ea;
|
||||||
|
color: black;
|
||||||
|
float: left;
|
||||||
|
}
|
||||||
|
|
||||||
|
.message-time {
|
||||||
|
font-size: 0.8em;
|
||||||
|
color: #666;
|
||||||
|
margin-top: 5px;
|
||||||
|
clear: both;
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge {
|
||||||
|
background-color: #d9534f;
|
||||||
|
}
|
||||||
|
|
||||||
|
.list-group-item.active {
|
||||||
|
background-color: #d9edf7;
|
||||||
|
border-color: #bce8f1;
|
||||||
|
color: #31708f;
|
||||||
|
}
|
||||||
|
|
||||||
|
.list-group-item.active .badge {
|
||||||
|
background-color: #d9534f;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<?php $this->render('_templates/footer'); ?>
|
||||||
@@ -35,6 +35,16 @@
|
|||||||
<td><?= ($user->user_active == 0 ? 'No' : 'Yes'); ?></td>
|
<td><?= ($user->user_active == 0 ? 'No' : 'Yes'); ?></td>
|
||||||
<td>
|
<td>
|
||||||
<a href="<?= Config::get('URL') . 'profile/showProfile/' . $user->user_id; ?>">Profile</a>
|
<a href="<?= Config::get('URL') . 'profile/showProfile/' . $user->user_id; ?>">Profile</a>
|
||||||
|
<?php
|
||||||
|
// Show unread message count for each user
|
||||||
|
if (Session::userIsLoggedIn()) {
|
||||||
|
$current_user_id = Session::get('user_id');
|
||||||
|
$unread_count = MessageModel::getUnreadCountForUser($current_user_id, $user->user_id);
|
||||||
|
if ($unread_count > 0) {
|
||||||
|
echo ' <span class="badge" style="background-color: #d9534f; color: white; padding: 2px 5px; border-radius: 50%;">' . $unread_count . '</span>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<?php } ?>
|
<?php } ?>
|
||||||
|
|||||||
@@ -265,4 +265,18 @@ body {
|
|||||||
/* error page */
|
/* error page */
|
||||||
.red-text {
|
.red-text {
|
||||||
color: red;
|
color: red;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Message badge */
|
||||||
|
.message-badge {
|
||||||
|
display: inline-block;
|
||||||
|
background-color: #d9534f;
|
||||||
|
color: white;
|
||||||
|
font-size: 10px;
|
||||||
|
font-weight: bold;
|
||||||
|
padding: 2px 5px;
|
||||||
|
border-radius: 50%;
|
||||||
|
margin-left: 5px;
|
||||||
|
position: relative;
|
||||||
|
top: -2px;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user