Files
deskhub/DeskHub-Webseite/api/staff/v1/help_topics.php
T
2026-10-16 19:12:13 +00:00

81 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/common.php';
api_require_method('GET', 'POST');
$staff = api_require_staff();
api_require_admin_or_permission($staff, 'manage_help_topics');
$pdo = get_db();
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$topics = $pdo->query(
'SELECT h.*, d.name AS department_name, p.name AS priority_name, p.color AS priority_color
FROM help_topics h
JOIN departments d ON d.id = h.department_id
JOIN priorities p ON p.id = h.priority_id
ORDER BY h.sort_order, h.name'
)->fetchAll();
$departments = departments_for_select($pdo, array_map('intval', $pdo->query('SELECT department_id FROM help_topics')->fetchAll(PDO::FETCH_COLUMN)));
$priorities = $pdo->query('SELECT id, name FROM priorities ORDER BY sort_order')->fetchAll();
$names = array_column($topics, 'name', 'id');
$parents = array_column($topics, 'parent_id', 'id');
$pathOf = function (int $id) use ($names, $parents): string {
$parts = []; $seen = [];
while ($id && isset($names[$id]) && !isset($seen[$id])) { $seen[$id] = true; array_unshift($parts, $names[$id]); $id = (int) ($parents[$id] ?? 0); }
return implode(' / ', $parts);
};
api_json([
'departments' => array_map(fn(array $d) => ['id' => (int) $d['id'], 'name' => $d['name']], $departments),
'priorities' => array_map(fn(array $p) => ['id' => (int) $p['id'], 'name' => $p['name']], $priorities),
'slaPlans' => array_map(fn(array $p) => ['id' => (int) $p['id'], 'name' => $p['name']], $pdo->query('SELECT id, name FROM sla_plans WHERE is_active = 1 ORDER BY name')->fetchAll()),
'items' => array_map(fn(array $t) => [
'id' => (int) $t['id'],
'name' => $t['name'],
'departmentId' => (int) $t['department_id'],
'departmentName' => $t['department_name'],
'priorityId' => (int) $t['priority_id'],
'priorityName' => $t['priority_name'],
'priorityColor' => $t['priority_color'],
'isActive' => (bool) $t['is_active'],
'isPublic' => (bool) ($t['is_public'] ?? 1),
'slaPlanId' => !empty($t['sla_plan_id']) ? (int) $t['sla_plan_id'] : null,
'parentId' => !empty($t['parent_id']) ? (int) $t['parent_id'] : null,
'path' => $pathOf((int) $t['id']),
'notes' => $t['notes'] ?? null,
'sortOrder' => (int) $t['sort_order'],
'createdAt' => $t['created_at'] ?? null,
'updatedAt' => $t['updated_at'] ?? null,
], $topics),
]);
}
$body = api_request_body();
$action = $body['action'] ?? '';
$id = (int) ($body['id'] ?? 0);
try {
if ($action === 'save') {
$in = [
'name' => $body['name'] ?? '', 'department_id' => $body['departmentId'] ?? 0, 'priority_id' => $body['priorityId'] ?? 0,
'sort_order' => $body['sortOrder'] ?? 0,
];
// Fehlende Schlüssel (ältere App-Version) lassen den bisherigen Wert unverändert
foreach (['isActive' => 'is_active', 'isPublic' => 'is_public', 'parentId' => 'parent_id', 'slaPlanId' => 'sla_plan_id', 'notes' => 'notes'] as $camel => $snake) {
if (array_key_exists($camel, $body)) $in[$snake] = $body[$camel];
}
$newId = help_topic_save($pdo, $id, $in);
audit_log($staff['id'], ($id === 0 ? 'Hilfethema erstellt: ' : 'Hilfethema aktualisiert: ') . trim((string) ($body['name'] ?? '')) . ' (Mobile App)');
api_json(['id' => $newId]);
}
if ($action === 'delete') {
$deleted = help_topic_delete($pdo, $id);
audit_log($staff['id'], "Hilfethema gelöscht: {$deleted} (Mobile App)");
api_json(['ok' => true]);
}
} catch (AdminModelException $e) {
api_error($e->httpStatus(), $e->errorCode(), $e->getMessage());
}
api_error(422, 'INVALID_ACTION', 'Unbekannte Aktion.');