[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/calendar/calendar_multiday/theme/ -> theme.inc (source)

   1  <?php
   2  // $Id: theme.inc,v 1.1.2.4 2011/01/03 12:28:48 karens Exp $
   3  /**
   4   * Display a calendar navigation and links
   5   */
   6  function template_preprocess_calendar_main(&$vars) {
   7    require_once('./'. drupal_get_path('module', 'calendar_multiday') .'/includes/calendar.inc');
   8    $view     = $vars['view'];
   9      
  10    $result   = (array) $view->result;
  11    $options  = $view->style_plugin->options;
  12    $handler  = $view->style_plugin;
  13  
  14    $vars['display_type'] = $view->date_info->granularity;
  15    $vars['min_date_formatted'] = date_format($view->date_info->min_date, DATE_FORMAT_DATETIME);
  16    $vars['max_date_formatted'] = date_format($view->date_info->max_date, DATE_FORMAT_DATETIME);
  17    $view->date_info->mini = isset($view->date_info->mini) ? $view->date_info->mini : $view->date_info->granularity == 'year';
  18    
  19    $url = $view->get_url();
  20    $view->date_info->url = $url;
  21    $arg = $view->date_info->date_arg;
  22    $displays = $view->date_info->display_types;
  23      
  24    // Set up the links to other calendar views.
  25    $current_date = $view->date_info->min_date;
  26    if (!empty($date->info->day)) {
  27      $current_date = date_make_date($date_info->date_arg .' 00:00:00');
  28    }
  29    elseif (!empty($view->date_info->week)) {
  30      $week = date_week_range($view->date_info->week, $view->date_info->year);
  31      $current_date = $week[0]; 
  32    }
  33    elseif (!empty($view->date_info->month)) {
  34      $current_date = date_make_date($view->date_info->year .'-'. date_pad($view->date_info->month) .'-01 00:00:00');
  35    }
  36    else {
  37      $current_date = date_make_date($view->date_info->year .'-01-01 00:00:00');
  38    }
  39    if (empty($view->date_info->month) || $view->date_info->month == $view->argument['date_argument']->options['wildcard']) {
  40      $view->date_info->month = date_format($current_date, 'm');
  41    }
  42    if (empty($view->date_info->day) || $view->date_info->day == $view->argument['date_argument']->options['wildcard']) {
  43      $view->date_info->day = date_format($current_date, 'd');
  44    }
  45    if (empty($view->date_info->week) || $view->date_info->week == $view->argument['date_argument']->options['wildcard']) {
  46      $view->date_info->week = date_week($view->date_info->year .'-'. date_pad($view->date_info->month) .'-'. date_pad($view->date_info->day));
  47    }
  48  
  49    $calendar_links = array();
  50    $base = array('attributes' => array('rel' => 'nofollow'));
  51    if (!empty($displays['year'])) {
  52      $calendar_links[] = $base + array('title' => date_t('Year', 'datetime'), 'href' => date_real_url($view, 'year'));
  53    }
  54    if (!empty($displays['month'])) {
  55      $calendar_links[] = $base + array('title' => date_t('Month', 'datetime'), 'href' => date_real_url($view, 'month'));
  56    }
  57    if (!empty($displays['week'])) {
  58      $calendar_links[] = $base + array('title' => date_t('Week', 'datetime'), 'href' => date_real_url($view, 'week'));
  59    }
  60    if (!empty($displays['day'])) {
  61      $calendar_links[] = $base + array('title' => date_t('Day', 'datetime'), 'href' => date_real_url($view, 'day'));
  62    }
  63    $vars['calendar_links'] = $calendar_links;
  64      
  65    // If the Date Popup module is enabled, add a popup date selector.
  66    if (!empty($view->date_info->calendar_popup)) {
  67      $vars['calendar_popup'] = '<div class="clear-block">'. calendar_date_select($view) .'</div>';
  68    }
  69    
  70    // If an 'Add new ... link is provided, add it here.
  71    // the query will bring the user back here after adding the node.
  72    if (!empty($view->date_info->calendar_date_link) && user_access('create '. $view->date_info->calendar_date_link .' content')) {
  73      $name = node_get_types('name', $view->date_info->calendar_date_link);
  74      $href = 'node/add/'. str_replace('_', '-', $view->date_info->calendar_date_link);
  75      $query = 'destination='. $view->date_info->url;    
  76      $vars['calendar_links'][] = $base + array(
  77        'title' => t('Add+'), 
  78        'href' => $href, 
  79        'query' => $query,
  80        );
  81    }
  82    
  83    $vars['view'] = $view;  
  84    $vars['mini'] = !empty($view->date_info->mini);
  85    $vars['block'] = !empty($view->date_info->block);
  86    $vars['block_identifier'] = date_block_identifier($view);
  87      
  88  }
  89  
  90  /**
  91   * Display a view as a calendar.
  92   * 
  93   * This preprocessor does all the work needed for all types of calendar
  94   * views and the template takes care of displaying links to related views.
  95   */
  96  function template_preprocess_calendar(&$vars) {
  97    require_once('./'. drupal_get_path('module', 'calendar_multiday') .'/includes/calendar.inc');
  98    $view     = $vars['view'];
  99    
 100    // Make sure we only run through this function one time.
 101    if (!empty($view->date_info->calendar_processed)) {
 102      return;
 103    }
 104      
 105    $result   = (array) $view->result;
 106    $options  = $view->style_plugin->options;
 107    $handler  = $view->style_plugin;
 108  
 109    $fields   = $view->field;
 110      
 111    // Render each field into an output array. We have to do the rendering
 112    // here because we don't apppear to have full access to the view 
 113    // handlers in the theme functions.
 114    $items = array();
 115    $calendar_fields = date_api_fields($view->base_table);
 116    $calendar_fields = array_keys($calendar_fields['alias']);
 117    
 118    foreach ($result as $num => $row) {
 119      $copy = drupal_clone($row);
 120      $items[$num] = $row;
 121      $items[$num]->raw = $copy;
 122      $items[$num]->calendar_fields = new stdClass();
 123  
 124      foreach ($row as $key => $value) {
 125        if (in_array($key, $calendar_fields)) {
 126          $items[$num]->calendar_fields->$key = $value;
 127        }
 128      }
 129      foreach ($fields as $name => $field) {
 130        // Some fields, like the node edit and delete links, have no alias.
 131        $field_alias = $field->field_alias != 'unknown' ? $field->field_alias : $name; 
 132        if (!empty($field) && is_object($field)) {
 133          // Theme the copy instead of the original row so duplicate date
 134          // fields each get a fresh copy of the original data to theme.
 135          $items[$num]->{$field_alias} = $field->theme($copy);
 136        }
 137      }
 138      foreach ($fields as $name => $field) {
 139        // Some fields, like the node edit and delete links, have no alias.
 140        $field_alias = $field->field_alias != 'unknown' ? $field->field_alias : $name; 
 141        if (!empty($field->options['exclude'])) {
 142          if (isset($items[$num]->{$field_alias})) unset($items[$num]->{$field_alias});
 143        }
 144      }
 145    }
 146    
 147    $vars['display_type'] = $view->date_info->granularity;
 148    $vars['min_date_formatted'] = date_format($view->date_info->min_date, DATE_FORMAT_DATETIME);
 149    $vars['max_date_formatted'] = date_format($view->date_info->max_date, DATE_FORMAT_DATETIME);
 150    
 151    // Massage the resulting items into formatted calendar items.
 152    $items = calendar_build_nodes($view, $items);
 153    
 154    // Merge in items from other sources.
 155    foreach (module_implements('calendar_add_items') as $module) {
 156      $function = $module .'_calendar_add_items';
 157      if (function_exists($function)) {
 158        if ($feeds = $function($view)) {
 159          foreach ($feeds as $feed) {
 160            $items = $feed;
 161          }
 162        }
 163      }
 164    }
 165  
 166    $view->date_info->mini = isset($view->date_info->mini) ? $view->date_info->mini : $view->date_info->granularity == 'year';
 167    
 168    // Create the calendar day names and rows.
 169    $rows = calendar_build_calendar($view, $items);
 170    
 171    $vars['items'] = $items;
 172    $vars['rows'] = $rows;
 173    $view->date_info->calendar_processed = TRUE;
 174    $vars['view'] = $view;  
 175    $vars['mini'] = !empty($view->date_info->mini);
 176    $vars['block'] = !empty($view->date_info->block);
 177  }
 178  
 179  /**
 180   * Display a month view.
 181   */
 182  function template_preprocess_calendar_month(&$vars) {
 183    // Add in all the $vars added by the main calendar preprocessor.
 184    template_preprocess_calendar($vars);
 185    $view = $vars['view'];
 186    
 187    $rows = $vars['rows'];
 188      
 189    if (sizeof($rows) > 1) {
 190      $day_names = array_shift($rows);
 191    }
 192    else {
 193      $day_names = $rows;
 194      $rows = array();
 195    }
 196    
 197    $month_rows = $rows;
 198    foreach ($rows as $weekno => $row) {
 199      foreach ($row as $day => $data) {
 200        $cell = $data['data'];
 201              
 202        // If this cell is already rendered, like the weekno column,
 203        // move to the next item.
 204        if (!is_array($cell)) {
 205          $month_rows[$weekno][$day]['data'] = $cell;
 206          continue;
 207        }
 208        $data = $cell['datebox'];
 209        if ($cell['empty']) {
 210          $data .= $cell['empty'];
 211        }
 212        else {
 213          $data .= implode($cell['all_day']);
 214          foreach ($cell['items'] as $hour => $item) {
 215            $data .= implode($item);
 216          }
 217          $data .= $cell['link'];
 218        }
 219        if ($view->date_info->mini) {
 220          $month_rows[$weekno][$day]['data'] = $data;
 221        }
 222        else {
 223          $month_rows[$weekno][$day]['data'] = '<div class="inner">'. $data .'</div>';
 224        }
 225      }
 226    }
 227    
 228    $vars['rows'] = $month_rows;
 229    $vars['day_names'] = $day_names;
 230    
 231    $vars['display_type'] = $view->date_info->granularity;
 232    $vars['min_date_formatted'] = date_format($view->date_info->min_date, DATE_FORMAT_DATETIME);
 233    $vars['max_date_formatted'] = date_format($view->date_info->max_date, DATE_FORMAT_DATETIME);
 234  }
 235  
 236  /**
 237   * Display a mini month view.
 238   */
 239  function template_preprocess_calendar_mini(&$vars) {
 240    // Add in all the $vars added by the main calendar preprocessor.
 241    template_preprocess_calendar_month($vars);
 242    
 243    $view = $vars['view'];
 244    $view->date_info->show_title = !empty($view->date_info->show_title) ? $view->date_info->show_title : FALSE;
 245    $vars['show_title'] = $view->date_info->show_title;
 246    $vars['view'] = $view;
 247  }
 248  
 249  /**
 250   * Display a year view.
 251   */
 252  function template_preprocess_calendar_year(&$vars) {
 253    // Add in all the $vars added by the main calendar preprocessor.
 254    $vars['view']->date_info->style_with_weekno = FALSE;
 255    template_preprocess_calendar($vars);
 256      
 257    // Get the url of the year view and remove the year argument from it.
 258    // TODO clean this up in case there is another arg that looks like
 259    // the year to make sure only the year gets removed.
 260    $view = $vars['view'];
 261    $year = date_format($view->date_info->min_date, 'Y');
 262      
 263    // Construct a calendar for each month, adjusting the $view passed 
 264    // to the theme so it will produce the right results.  
 265    $view = drupal_clone($vars['view']);
 266    $rows = $vars['rows'];
 267    $months = array();
 268    foreach ($rows as $month => $month_rows) {
 269      $view->date_info->month = $month;
 270      $view->date_info->granularity = 'month';
 271      $view->date_info->mini = TRUE;
 272      $view->date_info->hide_nav = TRUE;
 273      $view->date_info->show_title = TRUE;
 274      $view->date_info->url = date_real_url($view, NULL, date_pad($year, 4) .'-'. date_pad($month));
 275      $view->date_info->min_date = date_make_date($view->date_info->year .'-'. date_pad($month) .'-01 00:00:00', date_default_timezone_name());
 276      $view->date_info->max_date = drupal_clone($view->date_info->min_date);
 277      date_modify($view->date_info->max_date, '+1 month');
 278      date_modify($view->date_info->max_date, '-1 second');
 279      $months[$month] = theme('calendar_mini', $view, $vars['options'], $month_rows);
 280    }
 281    $vars['months'] = $months;
 282    $vars['view']->date_info->hide_nav = FALSE;
 283    $vars['view']->date_info->granularity = 'year';
 284    
 285  }
 286  
 287  /**
 288   * Display a day overlap view.
 289   */
 290  function template_preprocess_calendar_day_overlap(&$vars) {
 291    template_preprocess_calendar_day($vars);
 292  }
 293  
 294  /**
 295   * Display a day view.
 296   */
 297  function template_preprocess_calendar_day(&$vars) {
 298    
 299    // Add in all the $vars added by the main calendar preprocessor.
 300    $vars['view']->style_with_weekno = FALSE;
 301    template_preprocess_calendar($vars);
 302    
 303    $view = $vars['view'];
 304    $rows = $vars['rows'];
 305    
 306    $item_count = 0;
 307    $by_hour_count = 0;
 308    $grouping_field = $view->date_info->style_groupby_field;
 309    
 310    $display_overlap = $view->date_info->style_theme_style == '1';
 311    
 312    // Add optional css
 313    if ($display_overlap) {
 314      drupal_add_css(drupal_get_path('module', 'calendar_multiday')  .'/calendar-overlap.css');
 315      drupal_add_js(drupal_get_path('module', 'calendar_multiday') . '/calendar_overlap.js');
 316      $overlapped_items = array();
 317    }
 318    
 319    // If we're not grouping by time, move all items into the 'all day' array.
 320    if (empty($view->date_info->style_groupby_times)) {
 321      // Items are already grouped into times, so we need to process each time-group.
 322      foreach ($rows['items'] as $time => $items) {
 323        foreach ($items as $item) {
 324          $rows['all_day'][] = $item;
 325        }
 326      }
 327      $rows['items'] = array();
 328    }
 329          
 330    $columns = array();
 331    
 332    // Move all_day items into the right columns and render them.
 333    $grouped_items = array();
 334    foreach ($rows['all_day'] as $item) {
 335      if (isset($item->{$grouping_field})) {
 336        $column = $item->{$grouping_field};        
 337        $item->{$grouping_field} = ''; // Remove the grouping field from the results.
 338        if (!in_array($column, $columns)) {
 339          $columns[] = $column;
 340        }
 341      }
 342      else {
 343        $column = t('Items');
 344      }
 345      $theme = isset($item->calendar_node_theme) ? $item->calendar_node_theme : 'calendar_'. $view->date_info->granularity .'_node';
 346      $grouped_items[$column][] = theme($theme, $item, $view);
 347      $item_count++;
 348    }
 349    $vars['rows']['all_day'] = $grouped_items;
 350    
 351    // Moved timed items into the right columns and render them.
 352    $start_times = $view->date_info->style_groupby_times;
 353    $show_empty_times = $view->date_info->style_show_empty_times; 
 354    $end_start_time = '23:59:59';
 355    $start_time = array_shift($start_times);
 356    $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
 357    
 358    $grouped_items = array();
 359    foreach ($rows['items'] as &$items) {
 360      foreach ($items as &$item) {
 361        $time = date_format($item->date_start, 'H:i:s');
 362        if (isset($item->{$grouping_field})) {
 363          $column = $item->{$grouping_field};
 364          $item->{$grouping_field} = ''; // Remove the grouping field from the results.
 365          if (!in_array($column, $columns)) {
 366            $columns[] = $column;
 367          }
 368        }
 369        else {
 370          $column = t('Items');
 371        }
 372        // Find the next time slot and fill it. Populate the skipped
 373        // slots if the option to show empty times was chosen.
 374        while ($time >= $next_start_time && $time < $end_start_time) {
 375          if ((!empty($show_empty_times) || $display_overlap) && !array_key_exists($start_time, $grouped_items)) {
 376            $grouped_items[$start_time]['values'] = array();        
 377          }
 378          $start_time = $next_start_time;
 379          $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
 380        }
 381        $grouped_items[$start_time]['values'][$column][] = $item;
 382        if ($display_overlap) {
 383          $time_end = date_format($item->date_end, 'H:i:s');
 384          $item->time_start = $time;
 385          $item->time_end = $time_end; 
 386          _calc_indents($overlapped_items, $time, $time_end, $item);
 387        }
 388        $item_count++;
 389        $by_hour_count++;
 390      }
 391    }
 392    
 393    // Finish out the day's time values if we want to see empty times.
 394    if (!empty($show_empty_times) || $display_overlap) {
 395      while ($start_time < $end_start_time && ($start_time != NULL || $display_overlap)) {
 396        if ($start_time == NULL) {
 397          $start_times = $view->date_info->style_groupby_times;
 398          $start_time = array_shift($start_times);
 399          $next_start_time = array_shift($start_times);
 400        }
 401        if (!array_key_exists($start_time, $grouped_items)) {
 402          $grouped_items[$start_time]['values'] = array();        
 403        }
 404        $start_time = $next_start_time;
 405        $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
 406      }
 407    }
 408    
 409    // Do the headers last, once we know what the actual values are.
 410    $i = 0;
 411    $start_times = array_keys($grouped_items);
 412    foreach ($start_times as $start_time) {
 413      $next_start_time = array_key_exists($i + 1, $start_times) ? $start_times[$i + 1] : '23:59:59';
 414      $heading = theme('calendar_time_row_heading', $start_time, $next_start_time, $rows['date']);
 415      $grouped_items[$start_time]['hour'] = $heading['hour'];
 416      $grouped_items[$start_time]['ampm'] = $heading['ampm']; 
 417      foreach ($grouped_items[$start_time]['values'] as $column => &$items) {
 418        foreach ($items as $index => &$item) {
 419          if ($display_overlap) {
 420            $offset = round(date_format($item->date_start, 'i') / 15);
 421            $duration = date_convert('1970-01-01 ' . $item->time_end, DATE_DATETIME, DATE_UNIX) - 
 422                        date_convert('1970-01-01 ' . $item->time_start, DATE_DATETIME, DATE_UNIX);
 423            $duration = round($duration / (15 * 60));
 424            $item->class = 'd_'. $duration . ' o_'. $offset . ' i_' . $item->indent . ' md_' . min($item->max_depth, 5);
 425          }
 426          $theme = isset($item->calendar_node_theme) ? $item->calendar_node_theme : 'calendar_'. $view->date_info->granularity .'_node';
 427          $grouped_items[$start_time]['values'][$column][$index] = theme($theme, $item, $view);
 428        } 
 429      }
 430      $i++;      
 431    }
 432    ksort($grouped_items);
 433    $vars['rows']['items'] = $grouped_items;
 434    
 435    if (empty($columns)) {
 436      $columns = array(t('Items'));
 437    }
 438    $vars['columns'] = $columns;
 439    
 440    $vars['agenda_hour_class'] = 'calendar-agenda-hour';
 441    $first_column_width = 10;
 442    
 443    if (empty($view->date_info->style_groupby_times)) {
 444      $vars['agenda_hour_class'] .= ' calendar-agenda-no-hours';
 445      $first_column_width = 1;
 446    }
 447    
 448    $vars['first_column_width'] = $first_column_width;
 449    if (count($columns)) {
 450      $vars['column_width'] = round((100 - $first_column_width)/count($columns));
 451    }
 452    else {
 453      $vars['column_width'] = (100 - $first_column_width);
 454    }
 455    $vars['item_count'] = $item_count;
 456    $vars['by_hour_count'] = $by_hour_count;
 457    $vars['start_times'] = $view->date_info->style_groupby_times;
 458    return;
 459  }
 460  
 461  /**
 462   * Display a week overlap view.
 463   */
 464  function template_preprocess_calendar_week_overlap(&$vars) {
 465    template_preprocess_calendar_week($vars);
 466  }
 467  
 468  /**
 469   * Display a week view.
 470   */
 471  function template_preprocess_calendar_week(&$vars) {
 472    // Add in all the $vars added by the main calendar preprocessor.
 473    $vars['view']->style_with_weekno = FALSE;
 474    template_preprocess_calendar($vars);
 475    
 476    $view = $vars['view'];
 477    $rows = $vars['rows'];
 478    $item_count = 0;
 479    $by_hour_count = 0;
 480    $start_time = NULL;
 481    
 482    if (sizeof($rows) > 1) {
 483      $day_names = array_shift($rows);
 484    }
 485    else {
 486      $day_names = $rows;
 487      $rows = array();
 488    }
 489    
 490    // Moved timed items into the right columns and render them.
 491    $show_empty_times = $view->date_info->style_show_empty_times; 
 492    $end_start_time = '23:59:59';
 493    
 494    $grouped_items = array();
 495    
 496    // pass the multiday buckets
 497    $vars['all_day'] = $rows['multiday_buckets'];
 498  
 499    // Remove the count for singleday
 500    $vars['multiday_rows'] = max(0, $rows['total_rows'] - 1); 
 501    $display_overlap = ($view->date_info->style_multiday_theme == '1' && $view->date_info->style_theme_style == '1');
 502    $vars['display_overlap'] = $display_overlap;
 503    
 504    // Add optional css
 505    if ($display_overlap) {
 506      drupal_add_css(drupal_get_path('module', 'calendar_multiday')  .'/calendar-overlap.css');
 507      drupal_add_js(drupal_get_path('module', 'calendar_multiday') . '/calendar_overlap.js');
 508      $overlapped_items = array( array(), array(), array(), array(), array(), array(), array());
 509      
 510      // Locate the first item
 511      $first_time = '23:59:59';
 512      $first_time_index = -1;
 513      for ($i = 0; $i < 7; $i++) {
 514        if (count($rows['singleday_buckets'][$i]) > 0) {
 515          $time_slot = reset($rows['singleday_buckets'][$i]);
 516          $time = date_format($time_slot[0]['item']->date_start, 'H:i:s');
 517          if ($time < $first_time) {
 518            $first_time = $time;
 519            $first_time_index = $i;
 520          }
 521        } 
 522      }
 523      if ($first_time_index > -1) {
 524        $rows['singleday_buckets'][$first_time_index][$first_time][0]['is_first'] = TRUE;
 525      }
 526    }
 527    
 528    // If we're not grouping by time, move all items into the 'all day' array.
 529    if (empty($view->date_info->style_groupby_times)) {
 530      $add_row = FALSE;
 531      foreach ($vars['all_day'] as $index => &$day ) {
 532        foreach ($rows['singleday_buckets'][$index] as $item) {
 533          foreach ($item as $event) {
 534            $day[] = $event;
 535            $add_row = TRUE;
 536          }
 537        }
 538      }
 539      if ( $add_row ) {
 540        $vars['multiday_rows']++; 
 541      }
 542    } 
 543    else {
 544      foreach ($rows['singleday_buckets'] as $wday => $singleday_row) {
 545        $columns[] = $wday;
 546        foreach ($singleday_row as &$row) {
 547          $start_times = $view->date_info->style_groupby_times;
 548          $start_time = array_shift($start_times);
 549          $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
 550          foreach ($row as &$item) {
 551            $time = date_format($item['item']->date_start, 'H:i:s');
 552            if ($item['item']->calendar_all_day || ($item['item']->date_start == $item['item']->date_end)) {
 553              $vars['all_day'][$item['wday']][] = $item;
 554              if ($vars['multiday_rows'] == 0) {
 555                $vars['multiday_rows']++;
 556              }
 557            } 
 558            else {
 559              // Find the next time slot and fill it. Populate the skipped
 560              // slots if the option to show empty times was chosen.
 561              while ($time >= $next_start_time && $time < $end_start_time) {
 562                if (($show_empty_times || $display_overlap) && !array_key_exists($start_time, $grouped_items)) {
 563                  $grouped_items[$start_time]['values'][$wday] = array();   
 564                }
 565                $start_time = $next_start_time;
 566                $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
 567              }
 568              $grouped_items[$start_time]['values'][$wday][] = &$item; 
 569              if ($display_overlap) {
 570                $date_end = date_format($item['item']->date_end, 'H:i:s');
 571                _calc_indents($overlapped_items[$wday], $time, $date_end, $item);
 572              }
 573              $item_count++; 
 574              $by_hour_count++;
 575            }
 576          }
 577        }
 578        // Finish out the day's time values if we want to see empty times.
 579        if ($show_empty_times || $display_overlap) {
 580          while ($start_time < $end_start_time && ($start_time != NULL || $display_overlap)) {
 581            if ($start_time == NULL) {
 582              $start_times = $view->date_info->style_groupby_times;
 583              $start_time = array_shift($start_times);
 584              $next_start_time = array_shift($start_times);
 585            }
 586            if (!array_key_exists($start_time, $grouped_items)) {
 587              $grouped_items[$start_time]['values'][$wday] = array();        
 588            }
 589            $start_time = $next_start_time;
 590            $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
 591          }
 592        }
 593        ksort($grouped_items);
 594      }
 595    }
 596    
 597    // Do the headers last, once we know what the actual values are.
 598    $i = 0;
 599    $start_times = array_keys($grouped_items);
 600    foreach ($start_times as $start_time) {
 601      $next_start_time = array_key_exists($i + 1, $start_times) ? $start_times[$i + 1] : '23:59:59';
 602      $heading = theme('calendar_time_row_heading', $start_time, $next_start_time, (isset($row['data'])) ? $row['data']['date'] : NULL);
 603      $grouped_items[$start_time]['hour'] = $heading['hour'];
 604      $grouped_items[$start_time]['ampm'] = $heading['ampm'];
 605      $grouped_items[$start_time]['time'] = $start_time;
 606      if ($display_overlap) {
 607        foreach ($grouped_items[$start_time]['values'] as $wday => &$items) {
 608          foreach ($items as &$item) {
 609            if ($display_overlap) {
 610              $offset = round(date_format($item['item']->date_start, 'i') / 15);
 611              $duration = date_convert($item['item']->date_end, DATE_OBJECT, DATE_UNIX) - date_convert($item['item']->date_start, DATE_OBJECT, DATE_UNIX);
 612              $duration = round($duration / (15 * 60));
 613              $item['class'] = 'd_'. $duration . ' o_'. $offset . ' i_' . $item['indent'] . ' md_' . min($item['max_depth'], 5);
 614            }
 615          } 
 616        }
 617      }
 618    }
 619  
 620    $vars['items'] = $grouped_items;  
 621    
 622    $vars['day_names'] = $day_names;
 623    $vars['columns'] = $columns;
 624    $vars['start_times'] = $view->date_info->style_groupby_times;
 625    $vars['first_time'] = $first_time;
 626    
 627    $vars['agenda_hour_class'] = 'calendar-agenda-hour';
 628    $first_column_width = 10;
 629    
 630    if (empty($view->date_info->style_groupby_times)) {
 631      $vars['agenda_hour_class'] .= ' calendar-agenda-no-hours';
 632      $first_column_width = 1;
 633    }
 634    $vars['item_count'] = $item_count;
 635    $vars['by_hour_count'] = $by_hour_count;
 636    return;
 637  }
 638  
 639  /**
 640   * Create the calendar date box.
 641   */
 642  function template_preprocess_calendar_datebox(&$vars) {
 643    $date = $vars['date'];
 644    $view = $vars['view'];
 645    
 646    $vars['day'] = intval(substr($date, 8, 2));
 647    $force_view_url = !empty($view->date_info->block) ? TRUE : FALSE;
 648    $vars['url'] = date_real_url($view, NULL, $date, $force_view_url);
 649    $vars['link'] = l($vars['day'], $vars['url']);
 650    $vars['granularity'] = $view->date_info->granularity;
 651    $vars['mini'] = $view->date_info->mini;
 652    
 653    if ($view->date_info->mini) {
 654      if (!empty($vars['selected'])) {
 655        $vars['class'] = 'mini-day-on';
 656      }
 657      else {
 658        $vars['class'] = 'mini-day-off';
 659      }
 660    }
 661    else {
 662      $vars['class'] = 'day';
 663    }
 664  }
 665  
 666  /**
 667   * Format an calendar node for display.
 668   */
 669  function template_preprocess_calendar_node(&$vars) {
 670    $node = $vars['node'];
 671    $view = $vars['view'];
 672    $fields = array();
 673    foreach ($view->field as $name => $field) {
 674      // Some fields, like the node edit and delete links, have no alias.
 675      $field_alias = $field->field_alias != 'unknown' ? $field->field_alias : $name; 
 676      if (!empty($node->$field_alias)) {
 677        $data = $node->$field_alias;
 678        $label = $field->options['label'];
 679        
 680        // CCK has some special label options.
 681        if (!empty($field->content_field)) {
 682          switch ($field->options['label_type']) {
 683            case 'none':
 684              $label = '';
 685              break;
 686            case 'widget':
 687              $label = $field->content_field['widget']['label'];
 688              break;
 689          }
 690        }
 691        $fields[$field_alias] = array(
 692          'id' => views_css_safe($field_alias),
 693          'label' => $label,
 694          'data' => $data,
 695          );
 696      }
 697    }
 698    $vars['fields'] = $fields;
 699    $vars['calendar_start'] = $node->calendar_start;
 700    $vars['calendar_end'] = $node->calendar_end;
 701    $vars['calendar_start_date'] = $node->calendar_start_date;
 702    $vars['calendar_end_date'] = $node->calendar_end_date;
 703    
 704    // We added the node type to the results in the query,
 705    // but it will show up as $node->node_type instead of
 706    // $node->type. Rename it to match the normal way it 
 707    // would show up on a node object.
 708    $vars['node']->type = $vars['node']->node_type;
 709  }
 710  
 711  /**
 712   * Format an calendar month node for display.
 713   */
 714  function template_preprocess_calendar_month_node(&$vars) {
 715    template_preprocess_calendar_node($vars);
 716  }
 717  
 718  /**
 719   * Format an calendar month node for display.
 720   */
 721  function template_preprocess_calendar_month_multiple_node(&$vars) {
 722    $view = $vars['view'];
 723    $curday = $vars['curday'];
 724  
 725    // get the year month and date
 726    $parts = explode('-', substr($curday, 0, 10));
 727    $year = $parts[0];
 728    $month = intval($parts[1]);
 729    $day = intval($parts[2]);
 730    
 731    // create the link to the day
 732    $vars['link'] = date_real_url($view, NULL, date_pad($year, 4) .'-'. date_pad($month) .'-'. date_pad($day));
 733  }
 734  
 735  /**
 736   * Format an calendar day node for display.
 737   */
 738  function template_preprocess_calendar_day_node(&$vars) {
 739    template_preprocess_calendar_node($vars);
 740    $node = $vars['node'];
 741    // Remote items may have a teaser to show.
 742    if (!empty($node->remote) && !empty($node->teaser)) {
 743      $fields['teaser'] = '<div class="content">'. ($node->teaser) ."</div>\n";
 744    }
 745  }
 746  
 747  /**
 748   * Format an calendar week node for display.
 749   */
 750  function template_preprocess_calendar_week_node(&$vars) {
 751    template_preprocess_calendar_node($vars);
 752  }
 753  
 754  /**
 755   * Format an calendar week node for display.
 756   */
 757  function template_preprocess_calendar_week_multiple_node(&$vars) {
 758    $view = $vars['view'];
 759    $curday = $vars['curday'];
 760    
 761    // get the year month and date
 762    $parts = explode('-', substr($curday, 0, 10));
 763    $year = $parts[0];
 764    $month = intval($parts[1]);
 765    $day = intval($parts[2]);
 766    
 767    // create the link to the day
 768    $vars['link'] = date_real_url($view, NULL, date_pad($year, 4) .'-'. date_pad($month) .'-'. date_pad($day));
 769  }
 770  
 771  /**
 772   * Format the time row headings in the week and day view.
 773   */
 774  function theme_calendar_time_row_heading($start_time, $next_start_time, $curday_date) {
 775    static $format_hour, $format_ampm;
 776    if (empty($format_hour)) {
 777      $format = variable_get('date_format_short', 'm/d/Y - H:i');
 778      $format_hour = str_replace(array('a', 'A'), '', date_limit_format($format, array('hour', 'minute')));
 779      $format_ampm = strstr($format, 'a') ? 'a' : (strstr($format, 'A') ? 'A' : '');
 780    }
 781    if ($start_time == '00:00:00' && $next_start_time == '23:59:59') {
 782      $hour = t('All times');
 783    }
 784    elseif ($start_time == '00:00:00') {
 785      $date = date_create($curday_date .' '. $next_start_time);
 786      $hour = t('Before @time', array('@time' => date_format($date, $format_hour)));
 787    }
 788    else {
 789      $date = date_create($curday_date .' '. $start_time);
 790      $hour = date_format($date, $format_hour);
 791    }
 792    if (!empty($date)) {
 793      $ampm = date_format($date, $format_ampm);
 794    }
 795    else {
 796      $ampm = '';
 797    }
 798    return array('hour' => $hour, 'ampm' => $ampm);
 799  }
 800  
 801  /**
 802   * Format a node stripe legend
 803   */
 804  function theme_calendar_stripe_legend() {
 805    if (empty($GLOBALS['calendar_stripes'])) {
 806      return '';
 807    }
 808    $header = array(
 809        array('class' => 'calendar-legend', 'data' => t('Item')),
 810        array('class' => 'calendar-legend', 'data' => t('Key'))
 811        );
 812    $rows = array();
 813    $output = '';    
 814    foreach ((array) $GLOBALS['calendar_stripes'] as $label => $stripe) {
 815      if ($stripe) {
 816        $rows[] = array($label, '<div style="background-color:'. $stripe .';color:'. $stripe .'" class="stripe" title="Key: '. $label .'">&nbsp;</div>');
 817      }
 818    }
 819    if (!empty($rows)) {
 820      $output .= theme('table', $header, $rows, array('class' => 'mini calendar-legend'));
 821    }
 822    return $output;
 823  }
 824  
 825  /**
 826   * Format node stripes
 827   */
 828  function theme_calendar_stripe_stripe($node) {
 829    if (empty($node->stripe) || (!count($node->stripe))) {
 830      return;
 831    }
 832    $output = '';
 833    if (is_array($node->stripe_label)) {
 834      foreach ($node->stripe_label as $k => $stripe_label) {
 835        if (!empty($node->stripe[$k]) && !empty($stripe_label)) {
 836          $GLOBALS['calendar_stripes'][$stripe_label] = $node->stripe[$k];
 837          $output .= '<div style="background-color:'. $node->stripe[$k] .';color:'. $node->stripe[$k] .'" class="stripe" title="Key: '. $node->stripe_label[$k] .'">&nbsp;</div>'."\n";
 838        }
 839      }
 840    }
 841    return $output;
 842  }
 843  
 844  /**
 845   * Format an empty day on a calendar
 846   *
 847   * @param day
 848   *   The day to display.
 849   */
 850  function theme_calendar_empty_day($curday, $view) {
 851    
 852    if ($view->date_info->calendar_type != 'day') {
 853      return '<div class="calendar-empty">&nbsp;</div>'."\n";
 854    }
 855    else {
 856      return '<div class="calendar-dayview-empty">'. t('Empty day') .'</div>';
 857    }
 858  }
 859  
 860  
 861  /**
 862   * Indent items based off a nested tree structure of overlapping items
 863   *
 864   * @param array $overlapped_items
 865   *   Tree of overlapped items
 866   * @param date $start
 867   *   Start time of the event
 868   * @param date $end
 869   *   End tiem of the event
 870   * @param array $item
 871   *   The event to add to the tree
 872   * @param int $depth
 873   *   Current depth of the tree
 874   * @return rc
 875   *   Returns an array with the max depth of the branch and whether an overlap occurred
 876   */
 877  function _calc_indents(&$overlapped_items, $start, $end, &$item, $depth = 0) {
 878    
 879    // Are there any items at this depth?
 880    if (!empty($overlapped_items)) {
 881      
 882      // Iterate for each item as this depth and see if we overlap
 883      foreach ($overlapped_items as $index => &$entry) { 
 884  
 885        // We search depth-first, so if there are children for this item, recurse into
 886        // each child tree looking for an overlap
 887        if (!empty($entry['children'])) {
 888          $rc = _calc_indents($entry['children'], $start, $end, $item, $depth + 1);
 889          
 890          // Was there an overlap in the child tree?
 891          if ($rc['overlap']) {
 892            if (is_object($entry['item'])) {
 893              $entry['item']->indent = _calc_indent($entry['depth'], $rc['max_depth']);
 894              $entry['item']->max_depth = $rc['max_depth'];
 895            }
 896            else {
 897              $entry['item']['indent'] = _calc_indent($entry['depth'], $rc['max_depth']);
 898              $entry['item']['max_depth'] = $rc['max_depth'];
 899            }
 900            
 901            // There was an overlap, pop out of this depth
 902            return $rc;
 903          }
 904        }
 905        
 906        // No, child overlap, so check if we overlap this item
 907        if ($start >= $entry['start'] && $start <= $entry['end']) {
 908          
 909          // We overlap, create an overlapping entry
 910          $entry['children'][] = array('item' => &$item, 'depth' => $depth + 1, 'start' => $start, 'end' => $end, 'children' => array());
 911          if (is_object($entry['item'])) {
 912            $max_depth = max($entry['item']->max_depth, $depth + 1);
 913            $entry['item']->indent = _calc_indent($depth, $max_depth);
 914            $entry['item']->max_depth = $max_depth;
 915          }
 916          else {
 917            $max_depth = max($entry['item']['max_depth'], $depth + 1);
 918            $entry['item']['indent'] = _calc_indent($depth, $max_depth);
 919            $entry['item']['max_depth'] = $max_depth;
 920          }
 921          if (is_object($item)) {
 922            $item->indent = _calc_indent($depth + 1, $max_depth);
 923            $item->max_depth = $max_depth;
 924          }
 925          else {
 926            $item['indent'] = _calc_indent($depth + 1, $max_depth);
 927            $item['max_depth'] = $max_depth;
 928          }
 929          
 930          // We overlap, so pop out of this depth
 931          return array('overlap' => TRUE, 'max_depth' => $max_depth);
 932        }
 933      }
 934      
 935      // If there are items at this depth, but no overlap, then return no overlap and pop
 936      // out of this depth
 937      if ($depth > 0) {
 938        return array('overlap' => FALSE, 'max_depth' => 0);
 939      }
 940     }
 941    
 942    // No overlap at any depth, reset the array of overlaps
 943    if ($depth == 0) {
 944     reset($overlapped_items);
 945     $overlapped_items[0] = array('item' => &$item, 'depth' => $depth, 'start' => $start, 'end' => $end, 'children' => array());
 946    }
 947    else {
 948      $overlapped_items[] = array('item' => &$item, 'depth' => $depth, 'start' => $start, 'end' => $end, 'children' => array());
 949    }
 950    
 951    if (is_object($item)) {
 952      $item->indent = _calc_indent($depth, $depth);
 953      $item->max_depth = $depth;
 954    }
 955    else {
 956      $item['indent'] = _calc_indent($depth, $depth);
 957      $item['max_depth'] = $depth;
 958    }
 959    return array('overlap' => FALSE, 'max_depth' => $depth);
 960  }
 961  
 962  /**
 963   * Calculates the indent based of the current depth and the depth of this branch in the tree
 964   *
 965   * @param int $cur_depth
 966   * @param int $depth
 967   * @return number
 968   */
 969  function _calc_indent( $cur_depth, $depth ) {
 970    return round(10 * $cur_depth / ($depth + 1));
 971  }
 972  /** @} End of addtogroup themeable */


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7