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

82 lines
2.3 KiB
PHP

<?php
namespace App\Modules\Tasks\Models;
use CodeIgniter\Model;
use App\Models\Traits\TenantScopedModel;
class TaskBoardModel extends Model
{
use TenantScopedModel;
protected $table = 'task_boards';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $tenantField = 'organization_id';
protected $allowedFields = [
'organization_id',
'name',
'description',
'is_default',
];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
/**
* Получить все доски организации
*/
public function getBoardsByOrganization(int $organizationId): array
{
return $this->where('organization_id', $organizationId)
->orderBy('is_default', 'DESC')
->orderBy('created_at', 'DESC')
->findAll();
}
/**
* Получить доску по ID
*/
public function getBoard(int $boardId, int $organizationId): ?array
{
return $this->where('id', $boardId)
->where('organization_id', $organizationId)
->first();
}
/**
* Получить дефолтную доску организации
*/
public function getDefaultBoard(int $organizationId): ?array
{
return $this->where('organization_id', $organizationId)
->where('is_default', 1)
->first();
}
/**
* Создать дефолтную доску для новой организации
*/
public function createDefaultBoard(int $organizationId): int
{
$data = [
'organization_id' => $organizationId,
'name' => 'Мои задачи',
'description' => 'Основная доска задач',
'is_default' => 1,
];
$boardId = $this->insert($data);
if ($boardId) {
// Создаём дефолтные колонки
$columnModel = new TaskColumnModel();
$columnModel->createDefaultColumns($boardId);
}
return $boardId;
}
}