bp/app/Modules/Tasks/Models/TaskModel.php

141 lines
4.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Modules\Tasks\Models;
use CodeIgniter\Model;
use App\Models\Traits\TenantScopedModel;
class TaskModel extends Model
{
use TenantScopedModel;
protected $table = 'tasks';
protected $primaryKey = 'id';
protected $useSoftDeletes = false;
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $returnType = 'array';
protected $tenantField = 'organization_id';
protected $allowedFields = [
'organization_id',
'board_id',
'column_id',
'title',
'description',
'priority',
'due_date',
'completed_at',
'order_index',
'created_by',
];
/**
* Получить задачи с JOIN-ами для таблицы
*/
public function getForTable(int $organizationId): array
{
return $this->select('
tasks.id,
tasks.title,
tasks.description,
tasks.priority,
tasks.due_date,
tasks.completed_at,
tasks.created_at,
tc.name as column_name,
tc.color as column_color,
u.name as created_by_name
')
->join('task_columns tc', 'tasks.column_id = tc.id', 'left')
->join('users u', 'tasks.created_by = u.id', 'left')
->where('tasks.organization_id', $organizationId)
->orderBy('tasks.created_at', 'DESC')
->findAll();
}
/**
* Получить задачи, сгруппированные по колонкам (для Канбана)
*/
public function getTasksGroupedByColumn(int $boardId): array
{
$tasks = $this->select('tasks.*, tc.name as column_name, tc.color as column_color')
->join('task_columns tc', 'tasks.column_id = tc.id', 'left')
->where('tasks.board_id', $boardId)
->orderBy('tc.order_index', 'ASC')
->orderBy('tasks.order_index', 'ASC')
->orderBy('tasks.created_at', 'DESC')
->findAll();
$grouped = [];
foreach ($tasks as $task) {
$columnId = $task['column_id'] ?? 0;
if (!isset($grouped[$columnId])) {
$grouped[$columnId] = [
'column_name' => $task['column_name'] ?? 'Без колонки',
'column_color' => $task['column_color'] ?? '#6B7280',
'tasks' => [],
];
}
$grouped[$columnId]['tasks'][] = $task;
}
return $grouped;
}
/**
* Получить задачи для календаря
*/
public function getTasksForCalendar(int $organizationId, string $month): array
{
return $this->select('tasks.*, tc.color as column_color, tc.name as column_name')
->join('task_columns tc', 'tasks.column_id = tc.id', 'left')
->where('tasks.organization_id', $organizationId)
->where('tasks.due_date >=', date('Y-m-01', strtotime($month)))
->where('tasks.due_date <=', date('Y-m-t', strtotime($month)))
->where('tasks.completed_at', null)
->orderBy('tasks.due_date', 'ASC')
->findAll();
}
/**
* Получить задачу по ID
*/
public function getTask(int $taskId, int $organizationId): ?array
{
return $this->select('tasks.*, tc.name as column_name, tc.color as column_color')
->join('task_columns tc', 'tasks.column_id = tc.id', 'left')
->where('tasks.id', $taskId)
->where('tasks.organization_id', $organizationId)
->first();
}
/**
* Получить статистику по задачам
*/
public function getTaskStats(int $organizationId): array
{
$total = $this->select('COUNT(*) as count')
->where('organization_id', $organizationId)
->countAllResults();
$completed = $this->select('COUNT(*) as count')
->where('organization_id', $organizationId)
->where('completed_at IS NOT NULL')
->countAllResults();
$overdue = $this->select('COUNT(*) as count')
->where('organization_id', $organizationId)
->where('completed_at', null)
->where('due_date <', date('Y-m-d'))
->countAllResults();
return [
'total' => $total,
'completed' => $completed,
'overdue' => $overdue,
'pending' => $total - $completed,
];
}
}