Refactor: Create shared AttachmentService and AttachmentModel

- Created common AttachmentService for all modules (app/Services/)
- Created common AttachmentModel (app/Models/)
- Created common migration (app/Database/Migrations/)
- Updated Tasks module to use shared AttachmentService
- Added Twig functions: format_filesize(), get_file_icon()
- Removed duplicate TaskAttachmentModel and task_attachments migration
- AttachmentService can be used by any module: task, deal, contact, etc.
This commit is contained in:
2026-02-08 20:34:11 +00:00
parent 9c4aacefbe
commit 052560c9b4
7 changed files with 226 additions and 74 deletions
+116
View File
@@ -0,0 +1,116 @@
<?php
namespace App\Services;
use App\Models\AttachmentModel;
use CodeIgniter\Events\Events;
class AttachmentService
{
protected AttachmentModel $attachmentModel;
public function __construct()
{
$this->attachmentModel = new AttachmentModel();
}
/**
* Загрузить вложение
*/
public function upload(string $entityType, int $entityId, array $file, int $userId): ?int
{
$uploadPath = WRITEPATH . 'uploads/' . $entityType . '/' . $entityId . '/';
// Создаём директорию если не существует
if (!is_dir($uploadPath)) {
mkdir($uploadPath, 0755, true);
}
// Генерируем уникальное имя файла
$originalName = $file['name'];
$extension = pathinfo($originalName, PATHINFO_EXTENSION);
$newFileName = uniqid() . '_' . time() . '.' . $extension;
$filePath = $uploadPath . $newFileName;
// Перемещаем загруженный файл
if (!move_uploaded_file($file['tmp_name'], $filePath)) {
return null;
}
$attachmentId = $this->attachmentModel->insert([
'entity_type' => $entityType,
'entity_id' => $entityId,
'file_name' => $originalName,
'file_path' => $filePath,
'file_size' => $file['size'],
'file_type' => $file['type'] ?? '',
'uploaded_by' => $userId,
]);
if ($attachmentId) {
Events::trigger('attachment.uploaded', [
'entity_type' => $entityType,
'entity_id' => $entityId,
'attachment_id' => $attachmentId,
'user_id' => $userId,
]);
}
return $attachmentId;
}
/**
* Удалить вложение
*/
public function delete(string $entityType, int $entityId, int $attachmentId, int $userId): bool
{
if (!$this->attachmentModel->belongsToEntity($entityType, $entityId, $attachmentId)) {
return false;
}
$result = $this->attachmentModel->deleteAttachment($attachmentId);
if ($result) {
Events::trigger('attachment.deleted', [
'entity_type' => $entityType,
'entity_id' => $entityId,
'attachment_id' => $attachmentId,
'user_id' => $userId,
]);
}
return $result;
}
/**
* Получить вложения сущности
*/
public function getByEntity(string $entityType, int $entityId): array
{
return $this->attachmentModel->getByEntity($entityType, $entityId);
}
/**
* Получить количество вложений
*/
public function count(string $entityType, int $entityId): int
{
return $this->attachmentModel->countByEntity($entityType, $entityId);
}
/**
* Получить иконку файла
*/
public function getFileIcon(string $fileType): string
{
return $this->attachmentModel->getFileIcon($fileType);
}
/**
* Форматировать размер файла
*/
public function formatSize(int $bytes): string
{
return $this->attachmentModel->formatSize($bytes);
}
}