From 49b8b7ef4309d857319bdd50e4d74c432ab33d3a Mon Sep 17 00:00:00 2001 From: Jan Schneider Date: Tue, 15 Dec 2009 18:10:04 +0100 Subject: [PATCH] Fix calendar color hack. --- kronolith/lib/Api.php | 6 ++--- kronolith/lib/Driver.php | 50 +++++++++++++++++++++++++++++++++++---- kronolith/lib/Driver/Ical.php | 19 +++++++++++++-- kronolith/lib/Driver/Kolab.php | 28 ++++++++++++++++++---- kronolith/lib/Driver/Sql.php | 17 +++++++++++++ kronolith/lib/Event.php | 17 ++++--------- kronolith/lib/Event/Sql.php | 16 +++++++++---- kronolith/lib/Kronolith.php | 13 +++++----- kronolith/lib/Resource/Single.php | 1 - 9 files changed, 126 insertions(+), 41 deletions(-) diff --git a/kronolith/lib/Api.php b/kronolith/lib/Api.php index 9d0bd61f6..a8c4e3540 100644 --- a/kronolith/lib/Api.php +++ b/kronolith/lib/Api.php @@ -322,7 +322,6 @@ class Kronolith_Api extends Horde_Registry_Api if (is_a($content, 'Horde_iCalendar_vevent')) { $event = $kronolith_driver->getEvent(); $event->fromiCalendar($content); - $event->calendar = $calendar; $uid = $event->uid; // Remove from uids_remove list so we won't delete in the // end. @@ -608,9 +607,8 @@ class Kronolith_Api extends Horde_Registry_Api if (is_a($content, 'Horde_iCalendar_vevent')) { $event = $kronolith_driver->getEvent(); $event->fromiCalendar($content); - $event->calendar = $calendar; - // Check if the entry already exists in the data source, first - // by UID. + // Check if the entry already exists in the data source, + // first by UID. $uid = $event->uid; $existing_event = $kronolith_driver->getByUID($uid, array($calendar)); if (!is_a($existing_event, 'PEAR_Error')) { diff --git a/kronolith/lib/Driver.php b/kronolith/lib/Driver.php index a69d7f37a..680fa0c68 100644 --- a/kronolith/lib/Driver.php +++ b/kronolith/lib/Driver.php @@ -15,18 +15,32 @@ class Kronolith_Driver { /** - * A hash containing any parameters for the current driver. + * The current calendar. * - * @var array + * @var string */ - protected $_params = array(); + public $calendar; /** - * The current calendar. + * The HTML background color to be used for this event. * * @var string */ - public $calendar; + public $backgroundColor = '#ddd'; + + /** + * The HTML foreground color to be used for this event. + * + * @var string + */ + public $foregroundColor = '#000'; + + /** + * A hash containing any parameters for the current driver. + * + * @var array + */ + protected $_params = array(); /** * An error message to throw when something is wrong. @@ -76,12 +90,38 @@ class Kronolith_Driver $this->_params[$param] = $value; } + /** + * Selects a calendar as the currently opened calendar. + * + * @param string $calendar A calendar identifier. + */ public function open($calendar) { $this->calendar = $calendar; } /** + * Returns the background color of the current calendar. + * + * @return string The calendar color. + */ + public function backgroundColor() + { + return '#dddddd'; + } + + /** + * Returns the colors of the current calendar. + * + * @return array The calendar background and foreground color. + */ + public function colors() + { + $color = $this->backgroundColor(); + return array($color, Kronolith::foregroundColor($color)); + } + + /** * Renames a calendar. * * @param string $from The current name of the calendar. diff --git a/kronolith/lib/Driver/Ical.php b/kronolith/lib/Driver/Ical.php index badcfda31..8e31006de 100644 --- a/kronolith/lib/Driver/Ical.php +++ b/kronolith/lib/Driver/Ical.php @@ -30,6 +30,23 @@ class Kronolith_Driver_Ical extends Kronolith_Driver */ private $_cache = array(); + /** + * Returns the background color of the current calendar. + * + * @return string The calendar color. + */ + public function backgroundColor() + { + foreach ($GLOBALS['all_remote_calendars'] as $calendar) { + if ($calendar['url'] == $this->calendar) { + return empty($calendar['color']) + ? '#dddddd' + : $calendar['color']; + } + } + return '#dddddd'; + } + public function listAlarms($date, $fullevent = false) { return array(); @@ -87,7 +104,6 @@ class Kronolith_Driver_Ical extends Kronolith_Driver $event = new Kronolith_Event_Ical($this); $event->status = Kronolith::STATUS_FREE; $event->fromiCalendar($component); - $event->calendar = $this->calendar; // Force string so JSON encoding is consistent across drivers. $event->id = 'ical' . $i; @@ -152,7 +168,6 @@ class Kronolith_Driver_Ical extends Kronolith_Driver $event = new Kronolith_Event_Ical($this); $event->status = Kronolith::STATUS_FREE; $event->fromiCalendar($components[$eventId]); - $event->calendar = $this->calendar; $event->id = 'ical' . $eventId; return $event; diff --git a/kronolith/lib/Driver/Kolab.php b/kronolith/lib/Driver/Kolab.php index 1da76c84e..3252d12eb 100644 --- a/kronolith/lib/Driver/Kolab.php +++ b/kronolith/lib/Driver/Kolab.php @@ -58,16 +58,34 @@ class Kronolith_Driver_Kolab extends Kronolith_Driver } /** - * Change current calendar + * Selects a calendar as the currently opened calendar. + * + * @param string $calendar A calendar identifier. */ public function open($calendar) { - if ($this->calendar != $calendar) { - $this->calendar = $calendar; - $this->reset(); + if ($this->calendar == $calendar) { + return; } + $this->calendar = $calendar; + $this->reset(); + } - return true; + /** + * Returns the background color of the current calendar. + * + * @return string The calendar color. + */ + public function backgroundColor() + { + if (isset($GLOBALS['all_calendars'][$this->calendar])) { + $share = $GLOBALS['all_calendars'][$this->calendar]; + $color = $share->get('color'); + if (!empty($color)) { + return $color; + } + } + return '#dddddd'; } /** diff --git a/kronolith/lib/Driver/Sql.php b/kronolith/lib/Driver/Sql.php index a85557fa5..a43dadf96 100644 --- a/kronolith/lib/Driver/Sql.php +++ b/kronolith/lib/Driver/Sql.php @@ -47,6 +47,23 @@ class Kronolith_Driver_Sql extends Kronolith_Driver */ protected $_eventClass = 'Kronolith_Event_Sql'; + /** + * Returns the background color of the current calendar. + * + * @return string The calendar color. + */ + public function backgroundColor() + { + if (isset($GLOBALS['all_calendars'][$this->calendar])) { + $share = $GLOBALS['all_calendars'][$this->calendar]; + $color = $share->get('color'); + if (!empty($color)) { + return $color; + } + } + return '#dddddd'; + } + public function listAlarms($date, $fullevent = false) { $allevents = $this->listEvents($date, null, false, true); diff --git a/kronolith/lib/Event.php b/kronolith/lib/Event.php index acaaaa5d6..76c9d1fe6 100644 --- a/kronolith/lib/Event.php +++ b/kronolith/lib/Event.php @@ -226,7 +226,7 @@ abstract class Kronolith_Event * * @var string */ - protected $_foregroundColor = '#000'; + protected $_foregroundColor = '#000000'; /** * The VarRenderer class to use for printing select elements. @@ -253,21 +253,12 @@ abstract class Kronolith_Event public function __construct($driver, $eventObject = null) { $this->calendar = $driver->calendar; - // FIXME: Move color definitions anywhere else. - if (!empty($this->calendar) && - isset($GLOBALS['all_calendars'][$this->calendar])) { - $share = $GLOBALS['all_calendars'][$this->calendar]; - $backgroundColor = $share->get('color'); - if (!empty($backgroundColor)) { - $this->_backgroundColor = $backgroundColor; - $this->_foregroundColor = Horde_Image::brightness($this->_backgroundColor) < 128 ? '#fff' : '#000'; - } - } + list($this->_backgroundColor, $this->_foregroundColor) = $driver->colors(); - if ($eventObject !== null) { + if (!is_null($eventObject)) { + $this->fromDriver($eventObject); /* Get tags */ - $this->fromDriver($eventObject); $tagger = Kronolith::getTagger(); $this->tags = $tagger->getTags($this->uid, 'event'); diff --git a/kronolith/lib/Event/Sql.php b/kronolith/lib/Event/Sql.php index f7c2d048a..73d5d4103 100644 --- a/kronolith/lib/Event/Sql.php +++ b/kronolith/lib/Event/Sql.php @@ -34,16 +34,22 @@ class Kronolith_Event_Sql extends Kronolith_Event */ public function __construct($driver, $eventObject = null) { - static $alarm; - /* Set default alarm value. */ if (!isset($alarm) && isset($GLOBALS['prefs'])) { - $alarm = $GLOBALS['prefs']->getValue('default_alarm'); + $this->alarm = $GLOBALS['prefs']->getValue('default_alarm'); } - $this->alarm = $alarm; - parent::__construct($driver, $eventObject); + + if (!empty($this->calendar) && + isset($GLOBALS['all_calendars'][$this->calendar])) { + $share = $GLOBALS['all_calendars'][$this->calendar]; + $backgroundColor = $share->get('color'); + if (!empty($backgroundColor)) { + $this->_backgroundColor = $backgroundColor; + $this->_foregroundColor = Horde_Image::brightness($this->_backgroundColor) < 128 ? '#fff' : '#000'; + } + } } public function fromDriver($SQLEvent) diff --git a/kronolith/lib/Kronolith.php b/kronolith/lib/Kronolith.php index 0b6f5ff12..6a12db318 100644 --- a/kronolith/lib/Kronolith.php +++ b/kronolith/lib/Kronolith.php @@ -268,7 +268,6 @@ class Kronolith // Remote calendars foreach ($GLOBALS['all_remote_calendars'] as $calendar) { - $code['conf']['calendars']['remote'][$calendar['url']] = array_merge( array('name' => $calendar['name'], 'desc' => isset($calendar['desc']) ? $calendar['desc'] : '', @@ -2325,16 +2324,18 @@ class Kronolith } /** - * Returns the foreground color for a calendar. + * Returns the foreground color for a calendar or a background color. + * + * @param array|Horde_Share_Object|string $calendar A color string, a + * calendar share or a + * hash from a remote + * calender definition. * - * @param array|Horde_Share_Object $calendar A calendar share or a hash - * from a remote calender - * definition. * @return string A HTML color code. */ public static function foregroundColor($calendar) { - return Horde_Image::brightness(self::backgroundColor($calendar)) < 128 ? '#fff' : '#000'; + return Horde_Image::brightness(is_string($calendar) ? $calendar : self::backgroundColor($calendar)) < 128 ? '#fff' : '#000'; } /** diff --git a/kronolith/lib/Resource/Single.php b/kronolith/lib/Resource/Single.php index a5a618da7..4a6283191 100644 --- a/kronolith/lib/Resource/Single.php +++ b/kronolith/lib/Resource/Single.php @@ -94,7 +94,6 @@ class Kronolith_Resource_Single extends Kronolith_Resource_Base } else { /* Create a new event */ $e = $driver->getEvent(); - $e->calendar = $this->get('calendar'); $e->fromiCalendar($event->toiCalendar(new Horde_iCalendar('2.0'))); $e->save(); } -- 2.11.0