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