Google Calendar integration for booking notifications
- New GoogleCalendar library: createEvent/updateEvent/deleteEvent via service account, all wrapped in try/catch so failures never break booking flow - booking_process: creates worker + owner calendar events on new booking - manage_booking_process: deletes old events, creates new ones on modify - manage_booking_cancel: deletes events before cancellation - Service_model: updateBookingCalEvents() stores gcal event IDs - Admin worker form: Google Calendar ID field added - PHPMailer: enabled exceptions (was silently swallowing SMTP errors) - Config: application/config/google_calendar.php for service account path + Evelin calendar ID DB migration required: ALTER TABLE workers ADD COLUMN google_calendar_id VARCHAR(255) NULL DEFAULT NULL; ALTER TABLE bookings ADD COLUMN gcal_event_id_worker VARCHAR(255) NULL DEFAULT NULL; ALTER TABLE bookings ADD COLUMN gcal_event_id_owner VARCHAR(255) NULL DEFAULT NULL; Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?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.
|
||||
* @return string|null The new event ID, or null on failure.
|
||||
*/
|
||||
public function createEvent($calendarId, $title, $description, $startDatetime, $endDatetime) {
|
||||
if (!$this->service || empty($calendarId)) return null;
|
||||
try {
|
||||
$event = new Google_Service_Calendar_Event([
|
||||
'summary' => $title,
|
||||
'description' => $description,
|
||||
'start' => ['dateTime' => $startDatetime, 'timeZone' => 'Europe/Oslo'],
|
||||
'end' => ['dateTime' => $endDatetime, 'timeZone' => 'Europe/Oslo'],
|
||||
]);
|
||||
$result = $this->service->events->insert($calendarId, $event);
|
||||
return $result->getId();
|
||||
} catch (Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing calendar event.
|
||||
* @return string|null The event ID, or null on failure.
|
||||
*/
|
||||
public function updateEvent($calendarId, $eventId, $title, $description, $startDatetime, $endDatetime) {
|
||||
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',
|
||||
]));
|
||||
$result = $this->service->events->update($calendarId, $eventId, $event);
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user