Tasks Module Stage 3: Attachments
- Add migration for task_attachments table - Create TaskAttachmentModel with file handling methods - Add TaskService methods for upload/delete attachments - Add API routes for attachments (upload, delete, download) - Add controller methods for attachment operations - Add UI section in task view with file upload form - Add JavaScript handlers for AJAX file operations
This commit is contained in:
@@ -6,6 +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 CodeIgniter\Events\Events;
|
||||
|
||||
class TaskService
|
||||
@@ -14,6 +15,7 @@ class TaskService
|
||||
protected TaskAssigneeModel $assigneeModel;
|
||||
protected TaskColumnModel $columnModel;
|
||||
protected TaskSubtaskModel $subtaskModel;
|
||||
protected TaskAttachmentModel $attachmentModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -21,6 +23,7 @@ class TaskService
|
||||
$this->assigneeModel = new TaskAssigneeModel();
|
||||
$this->columnModel = new TaskColumnModel();
|
||||
$this->subtaskModel = new TaskSubtaskModel();
|
||||
$this->attachmentModel = new TaskAttachmentModel();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,6 +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']);
|
||||
|
||||
return $task;
|
||||
}
|
||||
@@ -349,4 +354,64 @@ class TaskService
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// ========== Вложения (Attachments) ==========
|
||||
|
||||
/**
|
||||
* Загрузить вложение
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Удалить вложение
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user