prepare($sql); $stmt->execute($params); $rows = []; foreach ($stmt->fetchAll() as $t) { [$statusLabel] = status_label($t['status']); $rows[] = [ $t['ticket_number'], $t['subject'], $t['requester_name'], $t['requester_email'], $t['department_name'], $t['priority_name'], $statusLabel, $t['staff_name'] ?: '', $t['created_at'], $t['resolution_due_at'], ]; } csv_response('tickets_' . date('Y-m-d') . '.csv', ['Ticketnummer', 'Betreff', 'Anfrager', 'E-Mail', 'Abteilung', 'Priorität', 'Status', 'Zugewiesen', 'Erstellt', 'Fällig'], $rows); } if ($type === 'report') { // Berichte sind seit Kurzem für alle Mitarbeiter sichtbar (abteilungsbeschränkt für Nicht-Admins), // der Export folgt derselben Regel wie staff/reports.php. $dim = $_GET['dim'] ?? 'dept'; $dimColumn = ['dept' => 'department_id', 'topic' => 'help_topic_id', 'agent' => 'assigned_staff_id'][$dim] ?? 'department_id'; $preset = $_GET['range'] ?? '30d'; $today = new DateTime('today'); switch ($preset) { case '7d': $from = (clone $today)->modify('-6 days'); $to = clone $today; break; case 'thismonth': $from = new DateTime(date('Y-m-01')); $to = clone $today; break; case 'thisyear': $from = new DateTime(date('Y-01-01')); $to = clone $today; break; case 'custom': $from = !empty($_GET['from']) ? new DateTime($_GET['from']) : (clone $today)->modify('-29 days'); $to = !empty($_GET['to']) ? new DateTime($_GET['to']) : clone $today; break; default: $from = (clone $today)->modify('-29 days'); $to = clone $today; } if ($from > $to) { [$from, $to] = [$to, $from]; } $rangeFrom = $from->format('Y-m-d 00:00:00'); $rangeTo = $to->format('Y-m-d 23:59:59'); $rows = build_report_table($pdo, $dimColumn, $rangeFrom, $rangeTo, $myDeptIds); $labelMap = match ($dim) { 'topic' => $pdo->query('SELECT id, name FROM help_topics')->fetchAll(PDO::FETCH_KEY_PAIR), 'agent' => $pdo->query('SELECT id, name FROM staff')->fetchAll(PDO::FETCH_KEY_PAIR), default => $pdo->query('SELECT id, name FROM departments')->fetchAll(PDO::FETCH_KEY_PAIR), }; $noneLabel = match ($dim) { 'topic' => 'Kein Hilfethema', 'agent' => 'Nicht zugewiesen', default => 'Ohne Abteilung', }; $out = []; foreach ($rows as $key => $r) { $label = $key === 'none' ? $noneLabel : ($labelMap[(int) $key] ?? "#{$key}"); $out[] = [ $label, $r['geoeffnet'], $r['zugewiesen'], $r['ueberfaellig'], $r['geschlossen'], $r['wieder_geoeffnet'], $r['geloescht'], $r['servicezeit'] ?? '', $r['reaktionszeit'] ?? '', ]; } csv_response('bericht_' . $dim . '_' . date('Y-m-d') . '.csv', ['Bezeichnung', 'Geöffnet', 'Zugewiesen', 'Überfällig', 'Geschlossen', 'Wieder geöffnet', 'Gelöscht', 'Servicezeit (Std)', 'Reaktionszeit (Std)'], $out); } http_response_code(400); echo 'Unbekannter Export-Typ.';