Tasks Module Stage 2: Subtasks
- Create task_subtasks table migration - Create TaskSubtaskModel with CRUD operations - Add subtask API methods: addSubtask, toggleSubtask, deleteSubtask - Update TaskService to include subtasks in getTask() - Add Routes for subtasks API - Update show.twig with subtasks UI and JavaScript
This commit is contained in:
parent
d7fec7169f
commit
cee6c636ad
|
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class CreateTaskSubtasksTable 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,
|
||||
],
|
||||
'title' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 255,
|
||||
],
|
||||
'is_completed' => [
|
||||
'type' => 'BOOLEAN',
|
||||
'default' => false,
|
||||
],
|
||||
'order_index' => [
|
||||
'type' => 'INT',
|
||||
'constraint' => 11,
|
||||
'default' => 0,
|
||||
],
|
||||
'created_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => true,
|
||||
],
|
||||
'updated_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => true,
|
||||
],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->addForeignKey('task_id', 'tasks', 'id', 'CASCADE', 'CASCADE');
|
||||
$this->forge->createTable('task_subtasks');
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
$this->forge->dropTable('task_subtasks');
|
||||
}
|
||||
}
|
||||
|
|
@ -27,6 +27,11 @@ $routes->group('tasks', ['filter' => ['org', 'subscription:tasks'], 'namespace'
|
|||
$routes->post('move-column', 'TasksController::moveColumn');
|
||||
$routes->post('(:num)/complete', 'TasksController::complete/$1');
|
||||
$routes->post('(:num)/reopen', 'TasksController::reopen/$1');
|
||||
|
||||
// Subtasks API
|
||||
$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');
|
||||
});
|
||||
|
||||
// API Routes для Tasks
|
||||
|
|
|
|||
|
|
@ -510,4 +510,108 @@ class TasksController extends BaseController
|
|||
|
||||
return $this->response->setJSON(['success' => $result]);
|
||||
}
|
||||
|
||||
// ========== Подзадачи (Subtasks) ==========
|
||||
|
||||
/**
|
||||
* API: добавить подзадачу
|
||||
*/
|
||||
public function addSubtask(int $taskId)
|
||||
{
|
||||
if (!$this->access->canEdit('tasks')) {
|
||||
return $this->response->setJSON([
|
||||
'success' => false,
|
||||
'error' => 'Нет прав для изменения задач'
|
||||
]);
|
||||
}
|
||||
|
||||
$organizationId = $this->requireActiveOrg();
|
||||
$userId = $this->getCurrentUserId();
|
||||
|
||||
$title = trim($this->request->getPost('title'));
|
||||
if (empty($title)) {
|
||||
return $this->response->setJSON([
|
||||
'success' => false,
|
||||
'error' => 'Название подзадачи обязательно'
|
||||
]);
|
||||
}
|
||||
|
||||
// Проверяем что задача принадлежит организации
|
||||
$task = $this->taskService->getTask($taskId, $organizationId);
|
||||
if (!$task) {
|
||||
return $this->response->setJSON([
|
||||
'success' => false,
|
||||
'error' => 'Задача не найдена'
|
||||
]);
|
||||
}
|
||||
|
||||
$subtaskId = $this->taskService->addSubtask($taskId, $title, $userId);
|
||||
|
||||
return $this->response->setJSON([
|
||||
'success' => (bool) $subtaskId,
|
||||
'subtask_id' => $subtaskId,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* API: переключить статус подзадачи
|
||||
*/
|
||||
public function toggleSubtask(int $taskId, int $subtaskId)
|
||||
{
|
||||
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->toggleSubtask($taskId, $subtaskId, $userId);
|
||||
|
||||
return $this->response->setJSON([
|
||||
'success' => $result,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* API: удалить подзадачу
|
||||
*/
|
||||
public function deleteSubtask(int $taskId, int $subtaskId)
|
||||
{
|
||||
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->deleteSubtask($taskId, $subtaskId, $userId);
|
||||
|
||||
return $this->response->setJSON([
|
||||
'success' => $result,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
namespace App\Modules\Tasks\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
use App\Models\Traits\TenantScopedModel;
|
||||
|
||||
class TaskSubtaskModel extends Model
|
||||
{
|
||||
use TenantScopedModel;
|
||||
|
||||
protected $table = 'task_subtasks';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = 'array';
|
||||
protected $useSoftDeletes = false;
|
||||
protected $tenantField = 'task.organization_id';
|
||||
protected $allowedFields = [
|
||||
'task_id',
|
||||
'title',
|
||||
'is_completed',
|
||||
'order_index',
|
||||
];
|
||||
protected $useTimestamps = true;
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
|
||||
/**
|
||||
* Получить подзадачи задачи
|
||||
*/
|
||||
public function getByTask(int $taskId): array
|
||||
{
|
||||
return $this->where('task_id', $taskId)
|
||||
->orderBy('order_index', 'ASC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить следующий порядковый номер для подзадачи
|
||||
*/
|
||||
public function getNextOrder(int $taskId): int
|
||||
{
|
||||
$max = $this->selectMax('order_index')
|
||||
->where('task_id', $taskId)
|
||||
->first();
|
||||
|
||||
return ($max['order_index'] ?? 0) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Переключить статус подзадачи
|
||||
*/
|
||||
public function toggle(int $subtaskId): bool
|
||||
{
|
||||
$subtask = $this->find($subtaskId);
|
||||
if (!$subtask) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->update($subtaskId, [
|
||||
'is_completed' => !$subtask['is_completed'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить количество незавершённых подзадач
|
||||
*/
|
||||
public function getIncompleteCount(int $taskId): int
|
||||
{
|
||||
return $this->where('task_id', $taskId)
|
||||
->where('is_completed', false)
|
||||
->countAllResults();
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ namespace App\Modules\Tasks\Services;
|
|||
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 CodeIgniter\Events\Events;
|
||||
|
||||
class TaskService
|
||||
|
|
@ -12,12 +13,14 @@ class TaskService
|
|||
protected TaskModel $taskModel;
|
||||
protected TaskAssigneeModel $assigneeModel;
|
||||
protected TaskColumnModel $columnModel;
|
||||
protected TaskSubtaskModel $subtaskModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->taskModel = new TaskModel();
|
||||
$this->assigneeModel = new TaskAssigneeModel();
|
||||
$this->columnModel = new TaskColumnModel();
|
||||
$this->subtaskModel = new TaskSubtaskModel();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -174,6 +177,9 @@ class TaskService
|
|||
}
|
||||
|
||||
$task['assignees'] = $this->assigneeModel->getAssigneesByTask($taskId);
|
||||
$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']));
|
||||
|
||||
return $task;
|
||||
}
|
||||
|
|
@ -264,4 +270,83 @@ class TaskService
|
|||
|
||||
return $columns[0]['id'] ?? 1;
|
||||
}
|
||||
|
||||
// ========== Подзадачи (Subtasks) ==========
|
||||
|
||||
/**
|
||||
* Добавить подзадачу
|
||||
*/
|
||||
public function addSubtask(int $taskId, string $title, int $userId): int
|
||||
{
|
||||
$orderIndex = $this->subtaskModel->getNextOrder($taskId);
|
||||
|
||||
$subtaskId = $this->subtaskModel->insert([
|
||||
'task_id' => $taskId,
|
||||
'title' => trim($title),
|
||||
'order_index' => $orderIndex,
|
||||
]);
|
||||
|
||||
if ($subtaskId) {
|
||||
Events::trigger('task.subtask_created', $taskId, $subtaskId, $userId);
|
||||
}
|
||||
|
||||
return $subtaskId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Переключить статус подзадачи
|
||||
*/
|
||||
public function toggleSubtask(int $taskId, int $subtaskId, int $userId): bool
|
||||
{
|
||||
$subtask = $this->subtaskModel->find($subtaskId);
|
||||
if (!$subtask || $subtask['task_id'] != $taskId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = $this->subtaskModel->toggle($subtaskId);
|
||||
|
||||
if ($result) {
|
||||
Events::trigger('task.subtask_toggled', $taskId, $subtaskId, !$subtask['is_completed'], $userId);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Удалить подзадачу
|
||||
*/
|
||||
public function deleteSubtask(int $taskId, int $subtaskId, int $userId): bool
|
||||
{
|
||||
$subtask = $this->subtaskModel->find($subtaskId);
|
||||
if (!$subtask || $subtask['task_id'] != $taskId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = $this->subtaskModel->delete($subtaskId);
|
||||
|
||||
if ($result) {
|
||||
Events::trigger('task.subtask_deleted', $taskId, $subtaskId, $userId);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновить подзадачу
|
||||
*/
|
||||
public function updateSubtask(int $taskId, int $subtaskId, array $data, int $userId): bool
|
||||
{
|
||||
$subtask = $this->subtaskModel->find($subtaskId);
|
||||
if (!$subtask || $subtask['task_id'] != $taskId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = $this->subtaskModel->update($subtaskId, $data);
|
||||
|
||||
if ($result) {
|
||||
Events::trigger('task.subtask_updated', $taskId, $subtaskId, $data, $userId);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -95,6 +95,50 @@
|
|||
</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-list-check me-2"></i>Подзадачи
|
||||
{% if task.subtasks_count %}
|
||||
<span class="badge bg-secondary ms-2">{{ task.subtasks_completed }}/{{ task.subtasks_count }}</span>
|
||||
{% endif %}
|
||||
</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
{% if task.subtasks %}
|
||||
<ul class="list-group list-group-flush mb-3" id="subtasks-list">
|
||||
{% for subtask in task.subtasks %}
|
||||
<li class="list-group-item d-flex align-items-center gap-2" data-subtask-id="{{ subtask.id }}">
|
||||
<input type="checkbox" class="form-check-input subtask-checkbox"
|
||||
{% if subtask.is_completed %}checked{% endif %}
|
||||
onchange="toggleSubtask({{ task.id }}, {{ subtask.id }})">
|
||||
<span class="{% if subtask.is_completed %}text-muted text-decoration-line-through{% endif %} flex-grow-1">
|
||||
{{ subtask.title }}
|
||||
</span>
|
||||
<button type="button" class="btn btn-outline-danger btn-sm"
|
||||
onclick="deleteSubtask({{ task.id }}, {{ subtask.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 ~ '/subtasks') }}" method="post" class="d-flex gap-2">
|
||||
{{ csrf_field()|raw }}
|
||||
<input type="text" name="title" class="form-control"
|
||||
placeholder="Добавить подзадачу..." required>
|
||||
<button type="submit" class="btn btn-primary">
|
||||
<i class="fa-solid fa-plus"></i>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{# Комментарии #}
|
||||
<div class="card border-0 shadow-sm">
|
||||
<div class="card-header bg-light">
|
||||
|
|
@ -178,3 +222,54 @@
|
|||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block scripts %}
|
||||
{{ parent() }}
|
||||
<script>
|
||||
function toggleSubtask(taskId, subtaskId) {
|
||||
fetch(`/tasks/${taskId}/subtasks/${subtaskId}/toggle`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (!data.success) {
|
||||
alert(data.error || 'Ошибка');
|
||||
location.reload();
|
||||
} else {
|
||||
// Перезагружаем страницу для обновления UI
|
||||
location.reload();
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
location.reload();
|
||||
});
|
||||
}
|
||||
|
||||
function deleteSubtask(taskId, subtaskId) {
|
||||
if (!confirm('Удалить подзадачу?')) return;
|
||||
|
||||
fetch(`/tasks/${taskId}/subtasks/${subtaskId}/delete`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (!data.success) {
|
||||
alert(data.error || 'Ошибка');
|
||||
} else {
|
||||
// Удаляем из DOM
|
||||
document.querySelector(`li[data-subtask-id="${subtaskId}"]`).remove();
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
}
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
|
|
|||
Loading…
Reference in New Issue