Tasks Module Stage 3: Checklists
- Add migration for task_checklists table - Create TaskChecklistModel with CRUD methods - Add TaskService methods for checklist operations - Add API routes for checklist endpoints - Add controller methods for checklist CRUD - Add UI section in task view with AJAX handlers
This commit is contained in:
parent
bf609257c3
commit
4a67f00aa7
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace App\Database\Migrations;
|
||||
|
||||
use CodeIgniter\Database\Migration;
|
||||
|
||||
class CreateTaskChecklistsTable 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,
|
||||
],
|
||||
'title' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 255,
|
||||
'null' => false,
|
||||
],
|
||||
'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->addKey('task_id');
|
||||
$this->forge->addForeignKey('task_id', 'tasks', 'id', '', 'CASCADE');
|
||||
|
||||
$this->forge->createTable('task_checklists');
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
$this->forge->dropTable('task_checklists');
|
||||
}
|
||||
}
|
||||
|
|
@ -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');
|
||||
|
||||
// Checklists API
|
||||
$routes->post('(:num)/checklists', 'TasksController::addChecklistItem/$1');
|
||||
$routes->post('(:num)/checklists/(:num)/toggle', 'TasksController::toggleChecklistItem/$1/$2');
|
||||
$routes->post('(:num)/checklists/(:num)/delete', 'TasksController::deleteChecklistItem/$1/$2');
|
||||
});
|
||||
|
||||
// API Routes для Tasks
|
||||
|
|
|
|||
|
|
@ -614,4 +614,108 @@ class TasksController extends BaseController
|
|||
'success' => $result,
|
||||
]);
|
||||
}
|
||||
|
||||
// ========== Чек-листы (Checklists) ==========
|
||||
|
||||
/**
|
||||
* API: добавить элемент чек-листа
|
||||
*/
|
||||
public function addChecklistItem(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' => 'Задача не найдена'
|
||||
]);
|
||||
}
|
||||
|
||||
$checklistId = $this->taskService->addChecklistItem($taskId, $title, $userId);
|
||||
|
||||
return $this->response->setJSON([
|
||||
'success' => (bool) $checklistId,
|
||||
'checklist_id' => $checklistId,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* API: переключить статус элемента чек-листа
|
||||
*/
|
||||
public function toggleChecklistItem(int $taskId, int $checklistId)
|
||||
{
|
||||
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->toggleChecklistItem($taskId, $checklistId, $userId);
|
||||
|
||||
return $this->response->setJSON([
|
||||
'success' => $result,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* API: удалить элемент чек-листа
|
||||
*/
|
||||
public function deleteChecklistItem(int $taskId, int $checklistId)
|
||||
{
|
||||
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->deleteChecklistItem($taskId, $checklistId, $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 TaskChecklistModel extends Model
|
||||
{
|
||||
use TenantScopedModel;
|
||||
|
||||
protected $table = 'task_checklists';
|
||||
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 $checklistId): bool
|
||||
{
|
||||
$checklist = $this->find($checklistId);
|
||||
if (!$checklist) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->update($checklistId, [
|
||||
'is_completed' => !$checklist['is_completed'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить количество незавершённых элементов чек-листа
|
||||
*/
|
||||
public function getIncompleteCount(int $taskId): int
|
||||
{
|
||||
return $this->where('task_id', $taskId)
|
||||
->where('is_completed', false)
|
||||
->countAllResults();
|
||||
}
|
||||
}
|
||||
|
|
@ -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\TaskChecklistModel;
|
||||
use CodeIgniter\Events\Events;
|
||||
|
||||
class TaskService
|
||||
|
|
@ -14,6 +15,7 @@ class TaskService
|
|||
protected TaskAssigneeModel $assigneeModel;
|
||||
protected TaskColumnModel $columnModel;
|
||||
protected TaskSubtaskModel $subtaskModel;
|
||||
protected TaskChecklistModel $checklistModel;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
|
@ -21,6 +23,7 @@ class TaskService
|
|||
$this->assigneeModel = new TaskAssigneeModel();
|
||||
$this->columnModel = new TaskColumnModel();
|
||||
$this->subtaskModel = new TaskSubtaskModel();
|
||||
$this->checklistModel = new TaskChecklistModel();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -180,6 +183,9 @@ 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['checklists'] = $this->checklistModel->getByTask($taskId);
|
||||
$task['checklists_count'] = count($task['checklists']);
|
||||
$task['checklists_completed'] = count(array_filter($task['checklists'], fn($c) => $c['is_completed']));
|
||||
|
||||
return $task;
|
||||
}
|
||||
|
|
@ -349,4 +355,83 @@ class TaskService
|
|||
|
||||
return $result;
|
||||
}
|
||||
|
||||
// ========== Чек-листы (Checklists) ==========
|
||||
|
||||
/**
|
||||
* Добавить элемент чек-листа
|
||||
*/
|
||||
public function addChecklistItem(int $taskId, string $title, int $userId): int
|
||||
{
|
||||
$orderIndex = $this->checklistModel->getNextOrder($taskId);
|
||||
|
||||
$checklistId = $this->checklistModel->insert([
|
||||
'task_id' => $taskId,
|
||||
'title' => trim($title),
|
||||
'order_index' => $orderIndex,
|
||||
]);
|
||||
|
||||
if ($checklistId) {
|
||||
Events::trigger('task.checklist_item_created', $taskId, $checklistId, $userId);
|
||||
}
|
||||
|
||||
return $checklistId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Переключить статус элемента чек-листа
|
||||
*/
|
||||
public function toggleChecklistItem(int $taskId, int $checklistId, int $userId): bool
|
||||
{
|
||||
$checklist = $this->checklistModel->find($checklistId);
|
||||
if (!$checklist || $checklist['task_id'] != $taskId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = $this->checklistModel->toggle($checklistId);
|
||||
|
||||
if ($result) {
|
||||
Events::trigger('task.checklist_item_toggled', $taskId, $checklistId, !$checklist['is_completed'], $userId);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Удалить элемент чек-листа
|
||||
*/
|
||||
public function deleteChecklistItem(int $taskId, int $checklistId, int $userId): bool
|
||||
{
|
||||
$checklist = $this->checklistModel->find($checklistId);
|
||||
if (!$checklist || $checklist['task_id'] != $taskId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = $this->checklistModel->delete($checklistId);
|
||||
|
||||
if ($result) {
|
||||
Events::trigger('task.checklist_item_deleted', $taskId, $checklistId, $userId);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Обновить элемент чек-листа
|
||||
*/
|
||||
public function updateChecklistItem(int $taskId, int $checklistId, array $data, int $userId): bool
|
||||
{
|
||||
$checklist = $this->checklistModel->find($checklistId);
|
||||
if (!$checklist || $checklist['task_id'] != $taskId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = $this->checklistModel->update($checklistId, $data);
|
||||
|
||||
if ($result) {
|
||||
Events::trigger('task.checklist_item_updated', $taskId, $checklistId, $data, $userId);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,6 +140,51 @@
|
|||
</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-check-square me-2"></i>Чек-лист
|
||||
{% if task.checklists_count %}
|
||||
<span class="badge bg-secondary ms-2">{{ task.checklists_completed }}/{{ task.checklists_count }}</span>
|
||||
{% endif %}
|
||||
</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
{% if task.checklists %}
|
||||
<ul class="list-group list-group-flush mb-3" id="checklists-list">
|
||||
{% for checklist in task.checklists %}
|
||||
<li class="list-group-item d-flex align-items-center gap-2" data-checklist-id="{{ checklist.id }}">
|
||||
<input type="checkbox" class="form-check-input checklist-checkbox"
|
||||
{% if checklist.is_completed %}checked{% endif %}
|
||||
onchange="toggleChecklistItem({{ task.id }}, {{ checklist.id }})">
|
||||
<span class="{% if checklist.is_completed %}text-muted text-decoration-line-through{% endif %} flex-grow-1">
|
||||
{{ checklist.title }}
|
||||
</span>
|
||||
<button type="button" class="btn btn-outline-danger btn-sm"
|
||||
onclick="deleteChecklistItem({{ task.id }}, {{ checklist.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 ~ '/checklists') }}" method="post"
|
||||
class="d-flex gap-2 checklist-form" onsubmit="addChecklistItem(event, {{ task.id }})">
|
||||
{{ 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">
|
||||
|
|
@ -314,5 +359,83 @@ function deleteSubtask(taskId, subtaskId) {
|
|||
console.error('Error:', error);
|
||||
});
|
||||
}
|
||||
|
||||
// ========== Чек-листы ==========
|
||||
|
||||
function addChecklistItem(event, taskId) {
|
||||
event.preventDefault();
|
||||
const form = event.target;
|
||||
const input = form.querySelector('input[name="title"]');
|
||||
const title = input.value.trim();
|
||||
|
||||
if (!title) return;
|
||||
|
||||
fetch(`/tasks/${taskId}/checklists`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
},
|
||||
body: 'title=' + encodeURIComponent(title)
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
location.reload();
|
||||
} else {
|
||||
alert(data.error || 'Ошибка при добавлении пункта чек-листа');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
alert('Ошибка при добавлении пункта чек-листа');
|
||||
});
|
||||
}
|
||||
|
||||
function toggleChecklistItem(taskId, checklistId) {
|
||||
fetch(`/tasks/${taskId}/checklists/${checklistId}/toggle`, {
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
function deleteChecklistItem(taskId, checklistId) {
|
||||
if (!confirm('Удалить пункт чек-листа?')) return;
|
||||
|
||||
fetch(`/tasks/${taskId}/checklists/${checklistId}/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