161 lines
4.6 KiB
PHP
161 lines
4.6 KiB
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
class TaskLookupRepository
|
||
|
|
{
|
||
|
|
public function listTaskCategories(bool $includeInactive = false): array
|
||
|
|
{
|
||
|
|
$sql = 'SELECT id, name, slug, sort_order, active
|
||
|
|
FROM task_category';
|
||
|
|
if (!$includeInactive) {
|
||
|
|
$sql .= ' WHERE active = 1';
|
||
|
|
}
|
||
|
|
$sql .= ' ORDER BY sort_order ASC, name ASC';
|
||
|
|
|
||
|
|
return TaskCertDb::fetchAll(
|
||
|
|
$sql
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function listMandants(): array
|
||
|
|
{
|
||
|
|
return TaskCertDb::fetchAll(
|
||
|
|
'SELECT id, description FROM main_mandant ORDER BY description ASC'
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function listDepartments(): array
|
||
|
|
{
|
||
|
|
return TaskCertDb::fetchAll(
|
||
|
|
'SELECT id, description FROM main_department ORDER BY description ASC'
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function listRoles(): array
|
||
|
|
{
|
||
|
|
return TaskCertDb::fetchAll(
|
||
|
|
'SELECT id, description FROM main_role ORDER BY description ASC'
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function listActiveContacts(): array
|
||
|
|
{
|
||
|
|
return TaskCertDb::fetchAll(
|
||
|
|
'SELECT id, name, email FROM main_contact WHERE active = 1 ORDER BY name ASC'
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function searchContacts(string $term, int $limit = 15): array
|
||
|
|
{
|
||
|
|
$term = trim($term);
|
||
|
|
if ($term === '' || mb_strlen($term) < 2) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
|
||
|
|
$limit = max(1, min(50, $limit));
|
||
|
|
$likeTerm = '%' . $term . '%';
|
||
|
|
|
||
|
|
return TaskCertDb::fetchAll(
|
||
|
|
'SELECT id, name, email
|
||
|
|
FROM main_contact
|
||
|
|
WHERE active = 1
|
||
|
|
AND (name LIKE ? OR email LIKE ?)
|
||
|
|
ORDER BY name ASC
|
||
|
|
LIMIT ' . $limit,
|
||
|
|
'ss',
|
||
|
|
[$likeTerm, $likeTerm]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getContactById(int $contactId): ?array
|
||
|
|
{
|
||
|
|
if ($contactId <= 0) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
$rows = TaskCertDb::fetchAll(
|
||
|
|
'SELECT id, name, email FROM main_contact WHERE id = ? AND active = 1',
|
||
|
|
'i',
|
||
|
|
[$contactId]
|
||
|
|
);
|
||
|
|
|
||
|
|
return $rows[0] ?? null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function listKnowledgePosts(int $languageId, int $limit = 500): array
|
||
|
|
{
|
||
|
|
$languageId = $languageId > 0 ? $languageId : 1;
|
||
|
|
$limit = max(1, min(1000, $limit));
|
||
|
|
|
||
|
|
return TaskCertDb::fetchAll(
|
||
|
|
'SELECT
|
||
|
|
p.id,
|
||
|
|
COALESCE(NULLIF(v.title, ""), CONCAT("Beitrag #", p.id)) AS title
|
||
|
|
FROM knowledgecenter_posts p
|
||
|
|
LEFT JOIN knowledgecenter_post_versions v
|
||
|
|
ON v.post_id = p.id
|
||
|
|
AND v.language_id = ?
|
||
|
|
AND v.version_number = (
|
||
|
|
SELECT MAX(v2.version_number)
|
||
|
|
FROM knowledgecenter_post_versions v2
|
||
|
|
WHERE v2.post_id = p.id
|
||
|
|
AND v2.language_id = ?
|
||
|
|
)
|
||
|
|
ORDER BY p.id DESC
|
||
|
|
LIMIT ' . $limit,
|
||
|
|
'ii',
|
||
|
|
[$languageId, $languageId]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function mapKnowledgePostTitlesByIds(int $languageId, array $postIds): array
|
||
|
|
{
|
||
|
|
$languageId = $languageId > 0 ? $languageId : 1;
|
||
|
|
$ids = array_values(array_unique(array_filter(array_map('intval', $postIds), static fn(int $id): bool => $id > 0)));
|
||
|
|
if ($ids === []) {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
|
||
|
|
$placeholders = implode(',', array_fill(0, count($ids), '?'));
|
||
|
|
$types = 'ii' . str_repeat('i', count($ids));
|
||
|
|
$params = array_merge([$languageId, $languageId], $ids);
|
||
|
|
|
||
|
|
$rows = TaskCertDb::fetchAll(
|
||
|
|
'SELECT
|
||
|
|
p.id,
|
||
|
|
COALESCE(NULLIF(v.title, ""), CONCAT("Beitrag #", p.id)) AS title
|
||
|
|
FROM knowledgecenter_posts p
|
||
|
|
LEFT JOIN knowledgecenter_post_versions v
|
||
|
|
ON v.post_id = p.id
|
||
|
|
AND v.language_id = ?
|
||
|
|
AND v.version_number = (
|
||
|
|
SELECT MAX(v2.version_number)
|
||
|
|
FROM knowledgecenter_post_versions v2
|
||
|
|
WHERE v2.post_id = p.id
|
||
|
|
AND v2.language_id = ?
|
||
|
|
)
|
||
|
|
WHERE p.id IN (' . $placeholders . ')',
|
||
|
|
$types,
|
||
|
|
$params
|
||
|
|
);
|
||
|
|
|
||
|
|
$map = [];
|
||
|
|
foreach ($rows as $row) {
|
||
|
|
$id = (int) ($row['id'] ?? 0);
|
||
|
|
if ($id <= 0) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
|
||
|
|
$map[$id] = trim((string) ($row['title'] ?? ('Beitrag #' . $id)));
|
||
|
|
}
|
||
|
|
|
||
|
|
foreach ($ids as $id) {
|
||
|
|
if (!isset($map[$id])) {
|
||
|
|
$map[$id] = 'Beitrag #' . $id;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $map;
|
||
|
|
}
|
||
|
|
}
|