![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.quebec/private_html/php-migration/models/ |
<?php
/**
* Message Model
* Migrated from Prisma Message model
*/
require_once __DIR__ . '/../config/database.php';
class Message {
private $conn;
private $table_name = "Message";
public $id;
public $content;
public $type;
public $chatRoomId;
public $userId;
public $createdAt;
public $updatedAt;
public $isEdited;
public $replyToId;
public $fileUrl;
public $fileName;
public $fileSize;
public function __construct($db) {
$this->conn = $db;
}
// Create message
public function create() {
$query = "INSERT INTO " . $this->table_name . "
(id, content, type, chatRoomId, userId, createdAt, updatedAt,
isEdited, replyToId, fileUrl, fileName, fileSize, )
VALUES
(:id, :content, :type, :chatRoomId, :userId, :createdAt, :updatedAt,
:isEdited, :replyToId, :fileUrl, :fileName, :fileSize, :)";
$stmt = $this->conn->prepare($query);
// Generate UUID for id
$this->id = $this->generateUUID();
$this->createdAt = date('Y-m-d H:i:s');
$this->updatedAt = date('Y-m-d H:i:s');
// Bind values
$stmt->bindParam(':id', $this->id);
$stmt->bindParam(':content', $this->content);
$stmt->bindParam(':type', $this->type);
$stmt->bindParam(':chatRoomId', $this->chatRoomId);
$stmt->bindParam(':userId', $this->userId);
$stmt->bindParam(':createdAt', $this->createdAt);
$stmt->bindParam(':updatedAt', $this->updatedAt);
$stmt->bindParam(':isEdited', $this->isEdited);
$stmt->bindParam(':replyToId', $this->replyToId);
$stmt->bindParam(':fileUrl', $this->fileUrl);
$stmt->bindParam(':fileName', $this->fileName);
$stmt->bindParam(':fileSize', $this->fileSize);
$stmt->bindParam(':', $this->);
if($stmt->execute()) {
return true;
}
return false;
}
// Read message by ID
public function findById($id) {
$query = "SELECT m.*, u.name as userName, u.profilePicture
FROM " . $this->table_name . " m
LEFT JOIN User u ON m.userId = u.id
WHERE m.id = :id LIMIT 1";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':id', $id);
$stmt->execute();
if($stmt->rowCount() > 0) {
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$this->id = $row['id'];
$this->content = $row['content'];
$this->type = $row['type'];
$this->chatRoomId = $row['chatRoomId'];
$this->userId = $row['userId'];
$this->createdAt = $row['createdAt'];
$this->updatedAt = $row['updatedAt'];
$this->isEdited = $row['isEdited'];
$this->replyToId = $row['replyToId'];
$this->fileUrl = $row['fileUrl'];
$this->fileName = $row['fileName'];
$this->fileSize = $row['fileSize'];
$this-> = $row[''];
return true;
}
return false;
}
// Get messages for chat room
public function getByChatRoomId($chatRoomId, $page = 1, $limit = 50) {
$offset = ($page - 1) * $limit;
$query = "SELECT m.*, u.name as userName, u.profilePicture, u.role as userRole
FROM " . $this->table_name . " m
LEFT JOIN User u ON m.userId = u.id
WHERE m.chatRoomId = :chatRoomId
ORDER BY m.createdAt DESC
LIMIT :limit OFFSET :offset";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':chatRoomId', $chatRoomId);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Get recent messages for chat room
public function getRecentByChatRoomId($chatRoomId, $limit = 20) {
$query = "SELECT m.*, u.name as userName, u.profilePicture, u.role as userRole
FROM " . $this->table_name . " m
LEFT JOIN User u ON m.userId = u.id
WHERE m.chatRoomId = :chatRoomId
ORDER BY m.createdAt DESC
LIMIT :limit";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':chatRoomId', $chatRoomId);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
return array_reverse($stmt->fetchAll(PDO::FETCH_ASSOC));
}
// Update message
public function update() {
$query = "UPDATE " . $this->table_name . "
SET content = :content,
type = :type,
updatedAt = :updatedAt,
isEdited = :isEdited,
fileUrl = :fileUrl,
fileName = :fileName,
fileSize = :fileSize,
= : WHERE id = :id";
$stmt = $this->conn->prepare($query);
$this->updatedAt = date('Y-m-d H:i:s');
// Bind values
$stmt->bindParam(':id', $this->id);
$stmt->bindParam(':content', $this->content);
$stmt->bindParam(':type', $this->type);
$stmt->bindParam(':updatedAt', $this->updatedAt);
$stmt->bindParam(':isEdited', $this->isEdited);
$stmt->bindParam(':fileUrl', $this->fileUrl);
$stmt->bindParam(':fileName', $this->fileName);
$stmt->bindParam(':fileSize', $this->fileSize);
$stmt->bindParam(':', $this->);
if($stmt->execute()) {
return true;
}
return false;
}
// Delete message (soft delete)
public function delete() {
$this->content = '[Message deleted]';
return $this->update();
}
// Edit message
public function edit($newContent) {
$this->content = $newContent;
$this->isEdited = 1;
return $this->update();
}
// Count messages in chat room
public function countByChatRoomId($chatRoomId) {
$query = "SELECT COUNT(*) as total FROM " . $this->table_name . "
WHERE chatRoomId = :chatRoomId";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':chatRoomId', $chatRoomId);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row['total'];
}
// Generate UUID
private function generateUUID() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
}
?>