141 lines
3.9 KiB
PHP
141 lines
3.9 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use CodeIgniter\Model;
|
||
|
||
class AttachmentModel extends Model
|
||
{
|
||
protected $table = 'attachments';
|
||
protected $primaryKey = 'id';
|
||
protected $useAutoIncrement = true;
|
||
protected $returnType = 'array';
|
||
protected $useSoftDeletes = false;
|
||
protected $allowedFields = [
|
||
'entity_type',
|
||
'entity_id',
|
||
'file_name',
|
||
'file_path',
|
||
'file_size',
|
||
'file_type',
|
||
'uploaded_by',
|
||
];
|
||
protected $useTimestamps = true;
|
||
protected $createdField = 'created_at';
|
||
protected $updatedField = 'updated_at';
|
||
|
||
/**
|
||
* Получить вложения по типу и ID сущности
|
||
*/
|
||
public function getByEntity(string $entityType, int $entityId): array
|
||
{
|
||
return $this->where('entity_type', $entityType)
|
||
->where('entity_id', $entityId)
|
||
->orderBy('created_at', 'DESC')
|
||
->findAll();
|
||
}
|
||
|
||
/**
|
||
* Получить вложение по ID
|
||
*/
|
||
public function getById(int $attachmentId): ?array
|
||
{
|
||
return $this->find($attachmentId);
|
||
}
|
||
|
||
/**
|
||
* Проверить принадлежность вложения сущности
|
||
*/
|
||
public function belongsToEntity(string $entityType, int $entityId, int $attachmentId): bool
|
||
{
|
||
$attachment = $this->find($attachmentId);
|
||
return $attachment && $attachment['entity_type'] === $entityType && $attachment['entity_id'] == $entityId;
|
||
}
|
||
|
||
/**
|
||
* Удалить вложение
|
||
*/
|
||
public function deleteAttachment(int $attachmentId): bool
|
||
{
|
||
$attachment = $this->find($attachmentId);
|
||
if (!$attachment) {
|
||
return false;
|
||
}
|
||
|
||
// Удаляем файл с диска
|
||
$filePath = $attachment['file_path'];
|
||
if (file_exists($filePath)) {
|
||
unlink($filePath);
|
||
}
|
||
|
||
return $this->delete($attachmentId);
|
||
}
|
||
|
||
/**
|
||
* Удалить все вложения сущности
|
||
*/
|
||
public function deleteByEntity(string $entityType, int $entityId): bool
|
||
{
|
||
$attachments = $this->where('entity_type', $entityType)
|
||
->where('entity_id', $entityId)
|
||
->findAll();
|
||
|
||
foreach ($attachments as $attachment) {
|
||
$this->deleteAttachment($attachment['id']);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
/**
|
||
* Получить количество вложений сущности
|
||
*/
|
||
public function countByEntity(string $entityType, int $entityId): int
|
||
{
|
||
return $this->where('entity_type', $entityType)
|
||
->where('entity_id', $entityId)
|
||
->countAllResults();
|
||
}
|
||
|
||
/**
|
||
* Получить размер файла в человекочитаемом формате
|
||
*/
|
||
public function formatSize(int $bytes): string
|
||
{
|
||
if ($bytes >= 1073741824) {
|
||
return number_format($bytes / 1073741824, 2) . ' GB';
|
||
} elseif ($bytes >= 1048576) {
|
||
return number_format($bytes / 1048576, 2) . ' MB';
|
||
} elseif ($bytes >= 1024) {
|
||
return number_format($bytes / 1024, 2) . ' KB';
|
||
}
|
||
|
||
return $bytes . ' B';
|
||
}
|
||
|
||
/**
|
||
* Получить иконку для типа файла
|
||
*/
|
||
public function getFileIcon(string $fileType): string
|
||
{
|
||
$icons = [
|
||
'image' => 'fa-file-image',
|
||
'pdf' => 'fa-file-pdf',
|
||
'word' => 'fa-file-word',
|
||
'excel' => 'fa-file-excel',
|
||
'powerpoint' => 'fa-file-powerpoint',
|
||
'zip' => 'fa-file-zipper',
|
||
'text' => 'fa-file-lines',
|
||
'video' => 'fa-file-video',
|
||
'audio' => 'fa-file-audio',
|
||
];
|
||
|
||
foreach ($icons as $key => $icon) {
|
||
if (stripos($fileType, $key) !== false) {
|
||
return $icon;
|
||
}
|
||
}
|
||
|
||
return 'fa-file';
|
||
}
|
||
} |