You are here

function calendar_build_week_day in Calendar 7

Same name and namespace in other branches
  1. 6.2 calendar_multiday/includes/calendar.inc \calendar_build_week_day()
  2. 7.2 calendar_multiday/includes/calendar.inc \calendar_build_week_day()

Build the contents of a single day for the $rows results.

1 call to calendar_build_week_day()
calendar_build_week in calendar_multiday/includes/calendar.inc
Build one week row.

File

calendar_multiday/includes/calendar.inc, line 431
Calendar building functions for the Calendar module.

Code

function calendar_build_week_day($curday, $view, $items, $wday, &$multiday_buckets, &$singleday_buckets) {
  $curday_date = date_format($curday, DATE_FORMAT_DATE);
  $max_events = !empty($view->date_info->style_max_items) ? $view->date_info->style_max_items : 0;
  $hide = !empty($view->date_info->style_max_items_behavior) ? $view->date_info->style_max_items_behavior == 'hide' : FALSE;
  $multiday_theme = $view->date_info->style_multiday_theme == '1';
  $cur_cnt = 0;
  $total_cnt = 0;
  $types = array();

  // If we are hiding, count before processing further
  if ($max_events != CALENDAR_SHOW_ALL) {
    foreach ($items as $date => $day) {
      if ($date == $curday_date) {
        foreach ($day as $time => $hour) {
          foreach ($hour as $key => $item) {
            $total_cnt++;
            $types[$item->type] = $item;
          }
        }
      }
    }
  }

  // If we haven't already exceeded the max or we'll showing all, then process the items
  if ($max_events == CALENDAR_SHOW_ALL || !$hide || $total_cnt <= $max_events) {

    // Count currently filled items
    foreach ($multiday_buckets[$wday] as $bucket) {
      if (!$bucket['avail']) {
        $cur_cnt++;
      }
    }
    foreach ($items as $date => $day) {
      if ($date == $curday_date) {
        $count = 0;
        ksort($day);
        foreach ($day as $time => $hour) {
          foreach ($hour as $key => $item) {
            $count++;

            // Can we add an item?
            if ($max_events == CALENDAR_SHOW_ALL || $cur_cnt <= $max_events) {
              $all_day = $item->calendar_start_date == $item->calendar_end_date;
              $theme = isset($item->calendar_node_theme) ? $item->calendar_node_theme : 'calendar_' . $view->date_info->granularity . '_node';

              // Parse out date part
              $start_ydate = date_format($item->date_start, DATE_FORMAT_DATE);
              $end_ydate = date_format($item->date_end, DATE_FORMAT_DATE);
              $cur_ydate = date_format($curday, DATE_FORMAT_DATE);
              $is_multi_day = $start_ydate < $cur_ydate || $end_ydate > $cur_ydate;

              // Does this event span multi-days?
              if ($multiday_theme && ($is_multi_day || $all_day)) {

                // If this the first day of the week, or is the start date of the multi-day event,
                // then record this item, otherwise skip over
                $day_no = date_format($curday, 'd');
                if ($wday == 0 || $start_ydate == $cur_ydate || $view->date_info->granularity == 'month' && $day_no == 1 || $all_day && !$is_multi_day) {
                  $cur_cnt++;

                  // Calculate the colspan for this event
                  // If the last day of this event exceeds the end of the current month or week,
                  // truncate the remaining days
                  $diff = $curday
                    ->difference($view->date_info->max_date, 'days');
                  $remaining_days = $view->date_info->granularity == 'month' ? min(6 - $wday, $diff) : $diff - 1;

                  // The bucket_cnt defines the colspan.  colspan = bucket_cnt + 1
                  $days = $curday
                    ->difference($item->date_end, 'days');
                  $bucket_cnt = max(0, min($days, $remaining_days));

                  // See if there is an avaiable slot to add an event.  This will allow
                  // an event to precede a row filled up by a previous day event
                  $avail = FALSE;
                  $bucket_index = count($multiday_buckets[$wday]);
                  for ($i = 0; $i < $bucket_index; $i++) {
                    if ($multiday_buckets[$wday][$i]['avail']) {
                      $bucket_index = $i;
                      break;
                    }
                  }

                  // Add continuation attributes
                  $item->continuation = $item->date_start < $curday;
                  $item->continues = $days > $bucket_cnt;

                  // Assign the item to the available bucket
                  $multiday_buckets[$wday][$bucket_index] = array(
                    'colspan' => $bucket_cnt + 1,
                    'rowspan' => 1,
                    'filled' => TRUE,
                    'avail' => FALSE,
                    'all_day' => $all_day,
                    'item' => $item,
                    'wday' => $wday,
                    'entry' => theme($theme, array(
                      'node' => $item,
                      'view' => $view,
                    )),
                  );

                  // Block out empty buckets for the next days in this event for this week
                  for ($i = 0; $i < $bucket_cnt; $i++) {
                    $bucket =& $multiday_buckets[$i + $wday + 1];
                    $bucket_row_count = count($bucket);
                    $row_diff = $bucket_index - $bucket_row_count;

                    // Fill up the preceding buckets - these are available for future
                    // events
                    for ($j = 0; $j < $row_diff; $j++) {
                      $bucket[$bucket_row_count + $j] = array(
                        'entry' => '&nbsp;',
                        'colspan' => 1,
                        'rowspan' => 1,
                        'filled' => TRUE,
                        'avail' => TRUE,
                        'wday' => $wday,
                        'item' => NULL,
                      );
                    }
                    $bucket[$bucket_index] = array(
                      'filled' => FALSE,
                      'avail' => FALSE,
                    );
                  }
                }
              }
              else {
                $cur_cnt++;

                // Assign to single day bucket
                $singleday_buckets[$wday][$time][] = array(
                  'entry' => theme($theme, array(
                    'node' => $item,
                    'view' => $view,
                  )),
                  'item' => $item,
                  'colspan' => 1,
                  'rowspan' => 1,
                  'filled' => TRUE,
                  'avail' => FALSE,
                  'wday' => $wday,
                );
              }
            }
            else {
              break;

              // exceeded count
            }
          }
        }
      }
    }
  }

  // Add a more link if necessary
  if ($max_events != CALENDAR_SHOW_ALL && $total_cnt > 0 && $cur_cnt < $total_cnt) {
    $singleday_buckets[$wday][][] = array(
      'entry' => theme('calendar_' . $view->date_info->calendar_type . '_multiple_node', array(
        'curday' => $curday_date,
        'count' => $total_cnt,
        'view' => $view,
        'types' => $types,
      )),
      'more_link' => TRUE,
      'item' => NULL,
    );
  }
}