Initial commit
This commit is contained in:
267
components/EmbeddedCalendar.php
Normal file
267
components/EmbeddedCalendar.php
Normal file
@@ -0,0 +1,267 @@
|
||||
<?php namespace NicoSt\GCalendar\Components;
|
||||
|
||||
use Cms\Classes\ComponentBase;
|
||||
use NicoSt\GCalendar\Classes\Event;
|
||||
use NicoSt\GCalendar\Classes\GoogleCalendarService;
|
||||
use Mobile_Detect;
|
||||
|
||||
use Log;
|
||||
|
||||
class EmbeddedCalendar extends ComponentBase {
|
||||
private $calPropSeparator = '§§';
|
||||
private $googleEmbedUrl = 'https://calendar.google.com/calendar/embed';
|
||||
|
||||
/**
|
||||
* Returns information about this component, including name and description.
|
||||
*/
|
||||
public function componentDetails() {
|
||||
return [
|
||||
'name' => 'nicost.gcalendar::lang.component.embeddedCalendar.name',
|
||||
'description' => 'nicost.gcalendar::lang.component.embeddedCalendar.description'
|
||||
];
|
||||
}
|
||||
|
||||
public function defineProperties() {
|
||||
|
||||
$calendars = $this->calendarProperties();
|
||||
$customProperties = [
|
||||
'calendarTitle' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.calendarTitle.title',
|
||||
'type' => 'string'
|
||||
],
|
||||
'width' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.width.title',
|
||||
'type' => 'string',
|
||||
'default' => 1200,
|
||||
'validationPattern' => '^[0-9]{1,4}$',
|
||||
'validationMessage' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.width.validationMessage'
|
||||
],
|
||||
'height' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.height.title',
|
||||
'type' => 'string',
|
||||
'default' => 1000,
|
||||
'validationPattern' => '^[0-9]{1,4}$',
|
||||
'validationMessage' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.height.validationMessage'
|
||||
],
|
||||
'timezone' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.timezone.title',
|
||||
'type' => 'dropdown',
|
||||
'default' => 'Europe/Berlin'
|
||||
],
|
||||
'language' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.language.title',
|
||||
'type' => 'dropdown',
|
||||
'default' => 'en'
|
||||
],
|
||||
'viewMode' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.viewMode.title',
|
||||
'type' => 'dropdown',
|
||||
'default' => 'month',
|
||||
'placeholder' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.viewMode.placeholder',
|
||||
'description' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.viewMode.description',
|
||||
'options' => ['month'=>'nicost.gcalendar::lang.component.embeddedCalendar.properties.viewMode.month',
|
||||
'week'=>'nicost.gcalendar::lang.component.embeddedCalendar.properties.viewMode.week',
|
||||
'agenda'=>'nicost.gcalendar::lang.component.embeddedCalendar.properties.viewMode.agenda',
|
||||
'dynamic'=>'nicost.gcalendar::lang.component.embeddedCalendar.properties.viewMode.dynamic']
|
||||
],
|
||||
'weekStart' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.weekStart.title',
|
||||
'type' => 'dropdown',
|
||||
'default' => 'mon',
|
||||
'options' => ['sat'=>'nicost.gcalendar::lang.component.embeddedCalendar.properties.weekStart.sat',
|
||||
'sun'=>'nicost.gcalendar::lang.component.embeddedCalendar.properties.weekStart.sun',
|
||||
'mon'=>'nicost.gcalendar::lang.component.embeddedCalendar.properties.weekStart.mon']
|
||||
],
|
||||
'bgcolor' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.bgcolor.title',
|
||||
'type' => 'string',
|
||||
'default' => '#FFFFFF',
|
||||
'description' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.bgcolor.description',
|
||||
'validationPattern' => '^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$',
|
||||
'validationMessage' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.bgcolor.validationMessage'
|
||||
],
|
||||
'showTitle' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.showTitle.title',
|
||||
'type' => 'checkbox',
|
||||
'default' => 1,
|
||||
'group' => 'nicost.gcalendar::lang.component.embeddedCalendar.groups.toggleElements'
|
||||
],
|
||||
'showPrint' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.showPrint.title',
|
||||
'type' => 'checkbox',
|
||||
'default' => 1,
|
||||
'group' => 'nicost.gcalendar::lang.component.embeddedCalendar.groups.toggleElements'
|
||||
],
|
||||
'showTimezone' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.showTimezone.title',
|
||||
'type' => 'checkbox',
|
||||
'default' => 1,
|
||||
'group' => 'nicost.gcalendar::lang.component.embeddedCalendar.groups.toggleElements'
|
||||
],
|
||||
'showNav' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.showNav.title',
|
||||
'type' => 'checkbox',
|
||||
'default' => 1,
|
||||
'group' => 'nicost.gcalendar::lang.component.embeddedCalendar.groups.toggleElements'
|
||||
],
|
||||
'showDate' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.showDate.title',
|
||||
'type' => 'checkbox',
|
||||
'default' => 1,
|
||||
'group' => 'nicost.gcalendar::lang.component.embeddedCalendar.groups.toggleElements'
|
||||
],
|
||||
'showTabs' => [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.showTabs.title',
|
||||
'type' => 'checkbox',
|
||||
'default' => 1,
|
||||
'group' => 'nicost.gcalendar::lang.component.embeddedCalendar.groups.toggleElements'
|
||||
],
|
||||
'showCalendarList'=> [
|
||||
'title' => 'nicost.gcalendar::lang.component.embeddedCalendar.properties.showCalendarList.title',
|
||||
'type' => 'checkbox',
|
||||
'default' => 1,
|
||||
'group' => 'nicost.gcalendar::lang.component.embeddedCalendar.groups.toggleElements'
|
||||
]
|
||||
];
|
||||
$result = array_merge($customProperties, $calendars);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function getUrl() {
|
||||
|
||||
$data = [
|
||||
'showPrint' => $this->property('showPrint', 1),
|
||||
'showTz' => $this->property('showTimezone', 1),
|
||||
'showCalendars' => $this->property('showCalendars', 1),
|
||||
'showTabs' => $this->property('showTabs', 1),
|
||||
'showDate' => $this->property('showDate', 1),
|
||||
'showNav' => $this->property('showNav', 1),
|
||||
'showTitle' => $this->property('showTitle', 1),
|
||||
'hl' => $this->property('language', 'en'),
|
||||
'bgcolor' => $this->property('bgcolor', '#FFFFFF'),
|
||||
'ctz' => $this->property('timezone'. 'Europe/Berlin')
|
||||
];
|
||||
|
||||
// Set title
|
||||
if(empty($this->property('calendarTitle')) ) {
|
||||
$data['showTitle'] = 0;
|
||||
} else {
|
||||
$data += ['title'=>$this->property('calendarTitle')];
|
||||
}
|
||||
|
||||
// Set view mode
|
||||
$viewMode = $this->property('viewMode', 'month');
|
||||
if($viewMode == 'month') {
|
||||
$data += ['mode' => 'MONTH'];
|
||||
} else if($viewMode == 'agenda') {
|
||||
$data += ['mode' => 'AGENDA'];
|
||||
} else if($viewMode == 'week') {
|
||||
$data += ['mode' => 'WEEK'];
|
||||
} else if($viewMode == 'dynamic') {
|
||||
$detect = new Mobile_Detect;
|
||||
if($detect->isMobile()) {
|
||||
$data += ['mode' => 'AGENDA'];
|
||||
} else {
|
||||
$data += ['mode' => 'MONTH'];
|
||||
}
|
||||
}
|
||||
|
||||
// Set week start
|
||||
$weekStart = $this->property('weekStart','mon');
|
||||
if($weekStart == 'sat') {
|
||||
$data += ['wkst' => 7];
|
||||
} else if($weekStart == 'sun') {
|
||||
$data += ['wkst' => 1];
|
||||
} else if($weekStart == 'mon') {
|
||||
$data += ['wkst' => 2];
|
||||
}
|
||||
|
||||
$calendarList = $this->getCalendarsFromProperties();
|
||||
$dataParams = http_build_query($data);
|
||||
|
||||
$calendarParams = "";
|
||||
$i = 0;
|
||||
$calCount = count($calendarList);
|
||||
foreach($calendarList as $calendarData) {
|
||||
$calQuery = http_build_query($calendarData);
|
||||
|
||||
$calendarParams = $calendarParams.$calQuery;
|
||||
if($i != $calCount - 1){
|
||||
$calendarParams = $calendarParams.'&';
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
$url = $this->googleEmbedUrl.'?'.$dataParams.'&'.$calendarParams;
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
public function getWidth() {
|
||||
return $this->property('width', 1200);
|
||||
}
|
||||
|
||||
public function getHeight() {
|
||||
return $this->property('height', 1000);
|
||||
}
|
||||
|
||||
public function getLanguageOptions() {
|
||||
$strJsonFileContents = file_get_contents(dirname(__FILE__)."/../resources/calendar_language.json");
|
||||
return json_decode($strJsonFileContents, true);
|
||||
}
|
||||
|
||||
public function getTimezoneOptions() {
|
||||
$strJsonFileContents = file_get_contents(dirname(__FILE__)."/../resources/calendar_timezone.json");
|
||||
return json_decode($strJsonFileContents, true);
|
||||
}
|
||||
|
||||
private function getCalendarsFromProperties() {
|
||||
$calendars = [];
|
||||
foreach($this->properties as $key => $value){
|
||||
$exp_key = explode($this->calPropSeparator, $key);
|
||||
if($exp_key[0] == 'cal' && $this->property($key) == true){
|
||||
$calendar = [];
|
||||
$calendar['src'] = $exp_key[2];
|
||||
$calendar['color'] = $exp_key[1];
|
||||
$calendars[] = $calendar;
|
||||
}
|
||||
}
|
||||
|
||||
return $calendars;
|
||||
}
|
||||
|
||||
private function calendarProperties() {
|
||||
// ToDo: Add cache => https://octobercms.com/docs/services/cache
|
||||
$service = new GoogleCalendarService();
|
||||
$calendars = $service->calendarList();
|
||||
|
||||
if (isset($calendars['error'])) {
|
||||
$calendars = '';
|
||||
}
|
||||
|
||||
$calendarProperties = [];
|
||||
if (!empty($calendars)) {
|
||||
foreach ($calendars as &$calendar) {
|
||||
$calendarData = [];
|
||||
$calendarData['id'] = $calendar->id;
|
||||
$calendarData['title'] = $calendar->summary;
|
||||
$calendarData['color'] = $calendar->backgroundColor;
|
||||
|
||||
$property = $this->createProperty($calendarData);
|
||||
$calendarProperties = array_merge($calendarProperties, $property);
|
||||
}
|
||||
}
|
||||
return $calendarProperties;
|
||||
}
|
||||
|
||||
private function createProperty(array $calendarData) {
|
||||
return [
|
||||
'cal'.$this->calPropSeparator.$calendarData['color'].$this->calPropSeparator.$calendarData['id'] => [
|
||||
'title' => $calendarData['title'],
|
||||
'type' => 'checkbox',
|
||||
'group' => 'nicost.gcalendar::lang.component.embeddedCalendar.groups.calendars',
|
||||
'default' => 0,
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
96
components/Upcoming.php
Normal file
96
components/Upcoming.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php namespace NicoSt\GCalendar\Components;
|
||||
|
||||
use Cms\Classes\ComponentBase;
|
||||
use NicoSt\GCalendar\Classes\Event;
|
||||
use NicoSt\GCalendar\Classes\GoogleCalendarService;
|
||||
use NicoSt\GCalendar\Models\Settings;
|
||||
|
||||
use Log;
|
||||
|
||||
class Upcoming extends ComponentBase {
|
||||
|
||||
/**
|
||||
* Returns information about this component, including name and description.
|
||||
*/
|
||||
public function componentDetails() {
|
||||
return [
|
||||
'name' => 'nicost.gcalendar::lang.component.upcoming.name',
|
||||
'description' => 'nicost.gcalendar::lang.component.upcoming.description'
|
||||
];
|
||||
}
|
||||
|
||||
/*
|
||||
public function get() {
|
||||
|
||||
$connector = new GoogleCalendarService();
|
||||
|
||||
return $connector->calendarList();
|
||||
}
|
||||
*/
|
||||
public function defineProperties() {
|
||||
return [
|
||||
'maxEvents' => [
|
||||
'title' => 'Max Events',
|
||||
'description' => 'The maximal amount of events which shall be displayed.',
|
||||
'default' => 5,
|
||||
'type' => 'string',
|
||||
'validationPattern' => '^[0-9]+$',
|
||||
'validationMessage' => 'The Max Events property can contain only numeric symbols'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function events() {
|
||||
$connector = new GoogleCalendarService();
|
||||
$maxEvents = $this->property('maxEvents');
|
||||
$calendarEvents = $connector->allEventList($maxEvents);
|
||||
// Check for error to pass through
|
||||
if (isset($calendarEvents['error'])) {
|
||||
return $calendarEvents;
|
||||
}
|
||||
|
||||
if (!is_array($calendarEvents) && !is_object($calendarEvents)) {
|
||||
return [];
|
||||
}
|
||||
$eventObjects = self::mapToEventDto($calendarEvents);
|
||||
|
||||
// Sort events from all calendars
|
||||
usort($eventObjects, array("NicoSt\GCalendar\Components\Upcoming", "sortEventsByDate"));
|
||||
|
||||
// Trim to maxEvents value and return
|
||||
return $output = array_slice($eventObjects, 0, $maxEvents);
|
||||
}
|
||||
|
||||
private static function mapToEventDto($calendarEvents){
|
||||
$savedCalendars = Settings::get('calendar_selector');
|
||||
$eventObjects = [];
|
||||
foreach ($calendarEvents as $calendarEvent) {
|
||||
foreach ($calendarEvent as $event) {
|
||||
// Map to data response object.
|
||||
$eventObject = new Event();
|
||||
$eventObject->setCalendarName($calendarEvent->summary);
|
||||
$eventObject->setEventName($event->summary);
|
||||
$eventObject->setLocation($event->location);
|
||||
$eventObject->setDescription($event->description);
|
||||
$eventObject->setStartTime($event->start->dateTime);
|
||||
$eventObject->setEndTime($event->end->dateTime);
|
||||
array_push($eventObjects, $eventObject);
|
||||
|
||||
foreach($savedCalendars as $calendar) {
|
||||
if($calendar['id'] == $event->organizer->email) {
|
||||
$eventObject->setColor($calendar['color']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $eventObjects;
|
||||
}
|
||||
|
||||
private function sortEventsByDate($a, $b) {
|
||||
if ($a->start_time == $b->start_time) {
|
||||
return 0;
|
||||
}
|
||||
return ($a->start_time < $b->start_time) ? -1 : 1;
|
||||
}
|
||||
|
||||
}
|
||||
6
components/embeddedcalendar/default.htm
Normal file
6
components/embeddedcalendar/default.htm
Normal file
@@ -0,0 +1,6 @@
|
||||
<iframe
|
||||
src="{{ __SELF__.getUrl|raw }}"
|
||||
width="{{ __SELF__.getWidth }}"
|
||||
height="{{ __SELF__.getHeight }}"
|
||||
frameborder="0">
|
||||
</iframe>
|
||||
37
components/upcoming/default.htm
Normal file
37
components/upcoming/default.htm
Normal file
@@ -0,0 +1,37 @@
|
||||
{% set events = upcoming.events %}
|
||||
|
||||
{% if not events.error and events is not empty %}
|
||||
<table>
|
||||
<thead>
|
||||
<th>Calendar Name</th>
|
||||
<th>Event Name</th>
|
||||
<th>Location</th>
|
||||
<th>Description</th>
|
||||
<th>Start Time</th>
|
||||
<th>End Time</th>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for event in events %}
|
||||
<tr>
|
||||
<td style="padding: 0 10px 5px 20px; border-left: 6px solid {{ event.color }};">{{ event.calendar_name }}</td>
|
||||
<td style="padding: 0 10px 5px 20px;">{{ event.event_name }}</td>
|
||||
<td style="padding: 0 10px 5px 20px;">{{ event.location }}</td>
|
||||
<td style="padding: 0 10px 5px 20px;">{{ event.description|raw }}</td>
|
||||
<td style="padding: 0 10px 5px 20px;">{{ event.start_time|date("Y-m-d H:i") }}</td>
|
||||
<td style="padding: 0 10px 5px 20px;">{{ event.end_time|date("Y-m-d H:i") }}</td>
|
||||
</tr>
|
||||
{% else %}
|
||||
<tr>
|
||||
<td colspan="6">No events found.</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% elseif events.error %}
|
||||
{% for item in events %}
|
||||
<p style="color: red"><b>{{ item.code }}</b> - {{ item.message }}</p>
|
||||
{% endfor %}
|
||||
|
||||
{% else %}
|
||||
<p>Events cannot be displayed.</p>
|
||||
{% endif %}
|
||||
Reference in New Issue
Block a user