Fix calendar color hack.
authorJan Schneider <jan@horde.org>
Tue, 15 Dec 2009 17:10:04 +0000 (18:10 +0100)
committerJan Schneider <jan@horde.org>
Tue, 15 Dec 2009 17:10:04 +0000 (18:10 +0100)
kronolith/lib/Api.php
kronolith/lib/Driver.php
kronolith/lib/Driver/Ical.php
kronolith/lib/Driver/Kolab.php
kronolith/lib/Driver/Sql.php
kronolith/lib/Event.php
kronolith/lib/Event/Sql.php
kronolith/lib/Kronolith.php
kronolith/lib/Resource/Single.php

index 9d0bd61..a8c4e35 100644 (file)
@@ -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')) {
index a69d7f3..680fa0c 100644 (file)
 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.
index badcfda..8e31006 100644 (file)
@@ -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;
index 1da76c8..3252d12 100644 (file)
@@ -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';
     }
 
     /**
index a85557f..a43dadf 100644 (file)
@@ -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);
index acaaaa5..76c9d1f 100644 (file)
@@ -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');
 
index f7c2d04..73d5d41 100644 (file)
@@ -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)
index 0b6f5ff..6a12db3 100644 (file)
@@ -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';
     }
 
     /**
index a5a618d..4a62831 100644 (file)
@@ -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();
         }