Set colors per calendar (Request #7480).
authorJan Schneider <jan@horde.org>
Tue, 3 Feb 2009 18:55:26 +0000 (19:55 +0100)
committerJan Schneider <jan@horde.org>
Tue, 3 Feb 2009 18:55:58 +0000 (19:55 +0100)
kronolith/calendars/edit.php
kronolith/docs/CHANGES
kronolith/lib/Driver.php
kronolith/lib/Forms/CreateCalendar.php
kronolith/lib/Forms/EditCalendar.php
kronolith/lib/Kronolith.php
kronolith/lib/Views/Day.php
kronolith/lib/Views/Month.php
kronolith/lib/Views/Week.php
kronolith/templates/panel.inc

index dadbdca..ab8e31e 100644 (file)
@@ -52,6 +52,7 @@ if ($form->validate($vars)) {
 }
 
 $vars->set('name', $calendar->get('name'));
+$vars->set('color', $calendar->get('color'));
 $vars->set('description', $calendar->get('desc'));
 $tags = new Kronolith_Tagger();
 $vars->set('tags', implode(',', array_values($tags->getTags($calendar->getName(), 'calendar'))));
index b64c0a6..280b225 100644 (file)
@@ -2,6 +2,7 @@
 v3.0-git
 --------
 
+[jan] Set colors per calendar (Request #7480).
 [jan] Add individual notification methods for single events (Alfonso Marín
       Marín <almarin@um.es>).
 [jan] Add Ajax interface.
index 13e9bb2..1634b77 100644 (file)
@@ -442,6 +442,20 @@ class Kronolith_Event {
     var $_calendar;
 
     /**
+     * The HTML background color to be used for this event.
+     *
+     * @var string
+     */
+    var $_backgroundColor;
+
+    /**
+     * The HTML foreground color to be used for this event.
+     *
+     * @var string
+     */
+    var $_foregroundColor;
+
+    /**
      * The VarRenderer class to use for printing select elements.
      *
      * @var Horde_UI_VarRenderer
@@ -474,6 +488,15 @@ class Kronolith_Event {
         $this->alarm = $alarm;
 
         $this->_calendar = $driver->getCalendar();
+        if (!empty($this->_calendar)) {
+            $share = $GLOBALS['all_calendars'][$this->_calendar];
+            $this->_backgroundColor = $share->get('color');
+            if (empty($this->_backgroundColor)) {
+                $this->_backgroundColor = '#dddddd';
+            }
+            $this->_foregroundColor = Horde_Image::brightness($this->_backgroundColor) < 128 ? '#f6f6f6' : '#000';
+        }
+
         if ($eventObject !== null) {
             $this->fromDriver($eventObject);
         }
@@ -2301,7 +2324,9 @@ class Kronolith_Event {
             $link = Horde::linkTooltip($this->getViewUrl(array('datetime' => $datetime->strftime('%Y%m%d%H%M%S'), 'url' => $from_url), $full),
                                        $event_title,
                                        $this->getStatusClass(), '', '',
-                                       $this->getTooltip());
+                                       $this->getTooltip(),
+                                       '',
+                                       array('style' => $this->getCSSColors(false)));
         }
 
         $link .= @htmlspecialchars($event_title, ENT_QUOTES, NLS::getCharset());
@@ -2313,10 +2338,7 @@ class Kronolith_Event {
         }
 
         if ($icons && $prefs->getValue('show_icons')) {
-            $icon_color = isset($GLOBALS['cManager_fgColors'][$this->category]) ?
-                ($GLOBALS['cManager_fgColors'][$this->category] == '#000' ? '000' : 'fff') :
-                ($GLOBALS['cManager_fgColors']['_default_'] == '#000' ? '000' : 'fff');
-
+            $icon_color = $this->_foregroundColor == '#000' ? '000' : 'fff';
             $status = '';
             if ($this->alarm) {
                 if ($this->alarm % 10080 == 0) {
@@ -2407,6 +2429,23 @@ class Kronolith_Event {
     }
 
     /**
+     * Returns the CSS color definition for this event.
+     *
+     * @param boolean $with_attribute  Whether to wrap the colors inside a
+     *                                 "style" attribute.
+     *
+     * @return string  A CSS string with color definitions.
+     */
+    function getCSSColors($with_attribute = true)
+    {
+        $css = 'background-color:' . $this->_backgroundColor . ';color:' . $this->_foregroundColor;
+        if ($with_attribute) {
+            $css = ' style="' . $css . '"';
+        }
+        return $css;
+    }
+
+    /**
      * @return string  A tooltip for quick descriptions of this event.
      */
     function getTooltip()
index 685da96..7ebe8c9 100755 (executable)
@@ -31,6 +31,7 @@ class Kronolith_CreateCalendarForm extends Horde_Form {
         parent::Horde_Form($vars, _("Create Calendar"));
 
         $this->addVariable(_("Name"), 'name', 'text', true);
+        $this->addVariable(_("Color"), 'color', 'colorpicker', false);
         $this->addVariable(_("Description"), 'description', 'longtext', false, false, null, array(4, 60));
         $this->addVariable(_("Tags"), 'tags', 'text', false);
         $this->setButtons(array(_("Create")));
@@ -44,6 +45,7 @@ class Kronolith_CreateCalendarForm extends Horde_Form {
             return $calendar;
         }
         $calendar->set('name', $this->_vars->get('name'));
+        $calendar->set('color', $this->_vars->get('color'));
         $calendar->set('desc', $this->_vars->get('description'));
         $tagger = new Kronolith_Tagger();
         $tagger->tag($calendar->getName(), $this->_vars->get('tags'), 'calendar');
index 079db82..6dbdc0b 100644 (file)
@@ -38,6 +38,7 @@ class Kronolith_EditCalendarForm extends Horde_Form {
 
         $this->addHidden('', 'c', 'text', true);
         $this->addVariable(_("Name"), 'name', 'text', true);
+        $this->addVariable(_("Color"), 'color', 'colorpicker', false);
         $this->addVariable(_("Description"), 'description', 'longtext', false, false, null, array(4, 60));
         $this->addVariable(_("Tags"), 'tags', 'text', false);
         $this->setButtons(array(_("Save")));
@@ -48,6 +49,7 @@ class Kronolith_EditCalendarForm extends Horde_Form {
         $original_name = $this->_calendar->get('name');
         $new_name = $this->_vars->get('name');
         $this->_calendar->set('name', $new_name);
+        $this->_calendar->set('color', $this->_vars->get('color'));
         $this->_calendar->set('desc', $this->_vars->get('description'));
         if ($original_name != $new_name) {
             $result = $GLOBALS['kronolith_driver']->rename($original_name, $new_name);
index ba37c65..219c7d1 100644 (file)
@@ -2385,6 +2385,26 @@ class Kronolith {
     }
 
     /**
+     * Returns the CSS color definition for a calendar.
+     *
+     * @param array|Horde_Share_Object $calendar  A calendar share or a hash
+     *                                            from a remote calender
+     *                                            definition.
+     * @param boolean $with_attribute             Whether to wrap the colors
+     *                                            inside a "style" attribute.
+     *
+     * @return string  A CSS string with color definitions.
+     */
+    function getCSSColors($calendar, $with_attribute = true)
+    {
+        $css = 'background-color:' . Kronolith::backgroundColor($calendar) . ';color:' . Kronolith::foregroundColor($calendar);
+        if ($with_attribute) {
+            $css = ' style="' . $css . '"';
+        }
+        return $css;
+    }
+
+    /**
      * Builds Kronolith's list of menu items.
      */
     function getMenu($returnType = 'object')
index 8c0c9a5..33e013e 100644 (file)
@@ -131,7 +131,8 @@ class Kronolith_View_Day extends Kronolith_Day {
                         $eventCategories[$event->getCategory()] = true;
                     }
 
-                    $row .= '<td class="day-eventbox category' . hash('md5', $event->getCategory()) . '" '
+                    $row .= '<td class="day-eventbox"'
+                        . $event->getCSSColors()
                         . 'width="' . round(90 / count($this->_currentCalendars))  . '%" '
                         . 'valign="top" colspan="' . $this->_span[$cid] . '">'
                         . $event->getLink($this, true, $this->link(0, true));
@@ -246,7 +247,8 @@ class Kronolith_View_Day extends Kronolith_Day {
                             }
                         }
 
-                        $row .= '<td class="day-eventbox category' . hash('md5', $event->getCategory()) . '" '
+                        $row .= '<td class="day-eventbox"'
+                            . $event->getCSSColors()
                             . 'width="' . round((90 / count($this->_currentCalendars)) * ($span / $this->_span[$cid]))  . '%" '
                             . 'valign="top" colspan="' . $span . '" rowspan="' . $event->rowspan . '">'
                             . $event->getLink($this, true, $this->link(0, true));
index f53dec9..4af00e5 100644 (file)
@@ -206,7 +206,7 @@ class Kronolith_View_Month {
                             if ($event->hasPermission(PERMS_READ)) {
                                 $eventCategories[$event->getCategory()] = true;
                             }
-                            $html .= '<div class="month-eventbox category' . hash('md5', $event->getCategory()) . '">'
+                            $html .= '<div class="month-eventbox"' . $event->getCSSColors() . '>'
                                 . $event->getLink($date, true, $this->link(0, true));
                             if ($showTime) {
                                 $html .= '<div class="event-time">' . htmlspecialchars($event->getTimeRange()) . '</div>';
index d9ac503..e0f3698 100644 (file)
@@ -145,7 +145,8 @@ class Kronolith_View_Week {
                                 $eventCategories[$event->getCategory()] = true;
                             }
 
-                            $row .= '<td class="week-eventbox category' . hash('md5', $event->getCategory()) . '" '
+                            $row .= '<td class="week-eventbox"'
+                                . $event->getCSSColors()
                                 . 'width="' . round(99 / count($this->days[$j]->_currentCalendars)) . '%" '
                                 . 'valign="top">'
                                 . $event->getLink($this->days[$j], true, $this->link(0, true));
@@ -272,7 +273,8 @@ class Kronolith_View_Week {
                                     }
                                 }
 
-                                $row .= '<td class="week-eventbox category' . hash('md5', $event->getCategory()) . '" '
+                                $row .= '<td class="week-eventbox"'
+                                    . $event->getCSSColors()
                                     . 'valign="top" '
                                     . 'width="' . floor(((90 / count($this->days)) / count($this->_currentCalendars)) * ($span / $this->days[$j]->_span[$cid])) . '%" '
                                     . 'colspan="' . $span . '" rowspan="' . $event->rowspan . '">'
index a936eba..88267cc 100644 (file)
@@ -51,7 +51,7 @@ $tagger = new Kronolith_Tagger();
  <h4><?php echo _("My Calendars:") ?></h4>
  <ul id="mycalendars">
 <?php foreach ($my_calendars as $id => $cal): ?>
-  <li><label><input type="checkbox" class="checkbox" name="display_cal[]" value="<?php echo htmlspecialchars($id) ?>"<?php echo (in_array($id, $display_calendars) ? ' checked="checked"' : '') . ' /> ' . htmlspecialchars($cal->get('name')) ?></label> <?php echo $info_img ?>
+  <li<?php echo Kronolith::getCSSColors($cal) ?>><label><input type="checkbox" class="checkbox" name="display_cal[]" value="<?php echo htmlspecialchars($id) ?>"<?php echo (in_array($id, $display_calendars) ? ' checked="checked"' : '') . ' /> ' . htmlspecialchars($cal->get('name')) ?></label> <?php echo $info_img ?>
    <div class="panel-tags">
    <?php $tags = $tagger->getTags($id, 'calendar'); ?>
     <span style="cursor:pointer;" id="tag-show_<?php echo $id?>" onclick="toggleTags('<?php echo $id?>');" title="<?php echo _("Add new tags")?>">+</span><span id="tag-hide_<?php echo $id?>" style="display:none;cursor:pointer;" onclick="toggleTags('<?php echo $id?>');">-</span>
@@ -91,7 +91,7 @@ $tagger = new Kronolith_Tagger();
  <h4><?php echo _("Shared Calendars:") ?></h4>
  <ul id="sharedcalendars">
 <?php foreach ($shared_calendars as $id => $cal): ?>
-  <li><label><input type="checkbox" class="checkbox" name="display_cal[]" value="<?php echo htmlspecialchars($id) ?>"<?php echo (in_array($id, $display_calendars) ? ' checked="checked"' : '') . ' /> [' . htmlspecialchars(Auth::removeHook($cal->get('owner'))) . '] ' . htmlspecialchars($cal->get('name')) ?></label> <?php echo $info_img ?></li>
+  <li<?php echo Kronolith::getCSSColors($cal) ?>><label><input type="checkbox" class="checkbox" name="display_cal[]" value="<?php echo htmlspecialchars($id) ?>"<?php echo (in_array($id, $display_calendars) ? ' checked="checked"' : '') . ' /> [' . htmlspecialchars(Auth::removeHook($cal->get('owner'))) . '] ' . htmlspecialchars($cal->get('name')) ?></label> <?php echo $info_img ?></li>
 <?php endforeach; ?>
  </ul>
 <?php endif; ?>
@@ -114,7 +114,7 @@ $tagger = new Kronolith_Tagger();
  <h4><?php echo _("Remote Calendars:") ?></h4>
  <ul id="remotecalendars">
 <?php foreach ($remote_calendars as $id => $cal): ?>
-  <li><label><input type="checkbox" class="checkbox" name="display_cal[]" value="remote_<?php echo htmlspecialchars($cal['url']) ?>"<?php echo (in_array($cal['url'], $display_remote_calendars) ? ' checked="checked"' : '') . ' /> ' . htmlspecialchars($cal['name']) ?></label> <?php echo $info_img ?></li>
+  <li<?php echo Kronolith::getCSSColors($cal) ?>><label><input type="checkbox" class="checkbox" name="display_cal[]" value="remote_<?php echo htmlspecialchars($cal['url']) ?>"<?php echo (in_array($cal['url'], $display_remote_calendars) ? ' checked="checked"' : '') . ' /> ' . htmlspecialchars($cal['name']) ?></label> <?php echo $info_img ?></li>
 <?php endforeach; ?>
  </ul>
 <?php endif; ?>