75 lines
1.9 KiB
PHP
75 lines
1.9 KiB
PHP
<?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();
|
|
}
|
|
}
|