Projet

Général

Profil

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

root / drupal7 / sites / all / modules / calendar / theme / theme.inc @ 0695d136

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
    // If this row is already rendered, don't do anything.
38
    if (!isset($row['data'])) {
39
      foreach ($row as $day => $data) {
40
        $cell = $data['data'];
41

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

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

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

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

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

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

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

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

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

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

    
132
}
133

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

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

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

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

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

    
178
  $columns = array();
179

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
356
  $grouped_items = array();
357

    
358
  // pass the multiday buckets
359
  $vars['all_day'] = $rows['multiday_buckets'];
360

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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