Projet

Général

Profil

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

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

1
<?php
2
/**
3
 * @file
4
 * Contains the Calendar row style plugin.
5
 *
6
 * This plugin takes the view results, finds the date value for each,
7
 * then compares that date to the date range for the current view.
8
 * Items that started before or ended after the current date range
9
 * are shortened to the current range. Items that extend over more
10
 * than one day are cloned to create a calendar item for each day.
11
 * The resulting array of results (which may have a different number
12
 * of items than the original view result) are then passed back
13
 * to the style plugin so they can be displayed in a calendar.
14
 *
15
 */
16

    
17
/**
18
 * Plugin which creates a view on the resulting object
19
 * and formats it as a Calendar node.
20
 */
21
class calendar_plugin_row extends views_plugin_row_fields {
22

    
23
  // Stores the entities loaded with pre_render.
24
  var $entities = array();
25

    
26
  function init(&$view, &$display, $options = NULL) {
27
    parent::init($view, $display, $options);
28
    $this->base_table = $view->base_table;
29
    $this->base_field = $view->base_field;
30
  }
31

    
32
  /**
33
   * Helper function to find the date argument handler for this view.
34
   */
35
  function date_argument_handler() {
36
    foreach ($this->view->argument as $name => $handler) {
37
      if (date_views_handler_is_date($handler, 'argument')) {
38
        return $handler;
39
      }
40
    }
41
  }
42

    
43
  function option_definition() {
44
    $options = parent::option_definition();
45
    $options['date_fields'] = array('default' => array());
46
    $options['calendar_date_link'] = array('default' => '');
47
    $options['colors'] = array(
48
      'contains' => array(
49
        'legend' => array('default' => ''),
50
        'calendar_colors_type' => array('default' => array()),
51
        'taxonomy_field' => array('default' => ''),
52
        'calendar_colors_vocabulary' => array('default' => array()),
53
        'calendar_colors_taxonomy' => array('default' => array()),
54
        'calendar_colors_group' => array('default' => array()),
55
    ));
56
    return $options;
57
  }
58

    
59
  /**
60
   * Provide a form for setting options.
61
   */
62
  function options_form(&$form, &$form_state) {
63
    parent::options_form($form, $form_state);
64

    
65
    $form['markup']['#markup'] = t("The calendar row plugin will format view results as calendar items. Make sure this display has a 'Calendar' format and uses a 'Date' contextual filter, or this plugin will not work correctly.");
66
    $form['calendar_date_link'] = array(
67
      '#title' => t('Add new date link'),
68
      '#type' => 'select',
69
      '#default_value' => $this->options['calendar_date_link'],
70
      '#options' => array('' => t('No link')) + node_type_get_names(),
71
      '#description' => t('Display a link to add a new date of the specified content type. Displayed only to users with appropriate permissions.'),
72
      );
73
    $form['colors'] = array(
74
      '#type' => 'fieldset',
75
      '#title' => t('Legend Colors'),
76
      '#description' =>  t('Set a hex color value (like #ffffff) to use in the calendar legend for each content type. Items with empty values will have no stripe in the calendar and will not be added to the legend.'),
77
    );
78
    $options = array(
79
      '' => t('None')
80
    );
81
    if ($this->view->base_table == 'node') {
82
      $options['type'] = t('Based on Content Type');
83
    }
84
    if (module_exists('taxonomy')) {
85
      $options['taxonomy'] = t('Based on Taxonomy');
86
    }
87
    if (module_exists('og')) {
88
      $options['group'] = t('Based on Organic Group');
89
    }
90
    // If none of the options but the None option is available, stop here.
91
    if (count($options) == 1) {
92
      return;
93
    }
94
    $form['colors']['legend'] = array(
95
      '#title' => t('Stripes'),
96
      '#description' => t('Add stripes to calendar items.'),
97
      '#type' => 'select',
98
      '#options' => $options,
99
      '#default_value' => $this->options['colors']['legend'],
100
    );
101
    if ($this->view->base_table == 'node') {
102
      $colors = $this->options['colors']['calendar_colors_type'];
103
      $type_names = node_type_get_names();
104
      foreach ($type_names as $key => $name) {
105
        $form['colors']['calendar_colors_type'][$key] = array(
106
          '#title' => check_plain($name),
107
          '#default_value' => isset($colors[$key]) ? $colors[$key] : CALENDAR_EMPTY_STRIPE,
108
          '#dependency' => array('edit-row-options-colors-legend' => array('type')),
109
          '#type' => 'textfield',
110
          '#size' => 7,
111
          '#maxlength' => 7,
112
          '#element_validate' => array('calendar_validate_hex_color'),
113
          '#prefix' => '<div class="calendar-colorpicker-wrapper">',
114
          '#suffix' => '<div class="calendar-colorpicker"></div></div>',
115
          '#attributes' => array('class' => array('edit-calendar-colorpicker')),
116
          '#attached' => array(
117
            // Add Farbtastic color picker.
118
            'library' => array(
119
              array('system', 'farbtastic'),
120
            ),
121
            // Add javascript to trigger the colorpicker.
122
            'js' => array(drupal_get_path('module', 'calendar') . '/js/calendar_colorpicker.js'),
123
          ),
124
        );
125
      }
126
    }
127
    if (module_exists('taxonomy')) {
128
      // Get the display's field names of taxonomy fields.
129
      $vocab_field_options = array();
130
      $fields = $this->display->handler->get_option('fields');
131
      foreach ($fields as $name => $field_info) {
132
        $field = field_info_field($name);
133
        // Select the proper field type.
134
        if (!empty($field['type']) && $field['type'] == 'taxonomy_term_reference') {
135
          $vocab_field_options[$name] = $field_info['label'];
136
        }
137
      }
138
      $form['colors']['taxonomy_field'] = array(
139
        '#title' => t('Term field'),
140
        '#type' => !empty($vocab_field_options) ? 'select' : 'hidden',
141
        '#default_value' => $this->options['colors']['taxonomy_field'],
142
        '#description' => t("Select the taxonomy term field to use when setting stripe colors. This works best for vocabularies with only a limited number of possible terms."),
143
        '#options' => $vocab_field_options,
144
        '#dependency' => array('edit-row-options-colors-legend' => array('taxonomy')),
145
        );
146
      if (empty($vocab_field_options)) {
147
        $form['colors']['taxonomy_field']['#options'] = array('' => '');
148
        $form['colors']['taxonomy_field']['#suffix'] = t('You must add a term field to this view to use taxonomy stripe values. This works best for vocabularies with only a limited number of possible terms.');
149
      }
150

    
151
      // Get the Vocabulary names.
152
      $vocab_names = array();
153
      foreach ($vocab_field_options as $field_name => $label) {
154
        $taxonomy_field = field_info_field($field_name);
155
        foreach ((array) $taxonomy_field['settings']['allowed_values'] as $delta => $options) {
156
          $vocab_names[$field_name] = $options['vocabulary'];
157
        }
158
      }
159

    
160
      // Get the Vocabulary id's.
161
      $vocab_vids = array();
162
      foreach ($vocab_names as $field_name => $vocab_name) {
163
          $vocab = taxonomy_vocabulary_machine_name_load($vocab_name);
164
          $vocab_vids[$field_name] = $vocab->vid;
165
      }
166

    
167
      $this->options['colors']['calendar_colors_vocabulary'] = $vocab_vids;
168

    
169
      $form['colors']['calendar_colors_vocabulary'] = array(
170
        '#title' => t('Vocabulary Legend Types'),
171
        '#type' => 'value',
172
        '#value' => $vocab_vids,
173
      );
174

    
175
      // Get the Vocabulary term id's and map to colors.
176
      $term_colors = $this->options['colors']['calendar_colors_taxonomy'];
177
      foreach ($vocab_vids as $field_name => $vid) {
178
        $vocab = taxonomy_get_tree($vid);
179
        foreach ($vocab as $key => $term) {
180
          $form['colors']['calendar_colors_taxonomy'][$term->tid] = array(
181
            '#title' => check_plain(t($term->name)),
182
            '#default_value' => isset($term_colors[$term->tid]) ? $term_colors[$term->tid] : CALENDAR_EMPTY_STRIPE,
183
            '#access' => !empty($vocab_field_options),
184
            '#dependency_count' => 2, // All 2 of the following #dependencies must be met.
185
            '#dependency' => array(
186
                                   'edit-row-options-colors-legend' => array('taxonomy'),
187
                                   'edit-row-options-colors-taxonomy-field' => array($field_name),
188
                             ),
189
            '#type' => 'textfield',
190
            '#size' => 7,
191
            '#maxlength' => 7,
192
            '#element_validate' => array('calendar_validate_hex_color'),
193
            '#prefix' => '<div class="calendar-colorpicker-wrapper">',
194
            '#suffix' => '<div class="calendar-colorpicker"></div></div>',
195
            '#attributes' => array('class' => array('edit-calendar-colorpicker')),
196
            '#attached' => array(
197
              // Add Farbtastic color picker.
198
              'library' => array(
199
                array('system', 'farbtastic'),
200
              ),
201
              // Add javascript to trigger the colorpicker.
202
              'js' => array(drupal_get_path('module', 'calendar') . '/js/calendar_colorpicker.js'),
203
            ),
204
          );
205
        }
206
      }
207
    }
208
    if (module_exists('og')) {
209
      $colors_group = $this->options['colors']['calendar_colors_group'];
210
      $options = array();
211

    
212
      // The 7.1 version of OG.
213
      if (function_exists('og_label')) {
214
        $gids = og_get_all_group();
215
        foreach ($gids as $gid) {
216
          $options[$gid] = check_plain(t(og_label($gid)));
217
        }
218
      }
219

    
220
      // The 7.2 version of OG.
221
      else {
222
        $types = og_get_all_group_entity();
223
        foreach ($types as $entity_type => $name) {
224
          $gids = og_get_all_group($entity_type);
225
          $entities = entity_load($entity_type, $gids);
226
          foreach ($entities as $entity) {
227
            list($gid) = entity_extract_ids($entity_type, $entity);
228
            $options[$gid] = check_plain(t(entity_label($entity_type, $entity)));
229
          }
230
        }
231
      }
232

    
233
      foreach ($options as $gid => $title) {
234
        $form['colors']['calendar_colors_group'][$gid] = array(
235
          '#title' => $title,
236
          '#default_value' => isset($colors_group[$gid]) ? $colors_group[$gid] : CALENDAR_EMPTY_STRIPE,
237
          '#dependency' => array('edit-row-options-colors-legend' => array('group')),
238
          '#type' => 'textfield',
239
          '#size' => 7,
240
          '#maxlength' => 7,
241
          '#element_validate' => array('calendar_validate_hex_color'),
242
          '#prefix' => '<div class="calendar-colorpicker-wrapper">',
243
          '#suffix' => '<div class="calendar-colorpicker"></div></div>',
244
          '#attributes' => array('class' => array('edit-calendar-colorpicker')),
245
          '#attached' => array(
246
            // Add Farbtastic color picker.
247
            'library' => array(
248
              array('system', 'farbtastic'),
249
            ),
250
            // Add javascript to trigger the colorpicker.
251
            'js' => array(drupal_get_path('module', 'calendar') . '/js/calendar_colorpicker.js'),
252
          ),
253
        );
254
      }
255
    }
256
  }
257

    
258
  function options_submit(&$form, &$form_state) {
259
    parent::options_submit($form, $form_state);
260
    if ($this->view->base_table == 'node') {
261
      if (isset($this->view->display['default']->display_options['link_display'])) {
262
        $default_display = $this->view->display['default']->display_options['link_display'];
263
        $path = $this->view->display[$default_display]->handler->get_option('path');
264
        // If this display has been set up as a default tab, the current path
265
        // is actually the base path, i.e. if the path is 'calendar/month'
266
        // and this is a default tab, the path for this display will actually
267
        // be 'calendar'.
268
        if ($this->view->display[$default_display]->handler->options['menu']['type'] == 'default tab') {
269
          $parts = explode('/', $path);
270
          array_pop($parts);
271
          $path = implode('/', $parts);
272
        }
273
        calendar_clear_link_path($path);
274
        if (!empty($form_state['values']['row_options']['calendar_date_link'])) {
275
          $node_type = $form_state['values']['row_options']['calendar_date_link'];
276
          calendar_set_link('node', $node_type, $path);
277
        }
278
      }
279
    }
280
  }
281

    
282
  function pre_render($values) {
283

    
284
    // Preload each entity used in this view from the cache.
285
    // Provides all the entity values relatively cheaply, and we don't
286
    // need to do it repeatedly for the same entity if there are
287
    // multiple results for one entity.
288
    $ids = array();
289
    foreach ($values as $row) {
290
      // Use the $id as the key so we don't create more than one value per entity.
291
      // This alias will automatically adjust to be the id of related entity, if applicable.
292
      $id = $row->{$this->field_alias};
293

    
294
      // Node revisions need special loading.
295
      if ($this->view->base_table == 'node_revision') {
296
        $this->entities[$id] = node_load(NULL, $id);
297
      }
298
      // For other entities we just create an array of ids to pass
299
      // to entity_load().
300
      else {
301
        $ids[$id] = $id;
302
      }
303
    }
304

    
305
    $base_tables = date_views_base_tables();
306
    $this->entity_type = $base_tables[$this->view->base_table];
307
    if (!empty($ids)) {
308
      $this->entities = entity_load($this->entity_type, $ids);
309
    }
310

    
311
    // Let the style know if a link to create a new date is required.
312
    $this->view->date_info->calendar_date_link = $this->options['calendar_date_link'];
313

    
314
    // Identify the date argument and fields that apply to this view.
315
    // Preload the Date Views field info for each field, keyed by the
316
    // field name, so we know how to retrieve field values from the cached node.
317
    $data = date_views_fields($this->view->base_table);
318
    $data = $data['name'];
319
    $date_fields = array();
320
    foreach ($this->view->argument as $handler) {
321
      if (date_views_handler_is_date($handler, 'argument')) {
322
        // If this is the complex Date argument, the date fields are stored in the handler options,
323
        // otherwise we are using the simple date field argument handler.
324
        if ($handler->definition['handler'] != 'date_views_argument_handler') {
325
          $alias = $handler->table_alias . '.' . $handler->field;
326
          $info = $data[$alias];
327
          $field_name  = str_replace(array('_value2', '_value'), '', $info['real_field_name']);
328
          $date_fields[$field_name] = $info;
329
        }
330
        else {
331
          foreach ($handler->options['date_fields'] as $alias) {
332
            // If this date field is unknown (as when it has been deleted), ignore it.
333
            if (!array_key_exists($alias, $data)) {
334
              continue;
335
            }
336
            $info = $data[$alias];
337
            $field_name  = str_replace(array('_value2', '_value'), '', $info['real_field_name']);
338

    
339
            // This is ugly and hacky but I can't figure out any generic way to
340
            // recognize that the node module is going to give some the revision timestamp
341
            // a different field name on the entity than the actual column name in the database.
342
            if ($this->view->base_table == 'node_revision' && $field_name == 'timestamp') {
343
              $field_name = 'revision_timestamp';
344
            }
345

    
346
            $date_fields[$field_name] = $info;
347
          }
348
        }
349
        $this->date_argument = $handler;
350
        $this->date_fields = $date_fields;
351
      }
352
    }
353

    
354
    // Get the language for this view.
355
    $this->language = $this->display->handler->get_option('field_language');
356
    $substitutions = views_views_query_substitutions($this->view);
357
    if (array_key_exists($this->language, $substitutions)) {
358
      $this->language = $substitutions[$this->language];
359
    }
360
  }
361

    
362
  function render($row) {
363
    global $base_url;
364
    $rows = array();
365
    $date_info = $this->date_argument->view->date_info;
366
    $id = $row->{$this->field_alias};
367

    
368
    if (!is_numeric($id)) {
369
      return $rows;
370
    }
371

    
372
    // There could be more than one date field in a view
373
    // so iterate through all of them to find the right values
374
    // for this view result.
375
    foreach ($this->date_fields as $field_name => $info) {
376

    
377
      // Load the specified node:
378
      // We have to clone this or nodes on other views on this page,
379
      // like an Upcoming block on the same page as a calendar view,
380
      // will end up acquiring the values we set here.
381
      $entity = clone($this->entities[$id]);
382

    
383
      if (empty($entity)) {
384
        return $rows;
385
      }
386

    
387
      $table_name  = $info['table_name'];
388
      $delta_field = $info['delta_field'];
389
      $tz_handling = $info['tz_handling'];
390
      $tz_field    = $info['timezone_field'];
391
      $rrule_field = $info['rrule_field'];
392
      $is_field    = $info['is_field'];
393

    
394
      $info = entity_get_info($this->entity_type);
395
      $this->id_field = $info['entity keys']['id'];
396
      $this->id = $entity->{$this->id_field};
397
      $this->type = !empty($info['entity keys']['bundle']) ? $info['entity keys']['bundle'] : $this->entity_type;
398
      $this->title = entity_label($this->entity_type, $entity);
399
      $uri = entity_uri($this->entity_type, $entity);
400
      $uri['options']['absolute'] = TRUE;
401
      $this->url = '';
402
      if (isset($uri['path'])) {
403
        $this->url = url($uri['path'], $uri['options']);
404
      }
405

    
406
      // Retrieve the field value(s) that matched our query from the cached node.
407
      // Find the date and set it to the right timezone.
408

    
409
      $entity->date_id = array();
410
      $item_start_date = NULL;
411
      $item_end_date   = NULL;
412
      $granularity = 'second';
413
      $increment = 1;
414
      if ($is_field) {
415

    
416
        // Set the date_id for the node, used to identify which field value to display for
417
        // fields that have multiple values. The theme expects it to be an array.
418
        $date_id = 'date_id_' . $field_name;
419
        $date_delta = 'date_delta_' . $field_name;
420
        if (isset($row->$date_id)) {
421
          $delta = $row->$date_delta;
422
          $entity->date_id = array('calendar-' . $row->$date_id . '-' . $field_name. '-' . $delta);
423
          $delta_field = $date_delta;
424
        }
425
        else {
426
          $delta = isset($row->$delta_field) ? $row->$delta_field : 0;
427
          $entity->date_id = array('calendar-' . $id . '-' . $field_name . '-' . $delta);
428
        }
429

    
430
        $items = field_get_items($this->entity_type, $entity, $field_name, $this->language);
431
        $item  = $items[$delta];
432
        $db_tz   = date_get_timezone_db($tz_handling, isset($item->$tz_field) ? $item->$tz_field : $date_info->display_timezone_name);
433
        $to_zone = date_get_timezone($tz_handling, isset($item->$tz_field) ? $item->$tz_field : $date_info->display_timezone_name);
434
        if (isset($item['value'])) {
435
          $item_start_date = new dateObject($item['value'], $db_tz);
436
          $item_end_date   = (array_key_exists('value2', $item) && !empty($item['value2'])) ? new dateObject($item['value2'], $db_tz) : $item_start_date;
437
        }
438

    
439
        $cck_field = field_info_field($field_name);
440
        $instance = field_info_instance($this->entity_type, $field_name, $this->type);
441
        $granularity = date_granularity_precision($cck_field['settings']['granularity']);
442
        if ($instance) {
443
          $increment = $instance['widget']['settings']['increment'];
444
        }
445
      }
446
      elseif (!empty($entity->$field_name)) {
447
        $item = $entity->$field_name;
448
        $db_tz   = date_get_timezone_db($tz_handling, isset($item->$tz_field) ? $item->$tz_field : $date_info->display_timezone_name);
449
        $to_zone = date_get_timezone($tz_handling, isset($item->$tz_field) ? $item->$tz_field : $date_info->display_timezone_name);
450
        $item_start_date = new dateObject($item, $db_tz);
451
        $item_end_date   = $item_start_date;
452
        $entity->date_id = array('calendar-' . $id . '-' . $field_name);
453
      }
454

    
455
      // If we don't have a date value, go no further.
456
      if (empty($item_start_date)) {
457
        continue;
458
      }
459

    
460
      // Set the item date to the proper display timezone;
461
      $item_start_date->setTimezone(new dateTimezone($to_zone));
462
      $item_end_date->setTimezone(new dateTimezone($to_zone));
463

    
464
      $event = new stdClass();
465
      $event->id = $this->id;
466
      $event->title = $this->title;
467
      $event->type = $this->type;
468
      $event->date_start = $item_start_date;
469
      $event->date_end = $item_end_date;
470
      $event->db_tz = $db_tz;
471
      $event->to_zone = $to_zone;
472
      $event->granularity = $granularity;
473
      $event->increment = $increment;
474
      $event->field = $is_field ? $item : NULL;
475
      $event->url = $this->url;
476
      $event->row = $row;
477
      $event->entity = $entity;
478
      $event->stripe = array();
479
      $event->stripe_label = array();
480

    
481
      // All calendar row plugins should provide a date_id that the theme can use.
482
      $event->date_id = $entity->date_id[0];
483

    
484
      // We are working with an array of partially rendered items
485
      // as we process the calendar, so we can group and organize them.
486
      // At the end of our processing we'll need to swap in the fully formatted
487
      // display of the row. We save it here and switch it in
488
      // template_preprocess_calendar_item().
489
      $event->rendered = theme($this->theme_functions(),
490
      array(
491
        'view' => $this->view,
492
        'options' => $this->options,
493
        'row' => $row,
494
        'field_alias' => isset($this->field_alias) ? $this->field_alias : '',
495
      ));
496

    
497
      $entities = $this->explode_values($event);
498
      $date_info = $this->date_argument->view->date_info;
499
      $from = $date_info->date_range[0];
500
      $to = $date_info->date_range[1];
501
      foreach ($entities as $entity) {
502
        if(($entity->date_start < $from && $entity->date_end < $from) || ($entity->date_start > $to && $entity->date_end > $to)) continue;
503
        switch ($this->options['colors']['legend']) {
504
          case 'type':
505
            $this->calendar_node_type_stripe($entity);
506
            break;
507
          case 'taxonomy':
508
            $this->calendar_taxonomy_stripe($entity);
509
            break;
510
          case 'group':
511
            $this->calendar_group_stripe($entity);
512
            break;
513
        }
514
        $rows[] = $entity;
515
      }
516

    
517
    }
518

    
519
    return $rows;
520
  }
521

    
522
  function explode_values($event) {
523
    $rows = array();
524

    
525
    $date_info = $this->date_argument->view->date_info;
526
    $item_start_date = $event->date_start;
527
    $item_end_date = $event->date_end;
528
    $to_zone = $event->to_zone;
529
    $db_tz = $event->db_tz;
530
    $granularity = $event->granularity;
531
    $increment = $event->increment;
532

    
533
    // Now that we have an 'entity' for each view result, we need
534
    // to remove anything outside the view date range,
535
    // and possibly create additional nodes so that we have a 'node'
536
    // for each day that this item occupies in this view.
537
    $now = max($date_info->min_zone_string, $item_start_date->format(DATE_FORMAT_DATE));
538
    $to  = min($date_info->max_zone_string, $item_end_date->format(DATE_FORMAT_DATE));
539
    $next = new DateObject($now . ' 00:00:00', $date_info->display_timezone);
540
    if ($date_info->display_timezone_name != $to_zone) {
541
      // Make $start and $end (derived from $node) use the timezone $to_zone, just as the original dates do.
542
      date_timezone_set($next, timezone_open($to_zone));
543
    }
544
    if (empty($to) || $now > $to) {
545
      $to = $now;
546
    }
547
    // $now and $next are midnight (in display timezone) on the first day where node will occur.
548
    // $to is midnight on the last day where node will occur.
549
    // All three were limited by the min-max date range of the view.
550
    $pos = 0;
551
    while (!empty($now) && $now <= $to) {
552
      $entity = clone($event);
553

    
554
      // Get start and end of current day.
555
      $start = $next->format(DATE_FORMAT_DATETIME);
556
      date_modify($next, '+1 day');
557
      date_modify($next, '-1 second');
558
      $end = $next->format(DATE_FORMAT_DATETIME);
559

    
560
      // Get start and end of item, formatted the same way.
561
      $item_start = $item_start_date->format(DATE_FORMAT_DATETIME);
562
      $item_end = $item_end_date->format(DATE_FORMAT_DATETIME);
563

    
564
      // Get intersection of current day and the node value's duration (as strings in $to_zone timezone).
565
      $entity->calendar_start = $item_start < $start ? $start : $item_start;
566
      $entity->calendar_end = !empty($item_end) ? ($item_end > $end ? $end : $item_end) : $entity->calendar_start;
567

    
568
      // Make date objects
569
      $entity->calendar_start_date = date_create($entity->calendar_start, timezone_open($to_zone));
570
      $entity->calendar_end_date = date_create($entity->calendar_end, timezone_open($to_zone));
571

    
572
      // Change string timezones into
573
      // calendar_start and calendar_end are UTC dates as formatted strings
574
      $entity->calendar_start = date_format($entity->calendar_start_date, DATE_FORMAT_DATETIME);
575
      $entity->calendar_end = date_format($entity->calendar_end_date, DATE_FORMAT_DATETIME);
576
      $entity->calendar_all_day = date_is_all_day($entity->calendar_start, $entity->calendar_end, $granularity, $increment);
577

    
578
      unset($entity->calendar_fields);
579
      if (isset($entity) && (empty($entity->calendar_start))) {
580
        // if no date for the node and no date in the item
581
        // there is no way to display it on the calendar
582
        unset($entity);
583
      }
584
      else {
585
        $entity->date_id .= '.' . $pos;
586

    
587
        $rows[] = $entity;
588
        unset($entity);
589
      }
590
      date_modify($next, '+1 second');
591
      $now = date_format($next, DATE_FORMAT_DATE);
592
      $pos++;
593

    
594
    }
595
    return $rows;
596
  }
597

    
598
  /**
599
   * Create a stripe base on node type.
600
   */
601
  function calendar_node_type_stripe(&$result) {
602
    $colors = isset($this->options['colors']['calendar_colors_type']) ? $this->options['colors']['calendar_colors_type'] : array();
603
    if (empty($colors)) {
604
      return;
605
    }
606
    $entity = $result->entity;
607
    if (empty($entity->type)) {
608
      return;
609
    }
610

    
611
    $type_names = node_type_get_names();
612
    $type = $entity->type;
613
    $label = '';
614
    $stripe = '';
615
    if (array_key_exists($type, $type_names) || $colors[$type] == CALENDAR_EMPTY_STRIPE) {
616
      $label = $type_names[$type];
617
    }
618
    if (array_key_exists($type, $colors)) {
619
      $stripe = $colors[$type];
620
    }
621

    
622
    $result->stripe[] = $stripe;
623
    $result->stripe_label[] = $label;
624
    return;
625
  }
626

    
627
   /**
628
   * Create a stripe based on a taxonomy term.
629
   */
630

    
631
  function calendar_taxonomy_stripe(&$result) {
632
    $colors = isset($this->options['colors']['calendar_colors_taxonomy']) ? $this->options['colors']['calendar_colors_taxonomy'] : array();
633
    if (empty($colors)) {
634
      return;
635
    }
636

    
637
    $term_field_name = 'field_' . $this->options['colors']['taxonomy_field'];
638
    $terms = $result->row->{$term_field_name};
639
    foreach ($terms as $term_key => $term_value) {
640
      $term = $term_value['raw']['taxonomy_term'];
641
      if (!array_key_exists($term->tid, $colors) || $colors[$term->tid] == CALENDAR_EMPTY_STRIPE) {
642
        continue;
643
      }
644
      $result->stripe[] = $colors[$term->tid];
645
      $result->stripe_label[] = $term->name;
646
    }
647

    
648
    return;
649
  }
650

    
651
  /**
652
   * Create a stripe based on group.
653
   */
654
  function calendar_group_stripe(&$result) {
655
    $colors = isset($this->options['colors']['calendar_colors_group']) ? $this->options['colors']['calendar_colors_group'] : array();
656

    
657
    if (empty($colors)) {
658
      return;
659
    }
660
    if (!function_exists('og_get_entity_groups')) {
661
      return;
662
    }
663

    
664
    $entity = $result->entity;
665
    $groups_for_entity = og_get_entity_groups($this->view->base_table, $entity);
666

    
667
    // The 7.1 version of OG.
668
    if (function_exists('og_label')) {
669
      if (count($groups_for_entity)) {
670
        foreach ($groups_for_entity as $gid => $group_name) {
671
          if (!array_key_exists($gid, $colors) || $colors[$gid] == CALENDAR_EMPTY_STRIPE) {
672
            continue;
673
          }
674
          $result->stripe[] = $colors[$gid];
675
          $result->stripe_label[] = $group_name;
676
        }
677
      }
678
    }
679
    // The 7.2 version of OG.
680
    else {
681
      if (count($groups_for_entity)) {
682
        foreach ($groups_for_entity as $entity_type => $gids) {
683
          foreach ($gids as $gid => $group_name) {
684
            if (!array_key_exists($gid, $colors) || $colors[$gid] == CALENDAR_EMPTY_STRIPE) {
685
              continue;
686
            }
687
            $result->stripe[] = $colors[$gid];
688
            $result->stripe_label[] = $group_name;
689
          }
690
        }
691
      }
692
    }
693
    return;
694
  }
695
}