add quickAdd API method
authorChuck Hagenbuch <chuck@horde.org>
Fri, 14 Aug 2009 20:38:10 +0000 (16:38 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Mon, 17 Aug 2009 02:04:57 +0000 (22:04 -0400)
kronolith/lib/Api.php

index da35a5d..efbfeb8 100644 (file)
@@ -667,6 +667,58 @@ class Kronolith_Api extends Horde_Registry_Api
     }
 
     /**
+     * Imports an event parsed from a string.
+     *
+     * @param string $text      The text to parse into an event
+     * @param string $calendar  The calendar into which the event will be
+     *                          imported.  If 'null', the user's default
+     *                          calendar will be used.
+     *
+     * @return array  The UID of all events that were added.
+     */
+    public function quickAdd($text, $calendar = null)
+    {
+        $no_maint = true;
+        require_once dirname(__FILE__) . '/base.php';
+        global $kronolith_shares;
+
+        if (!isset($calendar)) {
+            $calendar = Kronolith::getDefaultCalendar(PERMS_EDIT);
+        }
+        if (!array_key_exists($calendar,
+            Kronolith::listCalendars(false, PERMS_EDIT))) {
+            return PEAR::raiseError(_("Permission Denied"));
+        }
+
+        $text = trim($text);
+        list($title, $description) = explode($text, "\n", 2);
+        $title = trim($title);
+        $description = trim($description);
+
+        $dateParser = Horde_Date_Parser::factory();
+        $r = $dateParser->parse($title, array('return' => 'result'));
+        if (! ($d = $r->guess())) {
+            throw new Horde_Exception(json_encode(array('name' => $title, 'description' => $description)));
+        }
+
+        $title = $r->untaggedText();
+        $start = $d->timestamp();
+
+        $kronolith_driver = Kronolith::getDriver(null, $calendar);
+        $event = $kronolith_driver->getEvent();
+        $event->setTitle($title);
+        $event->setDescription($description);
+        $event->start = $d;
+        $event->end = $d->add(array('hour' => 1));
+
+        $eventId = $event->save();
+        if (is_a($eventId, 'PEAR_Error')) {
+            return $eventId;
+        }
+        return $event->getUID();
+    }
+
+    /**
      * Exports an event, identified by UID, in the requested content type.
      *
      * @param string $uid         Identify the event to export.