99 lines
3.6 KiB
PHP
99 lines
3.6 KiB
PHP
<?php
|
||
// Direkter Web-Zugriff (Browser/URL) wird geblockt. Ausnahmen: echter CLI-Cronjob
|
||
// sowie Einbindung per require_once aus dem authentifizierten Backend (staff/settings.php),
|
||
// das vor dem require_once die Konstante DESKHUB_CRON_RUNNER definiert.
|
||
if (php_sapi_name() !== 'cli' && !defined('DESKHUB_CRON_RUNNER')) { http_response_code(403); exit('Kein Web-Zugriff.'); }
|
||
/**
|
||
* DeskHub – Cron: Wiederkehrende Tickets erstellen
|
||
* Aufruf: php cron/process_recurring.php
|
||
* Empfehlung: täglich einmal, z.B. um 06:00 Uhr
|
||
* 0 6 * * * /usr/bin/php /pfad/zu/deskhub/cron/process_recurring.php >> /pfad/zu/deskhub/cron/recurring.log 2>&1
|
||
*
|
||
* Kann außerdem manuell aus dem Backend (Einstellungen → Cron-Jobs) ausgeführt werden.
|
||
*/
|
||
|
||
define('CRON_CONTEXT', true);
|
||
require_once __DIR__ . '/../includes/bootstrap.php';
|
||
|
||
/**
|
||
* Erstellt fällige wiederkehrende Tickets. Kann vom Cron und manuell aus settings.php aufgerufen werden.
|
||
*
|
||
* @return array{created: int, log: string[]}
|
||
*/
|
||
function run_process_recurring(PDO $pdo, bool $verbose = false): array
|
||
{
|
||
$today = date('Y-m-d');
|
||
$log = [];
|
||
|
||
$stmt = $pdo->query(
|
||
"SELECT rt.*, d.email AS dept_email
|
||
FROM recurring_tickets rt
|
||
JOIN departments d ON d.id = rt.department_id
|
||
WHERE rt.is_active = 1 AND rt.next_run <= '{$today}'"
|
||
);
|
||
$due = $stmt->fetchAll();
|
||
|
||
if (!$due) {
|
||
$log[] = date('[Y-m-d H:i:s]') . ' Keine fälligen wiederkehrenden Tickets.';
|
||
return ['created' => 0, 'log' => $log];
|
||
}
|
||
|
||
$created = 0;
|
||
|
||
foreach ($due as $rt) {
|
||
$tn = generate_ticket_number();
|
||
$now = db_now();
|
||
$nextRun = (new DateTimeImmutable($rt['next_run']))
|
||
->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 (Wiederkehrend)',
|
||
'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, $rt['created_by'], $rt['message'], $now]);
|
||
$pdo->prepare('UPDATE recurring_tickets SET next_run = ? WHERE id = ?')
|
||
->execute([$nextRun, $rt['id']]);
|
||
$pdo->commit();
|
||
} catch (Throwable $e) {
|
||
if ($pdo->inTransaction()) {
|
||
$pdo->rollBack();
|
||
}
|
||
$log[] = date('[Y-m-d H:i:s]') . " Fehler bei Muster \"{$rt['subject']}\": " . $e->getMessage();
|
||
continue;
|
||
}
|
||
|
||
set_setting('cron_process_recurring_last_run', $now);
|
||
$created++;
|
||
if ($verbose) {
|
||
$log[] = date('[Y-m-d H:i:s]') . " Ticket #{$tn} erstellt für Muster: {$rt['subject']}";
|
||
}
|
||
}
|
||
|
||
$log[] = date('[Y-m-d H:i:s]') . " Fertig. {$created} Ticket(s) erstellt.";
|
||
|
||
return ['created' => $created, 'log' => $log];
|
||
}
|
||
|
||
if (php_sapi_name() === 'cli') {
|
||
$result = run_process_recurring(get_db(), true);
|
||
foreach ($result['log'] as $line) {
|
||
echo $line . "\n";
|
||
}
|
||
}
|