91 lines
3.1 KiB
PHP
91 lines
3.1 KiB
PHP
<?php
|
||
/**
|
||
* @package Bicycle
|
||
* @author Egor Isaev
|
||
* @description CategoryGroupRepository.php
|
||
* @copyright (c) 15/08/2026
|
||
*/
|
||
|
||
namespace App\Repositories;
|
||
|
||
use System\Classes\Repository;
|
||
|
||
/**
|
||
* Репозиторий таблицы category_groups — группа статей как самостоятельная сущность (не статья
|
||
* с детьми, см. схему и .claude/memory/бюджет_текущий_план.md). Статья (categories) ссылается
|
||
* на группу через group_id (может быть NULL — статья без группы).
|
||
*/
|
||
class CategoryGroupRepository extends Repository
|
||
{
|
||
/**
|
||
* @param string|null $connection Имя подключения (Database::instance($connection)); null — по умолчанию
|
||
*/
|
||
public function __construct(?string $connection = null)
|
||
{
|
||
parent::__construct('category_groups', connection: $connection);
|
||
}
|
||
|
||
/**
|
||
* Активная (is_delete=0) группа с тем же названием у пользователя, в рамках type.
|
||
*
|
||
* @param int $user_id
|
||
* @param string $type income|expense
|
||
* @param string $title
|
||
* @param int|null $exclude_id Исключить этот id (правка своего же названия — не дубль)
|
||
* @return object|false
|
||
*/
|
||
public function findActiveDuplicate(int $user_id, string $type, string $title, ?int $exclude_id = null): mixed
|
||
{
|
||
return $this->findDuplicate($user_id, $type, $title, false, $exclude_id);
|
||
}
|
||
|
||
/**
|
||
* Архивная (is_delete=1) группа с тем же названием у пользователя.
|
||
*
|
||
* @param int $user_id
|
||
* @param string $type
|
||
* @param string $title
|
||
* @param int|null $exclude_id
|
||
* @return object|false
|
||
*/
|
||
public function findArchivedDuplicate(int $user_id, string $type, string $title, ?int $exclude_id = null): mixed
|
||
{
|
||
return $this->findDuplicate($user_id, $type, $title, true, $exclude_id);
|
||
}
|
||
|
||
/**
|
||
* @param int $user_id
|
||
* @param string $type
|
||
* @param string $title
|
||
* @param bool $is_delete
|
||
* @param int|null $exclude_id
|
||
* @return object|false
|
||
*/
|
||
protected function findDuplicate(int $user_id, string $type, string $title, bool $is_delete, ?int $exclude_id): mixed
|
||
{
|
||
$pdo = $this->pdo->pdo();
|
||
|
||
$where = "user_id = $user_id"
|
||
. ' AND type = ' . $pdo->quote($type)
|
||
. ' AND is_delete = ' . ($is_delete ? 1 : 0)
|
||
. ' AND title = ' . $pdo->quote($title);
|
||
|
||
if ($exclude_id !== null) {
|
||
$where .= " AND id != $exclude_id";
|
||
}
|
||
|
||
return $this->getItemWhere($where);
|
||
}
|
||
|
||
/**
|
||
* Активные группы пользователя.
|
||
*
|
||
* @param int $user_id
|
||
* @return object[]
|
||
*/
|
||
public function getForUser(int $user_id): array
|
||
{
|
||
return $this->getList('*', ['user_id' => $user_id, 'is_delete' => 0], '`order`') ?: [];
|
||
}
|
||
}
|