Projet

Général

Profil

Paste
Télécharger (29,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / calendar / theme / theme.inc @ 74f6bef0

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
/**
18
 * Display a month view.
19
 */
20
function template_preprocess_calendar_month(&$vars) {
21
  $view = $vars['view'];
22
  $rows = $vars['rows'];
23
  if (empty($rows)) {
24
    $rows = array();
25
    $day_names = array();
26
  }
27
  elseif (sizeof($rows) > 1) {
28
    $day_names = array_shift($rows);
29
  }
30
  else {
31
    $day_names = $rows;
32
    $rows = array();
33
  }
34

    
35
  $month_rows = $rows;
36
  foreach ($rows as $weekno => $row) {
37
    foreach ($row as $day => $data) {
38
      $cell = $data['data'];
39

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

    
66
  $vars['rows'] = $month_rows;
67
  $vars['day_names'] = $day_names;
68

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

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

    
81
  $view = $vars['view'];
82

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

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

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

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

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

    
129
}
130

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

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

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

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

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

    
175
  $columns = array();
176

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

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

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

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

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

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

    
300
  $vars['agenda_hour_class'] = 'calendar-agenda-hour';
301
  $first_column_width = 10;
302

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

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

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

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

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

    
341
  if (sizeof($rows) > 1) {
342
    $day_names = array_shift($rows);
343
  }
344
  else {
345
    $day_names = $rows;
346
    $rows = array();
347
  }
348

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

    
353
  $grouped_items = array();
354

    
355
  // pass the multiday buckets
356
  $vars['all_day'] = $rows['multiday_buckets'];
357

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

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

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

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

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

    
505
  $vars['items'] = $grouped_items;
506

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

    
512
  $vars['agenda_hour_class'] = 'calendar-agenda-hour';
513
  $first_column_width = 10;
514

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

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

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

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

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

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

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

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

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

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

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

    
653
  list($view_name, $display_id) = explode(':', $view);
654

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

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

    
664
    case 'type':
665
      $options = node_type_get_names();
666
      break;
667

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
877
/** @} End of addtogroup themeable */