Add attendees to calendar events with sendUpdates=all so workers and Evelin receive an instant push notification + invitation email when a booking is created or modified, instead of the event silently appearing. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
102 lines
4.2 KiB
PHP
102 lines
4.2 KiB
PHP
<?php
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
class GoogleCalendar {
|
|
|
|
private $service = null;
|
|
|
|
public function __construct() {
|
|
require_once FCPATH . 'vendor/autoload.php';
|
|
|
|
$CI =& get_instance();
|
|
$CI->config->load('google_calendar');
|
|
$serviceAccountPath = $CI->config->item('gcal_service_account_path');
|
|
|
|
if (!file_exists($serviceAccountPath)) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$client = new Google_Client();
|
|
$client->setAuthConfig($serviceAccountPath);
|
|
$client->setScopes([Google_Service_Calendar::CALENDAR]);
|
|
$this->service = new Google_Service_Calendar($client);
|
|
} catch (Exception $e) {
|
|
// Silently fail — calendar errors must never break the booking flow
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a calendar event and notify the attendee.
|
|
* @param string $calendarId Calendar to insert into
|
|
* @param string $title
|
|
* @param string $description
|
|
* @param string $startDatetime ISO 8601 (e.g. 2026-03-10T10:00:00)
|
|
* @param string $endDatetime
|
|
* @param string|null $attendeeEmail If set, added as attendee — triggers push notification + invite email
|
|
* @return string|null The new event ID, or null on failure.
|
|
*/
|
|
public function createEvent($calendarId, $title, $description, $startDatetime, $endDatetime, $attendeeEmail = null) {
|
|
if (!$this->service || empty($calendarId)) return null;
|
|
try {
|
|
$eventData = [
|
|
'summary' => $title,
|
|
'description' => $description,
|
|
'start' => ['dateTime' => $startDatetime, 'timeZone' => 'Europe/Oslo'],
|
|
'end' => ['dateTime' => $endDatetime, 'timeZone' => 'Europe/Oslo'],
|
|
];
|
|
if (!empty($attendeeEmail)) {
|
|
$eventData['attendees'] = [['email' => $attendeeEmail]];
|
|
}
|
|
$event = new Google_Service_Calendar_Event($eventData);
|
|
$params = !empty($attendeeEmail) ? ['sendUpdates' => 'all'] : [];
|
|
$result = $this->service->events->insert($calendarId, $event, $params);
|
|
return $result->getId();
|
|
} catch (Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update an existing calendar event and notify the attendee.
|
|
* @param string|null $attendeeEmail If set, added as attendee — triggers update notification
|
|
* @return string|null The event ID, or null on failure.
|
|
*/
|
|
public function updateEvent($calendarId, $eventId, $title, $description, $startDatetime, $endDatetime, $attendeeEmail = null) {
|
|
if (!$this->service || empty($calendarId) || empty($eventId)) return null;
|
|
try {
|
|
$event = $this->service->events->get($calendarId, $eventId);
|
|
$event->setSummary($title);
|
|
$event->setDescription($description);
|
|
$event->setStart(new Google_Service_Calendar_EventDateTime([
|
|
'dateTime' => $startDatetime,
|
|
'timeZone' => 'Europe/Oslo',
|
|
]));
|
|
$event->setEnd(new Google_Service_Calendar_EventDateTime([
|
|
'dateTime' => $endDatetime,
|
|
'timeZone' => 'Europe/Oslo',
|
|
]));
|
|
if (!empty($attendeeEmail)) {
|
|
$event->setAttendees([new Google_Service_Calendar_EventAttendee(['email' => $attendeeEmail])]);
|
|
}
|
|
$params = !empty($attendeeEmail) ? ['sendUpdates' => 'all'] : [];
|
|
$result = $this->service->events->update($calendarId, $eventId, $event, $params);
|
|
return $result->getId();
|
|
} catch (Exception $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete a calendar event. Silently skips if not found.
|
|
*/
|
|
public function deleteEvent($calendarId, $eventId) {
|
|
if (!$this->service || empty($calendarId) || empty($eventId)) return;
|
|
try {
|
|
$this->service->events->delete($calendarId, $eventId);
|
|
} catch (Exception $e) {
|
|
// Silently skip — event may already be gone
|
|
}
|
|
}
|
|
}
|