Calandar is a realy nice module, Thanks a lot!

I want to theme my mini calendar in a way that I have a light background color for days with less events and a darker color when there are more events. I'm not sure how many gradients I will use.

Therefore the theming function needs to know how many events are on this day. Could you change the code in the function calendar_build_week (calendar.inc) to have the count variable in the theming function:

$rows[$week][] = array(
      'data' => $content,
      'class' => $class, 'id' => $view->name . '-' . $curday_date,
      'count' => count($items[$curday_date])
);

Thanks!

CommentFileSizeAuthor
#9 calendar.inc-406804-9.patch527 bytesosopolar
#4 Picture 2.png19.91 KBosopolar
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

osopolar’s picture

Title: Theming mini clendar - background color should depend on count of events » Theming mini calendar - I want background colors depending on count of events
osopolar’s picture

I see that there is a lot of activity here (in the calendar issue queue). If you want I can prepare a patch ... but its just one array element:
'count' => count($items[$curday_date])
Will you give it a try? Would be nice.

peterh’s picture

Hello,

I tried your suggestion as follows:

$rows[$week][] = array(
'data' => $view->mini ? $content : '

'. $content .'

',
//'data' => $content,
'class' => $class, 'id' => $view->name . '-' . $curday_date,
'count' => count($items[$curday_date]));
date_modify($curday, '+1 day');
}

This patch returns either
count=0 (calendar day has no events/items)
or
count=1 (calendar day has events/items)

It does not actually count the number of events on a given day.

What am I doing wrong?

osopolar’s picture

Status: Active » Needs review
FileSize
19.91 KB

Sorry, I had no time to test the code I posted. I didn't want to implement this feature on my one, instead I was waiting that it will made into the code (because I don't want to patch the module for each update).

But now I don't want to wait and I also was curious what my error was and I found out:

$items[$curday_date] is the array with items for the $curday_date, but they are grouped by start time (hours). I'm not sure if it is in every case like this, but in my case it is (see screen shot of one (debug) entry.

Therefore I need to count all values in side the hour groups. I'm not sure which is the best way to do so, I decided to count all elements of the 2 dimensional array and subtract the parents:

'count' => count($items[$curday_date], 1) - count($items[$curday_date],0)

Will this work in every case? Maybe this needs review of some one how knows a bit more about this.

peterh’s picture

Thank you ositoblanco,

This did the job!

I am now able to change background color in the mini calender based on the count of the number of calendar events
The count doesn't distinguish between different cck types, but that is not required in my case.

With a bit of code and css tweaking a developer is able to for instance apply colours only to the mini calendar and not the main calendar.

    $rows[$week][] = array(
      'data' => $view->mini ? $content : '<div class="inner">'. $content .'</div>',
      //'data' => $content,      
      'class' => $class . ' mini'.(count($items[$curday_date], 1) - count($items[$curday_date],0)),
      'id' => $view->name . '-' . $curday_date,
      'count' => count($items[$curday_date], 1) - count($items[$curday_date],0));    
    date_modify($curday, '+1 day');

.mini td.mini1,
.mini td.mini2,
.mini td.mini3,
.mini td.mini4,
.mini td.mini5
{
background-color: #CCFFCC;
}

osopolar’s picture

To complete this issue I want to provide the missing informations how to theme the mini calendar so that the background colors will depend on the amount of events:

Add the mini calendar preprocess function to your template.php:

function template_preprocess_calendar_mini(&$vars) {
  foreach ($vars['rows'] as $weekno => $row) {
    foreach ($row as $day => $data) {
      if (100 <= $data['count']) {
        $vars['rows'][$weekno][$day]['count'] = 'gte_100';
      }
      for ($i=0; 100 > $i; $i+=10) { 
        // set class for count which corresponds to its value
        if ($i <= $data['count'] && $i+10 > $data['count']) {
          $vars['rows'][$weekno][$day]['count'] = 'gte_'. $i;
          break;      
        }
      }
    }
  }
}

The values of $i in for ($i=0; 100 > $i; $i+=10) depends on how many events you expect to have and how you want to group them for your styling.

Copy the calendar-mini.tpl.php from the modules theme folder in your theme folder and add the class to style the background of your calendar cells (' event_count_'. $cell['count']) behind the other css classes like indicated in the following snipped:

    <?php foreach ($row as $cell): ?>
      <td id="<?php print $cell['id']; ?>" class="<?php print $cell['class'] .' event_count_'. $cell['count']; ?>">
        <?php print $cell['data']; ?>
      </td>
    <?php endforeach; ?>

The last thing you have to do is to style your calendar. Add something like this to your theme css:

.calendar-calendar tr td.event_count_gte_0 { background-color:#f7ffe4; }
.calendar-calendar tr td.event_count_gte_10 { background-color:#f0ffc6; }
.calendar-calendar tr td.event_count_gte_20 { background-color:#ecffb8; }
[...]
.calendar-calendar tr td.event_count_gte_100 { background-color:#f5e086; }

As result you will have a mini calender with different background colors for the table cells containing days with different amount of events. For an example see http://cyber4kids.de/kalender

Now I hope KarenS will have a look on this and adds the little change I explained in #4 to the calendar.inc ... to make calendar updates easier ;)

@peterh: I think the mini calender is a block. So you may theme the table cells just for this block. So you won't need to hack more of the modules code.

prophet108’s picture

Based on this approach, could someone please demonstrate how to show say, a red background color for events which are upcoming but within 2 days from the current date? I need to differentiate events which are happening soon from ones which are further away in the future. Any help with this would be much appreciated.

osopolar’s picture

@prophet108 (#7) I don't think your problem is related to this issue. This issue is about theming the mini calendar (where you only have the days of a month, as you can see here: http://cyber4kids.de/kalender). What you want is "how many days are left till the event start". I think this information can already provided by views, you only have to write some code to get this information into css attributes. If you need further help please open a new support request issue.

osopolar’s picture

FileSize
527 bytes

I'd really like to see this feature in the upcoming calendar release. Therefore I've created a patch for the little changes in includes/calendar.inc (see #4).

peterh in #5 confirmed that it is working. Anyway, I don't want to add the classes as suggested by peterh, because I think this will be different for each use of the calendar module. Although, maybe just define a basic use case like in #6 to get this work in general and someone who needs different classes may change it in template_preprocess_calendar_mini.

Any discussions and reviews are welcome.

mariomc’s picture

I also applied the patch and everything went smoothly.

I find this very useful and since it's a very small change, I too think this should be commited.

Artusamak’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)

6.x is entirely unmaintained.

osopolar’s picture

Not entirely true, it's part of d6lts.