Move iCal parsing/writing classes to libcalendaring

This commit is contained in:
Thomas Bruederli 2013-07-18 12:38:14 +02:00
parent f030275eb3
commit 18f9fa5c86
5 changed files with 32 additions and 18 deletions

View file

@ -219,8 +219,7 @@ class calendar extends rcube_plugin
public function get_ical() public function get_ical()
{ {
if (!$this->ical) { if (!$this->ical) {
require_once($this->home . '/lib/calendar_ical.php'); $this->ical = libcalendaring::get_ical();
$this->ical = new calendar_ical($this);
} }
return $this->ical; return $this->ical;

View file

@ -7,7 +7,7 @@
* - alarms display and dismissal * - alarms display and dismissal
* - attachment handling * - attachment handling
* - recurrence computation and UI elements (TODO) * - recurrence computation and UI elements (TODO)
* - ical parsing and exporting (TODO) * - ical parsing and exporting
* *
* @version @package_version@ * @version @package_version@
* @author Thomas Bruederli <bruederli@kolabsys.com> * @author Thomas Bruederli <bruederli@kolabsys.com>
@ -97,6 +97,15 @@ class libcalendaring extends rcube_plugin
} }
} }
/**
* Load iCalendar functions
*/
public static function get_ical()
{
$self = self::get_instance();
require_once($self->home . '/libvcalendar.php');
return new libvcalendar($self->timezone);
}
/** /**
* Shift dates into user's current timezone * Shift dates into user's current timezone

View file

@ -34,20 +34,18 @@
* > pear install horde/Horde_Icalendar * > pear install horde/Horde_Icalendar
* *
*/ */
class calendar_ical class libvcalendar
{ {
const EOL = "\r\n"; const EOL = "\r\n";
private $rc; private $timezone;
private $cal;
public $method; public $method;
public $events = array(); public $events = array();
function __construct($cal) function __construct($timezone = null)
{ {
$this->cal = $cal; $this->timezone = $timezone ? $timezone : new DateTimezone('UTC');
$this->rc = $cal->rc;
} }
/** /**
@ -117,6 +115,8 @@ class calendar_ical
} }
} }
$this->method = $parser->getAttributeDefault('METHOD', '');
return $this->events; return $this->events;
} }
@ -126,7 +126,7 @@ class calendar_ical
public function get_parser() public function get_parser()
{ {
if (!class_exists('Horde_iCalendar')) if (!class_exists('Horde_iCalendar'))
require_once($this->cal->home . '/lib/Horde_iCalendar.php'); require_once(__DIR__ . '/lib/Horde_iCalendar.php');
// set target charset for parsed events // set target charset for parsed events
$GLOBALS['_HORDE_STRING_CHARSET'] = RCMAIL_CHARSET; $GLOBALS['_HORDE_STRING_CHARSET'] = RCMAIL_CHARSET;
@ -160,12 +160,12 @@ class calendar_ical
// assign current timezone to event start/end // assign current timezone to event start/end
if (is_a($event['start'], 'DateTime')) if (is_a($event['start'], 'DateTime'))
$event['start']->setTimezone($this->cal->timezone); $event['start']->setTimezone($this->timezone);
else else
unset($event['start']); unset($event['start']);
if (is_a($event['end'], 'DateTime')) if (is_a($event['end'], 'DateTime'))
$event['end']->setTimezone($this->cal->timezone); $event['end']->setTimezone($this->timezone);
else else
unset($event['end']); unset($event['end']);
@ -233,6 +233,7 @@ class calendar_ical
break; break;
case 'EXDATE': case 'EXDATE':
$event['recurrence']['EXDATE'][] = $this->_date2time($attr['value']);
break; break;
case 'RECURRENCE-ID': case 'RECURRENCE-ID':
@ -243,6 +244,7 @@ class calendar_ical
$event['sequence'] = intval($attr['value']); $event['sequence'] = intval($attr['value']);
break; break;
case 'CATEGORIES':
case 'DESCRIPTION': case 'DESCRIPTION':
case 'LOCATION': case 'LOCATION':
case 'URL': case 'URL':
@ -332,8 +334,8 @@ class calendar_ical
array_unshift($event['attendees'], $organizer); array_unshift($event['attendees'], $organizer);
// make sure the event has an UID // make sure the event has an UID
if (!$event['uid']) # if (!$event['uid'])
$event['uid'] = $this->cal->generate_uid(); # $event['uid'] = $this->cal->generate_uid();
return $event; return $event;
} }
@ -345,7 +347,7 @@ class calendar_ical
{ {
// create timestamp at 12:00 in user's timezone // create timestamp at 12:00 in user's timezone
if (is_array($prop)) if (is_array($prop))
return date_create(sprintf('%04d%02d%02dT120000', $prop['year'], $prop['month'], $prop['mday']), $this->cal->timezone); return date_create(sprintf('%04d%02d%02dT120000', $prop['year'], $prop['month'], $prop['mday']), $this->timezone);
else if (is_numeric($prop)) else if (is_numeric($prop))
return date_create('@'.$prop); return date_create('@'.$prop);
@ -425,8 +427,12 @@ class calendar_ical
} }
if ($event['recurrence'] && !$recurrence_id) { if ($event['recurrence'] && !$recurrence_id) {
$vevent .= "RRULE:" . libcalendaring::to_rrule($event['recurrence'], self::EOL) . self::EOL; $vevent .= "RRULE:" . libcalendaring::to_rrule($event['recurrence'], self::EOL) . self::EOL;
foreach ((array)$event['recurrence']['EXDATE'] as $ex) {
$vevent .= $this->format_datetime("EXDATE", $ex, false, true) . self::EOL;
} }
if(!empty($event['categories'])) { }
if (!empty($event['categories'])) {
$vevent .= "CATEGORIES:" . self::escape(strtoupper($event['categories'])) . self::EOL; $vevent .= "CATEGORIES:" . self::escape(strtoupper($event['categories'])) . self::EOL;
} }
if ($event['sensitivity'] > 0) { if ($event['sensitivity'] > 0) {