Add a FacebookEvents driver for timeobjects. FB does some weird things
authorMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 8 Jun 2009 17:33:52 +0000 (13:33 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 8 Jun 2009 17:33:52 +0000 (13:33 -0400)
with it's timestamps, so feedback on if the event times show up
correctly for people in various timezones would be welcome...

timeobjects/lib/Driver/FacebookEvents.php [new file with mode: 0644]
timeobjects/lib/api.php

diff --git a/timeobjects/lib/Driver/FacebookEvents.php b/timeobjects/lib/Driver/FacebookEvents.php
new file mode 100644 (file)
index 0000000..911da1d
--- /dev/null
@@ -0,0 +1,94 @@
+<?php
+/**
+ * TimeObjects driver for exposing a user's Facebook Events via the
+ * listTimeObjects API.
+ *
+ *
+ */
+class TimeObjects_Driver_FacebookEvents
+{
+    private $_fb_session;
+
+    public function ensure()
+    {
+        if (!$GLOBALS['conf']['facebook']['enabled']) {
+            return false;
+        }
+
+        $fbp = unserialize($GLOBALS['prefs']->getValue('facebook'));
+        if (empty($fbp['uid']) || empty($fbp['sid'])) {
+            return false;
+        } else {
+            $this->_fb_session = $fbp;
+        }
+
+        return true;
+    }
+
+    /**
+     *
+     * @param mixed $start  The start time of the period
+     * @param mixed $time   The end time of the period
+     *
+     * @return array of listTimeObjects arrays.
+     */
+    public function listTimeObjects($start = null, $time = null)
+    {
+        $fb = $this->_getFacebook();
+        $events = $fb->events->get();
+        $objects = array();
+        foreach ($events as $event) {
+            // FB s*cks. This may be right, or it may be wrong, or they may
+            // change it, who knows.
+            $event['start_time'] -= 21600; //60 * 60 * 6;
+            $start = new Horde_Date($event['start_time'], 'America/Los_Angeles');
+            $start->setTimezone($GLOBALS['prefs']->getValue('timezone', date_default_timezone_get()));
+            $event['end_time'] -= 21600;
+            $end = new Horde_Date($event['end_time'], 'America/Los_Angeles');
+            $end->setTimezone($GLOBALS['prefs']->getValue('timezone', date_default_timezone_get()));
+
+            $objects[] = array('id' => $event['eid'],
+                               'title' => $event['name'] . ' - ' . $event['tagline'],
+                               'start' => sprintf('%d-%02d-%02dT%02d:%02d:00',
+                                                  $start->year,
+                                                  $start->month,
+                                                  $start->mday,
+                                                  $start->hour,
+                                                  $start->min),
+                               'end' => sprintf('%d-%02d-%02dT%02d:%02d:00',
+                                                  $end->year,
+                                                  $end->month,
+                                                  $end->mday,
+                                                  $end->hour,
+                                                  $end->min),
+                               'recurrence' => Horde_Date_Recurrence::RECUR_NONE,
+                               'params' => array(),
+                               'icon' => $event['pic_small']
+                              );
+        }
+
+        return $objects;
+    }
+
+    private function _getFacebook()
+    {
+        global $conf;
+
+        if (empty($this->_fb_session['uid']) || empty($this->_fb_session['sid'])) {
+            if (!$this->ensure()) {
+                throw new TimeObjects_Exception('Cannot load Facebook object.');
+            }
+        }
+
+        $context = array('http_client' => new Horde_Http_Client(),
+                         'http_request' => new Horde_Controller_Request_Http());
+        $facebook = new Horde_Service_Facebook($conf['facebook']['key'],
+                                               $conf['facebook']['secret'],
+                                               $context);
+        $facebook->auth->setUser($this->_fb_session['uid'],
+                                        $this->_fb_session['sid'],
+                                        0);
+       return $facebook;
+
+    }
+}
\ No newline at end of file
index e191a24..203df4a 100644 (file)
@@ -32,12 +32,19 @@ function _timeobjects_listTimeObjectCategories()
     // @TODO: Probably want to iterate the driver directory
     //        and dynamically build this list and/or maybe provide
     //        a $conf[] setting to explicitly disable certain drivers?
+    $drivers = array();
+
     $drv = TimeObjects_Driver::factory('Weatherdotcom');
     if ($drv->ensure()) {
-        return array('Weatherdotcom' => _("Weather"));
-    } else {
-        return array();
+        $drivers['Weatherdotcom'] = _("Weather");
     }
+
+    $drv = TimeObjects_Driver::factory('FacebookEvents');
+    if ($drv->ensure()) {
+        $drivers['FacebookEvents'] = _("Facebook Events");
+    }
+
+    return $drivers;
 }
 
 /**