Initial commit

This commit is contained in:
2019-10-13 18:38:03 +02:00
commit 759eb88ef8
10124 changed files with 838037 additions and 0 deletions

110
classes/Event.php Normal file
View File

@@ -0,0 +1,110 @@
<?php namespace NicoSt\GCalendar\Classes;
class Event {
var $calendar_name;
var $event_name;
var $location;
var $description;
var $start_time;
var $end_time;
var $color;
/**
* @return mixed
*/
public function getCalendarName() {
return $this->calendar_name;
}
/**
* @param mixed $calendar_name
*/
public function setCalendarName($calendar_name) {
$this->calendar_name = $calendar_name;
}
/**
* @return mixed
*/
public function getEventName() {
return $this->event_name;
}
/**
* @param mixed $event_name
*/
public function setEventName($event_name) {
$this->event_name = $event_name;
}
/**
* @return mixed
*/
public function getLocation() {
return $this->location;
}
/**
* @param mixed $location
*/
public function setLocation($location) {
$this->location = $location;
}
/**
* @return mixed
*/
public function getDescription() {
return $this->description;
}
/**
* @param mixed $description
*/
public function setDescription($description) {
$this->description = $description;
}
/**
* @return mixed
*/
public function getStartTime() {
return $this->start_time;
}
/**
* @param mixed $start_time
*/
public function setStartTime($start_time) {
$this->start_time = $start_time;
}
/**
* @return mixed
*/
public function getEndTime() {
return $this->end_time;
}
/**
* @param mixed $end_time
*/
public function setEndTime($end_time) {
$this->end_time = $end_time;
}
/**
* @return mixed
*/
public function getColor() {
return $this->color;
}
/**
* @param mixed $color
*/
public function setColor($color) {
$this->color = $color;
}
}

View File

@@ -0,0 +1,53 @@
<?php namespace NicoSt\GCalendar\Classes;
use Google_Client;
use Google_Service_Calendar;
use Log;
use NicoSt\GCalendar\Models\Settings;
class GoogleCalendarClient {
protected $client;
public function __construct($tryRefreshToken = false) {
$this->client = new Google_Client();
$this->client->setApplicationName(Settings::get('application_name'));
$this->client->setClientId(Settings::get('client_id'));
$this->client->setClientSecret(Settings::get('client_secret'));
$this->client->setAccessType('offline');
$this->client->setRedirectUri(url('/gcalendar/oauth2callback'));
$this->client->addScope(Google_Service_Calendar::CALENDAR);
// Set access toke on client if exist.
$accessToken = Settings::get('access_token');
if (!empty($accessToken)) {
$this->client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($tryRefreshToken && $this->client->isAccessTokenExpired()) {
// Refresh the token if possible.
if ($this->client->getRefreshToken()) {
$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
// Save new access token
Settings::set('access_token', $this->client->getAccessToken());
} else {
Log::warning('G-Calendar: No valid access token given.');
}
}
}
public function getClient() {
return $this->client;
}
public function getService() {
return new Google_Service_Calendar($this->client);
}
}

View File

@@ -0,0 +1,106 @@
<?php namespace NicoSt\GCalendar\Classes;
use Log;
use Cache;
use NicoSt\GCalendar\Models\Settings;
class GoogleCalendarService {
private $cacheTimeDefault = 15;
private $cacheKeyCalendarList = 'GCalendar-CalendarList';
private $cacheKeyCalendarGet = 'GCalendar-CalendarGet#';
private $service;
public function __construct() {
$client = new GoogleCalendarClient(true);
$this->service = $client->getService();
}
public function getCalendar($calendarId, $maxEvents = '10') {
$cachedCalendarGet = Cache::get($this->cacheKeyCalendarGet.$calendarId);
$cacheTime = Settings::get('cache_time', $this->cacheTimeDefault);
if(!isset($cachedCalendarGet) || $cacheTime == 0) {
// "timeMin" -> Set param to only get upcoming events
// "singleEvents" -> Split recurring events into single events
// "orderBy" -> Order events by startTime
// "maxResults" -> Maximum number of events returned on one page
$params = array(
'timeMin' => date( DATE_RFC3339, time()),
'singleEvents' => 'true',
'orderBy' => 'startTime',
'maxResults' => $maxEvents
);
try {
Log::debug('G-Calendar: Call google with #getCalendar.');
$response = $this->service->events->listEvents($calendarId, $params);
Cache::put($this->cacheKeyCalendarGet.$calendarId, $response, $cacheTime);
return $response;
} catch (\Exception $e) {
$json = json_decode($e->getMessage(), true);
Log::error('G-Calendar: Exception occurred on #getCalendar => ['. print_r($json, true).']');
return $json;
}
}
return $cachedCalendarGet;
}
public function calendarList() {
$cachedCalendarList = Cache::get($this->cacheKeyCalendarList);
$cacheTime = Settings::get('cache_time', $this->cacheTimeDefault);
if(!isset($cachedCalendarList) || $cacheTime == 0) {
try {
Log::debug('G-Calendar: Call google with #calendarList.');
$response = $this->service->calendarList->listCalendarList()->getItems();
Cache::put($this->cacheKeyCalendarList, $response, $cacheTime);
return $response;
} catch (\Exception $e) {
$json = json_decode($e->getMessage(), true);
Log::error('G-Calendar: Exception occurred on #calendarList => ['. print_r($json, true).']');
return $json;
}
}
return $cachedCalendarList;
}
/**
* Returns a array like this:
*
* @param String $maxEvents - Amount of events to return
*
* @return array['calendar_1']
* ['event_1']
* ['event_2']
* array['calendar_2']
* ['event_1']
* ['event_2']
* ...
*/
public function allEventList($maxEvents) {
$savedCalendars = array();
$events = array();
if (!empty(Settings::get('calendar_selector'))) {
$savedCalendars = Settings::get('calendar_selector');
}
foreach($savedCalendars as $savedCalendar) {
$calendarEvents = $this->getCalendar($savedCalendar['id'], $maxEvents);
// Check for error to passthrough
if (isset($calendarEvents['error'])) {
return $calendarEvents;
}
array_push($events, $calendarEvents);
}
return $events;
}
}