85 lines
4.3 KiB
PHP
85 lines
4.3 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_departments');
|
|
$pdo = get_db();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
$departments = $pdo->query(
|
|
'SELECT d.*, m.name AS manager_name FROM departments d LEFT JOIN staff m ON m.id = d.manager_id ORDER BY d.name'
|
|
)->fetchAll();
|
|
$emailOptions = $pdo->query(
|
|
'SELECT ea.email, d.name AS department_name FROM email_addresses ea JOIN departments d ON d.id = ea.department_id ORDER BY ea.email'
|
|
)->fetchAll();
|
|
$staffOptions = $pdo->query('SELECT id, name FROM staff WHERE is_active = 1 ORDER BY name')->fetchAll();
|
|
|
|
// Mitglieder = Hauptabteilung oder zusätzlich zugewiesen
|
|
$membersByDept = [];
|
|
$memberRows = $pdo->query(
|
|
'SELECT s.id, s.name, s.department_id AS dept_id, 1 AS is_primary FROM staff s WHERE s.department_id IS NOT NULL
|
|
UNION ALL
|
|
SELECT s.id, s.name, sd.department_id, 0 FROM staff_department sd JOIN staff s ON s.id = sd.staff_id'
|
|
)->fetchAll();
|
|
foreach ($memberRows as $r) {
|
|
$membersByDept[(int) $r['dept_id']][(int) $r['id']] ??= ['id' => (int) $r['id'], 'name' => $r['name'], 'isPrimary' => (bool) $r['is_primary']];
|
|
}
|
|
|
|
api_json([
|
|
'emailOptions' => array_map(fn(array $o) => ['email' => $o['email'], 'departmentName' => $o['department_name']], $emailOptions),
|
|
'staffOptions' => array_map(fn(array $s) => ['id' => (int) $s['id'], 'name' => $s['name']], $staffOptions),
|
|
'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 $d) => [
|
|
'id' => (int) $d['id'],
|
|
'name' => $d['name'],
|
|
'email' => $d['email'],
|
|
'isDefault' => (bool) $d['is_default'],
|
|
'signature' => $d['signature'],
|
|
'isActive' => (bool) ($d['is_active'] ?? 1),
|
|
'slaPlanId' => !empty($d['sla_plan_id']) ? (int) $d['sla_plan_id'] : null,
|
|
'managerId' => !empty($d['manager_id']) ? (int) $d['manager_id'] : null,
|
|
'managerName' => $d['manager_name'] ?? null,
|
|
'autoAssignMode' => (string) ($d['auto_assign_mode'] ?? 'none'),
|
|
'autoAssignStaffId' => !empty($d['auto_assign_staff_id']) ? (int) $d['auto_assign_staff_id'] : null,
|
|
'notes' => $d['notes'] ?? null,
|
|
'members' => array_values($membersByDept[(int) $d['id']] ?? []),
|
|
'createdAt' => $d['created_at'] ?? null,
|
|
'updatedAt' => $d['updated_at'] ?? null,
|
|
], $departments),
|
|
]);
|
|
}
|
|
|
|
$body = api_request_body();
|
|
$action = $body['action'] ?? '';
|
|
$id = (int) ($body['id'] ?? 0);
|
|
|
|
try {
|
|
if ($action === 'save') {
|
|
$in = [
|
|
'name' => $body['name'] ?? '', 'email' => $body['email'] ?? '', 'is_default' => !empty($body['isDefault']),
|
|
'manager_id' => $body['managerId'] ?? null, 'notes' => $body['notes'] ?? '', 'signature' => $body['signature'] ?? '',
|
|
];
|
|
// Fehlende Schlüssel (ältere App-Version) lassen Status und SLA-Plan unverändert
|
|
if (array_key_exists('isActive', $body)) $in['is_active'] = !empty($body['isActive']);
|
|
if (array_key_exists('slaPlanId', $body)) $in['sla_plan_id'] = $body['slaPlanId'];
|
|
if (array_key_exists('autoAssignMode', $body)) {
|
|
$in['auto_assign_mode'] = $body['autoAssignMode'];
|
|
$in['auto_assign_staff_id'] = $body['autoAssignStaffId'] ?? null;
|
|
}
|
|
$newId = department_save($pdo, $id, $in);
|
|
audit_log($staff['id'], ($id === 0 ? 'Abteilung erstellt: ' : 'Abteilung aktualisiert: ') . trim((string) ($body['name'] ?? '')) . ' (Mobile App)');
|
|
api_json(['id' => $newId]);
|
|
}
|
|
if ($action === 'delete') {
|
|
$deleted = department_delete($pdo, $id);
|
|
audit_log($staff['id'], "Abteilung 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.');
|