![]() 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
/**
* Document Model
* Migrated from Prisma Document model
*/
require_once __DIR__ . '/../config/database.php';
class Document {
private $conn;
private $table_name = "documents";
public $id;
public $title;
public $description;
public $url;
public $fileName;
public $fileSize;
public $fileType;
public $userId;
public $caseId;
public $createdAt;
public $updatedAt;
public $isPublic;
public $version;
public $tags;
public $category;
public $status;
public function __construct($db) {
$this->conn = $db;
}
// Create document
public function create() {
$query = "INSERT INTO " . $this->table_name . "
(id, title, description, url, fileName, fileSize, fileType,
userId, caseId, createdAt, updatedAt, isPublic, version, tags, category, status)
VALUES
(:id, :title, :description, :url, :fileName, :fileSize, :fileType,
:userId, :caseId, :createdAt, :updatedAt, :isPublic, :version, :tags, :category, :status)";
$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');
// Convert arrays to JSON
$this->tags = is_array($this->tags) ? json_encode($this->tags) : $this->tags;
// Bind values
$stmt->bindParam(':id', $this->id);
$stmt->bindParam(':title', $this->title);
$stmt->bindParam(':description', $this->description);
$stmt->bindParam(':url', $this->url);
$stmt->bindParam(':fileName', $this->fileName);
$stmt->bindParam(':fileSize', $this->fileSize);
$stmt->bindParam(':fileType', $this->fileType);
$stmt->bindParam(':userId', $this->userId);
$stmt->bindParam(':caseId', $this->caseId);
$stmt->bindParam(':createdAt', $this->createdAt);
$stmt->bindParam(':updatedAt', $this->updatedAt);
$stmt->bindParam(':isPublic', $this->isPublic);
$stmt->bindParam(':version', $this->version);
$stmt->bindParam(':tags', $this->tags);
$stmt->bindParam(':category', $this->category);
$stmt->bindParam(':status', $this->status);
if($stmt->execute()) {
return true;
}
return false;
}
// Read document 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->title = $row['title'];
$this->description = $row['description'];
$this->url = $row['url'];
$this->fileName = $row['fileName'];
$this->fileSize = $row['fileSize'];
$this->fileType = $row['fileType'];
$this->userId = $row['userId'];
$this->caseId = $row['caseId'];
$this->createdAt = $row['createdAt'];
$this->updatedAt = $row['updatedAt'];
$this->isPublic = $row['isPublic'];
$this->version = $row['version'];
$this->tags = json_decode($row['tags'], true);
$this->category = $row['category'];
$this->status = $row['status'];
return true;
}
return false;
}
// Get documents by user ID
public function getByUserId($userId, $page = 1, $limit = 20) {
$offset = ($page - 1) * $limit;
$query = "SELECT * FROM " . $this->table_name . "
WHERE userId = :userId AND status = 'ACTIVE'
ORDER BY createdAt DESC LIMIT :limit OFFSET :offset";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':userId', $userId);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$documents = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Decode JSON fields
foreach ($documents as &$doc) {
$doc['tags'] = json_decode($doc['tags'], true);
}
return $documents;
}
// Get documents by case ID
public function getByCaseId($caseId, $page = 1, $limit = 20) {
$offset = ($page - 1) * $limit;
$query = "SELECT * FROM " . $this->table_name . "
WHERE caseId = :caseId AND status = 'ACTIVE'
ORDER BY createdAt DESC LIMIT :limit OFFSET :offset";
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':caseId', $caseId);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$documents = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Decode JSON fields
foreach ($documents as &$doc) {
$doc['tags'] = json_decode($doc['tags'], true);
}
return $documents;
}
// Get public documents
public function getPublicDocuments($page = 1, $limit = 20, $filters = []) {
$offset = ($page - 1) * $limit;
$whereClause = "WHERE isPublic = 1 AND status = 'ACTIVE'";
$params = [];
// Apply filters
if (!empty($filters['category'])) {
$whereClause .= " AND category = :category";
$params[':category'] = $filters['category'];
}
if (!empty($filters['search'])) {
$whereClause .= " AND (title LIKE :search OR description LIKE :search)";
$params[':search'] = '%' . $filters['search'] . '%';
}
$query = "SELECT * FROM " . $this->table_name . " " . $whereClause . " ORDER BY createdAt DESC LIMIT :limit OFFSET :offset";
$stmt = $this->conn->prepare($query);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$documents = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Decode JSON fields
foreach ($documents as &$doc) {
$doc['tags'] = json_decode($doc['tags'], true);
}
return $documents;
}
// Update document
public function update() {
$query = "UPDATE " . $this->table_name . "
SET title = :title,
description = :description,
updatedAt = :updatedAt,
isPublic = :isPublic,
version = :version,
tags = :tags,
category = :category,
status = :status
WHERE id = :id";
$stmt = $this->conn->prepare($query);
$this->updatedAt = date('Y-m-d H:i:s');
// Convert arrays to JSON
$this->tags = is_array($this->tags) ? json_encode($this->tags) : $this->tags;
// Bind values
$stmt->bindParam(':id', $this->id);
$stmt->bindParam(':title', $this->title);
$stmt->bindParam(':description', $this->description);
$stmt->bindParam(':updatedAt', $this->updatedAt);
$stmt->bindParam(':isPublic', $this->isPublic);
$stmt->bindParam(':version', $this->version);
$stmt->bindParam(':tags', $this->tags);
$stmt->bindParam(':category', $this->category);
$stmt->bindParam(':status', $this->status);
if($stmt->execute()) {
return true;
}
return false;
}
// Delete document (soft delete)
public function delete() {
$this->status = 'DELETED';
return $this->update();
}
// Count documents
public function count($filters = []) {
$whereClause = "WHERE 1=1";
$params = [];
// Apply filters
if (!empty($filters['userId'])) {
$whereClause .= " AND userId = :userId";
$params[':userId'] = $filters['userId'];
}
if (!empty($filters['caseId'])) {
$whereClause .= " AND caseId = :caseId";
$params[':caseId'] = $filters['caseId'];
}
if (!empty($filters['isPublic'])) {
$whereClause .= " AND isPublic = :isPublic";
$params[':isPublic'] = $filters['isPublic'];
}
if (!empty($filters['status'])) {
$whereClause .= " AND status = :status";
$params[':status'] = $filters['status'];
}
$query = "SELECT COUNT(*) as total FROM " . $this->table_name . " " . $whereClause;
$stmt = $this->conn->prepare($query);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row['total'];
}
// Get file size in human readable format
public function getFormattedFileSize() {
$bytes = $this->fileSize;
$units = ['B', 'KB', 'MB', 'GB'];
for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
$bytes /= 1024;
}
return round($bytes, 2) . ' ' . $units[$i];
}
// Get file icon based on type
public function getFileIcon() {
$extension = strtolower(pathinfo($this->fileName, PATHINFO_EXTENSION));
$icons = [
'pdf' => '📄',
'doc' => '📝',
'docx' => '📝',
'txt' => '📄',
'jpg' => '🖼️',
'jpeg' => '🖼️',
'png' => '🖼️',
'gif' => '🖼️',
'mp4' => '🎥',
'avi' => '🎥',
'mov' => '🎥',
'mp3' => '🎵',
'wav' => '🎵',
'zip' => '📦',
'rar' => '📦'
];
return $icons[$extension] ?? '📄';
}
// 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)
);
}
}
?>