Files
osticket-caldav-plugin/hooks.php
2025-11-26 18:08:49 +01:00

62 lines
2.0 KiB
PHP

<?php
// Hook into osTicket events
class CaldavHooks {
static function onTicketCreated($ticket) {
// Trigger calendar entry creation when ticket is created
$config = Plugin::getConfig('caldav');
if ($config && $config['calendar_url'] && $config['app_token']) {
self::createCalendarEntry($ticket, $config);
}
}
static function createCalendarEntry($ticket, $config) {
// Your CalDAV implementation here
$url = $config['calendar_url'];
$token = $config['app_token'];
// Example using cURL for CalDAV operations:
$icalData = self::generateIcalEvent($ticket);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $icalData);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: text/calendar',
'Authorization: Bearer ' . $token
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Handle response (log errors, etc.)
if ($httpCode >= 200 && $httpCode < 300) {
// Success
} else {
// Log error
error_log("CalDAV Error: " . $response);
}
}
static function generateIcalEvent($ticket) {
$summary = 'Ticket #' . $ticket->getNumber();
$description = $ticket->getSubject();
return "BEGIN:VCALENDAR\n" .
"VERSION:2.0\n" .
"PRODID:-//osTicket CalDAV Plugin\n" .
"BEGIN:VEVENT\n" .
"UID:" . uniqid() . "@osticket.com\n" .
"DTSTAMP:" . date('Ymd\THis\Z') . "\n" .
"DTSTART:" . date('Ymd\THis\Z') . "\n" .
"SUMMARY:" . $summary . "\n" .
"DESCRIPTION:" . $description . "\n" .
"END:VEVENT\n" .
"END:VCALENDAR";
}
}
?>