Files
awo-hamburg-intranet/module/tasks/repo/TaskCategoryRepository.php

170 lines
4.4 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
class TaskCategoryRepository
{
public function listForAdmin(string $search = '', string $state = 'active'): array
{
$search = trim($search);
$state = trim($state);
if (!in_array($state, ['active', 'inactive', 'all'], true)) {
$state = 'active';
}
$conditions = [];
$types = '';
$params = [];
if ($state === 'active') {
$conditions[] = 'c.active = 1';
} elseif ($state === 'inactive') {
$conditions[] = 'c.active = 0';
}
if ($search !== '') {
$searchLike = '%' . $search . '%';
$conditions[] = '(
c.name LIKE ?
OR c.slug LIKE ?
OR CAST(c.id AS CHAR) LIKE ?
)';
$types .= 'sss';
$params[] = $searchLike;
$params[] = $searchLike;
$params[] = $searchLike;
}
$sql = 'SELECT
c.id,
c.name,
c.slug,
c.sort_order,
c.active,
c.created_at,
c.modified_at,
COUNT(d.id) AS task_count
FROM task_category c
LEFT JOIN task_definition d ON d.task_category_id = c.id';
if ($conditions !== []) {
$sql .= ' WHERE ' . implode(' AND ', $conditions);
}
$sql .= ' GROUP BY c.id
ORDER BY c.active DESC, c.sort_order ASC, c.name ASC';
return TaskCertDb::fetchAll($sql, $types, $params);
}
public function findById(int $id): ?array
{
return TaskCertDb::fetchOne(
'SELECT id, name, slug, sort_order, active, created_at, modified_at
FROM task_category
WHERE id = ?',
'i',
[$id]
);
}
public function findBySlug(string $slug): ?array
{
$slug = trim($slug);
if ($slug === '') {
return null;
}
return TaskCertDb::fetchOne(
'SELECT id, name, slug, sort_order, active, created_at, modified_at
FROM task_category
WHERE slug = ?
LIMIT 1',
's',
[$slug]
);
}
public function findByNormalizedName(string $normalizedName): ?array
{
$normalizedName = trim($normalizedName);
if ($normalizedName === '') {
return null;
}
return TaskCertDb::fetchOne(
'SELECT id, name, slug, sort_order, active, created_at, modified_at
FROM task_category
WHERE LOWER(TRIM(name)) = ?
LIMIT 1',
's',
[$normalizedName]
);
}
public function create(array $data): int
{
return TaskCertDb::insert(
'INSERT INTO task_category (name, slug, sort_order, active)
VALUES (?, ?, ?, ?)',
'ssii',
[
(string) ($data['name'] ?? ''),
(string) ($data['slug'] ?? ''),
(int) ($data['sort_order'] ?? 0),
(int) ($data['active'] ?? 1),
]
);
}
public function update(int $id, array $data): void
{
TaskCertDb::execute(
'UPDATE task_category
SET name = ?, sort_order = ?, active = ?
WHERE id = ?',
'siii',
[
(string) ($data['name'] ?? ''),
(int) ($data['sort_order'] ?? 0),
(int) ($data['active'] ?? 1),
$id,
]
);
}
public function setActive(int $id, int $active): void
{
TaskCertDb::execute(
'UPDATE task_category SET active = ? WHERE id = ?',
'ii',
[$active, $id]
);
}
public function delete(int $id): int
{
return TaskCertDb::execute(
'DELETE FROM task_category WHERE id = ?',
'i',
[$id]
);
}
public function nextSortOrder(int $step = 10): int
{
$step = max(1, $step);
$maxSortOrder = (int) TaskCertDb::scalar('SELECT COALESCE(MAX(sort_order), 0) FROM task_category');
return $maxSortOrder + $step;
}
public function countTasks(int $id): int
{
return (int) TaskCertDb::scalar(
'SELECT COUNT(*) FROM task_definition WHERE task_category_id = ?',
'i',
[$id]
);
}
}