Turn timeObjects into a driver.
authorJan Schneider <jan@horde.org>
Tue, 3 Mar 2009 23:09:01 +0000 (00:09 +0100)
committerJan Schneider <jan@horde.org>
Tue, 3 Mar 2009 23:09:26 +0000 (00:09 +0100)
kronolith/lib/Driver/Horde.php [new file with mode: 0644]
kronolith/lib/Event/Horde.php [new file with mode: 0644]
kronolith/lib/Kronolith.php

diff --git a/kronolith/lib/Driver/Horde.php b/kronolith/lib/Driver/Horde.php
new file mode 100644 (file)
index 0000000..f6b49bb
--- /dev/null
@@ -0,0 +1,99 @@
+<?php
+/**
+ * The Kronolith_Driver_Horde class implements the Kronolith_Driver API for
+ * time objects retrieved from other Horde applications.
+ *
+ * Possible driver parameters:
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Jan Schneider <jan@horde.org>
+ * @package Kronolith
+ */
+class Kronolith_Driver_Horde extends Kronolith_Driver
+{
+    /**
+     * The API (application) of the current calendar.
+     *
+     * @var string
+     */
+    public $api;
+
+    public function open($calendar)
+    {
+        parent::open($calendar);
+        list($this->api,) = explode('/', $this->_calendar, 2);
+    }
+
+    public function listAlarms($date, $fullevent = false)
+    {
+        return array();
+    }
+
+    /**
+     * Lists all events in the time range, optionally restricting results to
+     * only events with alarms.
+     *
+     * @param Horde_Date $startInterval  Start of range date object.
+     * @param Horde_Date $endInterval    End of range data object.
+     * @param boolean $showRecurrence    Return every instance of a recurring
+     *                                   event? If false, will only return
+     *                                   recurring events once inside the
+     *                                   $startDate - $endDate range.
+     * @param boolean $hasAlarm          Only return events with alarms?
+     *
+     * @return array  Events in the given time range.
+     */
+    public function listEvents($startDate = null, $endDate = null,
+                               $showRecurrence = false, $hasAlarm = false)
+    {
+        list($this->api, $category) = explode('/', $this->_calendar, 2);
+        if (!$this->_params['registry']->hasMethod($this->api . '/listTimeObjects')) {
+            return array();
+        }
+
+        $eventsList = $this->_params['registry']->call($this->api . '/listTimeObjects', array(array($category), $startDate, $endDate));
+        if (is_a($eventsList, 'PEAR_Error')) {
+            return $eventsList;
+        }
+
+        $startDate = clone $startDate;
+        $startDate->hour = $startDate->min = $startDate->sec = 0;
+        $endDate = clone $endDate;
+        $endDate->hour = 23;
+        $endDate->min = $endDate->sec = 59;
+
+        $results = array();
+        foreach ($eventsList as $eventsListItem) {
+            $event = new Kronolith_Event_Horde($this, $eventsListItem);
+
+            /* Ignore events out of the period. */
+            if (
+                /* Starts after the period. */
+                $event->start->compareDateTime($endDate) > 0 ||
+                /* End before the period and doesn't recur. */
+                (!$event->recurs() &&
+                 $event->end->compareDateTime($startDate) < 0) ||
+                /* Recurs and ... */
+                ($event->recurs() &&
+                 /* ... has a recurrence end before the period. */
+                 ($event->recurrence->hasRecurEnd() &&
+                  $event->recurrence->recurEnd->compareDateTime($startDate) < 0))) {
+                continue;
+            }
+
+            Kronolith::addEvents($results, $event, $startDate,
+                                 $endDate, $showRecurrence);
+        }
+
+        return $results;
+    }
+
+    public function getEvent($eventId = null)
+    {
+    }
+
+}
diff --git a/kronolith/lib/Event/Horde.php b/kronolith/lib/Event/Horde.php
new file mode 100644 (file)
index 0000000..745af15
--- /dev/null
@@ -0,0 +1,74 @@
+<?php
+/**
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Jan Schneider <jan@horde.org>
+ * @package Kronolith
+ */
+class Kronolith_Event_Horde extends Kronolith_Event
+{
+    /**
+     * The API (application) of this event.
+     *
+     * @var string
+     */
+    private $_api;
+
+    /**
+     * Constructor.
+     *
+     * @param Kronolith_Driver $driver  The backend driver that this event is
+     *                                  stored in.
+     * @param mixed $eventObject        Backend specific event object
+     *                                  that this will represent.
+     */
+    public function __construct($driver, $eventObject = null)
+    {
+        $this->_api = $driver->api;
+        parent::__construct($driver, $eventObject);
+    }
+
+    public function fromDriver($event)
+    {
+        $eventStart = new Horde_Date($event['start']);
+        $eventEnd = new Horde_Date($event['end']);
+        $this->eventID = '_' . $this->_api . $event['id'];
+        $this->external = $this->_api;
+        $this->external_params = $event['params'];
+        $this->title = $event['title'];
+        $this->description = isset($event['description']) ? $event['description'] : '';
+        $this->start = $eventStart;
+        $this->end = $eventEnd;
+        $this->status = Kronolith::STATUS_FREE;
+
+        if (isset($event['recurrence'])) {
+            $recurrence = new Horde_Date_Recurrence($eventStart);
+            $recurrence->setRecurType($event['recurrence']['type']);
+            if (isset($event['recurrence']['end'])) {
+                $recurrence->setRecurEnd($event['recurrence']['end']);
+            }
+            if (isset($event['recurrence']['interval'])) {
+                $recurrence->setRecurInterval($event['recurrence']['interval']);
+            }
+            if (isset($event['recurrence']['count'])) {
+                $recurrence->setRecurCount($event['recurrence']['count']);
+            }
+            if (isset($event['recurrence']['days'])) {
+                $recurrence->setRecurOnDay($event['recurrence']['days']);
+            }
+            if (isset($event['recurrence']['exceptions'])) {
+                foreach ($event['recurrence']['exceptions'] as $exception) {
+                    $recurrence->addException(new Horde_Date($exception));
+                }
+            }
+            $this->recurrence = $recurrence;
+        }
+
+        $this->initialized = true;
+        $this->stored = true;
+    }
+
+}
index 0b15b13..35b1810 100644 (file)
@@ -554,96 +554,29 @@ class Kronolith
                                       $showRecurrence = true,
                                       $alarmsOnly = false, $showRemote = true)
     {
-        global $registry;
+        $results = array();
 
+        /* Internal calendars. */
         if (!isset($calendars)) {
             $calendars = $GLOBALS['display_calendars'];
         }
-        $kronolith_driver = Kronolith::getDriver();
-
-        $results = array();
+        $driver = Kronolith::getDriver();
         foreach ($calendars as $calendar) {
-            $kronolith_driver->open($calendar);
-            $events = $kronolith_driver->listEvents($startDate, $endDate, true);
+            $driver->open($calendar);
+            $events = $driver->listEvents($startDate, $endDate, true);
             if (!is_a($events, 'PEAR_Error')) {
                 Kronolith::mergeEvents($results, $events);
             }
         }
 
         if ($showRemote) {
-            /* Check for listTimeObjects */
+            /* Horde applications providing listTimeObjects. */
+            $driver = Kronolith::getDriver('Horde');
             foreach ($GLOBALS['display_external_calendars'] as $external_cal) {
-                list($api, $category) = explode('/', $external_cal, 2);
-                if (!isset($apis[$api])) {
-                    $apis[$api] = array();
-                }
-                if (!array_search($category, $apis[$api])) {
-                    $apis[$api][] = $category;
-                }
-            }
-            if (!empty($apis)) {
-                $endStamp = new Horde_Date(array('month' => $endDate->month,
-                                                 'mday' => $endDate->mday + 1,
-                                                 'year' => $endDate->year));
-                foreach ($apis as $api => $categories) {
-                    if ($registry->hasMethod($api . '/listTimeObjects')) {
-                        $eventsList = $registry->call($api . '/listTimeObjects', array($categories, $startDate, $endDate));
-                        if (is_a($eventsList, 'PEAR_Error')) {
-                            $GLOBALS['notification']->push($eventsList);
-                            continue;
-                        }
-                    }
-
-                    foreach ($eventsList as $eventsListItem) {
-                        $eventStart = new Horde_Date($eventsListItem['start']);
-                        $eventEnd = new Horde_Date($eventsListItem['end']);
-                        /* Ignore events out of our period. */
-                        if (
-                            /* Starts after the period. */
-                            $eventStart->compareDateTime($endDate) > 0 ||
-                            /* End before the period and doesn't recur. */
-                            (!isset($eventsListItem['recurrence']) &&
-                             $eventEnd->compareDateTime($startDate) < 0)) {
-                            continue;
-                        }
-
-                        $event = $kronolith_driver->getEvent();
-                        $event->eventID = '_' . $api . $eventsListItem['id'];
-                        $event->external = $api;
-                        $event->external_params = $eventsListItem['params'];
-                        $event->title = $eventsListItem['title'];
-                        $event->description = isset($eventsListItem['description']) ? $eventsListItem['description'] : '';
-                        $event->start = $eventStart;
-                        $event->end = $eventEnd;
-                        $event->status = Kronolith::STATUS_FREE;
-                        if (isset($eventsListItem['recurrence'])) {
-                            $recurrence = new Horde_Date_Recurrence($eventStart);
-                            $recurrence->setRecurType($eventsListItem['recurrence']['type']);
-                            if (isset($eventsListItem['recurrence']['end'])) {
-                                $recurrence->setRecurEnd($eventsListItem['recurrence']['end']);
-                                if ($recurrence->recurEnd->compareDateTime($startDate) < 0) {
-                                    continue;
-                                }
-                            }
-                            if (isset($eventsListItem['recurrence']['interval'])) {
-                                $recurrence->setRecurInterval($eventsListItem['recurrence']['interval']);
-                            }
-                            if (isset($eventsListItem['recurrence']['count'])) {
-                                $recurrence->setRecurCount($eventsListItem['recurrence']['count']);
-                            }
-                            if (isset($eventsListItem['recurrence']['days'])) {
-                                $recurrence->setRecurOnDay($eventsListItem['recurrence']['days']);
-                            }
-                            if (isset($eventsListItem['recurrence']['exceptions'])) {
-                                foreach ($eventsListItem['recurrence']['exceptions'] as $exception) {
-                                    $recurrence->addException(new Horde_Date($exception));
-                                }
-                            }
-                            $event->recurrence = $recurrence;
-                        }
-                        Kronolith::addEvents($results, $event, $startDate,
-                                             $endDate, $showRecurrence);
-                    }
+                $driver->open($external_cal);
+                $events = $driver->listEvents($startDate, $endDate, true);
+                if (!is_a($events, 'PEAR_Error')) {
+                    Kronolith::mergeEvents($results, $events);
                 }
             }
 
@@ -658,18 +591,19 @@ class Kronolith
             }
         }
 
-        /* Holidays */
+        /* Holidays. */
         if (!empty($GLOBALS['conf']['holidays']['enable'])) {
-            $dhDriver = Kronolith::getDriver('Holidays');
-            foreach (unserialize($GLOBALS['prefs']->getValue('holiday_drivers')) as $driver) {
-                $dhDriver->open($driver);
-                $events = $dhDriver->listEvents($startDate, $endDate, true);
+            $driver = Kronolith::getDriver('Holidays');
+            foreach (unserialize($GLOBALS['prefs']->getValue('holiday_drivers')) as $holiday) {
+                $driver->open($holiday);
+                $events = $driver->listEvents($startDate, $endDate, true);
                 if (!is_a($events, 'PEAR_Error')) {
                     Kronolith::mergeEvents($results, $events);
                 }
             }
         }
 
+        /* Sort events. */
         foreach ($results as $day => $devents) {
             if (count($devents)) {
                 uasort($devents, array('Kronolith', '_sortEventStartTime'));
@@ -2003,6 +1937,10 @@ class Kronolith
 
                 break;
 
+            case 'Horde':
+                $params['registry'] = $GLOBALS['registry'];
+                break;
+
             case 'Holidays':
                 $params['language'] = $GLOBALS['language'];
                 break;