Files
oc-gcalendar-plugin/components/Download.php

95 lines
3.0 KiB
PHP

<?php namespace NicoSt\GCalendar\Components;
use Cms\Classes\ComponentBase;
use NicoSt\GCalendar\Classes\GoogleCalendarService;
class Download extends ComponentBase {
private $calPropSeparator = '§§';
private $googleIcalUrl = 'https://calendar.google.com/calendar/ical/%s/public/basic.ics';
/**
* Returns information about this component, including name and description.
*/
public function componentDetails() {
return [
'name' => 'nicost.gcalendar::lang.component.download.name',
'description' => 'nicost.gcalendar::lang.component.download.description'
];
}
public function defineProperties() {
$calendars = $this->calendarProperties();
$customProperties = [];
$result = array_merge($customProperties, $calendars);
return $result;
}
public function getCalendarDownloads() {
// Calenders from config
$calendarList = $this->getCalendarsFromProperties();
$data = [];
foreach($calendarList as $calendarData) {
$calendar = [];
$calendar['link'] = sprintf($this->googleIcalUrl, $calendarData['id']);
$calendar['title'] = $calendarData['title'];
array_push($data, $calendar);
}
return $data;
}
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['id'] = $exp_key[1];
$calendar['title'] = $exp_key[2];
array_push($calendars, $calendar);
}
}
return $calendars;
}
private function calendarProperties() {
$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;
$property = $this->createProperty($calendarData);
$calendarProperties = array_merge($calendarProperties, $property);
}
}
return $calendarProperties;
}
private function createProperty(array $calendarData) {
return [
'cal'.$this->calPropSeparator.$calendarData['id'].$this->calPropSeparator.$calendarData['title'] => [
'title' => $calendarData['title'],
'type' => 'checkbox',
'group' => 'nicost.gcalendar::lang.component.download.groups.calendars',
// Default to setting from config
'default' => 0,
]
];
}
}