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:
@@ -6,7 +6,7 @@ use App\Modules\Tasks\Models\TaskModel;
|
||||
use App\Modules\Tasks\Models\TaskAssigneeModel;
|
||||
use App\Modules\Tasks\Models\TaskColumnModel;
|
||||
use App\Modules\Tasks\Models\TaskSubtaskModel;
|
||||
use App\Modules\Tasks\Models\TaskAttachmentModel;
|
||||
use App\Services\AttachmentService;
|
||||
use CodeIgniter\Events\Events;
|
||||
|
||||
class TaskService
|
||||
@@ -15,7 +15,7 @@ class TaskService
|
||||
protected TaskAssigneeModel $assigneeModel;
|
||||
protected TaskColumnModel $columnModel;
|
||||
protected TaskSubtaskModel $subtaskModel;
|
||||
protected TaskAttachmentModel $attachmentModel;
|
||||
protected AttachmentService $attachmentService;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -23,7 +23,7 @@ class TaskService
|
||||
$this->assigneeModel = new TaskAssigneeModel();
|
||||
$this->columnModel = new TaskColumnModel();
|
||||
$this->subtaskModel = new TaskSubtaskModel();
|
||||
$this->attachmentModel = new TaskAttachmentModel();
|
||||
$this->attachmentService = new AttachmentService();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,8 +183,8 @@ class TaskService
|
||||
$task['subtasks'] = $this->subtaskModel->getByTask($taskId);
|
||||
$task['subtasks_count'] = count($task['subtasks']);
|
||||
$task['subtasks_completed'] = count(array_filter($task['subtasks'], fn($s) => $s['is_completed']));
|
||||
$task['attachments'] = $this->attachmentModel->getByTask($taskId);
|
||||
$task['attachments_count'] = count($task['attachments']);
|
||||
$task['attachments'] = $this->attachmentService->getByEntity('task', $taskId);
|
||||
$task['attachments_count'] = $this->attachmentService->count('task', $taskId);
|
||||
|
||||
return $task;
|
||||
}
|
||||
@@ -355,45 +355,14 @@ class TaskService
|
||||
return $result;
|
||||
}
|
||||
|
||||
// ========== Вложения (Attachments) ==========
|
||||
// ========== Вложения (Attachments) - используем общий AttachmentService ==========
|
||||
|
||||
/**
|
||||
* Загрузить вложение
|
||||
*/
|
||||
public function uploadAttachment(int $taskId, array $file, int $userId): ?int
|
||||
{
|
||||
$uploadPath = WRITEPATH . 'uploads/tasks/' . $taskId . '/';
|
||||
|
||||
// Создаём директорию если не существует
|
||||
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([
|
||||
'task_id' => $taskId,
|
||||
'file_name' => $originalName,
|
||||
'file_path' => $filePath,
|
||||
'file_size' => $file['size'],
|
||||
'file_type' => $file['type'] ?? '',
|
||||
'uploaded_by' => $userId,
|
||||
]);
|
||||
|
||||
if ($attachmentId) {
|
||||
Events::trigger('task.attachment_uploaded', $taskId, $attachmentId, $userId);
|
||||
}
|
||||
|
||||
return $attachmentId;
|
||||
return $this->attachmentService->upload('task', $taskId, $file, $userId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -401,17 +370,6 @@ class TaskService
|
||||
*/
|
||||
public function deleteAttachment(int $taskId, int $attachmentId, int $userId): bool
|
||||
{
|
||||
$attachment = $this->attachmentModel->find($attachmentId);
|
||||
if (!$attachment || $attachment['task_id'] != $taskId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = $this->attachmentModel->deleteAttachment($attachmentId, $userId);
|
||||
|
||||
if ($result) {
|
||||
Events::trigger('task.attachment_deleted', $taskId, $attachmentId, $userId);
|
||||
}
|
||||
|
||||
return $result;
|
||||
return $this->attachmentService->delete('task', $taskId, $attachmentId, $userId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user