Files
studiobeve.no/application/libraries/GoogleCalendar.php
T

86 lines
3.1 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.
* @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
}
}
}