Project

General

Profile

Paste
Download (29.5 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / calendar / theme / theme.inc @ 62e0cc08

1
<?php
2

    
3
/**
4
 * @file
5
 * Theme functions for the Calendar module.
6
 */
7

    
8
/**
9
 * Preprocess an RSS feed.
10
 */
11
function template_preprocess_calendar_style(&$vars) {
12
  global $base_url;
13
  global $language;
14
}
15

    
16
/**
17
 * Display a month view.
18
 */
19
function template_preprocess_calendar_month(&$vars) {
20
  $view = $vars['view'];
21
  $rows = $vars['rows'];
22
  if (empty($rows)) {
23
    $rows = array();
24
    $day_names = array();
25
  }
26
  elseif (count($rows) > 1) {
27
    $day_names = array_shift($rows);
28
  }
29
  else {
30
    $day_names = $rows;
31
    $rows = array();
32
  }
33

    
34
  $month_rows = $rows;
35
  foreach ($rows as $weekno => $row) {
36
    // If this row is already rendered, don't do anything.
37
    if (!isset($row['data'])) {
38
      foreach ($row as $day => $data) {
39
        $cell = $data['data'];
40

    
41
        // If this cell is already rendered, like the weekno column,
42
        // move to the next item.
43
        if (!is_array($cell)) {
44
          $month_rows[$weekno][$day]['data'] = $cell;
45
          continue;
46
        }
47
        $data = $cell['datebox'];
48
        if ($cell['empty']) {
49
          $data .= $cell['empty'];
50
        }
51
        else {
52
          $data .= implode($cell['all_day']);
53
          foreach ($cell['items'] as $hour => $item) {
54
            $data .= implode($item);
55
          }
56
          $data .= $cell['link'];
57
        }
58
        if ($view->date_info->mini) {
59
          $month_rows[$weekno][$day]['data'] = $data;
60
        }
61
        else {
62
          $month_rows[$weekno][$day]['data'] = '<div class="inner">' . $data . '</div>';
63
        }
64
      }
65
    }
66
  }
67

    
68
  $vars['rows'] = $month_rows;
69
  $vars['day_names'] = $day_names;
70

    
71
  $vars['display_type'] = $view->date_info->granularity;
72
  $vars['min_date_formatted'] = date_format($view->date_info->min_date, DATE_FORMAT_DATETIME);
73
  $vars['max_date_formatted'] = date_format($view->date_info->max_date, DATE_FORMAT_DATETIME);
74
}
75

    
76
/**
77
 * Display a mini month view.
78
 */
79
function template_preprocess_calendar_mini(&$vars) {
80
  // Add in all the $vars added by the main calendar preprocessor.
81
  template_preprocess_calendar_month($vars);
82

    
83
  $view = $vars['view'];
84

    
85
  // Make sure that the calendar title links go to the month view,
86
  // not the year view (if this is embedded in a year display).
87
  $view->override_path = calendar_granularity_path($view, 'month');
88

    
89
  $view->date_info->show_title = !empty($view->date_info->show_title) ? $view->date_info->show_title : FALSE;
90
  $vars['show_title'] = $view->date_info->show_title;
91
  $vars['view'] = $view;
92
}
93

    
94
/**
95
 * Display a year view.
96
 */
97
function template_preprocess_calendar_year(&$vars) {
98

    
99
  // Construct a calendar for each month, adjusting the $view passed
100
  // to the theme so it will produce the right results.
101
  $view = clone($vars['view']);
102
  $year = date_format($view->date_info->min_date, 'Y');
103
  $view->date_info->style_with_weekno = FALSE;
104
  $rows = $vars['rows'];
105
  $months = array();
106
  foreach ($rows as $month => $month_rows) {
107
    $view->date_info->month = $month;
108
    $view->date_info->granularity = 'month';
109
    $view->date_info->mini = TRUE;
110
    $view->date_info->hide_nav = TRUE;
111
    $view->date_info->show_title = TRUE;
112
    $view->date_info->url = date_pager_url($view, NULL, date_pad($year, 4) . '-' . date_pad($month));
113
    $view->date_info->min_date = new DateObject($view->date_info->year . '-' . date_pad($month) . '-01 00:00:00', date_default_timezone());
114
    $view->date_info->max_date = clone($view->date_info->min_date);
115
    date_modify($view->date_info->max_date, '+1 month');
116
    date_modify($view->date_info->max_date, '-1 second');
117
    $variables = array(
118
      'view' => $view,
119
      'options' => $vars['options'],
120
      'rows' => $month_rows,
121
    );
122
    $months[$month] = theme('calendar_mini', $variables);
123
  }
124
  $view->date_info->mini = FALSE;
125

    
126
  $vars['months'] = $months;
127
  $vars['view']->date_info->hide_nav = FALSE;
128
  $vars['view']->date_info->granularity = 'year';
129
  $vars['mini'] = FALSE;
130

    
131
}
132

    
133
/**
134
 * Display a day overlap view.
135
 */
136
function template_preprocess_calendar_day_overlap(&$vars) {
137
  template_preprocess_calendar_day($vars);
138
}
139

    
140
/**
141
 * Display a day view.
142
 */
143
function template_preprocess_calendar_day(&$vars) {
144
  $vars['view']->style_with_weekno = FALSE;
145
  $view = $vars['view'];
146
  $rows = $vars['rows'];
147

    
148
  $item_count = 0;
149
  $by_hour_count = 0;
150
  $grouping_field = !empty($view->date_info->style_groupby_field) ? ($view->date_info->style_groupby_field) : NULL;
151
  $display_overlap = !empty($view->date_info->style_theme_style) && !empty($view->date_info->style_groupby_times);
152
  $vars['scroll_content'] = !empty($view->date_info->style_theme_style) && $view->date_info->style_theme_style == 1;
153

    
154
  // Add optional css.
155
  if ($display_overlap) {
156
    $overlapped_items = array();
157
    drupal_add_css(drupal_get_path('module', 'calendar') . '/css/calendar-overlap.css');
158
    if (empty($view->live_preview) && !empty($vars['scroll_content'])) {
159
      drupal_add_js(drupal_get_path('module', 'calendar') . '/js/calendar_overlap.js');
160
    }
161
    if (empty($vars['scroll_content'])) {
162
      drupal_add_css(drupal_get_path('module', 'calendar') . '/css/calendar-overlap-no-scroll.css');
163
    }
164
  }
165

    
166
  // If we're not grouping by time, move all items into the 'all day' array.
167
  if (empty($view->date_info->style_groupby_times)) {
168
    // Items are already grouped into times, so we need to process each time-group.
169
    foreach ($rows['items'] as $time => $items) {
170
      foreach ($items as $item) {
171
        $rows['all_day'][] = $item;
172
      }
173
    }
174
    $rows['items'] = array();
175
  }
176

    
177
  $columns = array();
178

    
179
  // Move all_day items into the right columns and render them.
180
  $grouped_items = array();
181
  foreach ($rows['all_day'] as $item) {
182
    if (!empty($item->rendered_fields[$grouping_field])) {
183
      $column = $item->rendered_fields[$grouping_field];
184
      if (!in_array($column, $columns)) {
185
        $columns[] = $column;
186
      }
187
    }
188
    else {
189
      $column = t('Items');
190
    }
191
    $grouped_items[$column][] = theme('calendar_item', array('view' => $view, 'rendered_fields' => $item->rendered_fields, 'item' => $item));
192
    $item_count++;
193
  }
194
  $vars['rows']['all_day'] = $grouped_items;
195

    
196
  // Moved timed items into the right columns and render them.
197
  $start_times = $view->date_info->style_groupby_times;
198
  $show_empty_times = $view->date_info->style_show_empty_times;
199
  $end_start_time = '23:59:59';
200
  $start_time = array_shift($start_times);
201
  $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
202

    
203
  $grouped_items = array();
204
  foreach ($rows['items'] as &$items) {
205
    foreach ($items as &$item) {
206
      $time = date_format($item->calendar_start_date, 'H:i:s');
207
      if (!empty($item->rendered_fields[$grouping_field])) {
208
        $column = $item->rendered_fields[$grouping_field];
209
        if (!in_array($column, $columns)) {
210
          $columns[] = $column;
211
        }
212
      }
213
      else {
214
        $column = t('Items');
215
      }
216
      // Find the next time slot and fill it. Populate the skipped
217
      // slots if the option to show empty times was chosen.
218
      while ($time >= $next_start_time && $time < $end_start_time) {
219
        if ((!empty($show_empty_times) || $display_overlap) && !array_key_exists($start_time, $grouped_items)) {
220
          $grouped_items[$start_time]['values'] = array();
221
        }
222
        $start_time = $next_start_time;
223
        $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
224
      }
225
      $grouped_items[$start_time]['values'][$column][] = $item;
226
      if ($display_overlap) {
227
        $time_end = date_format($item->calendar_end_date, 'H:i:s');
228
        $item->time_start = $time;
229
        $item->time_end = $time_end;
230
        _calc_indents($overlapped_items, $time, $time_end, $item);
231
      }
232
      $item_count++;
233
      $by_hour_count++;
234
    }
235
  }
236

    
237
  // Finish out the day's time values if we want to see empty times.
238
  if (!empty($show_empty_times) || $display_overlap) {
239
    while ($start_time < $end_start_time && (!empty($start_time) || $display_overlap)) {
240
      if (empty($start_time)) {
241
        $start_times = $view->date_info->style_groupby_times;
242
        $start_time = array_shift($start_times);
243
        $next_start_time = array_shift($start_times);
244
      }
245
      if (!array_key_exists($start_time, $grouped_items)) {
246
        $grouped_items[$start_time]['values'] = array();
247
      }
248
      $start_time = $next_start_time;
249
      $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
250
    }
251
  }
252

    
253
  // Do the headers last, once we know what the actual values are.
254
  $i = 0;
255
  $start_times = array_keys($grouped_items);
256
  foreach ($start_times as $start_time) {
257
    $next_start_time = array_key_exists($i + 1, $start_times) ? $start_times[$i + 1] : '23:59:59';
258
    $variables = array(
259
      'start_time' => $start_time,
260
      'next_start_time' => $next_start_time,
261
      'curday_date' => $rows['date'],
262
    );
263
    $heading = theme('calendar_time_row_heading', $variables);
264
    $grouped_items[$start_time]['hour'] = $heading['hour'];
265
    $grouped_items[$start_time]['ampm'] = $heading['ampm'];
266
    foreach ($grouped_items[$start_time]['values'] as $column => &$items) {
267
      foreach ($items as $index => &$item) {
268
        $group_time = NULL;
269
        $divisor = NULL;
270
        if ($display_overlap) {
271
          if ($view->style_plugin->options['groupby_times'] == 'half') {
272
            $group_time = 30;
273
            $divisor = 7.5;
274
          }
275
          elseif ($view->style_plugin->options['groupby_times'] == 'hour') {
276
            $group_time = 60;
277
            $divisor = 15;
278
          }
279
          else {
280
            $item->class = '';
281
          }
282
          if (!empty($group_time) && !empty($divisor)) {
283
            $start_minute = intval(substr($start_time, 3, 2));
284
            $offset = round((date_format($item->date_start, 'i') - $start_minute) / $divisor);
285
            $duration = round(($item->date_end->format('U') - $item->date_start->format('U')) / 60 / $divisor);
286
            $item->class = 'd_' . $duration . ' o_' . $offset . ' i_' . $item->indent . ' md_' . min($item->max_depth, 5);
287
          }
288
        }
289
        $grouped_items[$start_time]['values'][$column][$index] = theme('calendar_item', array('view' => $view, 'rendered_fields' => $item->rendered_fields, 'item' => $item));
290
      }
291
    }
292
    $i++;
293
  }
294
  ksort($grouped_items);
295
  $vars['rows']['items'] = $grouped_items;
296

    
297
  if (empty($columns)) {
298
    $columns = array(t('Items'));
299
  }
300
  $vars['columns'] = $columns;
301

    
302
  $vars['agenda_hour_class'] = 'calendar-agenda-hour';
303
  $first_column_width = 10;
304

    
305
  if (empty($view->date_info->style_groupby_times)) {
306
    $vars['agenda_hour_class'] .= ' calendar-agenda-no-hours';
307
    $first_column_width = 1;
308
  }
309

    
310
  $vars['first_column_width'] = $first_column_width;
311
  if (count($columns)) {
312
    $vars['column_width'] = round((100 - $first_column_width) / count($columns));
313
  }
314
  else {
315
    $vars['column_width'] = (100 - $first_column_width);
316
  }
317
  $vars['item_count'] = $item_count;
318
  $vars['by_hour_count'] = $by_hour_count;
319
  $vars['start_times'] = $view->date_info->style_groupby_times;
320
  return;
321
}
322

    
323
/**
324
 * Display a week overlap view.
325
 */
326
function template_preprocess_calendar_week_overlap(&$vars) {
327
  template_preprocess_calendar_week($vars);
328
}
329

    
330
/**
331
 * Display a week view.
332
 */
333
function template_preprocess_calendar_week(&$vars) {
334
  $vars['view']->style_with_weekno = FALSE;
335

    
336
  $view = $vars['view'];
337
  $rows = $vars['rows'];
338
  $item_count = 0;
339
  $by_hour_count = 0;
340
  $start_time = NULL;
341
  $columns = array();
342

    
343
  if (count($rows) > 1) {
344
    $day_names = array_shift($rows);
345
  }
346
  else {
347
    $day_names = $rows;
348
    $rows = array();
349
  }
350

    
351
  // Moved timed items into the right columns and render them.
352
  $show_empty_times = $view->date_info->style_show_empty_times;
353
  $end_start_time = '23:59:59';
354

    
355
  $grouped_items = array();
356

    
357
  // Pass the multiday buckets.
358
  $vars['all_day'] = $rows['multiday_buckets'];
359

    
360
  // Remove the count for singleday.
361
  $vars['multiday_rows'] = max(0, $rows['total_rows'] - 1);
362
  $display_overlap = ($view->date_info->style_multiday_theme == '1' && !empty($view->date_info->style_theme_style));
363
  $vars['display_overlap'] = $display_overlap;
364
  $vars['scroll_content'] = !empty($view->date_info->style_theme_style) && $view->date_info->style_theme_style == 1;
365

    
366
  // Add optional css.
367
  if ($display_overlap) {
368
    drupal_add_css(drupal_get_path('module', 'calendar') . '/css/calendar-overlap.css');
369
    if (empty($view->live_preview) && !empty($vars['scroll_content'])) {
370
      drupal_add_js(drupal_get_path('module', 'calendar') . '/js/calendar_overlap.js');
371
    }
372
    if (empty($vars['scroll_content'])) {
373
      drupal_add_css(drupal_get_path('module', 'calendar') . '/css/calendar-overlap-no-scroll.css');
374
    }
375
    $overlapped_items = array(array(), array(), array(), array(), array(), array(), array());
376

    
377
    // Locate the first item.
378
    $first_time = '23:59:59';
379
    $first_time_index = -1;
380
    for ($i = 0; $i < 7; $i++) {
381
      if (count($rows['singleday_buckets'][$i]) > 0) {
382
        $time_slot = reset($rows['singleday_buckets'][$i]);
383
        $time = date_format($time_slot[0]['item']->date_start, 'H:i:s');
384
        if ($time < $first_time) {
385
          $first_time = $time;
386
          $first_time_index = $i;
387
        }
388
      }
389
    }
390
    if ($first_time_index > -1) {
391
      $rows['singleday_buckets'][$first_time_index][$first_time][0]['is_first'] = TRUE;
392
    }
393
  }
394

    
395
  // If we're not grouping by time, move all items into the 'all day' array.
396
  if (empty($view->date_info->style_groupby_times)) {
397
    $add_row = FALSE;
398
    foreach ($vars['all_day'] as $index => &$day) {
399
      foreach ($rows['singleday_buckets'][$index] as $item) {
400
        foreach ($item as $event) {
401
          $day[] = $event;
402
          $add_row = TRUE;
403
        }
404
      }
405
    }
406
    if ($add_row) {
407
      $vars['multiday_rows']++;
408
    }
409
  }
410
  else {
411
    foreach ($rows['singleday_buckets'] as $wday => $singleday_row) {
412
      $columns[] = $wday;
413
      foreach ($singleday_row as &$row) {
414
        $start_times = $view->date_info->style_groupby_times;
415
        $start_time = array_shift($start_times);
416
        $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
417
        foreach ($row as &$item) {
418
          $time = date_format($item['item']->date_start, 'H:i:s');
419
          if ($item['item']->calendar_all_day) {
420
            $vars['all_day'][$item['wday']][] = $item;
421
            if ($vars['multiday_rows'] == 0) {
422
              $vars['multiday_rows']++;
423
            }
424
          }
425
          else {
426
            // Find the next time slot and fill it. Populate the skipped
427
            // slots if the option to show empty times was chosen.
428
            while ($time >= $next_start_time && $time < $end_start_time) {
429
              if (($show_empty_times || $display_overlap) && !array_key_exists($start_time, $grouped_items)) {
430
                $grouped_items[$start_time]['values'][$wday] = array();
431
              }
432
              $start_time = $next_start_time;
433
              $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
434
            }
435
            $grouped_items[$start_time]['values'][$wday][] = &$item;
436
            if ($display_overlap) {
437
              $date_end = date_format($item['item']->date_end, 'H:i:s');
438
              _calc_indents($overlapped_items[$wday], $time, $date_end, $item);
439
            }
440
            $item_count++;
441
            $by_hour_count++;
442
          }
443
        }
444
      }
445
      // Finish out the day's time values if we want to see empty times.
446
      if ($show_empty_times || $display_overlap) {
447
        while ($start_time < $end_start_time && ($start_time != NULL || $display_overlap)) {
448
          if ($start_time == NULL) {
449
            $start_times = $view->date_info->style_groupby_times;
450
            $start_time = array_shift($start_times);
451
            $next_start_time = array_shift($start_times);
452
          }
453
          if (!array_key_exists($start_time, $grouped_items)) {
454
            $grouped_items[$start_time]['values'][$wday] = array();
455
          }
456
          $start_time = $next_start_time;
457
          $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
458
        }
459
      }
460
      ksort($grouped_items);
461
    }
462
  }
463

    
464
  // Do the headers last, once we know what the actual values are.
465
  $i = 0;
466
  $start_times = array_keys($grouped_items);
467
  foreach ($start_times as $start_time) {
468
    $next_start_time = array_key_exists($i + 1, $start_times) ? $start_times[$i + 1] : '23:59:59';
469
    $variables = array(
470
      'start_time' => $start_time,
471
      'next_start_time' => $next_start_time,
472
      'curday_date' => (isset($row['data'])) ? $row['data']['date'] : NULL,
473
    );
474
    $heading = theme('calendar_time_row_heading', $variables);
475
    $grouped_items[$start_time]['hour'] = $heading['hour'];
476
    $grouped_items[$start_time]['ampm'] = $heading['ampm'];
477
    $grouped_items[$start_time]['time'] = $start_time;
478
    if ($display_overlap) {
479
      foreach ($grouped_items[$start_time]['values'] as $wday => &$items) {
480
        foreach ($items as &$item) {
481
          if ($display_overlap) {
482
            $group_time = NULL;
483
            $divisor = NULL;
484
            if ($view->style_plugin->options['groupby_times'] == 'half') {
485
              $group_time = 30;
486
              $divisor = 7.5;
487
            }
488
            elseif ($view->style_plugin->options['groupby_times'] == 'hour') {
489
              $group_time = 60;
490
              $divisor = 15;
491
            }
492
            else {
493
              $item['class'] = '';
494
            }
495
            if (!empty($group_time) && !empty($divisor)) {
496
              $start_minute = intval(substr($start_time, 3, 2));
497
              $offset = round((date_format($item['item']->date_start, 'i') - $start_minute) / $divisor);
498
              $duration = round(($item['item']->date_end->format('U') - $item['item']->date_start->format('U')) / 60 / $divisor);
499
              $item['class'] = 'd_' . $duration . ' o_' . $offset . ' i_' . $item['indent'] . ' md_' . min($item['max_depth'], 5);
500
            }
501
          }
502
        }
503
      }
504
    }
505
  }
506

    
507
  $vars['items'] = $grouped_items;
508

    
509
  $vars['day_names'] = $day_names;
510
  $vars['columns'] = $columns;
511
  $vars['start_times'] = $view->date_info->style_groupby_times;
512
  $vars['first_time'] = !empty($first_time) ? $first_time : '';
513

    
514
  $vars['agenda_hour_class'] = 'calendar-agenda-hour';
515
  $first_column_width = 10;
516

    
517
  if (empty($view->date_info->style_groupby_times)) {
518
    $vars['agenda_hour_class'] .= ' calendar-agenda-no-hours';
519
    $first_column_width = 1;
520
  }
521
  $vars['item_count'] = $item_count;
522
  $vars['by_hour_count'] = $by_hour_count;
523
  return;
524
}
525

    
526
/**
527
 * Implementation of hook_preprocess_calendar_item().
528
 */
529
function template_preprocess_calendar_item(&$vars) {
530
  // At the last possible minute we fix the values in rendered_fields so it
531
  // contains the correct rendered content for the type of item and item display.
532
  $item = $vars['item'];
533

    
534
  $multiday_hidden = !empty($vars['view']->style_options['multiday_hidden']) ? $vars['view']->style_options['multiday_hidden'] : array();
535

    
536
  if (!empty($item->rendered) && empty($item->is_multi_day)) {
537
    $vars['rendered_fields'] = array($item->rendered);
538
  }
539
  else {
540
    foreach ($vars['view']->field as $id => $field) {
541
      if ($field->options['exclude'] || (!empty($item->is_multi_day) && in_array($id, $multiday_hidden))) {
542
        unset($vars['rendered_fields'][$id]);
543
      }
544
    }
545
  }
546
}
547

    
548
/**
549
 * Create the calendar date box.
550
 */
551
function template_preprocess_calendar_datebox(&$vars) {
552
  $date = $vars['date'];
553
  $view = $vars['view'];
554
  $vars['day'] = intval(substr($date, 8, 2));
555
  $force_view_url = !empty($view->date_info->block) ? TRUE : FALSE;
556
  $month_path = calendar_granularity_path($view, 'month');
557
  $year_path = calendar_granularity_path($view, 'year');
558
  $day_path = calendar_granularity_path($view, 'day');
559
  $vars['url'] = str_replace(array($month_path, $year_path), $day_path, date_pager_url($view, NULL, $date, $force_view_url));
560
  $vars['link'] = !empty($day_path) ? l($vars['day'], $vars['url']) : $vars['day'];
561
  $vars['granularity'] = $view->date_info->granularity;
562
  $vars['mini'] = !empty($view->date_info->mini);
563
  if ($vars['mini']) {
564
    if (!empty($vars['selected'])) {
565
      $vars['class'] = 'mini-day-on';
566
    }
567
    else {
568
      $vars['class'] = 'mini-day-off';
569
    }
570
  }
571
  else {
572
    $vars['class'] = 'day';
573
  }
574
}
575

    
576
/**
577
 * Format an calendar month node for display.
578
 */
579
function template_preprocess_calendar_month_multiple_entity(&$vars) {
580
  $view = $vars['view'];
581
  $curday = $vars['curday'];
582
  $count = $vars['count'];
583
  $ids = $vars['ids'];
584

    
585
  // Get the year month and date.
586
  $parts = explode('-', substr($curday, 0, 10));
587
  $year = $parts[0];
588
  $month = intval($parts[1]);
589
  $day = intval($parts[2]);
590

    
591
  // Create the link to the day.
592
  $month_path = calendar_granularity_path($view, 'month');
593
  $day_path = calendar_granularity_path($view, 'day');
594
  $vars['link'] = str_replace($month_path, $day_path, date_pager_url($view, NULL, date_pad($year, 4) . '-' . date_pad($month) . '-' . date_pad($day)));
595
}
596

    
597
/**
598
 * Theme function for rendering views fields as a calendar 'item'.
599
 *
600
 * $vars['rendered_fields'] = An array of the rendered display of each field in the View.
601
 * $vars['item'] = The source data for this item.
602
 * $vars['view'] = The view that this item is displayed on.
603
 *
604
 * @TODO We need some options about how to combine rendered fields.
605
 * Fields rendered in multiday containers need to be inline.
606
 */
607

    
608
/**
609
 * Format the time row headings in the week and day view.
610
 */
611
function theme_calendar_time_row_heading($vars) {
612
  $start_time = $vars['start_time'];
613
  $next_start_time = $vars['next_start_time'];
614
  $curday_date = $vars['curday_date'];
615
  static $format_hour, $format_ampm;
616
  if (empty($format_hour)) {
617
    $format = variable_get('date_format_short', 'm/d/Y - H:i');
618
    if (substr($start_time, -5) == '00:00' && substr($next_start_time, -5) == '00:00') {
619
      $limit = array('hour');
620
    }
621
    else {
622
      $limit = array('hour', 'minute');
623
    }
624
    $format_hour = str_replace(array('a', 'A'), '', date_limit_format($format, $limit));
625
    $format_ampm = strstr($format, 'a') ? 'a' : (strstr($format, 'A') ? 'A' : '');
626
  }
627
  if ($start_time == '00:00:00' && $next_start_time == '23:59:59') {
628
    $hour = t('All times');
629
  }
630
  elseif ($start_time == '00:00:00') {
631
    $date = date_create($curday_date . ' ' . $next_start_time);
632
    $hour = t('Before @time', array('@time' => date_format($date, $format_hour)));
633
  }
634
  else {
635
    $date = date_create($curday_date . ' ' . $start_time);
636
    $hour = date_format($date, $format_hour);
637
  }
638
  if (!empty($date)) {
639
    $ampm = date_format($date, $format_ampm);
640
  }
641
  else {
642
    $ampm = '';
643
  }
644
  return array('hour' => $hour, 'ampm' => $ampm);
645
}
646

    
647
/**
648
 * Format a node stripe legend.
649
 */
650
function theme_calendar_stripe_legend($vars) {
651
  if (empty($vars) || !$view = $vars['view']) {
652
    return '';
653
  }
654

    
655
  list($view_name, $display_id) = explode(':', $view);
656

    
657
  $view = views_get_view($view_name);
658
  $view->set_display($display_id);
659
  $row_options = $view->display_handler->get_option('row_options');
660

    
661
  $legend_type = $row_options['colors']['legend'];
662
  $display_options = $row_options['colors']['calendar_colors_' . $legend_type];
663
  $options = array();
664
  switch ($legend_type) {
665

    
666
    case 'type':
667
      $options = node_type_get_names();
668
      break;
669

    
670
    case 'taxonomy':
671
      $vocabularies = (array) $row_options['colors']['calendar_colors_vocabulary'];
672
      foreach ($vocabularies as $field_name => $vid) {
673
        $vocab = taxonomy_get_tree($vid);
674
        foreach ($vocab as $key => $term) {
675
          $options[$term->tid] = $term->name;
676
        }
677
      }
678
      break;
679

    
680
    case 'group':
681
      // The 7.1 version of OG.
682
      if (function_exists('og_label')) {
683
        $gids = og_get_all_group();
684
        foreach ($gids as $gid) {
685
          $options[$gid] = check_plain(t(og_label($gid)));
686
        }
687
      }
688

    
689
      // The 7.2 version of OG.
690
      else {
691
        $types = og_get_all_group_entity();
692
        foreach ($types as $entity_type => $name) {
693
          $gids = og_get_all_group($entity_type);
694
          $entities = entity_load($entity_type, $gids);
695
          foreach ($entities as $entity) {
696
            list($gid) = entity_extract_ids($entity_type, $entity);
697
            $options[$gid] = check_plain(t(entity_label($entity_type, $entity)));
698
          }
699
        }
700
      }
701
      break;
702
  }
703

    
704
  $header = array(
705
    array('class' => 'calendar-legend', 'data' => t('Item')),
706
    array('class' => 'calendar-legend', 'data' => t('Key')),
707
  );
708

    
709
  $rows = array();
710
  $output = '';
711
  foreach ($options as $key => $label) {
712
    $stripe = array_key_exists($key, $display_options) ? $display_options[$key] : CALENDAR_EMPTY_STRIPE;
713
    if ($stripe != CALENDAR_EMPTY_STRIPE) {
714
      $rows[] = array($label, '<div style="background-color:' . $stripe . ';color:' . $stripe . '" class="stripe" title="Key: ' . $label . '">&nbsp;</div>');
715
    }
716
  }
717

    
718
  if (!empty($rows)) {
719
    $variables = array(
720
      'header' => $header,
721
      'rows' => $rows,
722
      'attributes' => array('class' => array('mini', 'calendar-legend')),
723
    );
724
    $output .= theme('table', $variables);
725
    $output = '<div class="calendar legend">' . $output . '</div>';
726
  }
727
  return $output;
728
}
729

    
730
/**
731
 * Format item stripes.
732
 */
733
function theme_calendar_stripe_stripe($vars) {
734
  $item = $vars['item'];
735
  if (empty($item->stripe) || (!count($item->stripe))) {
736
    return;
737
  }
738
  $output = '';
739
  if (is_array($item->stripe_label)) {
740
    foreach ($item->stripe_label as $k => $stripe_label) {
741
      if (!empty($item->stripe[$k]) && !empty($stripe_label)) {
742
        $output .= '<div style="background-color:' . $item->stripe[$k] . ';color:' . $item->stripe[$k] . '" class="stripe" title="Key: ' . $item->stripe_label[$k] . '">&nbsp;</div>' . "\n";
743
      }
744
    }
745
  }
746
  return $output;
747
}
748

    
749
/**
750
 * Format an empty day on a calendar.
751
 *
752
 * @param day
753
 *   The day to display.
754
 */
755
function theme_calendar_empty_day($vars) {
756
  $curday = $vars['curday'];
757
  $view = $vars['view'];
758

    
759
  if ($view->date_info->calendar_type != 'day') {
760
    return '<div class="calendar-empty">&nbsp;</div>' . "\n";
761
  }
762
  else {
763
    return '<div class="calendar-dayview-empty">' . t('Empty day') . '</div>';
764
  }
765
}
766

    
767
/**
768
 * Indent items based off a nested tree structure of overlapping items.
769
 *
770
 * @param array $overlapped_items
771
 *   Tree of overlapped items.
772
 * @param date $start
773
 *   Start time of the event.
774
 * @param date $end
775
 *   End time of the event.
776
 * @param array $item
777
 *   The event to add to the tree.
778
 * @param int $depth
779
 *   Current depth of the tree.
780
 *
781
 * @return rc
782
 *   Returns an array with the max depth of the branch and whether an overlap occurred
783
 */
784
function _calc_indents(&$overlapped_items, $start, $end, &$item, $depth = 0) {
785

    
786
  // Are there any items at this depth?
787
  if (!empty($overlapped_items)) {
788

    
789
    // Iterate for each item as this depth and see if we overlap.
790
    foreach ($overlapped_items as $index => &$entry) {
791

    
792
      // We search depth-first, so if there are children for this item, recurse into
793
      // each child tree looking for an overlap.
794
      if (!empty($entry['children'])) {
795
        $rc = _calc_indents($entry['children'], $start, $end, $item, $depth + 1);
796

    
797
        // Was there an overlap in the child tree?
798
        if ($rc['overlap']) {
799
          if (is_object($entry['item'])) {
800
            $entry['item']->indent = _calc_indent($entry['depth'], $rc['max_depth']);
801
            $entry['item']->max_depth = $rc['max_depth'];
802
          }
803
          else {
804
            $entry['item']['indent'] = _calc_indent($entry['depth'], $rc['max_depth']);
805
            $entry['item']['max_depth'] = $rc['max_depth'];
806
          }
807

    
808
          // There was an overlap, pop out of this depth.
809
          return $rc;
810
        }
811
      }
812
      // No, child overlap, so check if we overlap this item.
813
      if ($start >= $entry['start'] && $start <= $entry['end']) {
814

    
815
        // We overlap, create an overlapping entry.
816
        $entry['children'][] = array('item' => &$item, 'depth' => $depth + 1, 'start' => $start, 'end' => $end, 'children' => array());
817
        if (is_object($entry['item'])) {
818
          $max_depth = max($entry['item']->max_depth, $depth + 1);
819
          $entry['item']->indent = _calc_indent($depth, $max_depth);
820
          $entry['item']->max_depth = $max_depth;
821
        }
822
        else {
823
          $max_depth = max($entry['item']['max_depth'], $depth + 1);
824
          $entry['item']['indent'] = _calc_indent($depth, $max_depth);
825
          $entry['item']['max_depth'] = $max_depth;
826
        }
827
        if (is_object($item)) {
828
          $item->indent = _calc_indent($depth + 1, $max_depth);
829
          $item->max_depth = $max_depth;
830
        }
831
        else {
832
          $item['indent'] = _calc_indent($depth + 1, $max_depth);
833
          $item['max_depth'] = $max_depth;
834
        }
835

    
836
        // We overlap, so pop out of this depth.
837
        return array('overlap' => TRUE, 'max_depth' => $max_depth);
838
      }
839
    }
840

    
841
    // If there are items at this depth, but no overlap, then return no overlap and pop
842
    // out of this depth.
843
    if ($depth > 0) {
844
      return array('overlap' => FALSE, 'max_depth' => 0);
845
    }
846
  }
847

    
848
  // No overlap at any depth, reset the array of overlaps.
849
  if ($depth == 0) {
850
    reset($overlapped_items);
851
    $overlapped_items[0] = array('item' => &$item, 'depth' => $depth, 'start' => $start, 'end' => $end, 'children' => array());
852
  }
853
  else {
854
    $overlapped_items[] = array('item' => &$item, 'depth' => $depth, 'start' => $start, 'end' => $end, 'children' => array());
855
  }
856

    
857
  if (is_object($item)) {
858
    $item->indent = _calc_indent($depth, $depth);
859
    $item->max_depth = $depth;
860
  }
861
  else {
862
    $item['indent'] = _calc_indent($depth, $depth);
863
    $item['max_depth'] = $depth;
864
  }
865
  return array('overlap' => FALSE, 'max_depth' => $depth);
866
}
867

    
868
/**
869
 * Calculates the indent based of the current depth and the depth of this branch in the tree.
870
 *
871
 * @param int $cur_depth
872
 * @param int $depth
873
 *
874
 * @return int
875
 */
876
function _calc_indent($cur_depth, $depth) {
877
  return round(10 * $cur_depth / ($depth + 1));
878
}
879

    
880
/**
881
 * @} End of addtogroup themeable */