query('SELECT department_id FROM recurring_tickets')->fetchAll(PDO::FETCH_COLUMN))); $priorities = $pdo->query('SELECT * FROM priorities ORDER BY sort_order')->fetchAll(); $agents = $pdo->query('SELECT id, name FROM staff WHERE is_active=1 ORDER BY name')->fetchAll(); $errors = []; if ($_SERVER['REQUEST_METHOD'] === 'POST') { verify_csrf(); $action = $_POST['action'] ?? ''; if ($action === 'create' || $action === 'update') { $subject = trim($_POST['subject'] ?? ''); $message = trim($_POST['message'] ?? ''); $deptId = (int) ($_POST['department_id'] ?? 0); $prioId = (int) ($_POST['priority_id'] ?? 0); $assignTo = ($_POST['assigned_staff_id'] ?? '') !== '' ? (int)$_POST['assigned_staff_id'] : null; $days = max(1, (int) ($_POST['interval_days'] ?? 30)); $nextRun = trim($_POST['next_run'] ?? date('Y-m-d')); $isActive = !empty($_POST['is_active']) ? 1 : 0; if ($subject === '') $errors[] = 'Bitte einen Betreff angeben.'; if ($message === '') $errors[] = 'Bitte eine Nachricht eingeben.'; if (!$deptId) $errors[] = 'Bitte eine Abteilung wählen.'; if (!$prioId) $errors[] = 'Bitte eine Priorität wählen.'; if (!$errors) { if ($action === 'create') { $pdo->prepare('INSERT INTO recurring_tickets (subject,message,department_id,priority_id,assigned_staff_id,interval_days,next_run,is_active,created_by,created_at) VALUES (?,?,?,?,?,?,?,?,?,?)') ->execute([$subject, $message, $deptId, $prioId, $assignTo, $days, $nextRun, $isActive, $staff['id'], db_now()]); audit_log($staff['id'], "Wiederkehrendes Ticket erstellt: {$subject}"); flash('success', 'Wiederkehrendes Ticket erstellt.'); } else { $id = (int) $_POST['id']; $pdo->prepare('UPDATE recurring_tickets SET subject=?,message=?,department_id=?,priority_id=?,assigned_staff_id=?,interval_days=?,next_run=?,is_active=? WHERE id=?') ->execute([$subject, $message, $deptId, $prioId, $assignTo, $days, $nextRun, $isActive, $id]); audit_log($staff['id'], "Wiederkehrendes Ticket bearbeitet: {$subject}"); flash('success', 'Gespeichert.'); } redirect('/staff/recurring_tickets.php'); } } if ($action === 'delete') { $id = (int) $_POST['id']; $pdo->prepare('DELETE FROM recurring_tickets WHERE id=?')->execute([$id]); audit_log($staff['id'], 'Wiederkehrendes Ticket gelöscht'); flash('success', 'Gelöscht.'); redirect('/staff/recurring_tickets.php'); } if ($action === 'toggle') { $id = (int) $_POST['id']; $pdo->prepare('UPDATE recurring_tickets SET is_active = 1 - is_active WHERE id=?')->execute([$id]); redirect('/staff/recurring_tickets.php'); } if ($action === 'run_now') { $id = (int) $_POST['id']; $rt = $pdo->prepare('SELECT * FROM recurring_tickets WHERE id=?'); $rt->execute([$id]); $rt = $rt->fetch(); if ($rt) { $tn = generate_ticket_number(); $now = db_now(); $nextRun = (new DateTimeImmutable('today')) ->modify('+' . (int) $rt['interval_days'] . ' days') ->format('Y-m-d'); try { $pdo->beginTransaction(); $pdo->prepare( 'INSERT INTO tickets (ticket_number,subject,requester_name,requester_email,department_id,priority_id,assigned_staff_id,status,source,created_at,updated_at) VALUES (?,?,?,?,?,?,?,\'open\',\'staff\',?,?)' )->execute([$tn, $rt['subject'], 'System', 'system@recurring', $rt['department_id'], $rt['priority_id'], $rt['assigned_staff_id'], $now, $now]); $ticketId = (int) $pdo->lastInsertId(); $pdo->prepare('INSERT INTO ticket_threads (ticket_id,author_type,staff_id,message,is_internal_note,created_at) VALUES (?,\'staff\',?,?,0,?)') ->execute([$ticketId, $staff['id'], $rt['message'], $now]); $pdo->prepare('UPDATE recurring_tickets SET next_run = ? WHERE id = ?') ->execute([$nextRun, $id]); $pdo->commit(); } catch (Throwable $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } throw $e; } flash('success', "Ticket #{$tn} sofort erstellt."); } redirect('/staff/recurring_tickets.php'); } } $items = $pdo->query( 'SELECT rt.*, d.name AS dept_name, p.name AS prio_name, s.name AS agent_name FROM recurring_tickets rt JOIN departments d ON d.id=rt.department_id JOIN priorities p ON p.id=rt.priority_id LEFT JOIN staff s ON s.id=rt.assigned_staff_id ORDER BY rt.next_run ASC' )->fetchAll(); $editing = null; if (!empty($_GET['edit'])) { $stmt = $pdo->prepare('SELECT * FROM recurring_tickets WHERE id=?'); $stmt->execute([(int)$_GET['edit']]); $editing = $stmt->fetch(); } $pageTitle = 'Wiederkehrende Tickets'; $activePage = 'recurring'; require __DIR__ . '/_header.php'; ?>

Wiederkehrende Tickets

+ Neues Muster

Tickets die automatisch nach einem festen Intervall neu erstellt werden (z.B. monatliche Wartung). Erstellung per Cronjob: php cron/process_recurring.php

Abbrechen
Noch keine Muster angelegt.
BetreffAbteilungIntervallNächste AusführungStatus

Alle Tage Bearbeiten