Add Kronolith_Calendar#toHash() and Kronolith_Calendar_External_Tasks.
authorJan Schneider <jan@horde.org>
Tue, 7 Dec 2010 14:43:10 +0000 (15:43 +0100)
committerJan Schneider <jan@horde.org>
Tue, 7 Dec 2010 14:43:10 +0000 (15:43 +0100)
kronolith/lib/Ajax/Application.php
kronolith/lib/Calendar.php
kronolith/lib/Calendar/External.php
kronolith/lib/Calendar/External/Tasks.php [new file with mode: 0644]
kronolith/lib/Calendar/Holiday.php
kronolith/lib/Calendar/Internal.php
kronolith/lib/Calendar/Remote.php
kronolith/lib/Kronolith.php

index 11a9fc3..a067a0f 100644 (file)
@@ -800,15 +800,7 @@ class Kronolith_Ajax_Application extends Horde_Core_Ajax_Application
         }
         $calendar = $GLOBALS['all_calendars'][$this->_vars->cal];
         $tagger = Kronolith::getTagger();
-        $result->calendar = array(
-            'name' => (!$calendar->owner() ? '' : '[' . $GLOBALS['registry']->convertUsername($calendar->owner(), false) . '] ') . $calendar->name(),
-            'desc' => $calendar->description(),
-            'owner' => false,
-            'fg' => $calendar->foreground(),
-            'bg' => $calendar->background(),
-            'show' => false,
-            'edit' => $calendar->hasPermission(Horde_Perms::EDIT),
-            'tg' => array_values($tagger->getTags($this->_vars->cal, 'calendar')));
+        $result->calendar = $calendar->toHash();
         return $result;
     }
 
index a815cef..f0ca80e 100644 (file)
@@ -118,4 +118,20 @@ abstract class Kronolith_Calendar
      * @return boolean  True if this calendar should be displayed.
      */
     abstract public function display();
+
+    /**
+     * Returns a hash representing this calendar.
+     *
+     * @return array  A simple hash.
+     */
+    public function toHash()
+    {
+        return array(
+            'name'  => $this->name(),
+            'desc'  => $this->description(),
+            'owner' => true,
+            'fg'    => $this->foreground(),
+            'bg'    => $this->background(),
+        );
+    }
 }
index ebd38b0..8074113 100644 (file)
@@ -75,4 +75,17 @@ class Kronolith_Calendar_External extends Kronolith_Calendar
     {
         return $this->_api;
     }
+
+    /**
+     * Returns a hash representing this calendar.
+     *
+     * @return array  A simple hash.
+     */
+    public function toHash()
+    {
+        $hash = parent::toHash();
+        $hash['api']  = $GLOBALS['registry']->get('name', $GLOBALS['registry']->hasInterface($this->api()));
+        $hash['show'] = in_array($this->_api . '/' . $this->_name, $GLOBALS['display_external_calendars']);
+        return $hash;
+    }
 }
diff --git a/kronolith/lib/Calendar/External/Tasks.php b/kronolith/lib/Calendar/External/Tasks.php
new file mode 100644 (file)
index 0000000..42b8e9b
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Kronolith_Calendar_External_Tasks defines an API for single task lists.
+ *
+ * Copyright 2010 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_Calendar_External_Tasks extends Kronolith_Calendar_External
+{
+    /**
+     * The share of this task list.
+     *
+     * @var Horde_Share_Object
+     */
+    protected $_share;
+
+    /**
+     * Returns a hash representing this calendar.
+     *
+     * @return array  A simple hash.
+     */
+    public function toHash()
+    {
+        $owner = $GLOBALS['registry']->getAuth() &&
+            $this->_share->get('owner') == $GLOBALS['registry']->getAuth();
+
+        $hash = parent::toHash();
+        $hash['name']  = ($owner || !$this->_share->get('owner') ? '' : '[' . $GLOBALS['registry']->convertUsername($this->_share->get('owner'), false) . '] ')
+            . $this->_share->get('name');
+        $hash['desc']  = $this->_share->get('desc');
+        $hash['owner'] = $owner;
+        $hash['fg']    = Kronolith::foregroundColor($this->_share);
+        $hash['bg']    = Kronolith::backgroundColor($this->_share);
+        $hash['show']  = in_array('tasks/' . $this->_share->getName(), $GLOBALS['display_external_calendars']);
+        $hash['edit']  = $this->_share->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT);
+        $hash['sub']   = Horde::url($GLOBALS['registry']->get('webroot', 'horde') . ($GLOBALS['conf']['urls']['pretty'] == 'rewrite' ? '/rpc/nag/' : '/rpc.php/nag/'), true, -1)
+            . ($this->_share->get('owner') ? $this->_share->get('owner') : '-system-') . '/'
+            . $this->_share->getName() . '.ics';
+        if ($owner) {
+            $hash['perms'] = Kronolith::permissionToJson($this->_share->getPermission());
+        }
+
+        return $hash;
+    }
+}
index ab4f960..f5a3192 100644 (file)
@@ -52,6 +52,18 @@ class Kronolith_Calendar_Holiday extends Kronolith_Calendar
      */
     public function display()
     {
-        return true;
+        return in_array($this->_driver['id'], $GLOBALS['display_holidays']);
+    }
+
+    /**
+     * Returns a hash representing this calendar.
+     *
+     * @return array  A simple hash.
+     */
+    public function toHash()
+    {
+        $hash = parent::toHash();
+        $hash['show'] = in_array($this->_driver['id'], $GLOBALS['display_holidays']);
+        return $hash;
     }
 }
index 3fb83d7..488d68f 100644 (file)
@@ -120,4 +120,34 @@ class Kronolith_Calendar_Internal extends Kronolith_Calendar
     {
         return $this->_share;
     }
+
+    /**
+     * Returns a hash representing this calendar.
+     *
+     * @return array  A simple hash.
+     */
+    public function toHash()
+    {
+        $id = $this->_share->getName();
+        $owner = $GLOBALS['registry']->getAuth() &&
+            $this->owner() == $GLOBALS['registry']->getAuth();
+
+        $hash = parent::toHash();
+        $hash['name']  = ($owner || !$this->owner() ? '' : '[' . $GLOBALS['registry']->convertUsername($this->owner(), false) . '] ')
+            . $this->name();
+        $hash['owner'] = $owner;
+        $hash['show']  = in_array($id, $GLOBALS['display_calendars']);
+        $hash['edit']  = $this->hasPermission(Horde_Perms::EDIT);
+        $hash['sub']   = Horde::url($GLOBALS['registry']->get('webroot', 'horde') . ($GLOBALS['conf']['urls']['pretty'] == 'rewrite' ? '/rpc/kronolith/' : '/rpc.php/kronolith/'), true, -1)
+            . ($this->owner() ? $this->owner() : '-system-') . '/'
+            . $id . '.ics';
+        $hash['feed']  = (string)Kronolith::feedUrl($id);
+        $hash['embed'] = Kronolith::embedCode($id);
+        $hash['tg']    = array_values(Kronolith::getTagger()->getTags($id, 'calendar'));
+        if ($owner) {
+            $hash['perms'] = Kronolith::permissionToJson($this->_share->getPermission());
+        }
+
+        return $hash;
+    }
 }
index 85cb88d..b3106d1 100644 (file)
@@ -137,7 +137,7 @@ class Kronolith_Calendar_Remote extends Kronolith_Calendar
      */
     public function display()
     {
-        return true;
+        return in_array($this->_url, $GLOBALS['display_remote_calendars']);
     }
 
     /**
@@ -162,4 +162,18 @@ class Kronolith_Calendar_Remote extends Kronolith_Calendar
         }
         return array();
     }
+
+    /**
+     * Returns a hash representing this calendar.
+     *
+     * @return array  A simple hash.
+     */
+    public function toHash()
+    {
+        return array_merge(
+            parent::toHash(),
+            array('show' => in_array($this->_url, $GLOBALS['display_remote_calendars'])),
+            $this->credentials()
+        );
+    }
 }
index e659ae8..2a75fe6 100644 (file)
@@ -124,8 +124,6 @@ class Kronolith
             }
         }
 
-        $tagger = self::getTagger();
-
         /* Variables used in core javascript files. */
         $code['conf'] = array(
             'URI_AJAX' => (string)Horde::getServiceLink('ajax', 'kronolith'),
@@ -186,36 +184,19 @@ class Kronolith
             $code['conf']['tasks'] = $registry->tasks->ajaxDefaults();
         }
 
-        $subscriptionCals = Horde::url($registry->get('webroot', 'horde') . ($GLOBALS['conf']['urls']['pretty'] == 'rewrite' ? '/rpc/kronolith/' : '/rpc.php/kronolith/'), true, -1);
-        $subscriptionTasks = Horde::url($registry->get('webroot', 'horde') . ($GLOBALS['conf']['urls']['pretty'] == 'rewrite' ? '/rpc/nag/' : '/rpc.php/nag/'), true, -1);
-
-        // Calendars
+        // Calendars. Do some twisting to sort own calendar before shared
+        // calendars.
         foreach (array(true, false) as $my) {
             foreach ($GLOBALS['all_calendars'] as $id => $calendar) {
-                if ($calendar->owner() != $GLOBALS['registry']->getAuth() &&
-                    !empty($GLOBALS['conf']['share']['hidden']) &&
+                if (!empty($GLOBALS['conf']['share']['hidden']) &&
+                    $calendar->owner() != $GLOBALS['registry']->getAuth() &&
                     !in_array($id, $GLOBALS['display_calendars'])) {
                     continue;
                 }
                 $owner = $GLOBALS['registry']->getAuth() &&
                     $calendar->owner() == $GLOBALS['registry']->getAuth();
                 if (($my && $owner) || (!$my && !$owner)) {
-                    $code['conf']['calendars']['internal'][$id] = array(
-                        'name' => ($owner || !$calendar->owner() ? '' : '[' . $GLOBALS['registry']->convertUsername($calendar->owner(), false) . '] ')
-                            . $calendar->name(),
-                        'desc' => $calendar->description(),
-                        'owner' => $owner,
-                        'fg' => $calendar->foreground(),
-                        'bg' => $calendar->background(),
-                        'show' => in_array($id, $GLOBALS['display_calendars']),
-                        'edit' => $calendar->hasPermission(Horde_Perms::EDIT),
-                        'sub' => $subscriptionCals . ($calendar->owner() ? $calendar->owner() : '-system-') . '/' . $id . '.ics',
-                        'feed' => (string)Kronolith::feedUrl($id),
-                        'embed' => self::embedCode($id),
-                        'tg' => array_values($tagger->getTags($id, 'calendar')));
-                    if ($owner) {
-                        $code['conf']['calendars']['internal'][$id]['perms'] = self::permissionToJson($calendar->share()->getPermission());
-                    }
+                    $code['conf']['calendars']['internal'][$id] = $calendar->toHash();
                 }
             }
 
@@ -224,27 +205,15 @@ class Kronolith
                 continue;
             }
             foreach ($registry->tasks->listTasklists($my, Horde_Perms::SHOW) as $id => $tasklist) {
-                if ($tasklist->get('owner') != $GLOBALS['registry']->getAuth() &&
-                    !empty($GLOBALS['conf']['share']['hidden']) &&
+                if (!empty($GLOBALS['conf']['share']['hidden']) &&
+                    $tasklist->get('owner') != $GLOBALS['registry']->getAuth() &&
                     !in_array('tasks/' . $id, $GLOBALS['display_external_calendars'])) {
                     continue;
                 }
                 $owner = $GLOBALS['registry']->getAuth() &&
                     $tasklist->get('owner') == $GLOBALS['registry']->getAuth();
                 if (($my && $owner) || (!$my && !$owner)) {
-                    $code['conf']['calendars']['tasklists']['tasks/' . $id] = array(
-                        'name' => ($owner || !$tasklist->get('owner') ? '' : '[' . $GLOBALS['registry']->convertUsername($tasklist->get('owner'), false) . '] ')
-                            . $tasklist->get('name'),
-                        'desc' => $tasklist->get('desc'),
-                        'owner' => $owner,
-                        'fg' => self::foregroundColor($tasklist),
-                        'bg' => self::backgroundColor($tasklist),
-                        'show' => in_array('tasks/' . $id, $GLOBALS['display_external_calendars']),
-                        'edit' => $tasklist->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::EDIT),
-                        'sub' => $subscriptionTasks . ($tasklist->get('owner') ? $tasklist->get('owner') : '-system-') . '/' . $tasklist->getName() . '.ics');
-                    if ($owner) {
-                        $code['conf']['calendars']['tasklists']['tasks/' . $id]['perms'] = self::permissionToJson($tasklist->getPermission());
-                    }
+                    $code['conf']['calendars']['tasklists']['tasks/' . $id] = $GLOBALS['all_external_calendars']['tasks/' . $id]->toHash();
                 }
             }
         }
@@ -258,33 +227,17 @@ class Kronolith
                 !in_array($id, $GLOBALS['display_external_calendars'])) {
                 continue;
             }
-            $code['conf']['calendars']['external'][$id] = array(
-                'name' => $calendar->name(),
-                'fg' => $calendar->foreground(),
-                'bg' => $calendar->background(),
-                'api' => $registry->get('name', $registry->hasInterface($calendar->api())),
-                'show' => in_array($id, $GLOBALS['display_external_calendars']));
+            $code['conf']['calendars']['external'][$id] = $calendar->toHash();
         }
 
         // Remote calendars
         foreach ($GLOBALS['all_remote_calendars'] as $url => $calendar) {
-            $code['conf']['calendars']['remote'][$url] = array_merge(
-                array('name' => $calendar->name(),
-                      'desc' => $calendar->description(),
-                      'owner' => true,
-                      'fg' => $calendar->foreground(),
-                      'bg' => $calendar->background(),
-                      'show' => in_array($url, $GLOBALS['display_remote_calendars'])),
-                $calendar->credentials());
+            $code['conf']['calendars']['remote'][$url] = $calendar->toHash();
         }
 
         // Holidays
         foreach ($GLOBALS['all_holidays'] as $id => $calendar) {
-            $code['conf']['calendars']['holiday'][$id] = array(
-                'name' => $calendar->name(),
-                'fg' => $calendar->foreground(),
-                'bg' => $calendar->background(),
-                'show' => in_array($id, $GLOBALS['display_holidays']));
+            $code['conf']['calendars']['holiday'][$id] = $calendar->toHash();
         }
 
         /* Gettext strings used in core javascript files. */
@@ -1102,10 +1055,16 @@ class Kronolith
         $GLOBALS['prefs']->setValue('holiday_drivers', serialize($GLOBALS['display_holidays']));
 
         /* Get a list of external calendars. */
+        $has_tasks = $GLOBALS['registry']->hasInterface('tasks');
+        $tasklists = $has_tasks
+            ? $GLOBALS['registry']->tasks->listTasklists()
+            : array();
         $GLOBALS['all_external_calendars'] = array();
         if ($GLOBALS['session']->exists('kronolith', 'all_external_calendars')) {
             foreach ($GLOBALS['session']->get('kronolith', 'all_external_calendars') as $calendar) {
-                $GLOBALS['all_external_calendars'][$calendar['a'] . '/' . $calendar['n']] = new Kronolith_Calendar_External(array('api' => $calendar['a'], 'name' => $calendar['d']));
+                $GLOBALS['all_external_calendars'][$calendar['a'] . '/' . $calendar['n']] = $calendar['a'] == 'tasks'
+                    ? new Kronolith_Calendar_External_Tasks(array('api' => $calendar['a'], 'name' => $calendar['d'], 'share' => $tasklists[$calendar['n']]))
+                    : new Kronolith_Calendar_External(array('api' => $calendar['a'], 'name' => $calendar['d']));
             }
         } else {
             $apis = array_unique($GLOBALS['registry']->listAPIs());
@@ -1123,7 +1082,9 @@ class Kronolith
                 }
 
                 foreach ($categories as $name => $description) {
-                    $GLOBALS['all_external_calendars'][$api . '/' . $name] = new Kronolith_Calendar_External(array('api' => $api, 'name' => $description));
+                    $GLOBALS['all_external_calendars'][$api . '/' . $name] = $api == 'tasks'
+                        ? new Kronolith_Calendar_External_Tasks(array('api' => $api, 'name' => $description, 'share' => $tasklists[$name]))
+                        : new Kronolith_Calendar_External(array('api' => $api, 'name' => $description));
                     $ext_cals[] = array(
                         'a' => $api,
                         'n' => $name,