From 2f23beeb4e047066c78aa7450b12a86b957333ff Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Tue, 7 Dec 2010 15:43:10 +0100 Subject: [PATCH] Add Kronolith_Calendar#toHash() and Kronolith_Calendar_External_Tasks. --- kronolith/lib/Ajax/Application.php | 10 +--- kronolith/lib/Calendar.php | 16 ++++++ kronolith/lib/Calendar/External.php | 13 +++++ kronolith/lib/Calendar/External/Tasks.php | 50 +++++++++++++++++++ kronolith/lib/Calendar/Holiday.php | 14 +++++- kronolith/lib/Calendar/Internal.php | 30 ++++++++++++ kronolith/lib/Calendar/Remote.php | 16 +++++- kronolith/lib/Kronolith.php | 81 ++++++++----------------------- 8 files changed, 159 insertions(+), 71 deletions(-) create mode 100644 kronolith/lib/Calendar/External/Tasks.php diff --git a/kronolith/lib/Ajax/Application.php b/kronolith/lib/Ajax/Application.php index 11a9fc318..a067a0f8f 100644 --- a/kronolith/lib/Ajax/Application.php +++ b/kronolith/lib/Ajax/Application.php @@ -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; } diff --git a/kronolith/lib/Calendar.php b/kronolith/lib/Calendar.php index a815cef54..f0ca80e92 100644 --- a/kronolith/lib/Calendar.php +++ b/kronolith/lib/Calendar.php @@ -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(), + ); + } } diff --git a/kronolith/lib/Calendar/External.php b/kronolith/lib/Calendar/External.php index ebd38b061..807411330 100644 --- a/kronolith/lib/Calendar/External.php +++ b/kronolith/lib/Calendar/External.php @@ -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 index 000000000..42b8e9b4a --- /dev/null +++ b/kronolith/lib/Calendar/External/Tasks.php @@ -0,0 +1,50 @@ + + * @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; + } +} diff --git a/kronolith/lib/Calendar/Holiday.php b/kronolith/lib/Calendar/Holiday.php index ab4f96010..f5a319281 100644 --- a/kronolith/lib/Calendar/Holiday.php +++ b/kronolith/lib/Calendar/Holiday.php @@ -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; } } diff --git a/kronolith/lib/Calendar/Internal.php b/kronolith/lib/Calendar/Internal.php index 3fb83d747..488d68fb7 100644 --- a/kronolith/lib/Calendar/Internal.php +++ b/kronolith/lib/Calendar/Internal.php @@ -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; + } } diff --git a/kronolith/lib/Calendar/Remote.php b/kronolith/lib/Calendar/Remote.php index 85cb88d37..b3106d1ad 100644 --- a/kronolith/lib/Calendar/Remote.php +++ b/kronolith/lib/Calendar/Remote.php @@ -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() + ); + } } diff --git a/kronolith/lib/Kronolith.php b/kronolith/lib/Kronolith.php index e659ae874..2a75fe6bb 100644 --- a/kronolith/lib/Kronolith.php +++ b/kronolith/lib/Kronolith.php @@ -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, -- 2.11.0