![]() 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
/**
* ChatRoom Model
* Migrated from Prisma ChatRoom model
*/
require_once __DIR__ . '/../config/database.php';
class ChatRoom {
private $conn;
private $table_name = "ChatRoom";
public $id;
public $name;
public $type;
public $createdAt;
public $updatedAt;
public $createdById;
public function __construct($db) {
$this->conn = $db;
}
// Create chat room
public function create() {
$query = "INSERT INTO " . $this->table_name . "
(id, name, type, createdAt, updatedAt, createdById)
VALUES
(:id, :name, :type, :createdAt, :updatedAt, :createdById)";
$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(':name', $this->name);
$stmt->bindParam(':type', $this->type);
$stmt->bindParam(':createdAt', $this->createdAt);
$stmt->bindParam(':updatedAt', $this->updatedAt);
$stmt->bindParam(':createdById', $this->createdById);
if($stmt->execute()) {
return true;
}
return false;
}
// Read chat room by ID
public function findById($id) {
$query = "SELECT * FROM " . $this->table_name . " WHERE 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->name = $row['name'];
$this->type = $row['type'];
$this->createdAt = $row['createdAt'];
$this->updatedAt = $row['updatedAt'];
$this->createdById = $row['createdById'];
return true;
}
return false;
}
// Get chat rooms for user
public function getByUserId($userId) {
$query = "SELECT cr.* FROM " . $this->table_name . " cr
INNER JOIN ChatParticipant cp ON cr.id = cp.chatRoomId
WHERE cp.userId = :userId
ORDER BY cr.updatedAt DESC";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':userId', $userId);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Update chat room
public function update() {
$query = "UPDATE " . $this->table_name . "
SET name = :name,
type = :type,
updatedAt = :updatedAt
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(':name', $this->name);
$stmt->bindParam(':type', $this->type);
$stmt->bindParam(':updatedAt', $this->updatedAt);
if($stmt->execute()) {
return true;
}
return false;
}
// Delete chat room
public function delete() {
$query = "DELETE FROM " . $this->table_name . " WHERE id = :id";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':id', $this->id);
if($stmt->execute()) {
return true;
}
return false;
}
// 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)
);
}
}
?>