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:
parent
7ebca3c762
commit
a9976a5d85
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class CreateTaskAttachmentsTable extends Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
$this->forge->addField([
|
||||
'id' => [
|
||||
'type' => 'INT',
|
||||
'constraint' => 11,
|
||||
'unsigned' => true,
|
||||
'auto_increment' => true,
|
||||
],
|
||||
'task_id' => [
|
||||
'type' => 'INT',
|
||||
'constraint' => 11,
|
||||
'unsigned' => true,
|
||||
'null' => false,
|
||||
],
|
||||
'file_name' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 255,
|
||||
'null' => false,
|
||||
],
|
||||
'file_path' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 500,
|
||||
'null' => false,
|
||||
],
|
||||
'file_size' => [
|
||||
'type' => 'INT',
|
||||
'constraint' => 11,
|
||||
'default' => 0,
|
||||
],
|
||||
'file_type' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 100,
|
||||
'default' => '',
|
||||
],
|
||||
'uploaded_by' => [
|
||||
'type' => 'INT',
|
||||
'constraint' => 11,
|
||||
'null' => false,
|
||||
],
|
||||
'created_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => true,
|
||||
],
|
||||
'updated_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => true,
|
||||
],
|
||||
]);
|
||||
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addKey('task_id');
|
||||
$this->forge->addForeignKey('task_id', 'tasks', 'id', '', 'CASCADE');
|
||||
|
||||
$this->forge->createTable('task_attachments');
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
$this->forge->dropTable('task_attachments');
|
||||
}
|
||||
}
|
||||
|
|
@ -32,6 +32,11 @@ $routes->group('tasks', ['filter' => ['org', 'subscription:tasks'], 'namespace'
|
|||
$routes->post('(:num)/subtasks', 'TasksController::addSubtask/$1');
|
||||
$routes->post('(:num)/subtasks/(:num)/toggle', 'TasksController::toggleSubtask/$1/$2');
|
||||
$routes->post('(:num)/subtasks/(:num)/delete', 'TasksController::deleteSubtask/$1/$2');
|
||||
|
||||
// Attachments API
|
||||
$routes->post('(:num)/attachments/upload', 'TasksController::uploadAttachment/$1');
|
||||
$routes->post('(:num)/attachments/(:num)/delete', 'TasksController::deleteAttachment/$1/$2');
|
||||
$routes->get('(:num)/attachments/(:num)/download', 'TasksController::downloadAttachment/$1/$2');
|
||||
});
|
||||
|
||||
// API Routes для Tasks
|
||||
|
|
|
|||
|
|
@ -614,4 +614,110 @@ class TasksController extends BaseController
|
|||
'success' => $result,
|
||||
]);
|
||||
}
|
||||
|
||||
// ========== Вложения (Attachments) ==========
|
||||
|
||||
/**
|
||||
* API: загрузить вложение
|
||||
*/
|
||||
public function uploadAttachment(int $taskId)
|
||||
{
|
||||
if (!$this->access->canEdit('tasks')) {
|
||||
return $this->response->setJSON([
|
||||
'success' => false,
|
||||
'error' => 'Нет прав для изменения задач'
|
||||
]);
|
||||
}
|
||||
|
||||
$organizationId = $this->requireActiveOrg();
|
||||
$userId = $this->getCurrentUserId();
|
||||
|
||||
// Проверяем что задача принадлежит организации
|
||||
$task = $this->taskService->getTask($taskId, $organizationId);
|
||||
if (!$task) {
|
||||
return $this->response->setJSON([
|
||||
'success' => false,
|
||||
'error' => 'Задача не найдена'
|
||||
]);
|
||||
}
|
||||
|
||||
$file = $this->request->getFile('file');
|
||||
if (!$file || !$file->isValid()) {
|
||||
return $this->response->setJSON([
|
||||
'success' => false,
|
||||
'error' => 'Файл не загружен'
|
||||
]);
|
||||
}
|
||||
|
||||
$attachmentId = $this->taskService->uploadAttachment($taskId, [
|
||||
'name' => $file->getName(),
|
||||
'type' => $file->getClientMimeType(),
|
||||
'tmp_name' => $file->getTempName(),
|
||||
'size' => $file->getSize(),
|
||||
], $userId);
|
||||
|
||||
return $this->response->setJSON([
|
||||
'success' => (bool) $attachmentId,
|
||||
'attachment_id' => $attachmentId,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* API: удалить вложение
|
||||
*/
|
||||
public function deleteAttachment(int $taskId, int $attachmentId)
|
||||
{
|
||||
if (!$this->access->canEdit('tasks')) {
|
||||
return $this->response->setJSON([
|
||||
'success' => false,
|
||||
'error' => 'Нет прав для изменения задач'
|
||||
]);
|
||||
}
|
||||
|
||||
$organizationId = $this->requireActiveOrg();
|
||||
$userId = $this->getCurrentUserId();
|
||||
|
||||
// Проверяем что задача принадлежит организации
|
||||
$task = $this->taskService->getTask($taskId, $organizationId);
|
||||
if (!$task) {
|
||||
return $this->response->setJSON([
|
||||
'success' => false,
|
||||
'error' => 'Задача не найдена'
|
||||
]);
|
||||
}
|
||||
|
||||
$result = $this->taskService->deleteAttachment($taskId, $attachmentId, $userId);
|
||||
|
||||
return $this->response->setJSON([
|
||||
'success' => $result,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* API: скачать вложение
|
||||
*/
|
||||
public function downloadAttachment(int $taskId, int $attachmentId)
|
||||
{
|
||||
$organizationId = $this->requireActiveOrg();
|
||||
|
||||
// Проверяем что задача принадлежит организации
|
||||
$task = $this->taskService->getTask($taskId, $organizationId);
|
||||
if (!$task) {
|
||||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Задача не найдена');
|
||||
}
|
||||
|
||||
$attachmentModel = new \App\Modules\Tasks\Models\TaskAttachmentModel();
|
||||
$attachment = $attachmentModel->find($attachmentId);
|
||||
|
||||
if (!$attachment || $attachment['task_id'] != $taskId) {
|
||||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Вложение не найдено');
|
||||
}
|
||||
|
||||
if (!file_exists($attachment['file_path'])) {
|
||||
throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound('Файл не найден');
|
||||
}
|
||||
|
||||
return $this->response->download($attachment['file_path'], null)
|
||||
->setFileName($attachment['file_name']);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,108 @@
|
|||
<?php
|
||||
|
||||
namespace App\Modules\Tasks\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
use App\Models\Traits\TenantScopedModel;
|
||||
|
||||
class TaskAttachmentModel extends Model
|
||||
{
|
||||
use TenantScopedModel;
|
||||
|
||||
protected $table = 'task_attachments';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = 'array';
|
||||
protected $useSoftDeletes = false;
|
||||
protected $tenantField = 'task.organization_id';
|
||||
protected $allowedFields = [
|
||||
'task_id',
|
||||
'file_name',
|
||||
'file_path',
|
||||
'file_size',
|
||||
'file_type',
|
||||
'uploaded_by',
|
||||
];
|
||||
protected $useTimestamps = true;
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
|
||||
/**
|
||||
* Получить вложения задачи
|
||||
*/
|
||||
public function getByTask(int $taskId): array
|
||||
{
|
||||
return $this->where('task_id', $taskId)
|
||||
->orderBy('created_at', 'DESC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить вложение по ID
|
||||
*/
|
||||
public function getById(int $attachmentId): ?array
|
||||
{
|
||||
return $this->find($attachmentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Удалить вложение
|
||||
*/
|
||||
public function deleteAttachment(int $attachmentId, int $userId): 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 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';
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,6 +140,53 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
{# Вложения #}
|
||||
<div class="card border-0 shadow-sm mb-4">
|
||||
<div class="card-header bg-light d-flex justify-content-between align-items-center">
|
||||
<h5 class="mb-0">
|
||||
<i class="fa-solid fa-paperclip me-2"></i>Вложения
|
||||
{% if task.attachments_count %}
|
||||
<span class="badge bg-secondary ms-2">{{ task.attachments_count }}</span>
|
||||
{% endif %}
|
||||
</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
{% if task.attachments %}
|
||||
<ul class="list-group list-group-flush mb-3" id="attachments-list">
|
||||
{% for attachment in task.attachments %}
|
||||
<li class="list-group-item d-flex align-items-center gap-2" data-attachment-id="{{ attachment.id }}">
|
||||
<i class="fa-solid fa-file text-muted"></i>
|
||||
<a href="{{ base_url('/tasks/' ~ task.id ~ '/attachments/' ~ attachment.id ~ '/download') }}"
|
||||
target="_blank" class="flex-grow-1 text-decoration-none">
|
||||
{{ attachment.file_name }}
|
||||
</a>
|
||||
<small class="text-muted">
|
||||
{{ attachment.file_size|filesize }}
|
||||
</small>
|
||||
<button type="button" class="btn btn-outline-danger btn-sm"
|
||||
onclick="deleteAttachment({{ task.id }}, {{ attachment.id }})"
|
||||
title="Удалить">
|
||||
<i class="fa-solid fa-times"></i>
|
||||
</button>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
{% else %}
|
||||
<p class="text-muted text-center mb-3">Нет вложений</p>
|
||||
{% endif %}
|
||||
|
||||
<form action="{{ base_url('/tasks/' ~ task.id ~ '/attachments/upload') }}" method="post"
|
||||
class="d-flex gap-2 attachment-form" enctype="multipart/form-data"
|
||||
onsubmit="uploadAttachment(event, {{ task.id }})">
|
||||
{{ csrf_field()|raw }}
|
||||
<input type="file" name="file" class="form-control" required>
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<i class="fa-solid fa-upload"></i>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{# Комментарии #}
|
||||
<div class="card border-0 shadow-sm">
|
||||
<div class="card-header bg-light">
|
||||
|
|
@ -314,5 +361,67 @@ function deleteSubtask(taskId, subtaskId) {
|
|||
console.error('Error:', error);
|
||||
});
|
||||
}
|
||||
|
||||
// ========== Вложения ==========
|
||||
|
||||
function uploadAttachment(event, taskId) {
|
||||
event.preventDefault();
|
||||
const form = event.target;
|
||||
const fileInput = form.querySelector('input[name="file"]');
|
||||
const file = fileInput.files[0];
|
||||
|
||||
if (!file) {
|
||||
alert('Выберите файл');
|
||||
return;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
formData.append('csrf_token', form.querySelector('input[name="csrf_token"]').value);
|
||||
|
||||
fetch(`/tasks/${taskId}/attachments/upload`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
},
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
location.reload();
|
||||
} else {
|
||||
alert(data.error || 'Ошибка при загрузке файла');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Ошибка при загрузке файла');
|
||||
});
|
||||
}
|
||||
|
||||
function deleteAttachment(taskId, attachmentId) {
|
||||
if (!confirm('Удалить вложение?')) return;
|
||||
|
||||
fetch(`/tasks/${taskId}/attachments/${attachmentId}/delete`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
},
|
||||
body: ''
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (!data.success) {
|
||||
alert(data.error || 'Ошибка');
|
||||
} else {
|
||||
location.reload();
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
}
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
|
|
|||
Loading…
Reference in New Issue