You are here

function template_preprocess_calendar_week in Calendar 8

Same name and namespace in other branches
  1. 6.2 theme/theme.inc \template_preprocess_calendar_week()
  2. 6.2 calendar_multiday/theme/theme.inc \template_preprocess_calendar_week()
  3. 7.3 theme/theme.inc \template_preprocess_calendar_week()
  4. 7 theme/theme.inc \template_preprocess_calendar_week()
  5. 7 calendar_multiday/theme/theme.inc \template_preprocess_calendar_week()
  6. 7.2 theme/theme.inc \template_preprocess_calendar_week()
  7. 7.2 calendar_multiday/theme/theme.inc \template_preprocess_calendar_week()

Display a week view.

1 call to template_preprocess_calendar_week()
template_preprocess_calendar_week_overlap in ./calendar.theme.inc
Display a week overlap view.

File

./calendar.theme.inc, line 351
Theme functions for the Calendar module.

Code

function template_preprocess_calendar_week(&$vars) {
  $vars['view']->styleInfo
    ->setShowWeekNumbers(FALSE);
  $view = $vars['view'];
  $rows = $vars['rows'];
  $item_count = 0;
  $by_hour_count = 0;
  $start_time = NULL;
  $columns = [];
  if (count($rows) > 1) {
    $day_names = array_shift($rows);
  }
  else {
    $day_names = $rows;
    $rows = [];
  }

  // Moved timed items into the right columns and render them.
  $show_empty_times = $view->styleInfo
    ->isShowEmptyTimes();
  $end_start_time = '23:59:59';
  $grouped_items = [];

  // Pass the multiday buckets.
  $vars['all_day'] = $rows['multiday_buckets'];

  // Remove the count for singleday.
  $vars['multiday_rows'] = max(0, $rows['total_rows'] - 1);
  $display_overlap = $view->styleInfo
    ->getMultiDayTheme() == '1' && !empty($view->styleInfo
    ->getThemeStyle());
  $vars['display_overlap'] = $display_overlap;
  $vars['scroll_content'] = !empty($view->styleInfo
    ->getThemeStyle()) && $view->styleInfo
    ->getThemeStyle() == 1;

  // Add optional css.
  if ($display_overlap) {
    $vars['#attached']['library'][] = 'calendar/calendar.overlapcss';
    if ((empty($vars['view']->live_preview) || !$vars['view']->live_preview) && !empty($vars['scroll_content'])) {
      $vars['#attached']['library'][] = 'calendar/calendar.overlapjs';
    }
    if (empty($vars['scroll_content'])) {
      $vars['#attached']['library'][] = 'calendar/calendar.noscroll';
    }
    $overlapped_items = [
      [],
      [],
      [],
      [],
      [],
      [],
      [],
    ];

    // Locate the first item.
    $first_time = '23:59:59';
    $first_time_index = -1;
    for ($i = 0; $i < 7; $i++) {
      if (count($rows['singleday_buckets'][$i]) > 0) {
        $time_slot = reset($rows['singleday_buckets'][$i]);

        // @todo item should not be empty at this point, find out why empty days
        // get to this point, fix that, and remove this hack.
        if (isset($time_slot[0]['item'])) {
          $time = date_format($time_slot[0]['item']
            ->getStartDate(), 'H:i:s');
          if ($time < $first_time) {
            $first_time = $time;
            $first_time_index = $i;
          }
        }
      }
    }
    if ($first_time_index > -1) {
      $rows['singleday_buckets'][$first_time_index][$first_time][0]['is_first'] = TRUE;
    }
  }

  // If we're not grouping by time, move all items into the 'all day' array.
  if (empty($view->styleInfo
    ->getGroupByTimes())) {
    $add_row = FALSE;
    foreach ($vars['all_day'] as $index => &$day) {
      foreach ($rows['singleday_buckets'][$index] as $item) {
        foreach ($item as $event) {
          $day[] = $event;
          $add_row = TRUE;
        }
      }
    }
    if ($add_row) {
      $vars['multiday_rows']++;
    }
  }
  else {
    foreach ($rows['singleday_buckets'] as $wday => $singleday_row) {
      $columns[] = $wday;
      foreach ($singleday_row as &$row) {
        $start_times = $view->styleInfo
          ->getGroupByTimes();
        $start_time = array_shift($start_times);
        $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
        foreach ($row as &$item) {

          // @todo find out how empty days should be handled properly.
          if (isset($item['entry']) && $item['entry']['#theme'] === 'calendar_empty_day') {
            continue;
          }

          // @todo find out what to do when the item is no event.
          if (!isset($item['item']) || !is_object($item['item'])) {
            continue;
          }
          $time = date_format($item['item']
            ->getStartDate(), 'H:i:s');
          if ($item['item']
            ->isAllDay()) {
            $vars['all_day'][$item['wday']][] = $item;
            if ($vars['multiday_rows'] == 0) {
              $vars['multiday_rows']++;
            }
          }
          else {

            // Find the next time slot and fill it. Populate the skipped
            // slots if the option to show empty times was chosen.
            while ($time >= $next_start_time && $time < $end_start_time) {
              if (($show_empty_times || $display_overlap) && !array_key_exists($start_time, $grouped_items)) {
                $grouped_items[$start_time]['values'][$wday] = [];
              }
              $start_time = $next_start_time;
              $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
            }
            $grouped_items[$start_time]['values'][$wday][] =& $item;
            if ($display_overlap) {
              $date_end = date_format($item['item']
                ->getEndDate(), 'H:i:s');
              _calc_indents($overlapped_items[$wday], $time, $date_end, $item);
            }
            $item_count++;
            $by_hour_count++;
          }
        }
      }

      // Finish out the day's time values if we want to see empty times.
      if ($show_empty_times || $display_overlap) {
        while ($start_time < $end_start_time && ($start_time != NULL || $display_overlap)) {
          if ($start_time == NULL) {
            $start_times = $view->styleInfo
              ->getGroupByTimes();
            $start_time = array_shift($start_times);
            $next_start_time = array_shift($start_times);
          }
          if (!array_key_exists($start_time, $grouped_items)) {
            $grouped_items[$start_time]['values'][$wday] = [];
          }
          $start_time = $next_start_time;
          $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
        }
      }
      ksort($grouped_items);
    }
  }

  // Do the headers last, once we know what the actual values are.
  $i = 0;
  $start_times = array_keys($grouped_items);
  foreach ($start_times as $start_time) {
    $next_start_time = array_key_exists($i + 1, $start_times) ? $start_times[$i + 1] : '23:59:59';
    $variables = [
      'start_time' => $start_time,
      'next_start_time' => $next_start_time,
      'curday_date' => isset($row['data']) ? $row['data']['date'] : NULL,
    ];
    $heading = template_preprocess_calendar_time_row_heading($variables);
    $grouped_items[$start_time]['hour'] = $heading['hour'];
    $grouped_items[$start_time]['ampm'] = $heading['ampm'];
    $grouped_items[$start_time]['time'] = $start_time;
    if ($display_overlap) {
      foreach ($grouped_items[$start_time]['values'] as $wday => &$items) {
        foreach ($items as &$item) {
          if ($display_overlap) {
            $group_time = NULL;
            $divisor = NULL;
            if ($view->styleInfo
              ->getGroupByTimes() == 'half') {
              $group_time = 30;
              $divisor = 7.5;
            }
            elseif ($view->styleInfo
              ->getGroupByTimes() == 'hour') {
              $group_time = 60;
              $divisor = 15;
            }
            else {
              $item['class'] = '';
            }
            if (!empty($group_time) && !empty($divisor)) {
              $start_minute = intval(substr($start_time, 3, 2));
              $offset = round((date_format($item['item']->date_start, 'i') - $start_minute) / $divisor);
              $duration = round(($item['item']->date_end
                ->format('U') - $item['item']->date_start
                ->format('U')) / 60 / $divisor);
              $item['class'] = 'd_' . $duration . ' o_' . $offset . ' i_' . $item['indent'] . ' md_' . min($item['max_depth'], 5);
            }
          }
        }
      }
    }
  }
  $vars['items'] = $grouped_items;
  $vars['day_names'] = $day_names;
  $vars['columns'] = $columns;
  $vars['start_times'] = $start_times;
  $vars['first_time'] = !empty($first_time) ? $first_time : '';
  $header_ids = [];
  foreach ($day_names as $key => $value) {
    $header_ids[$key] = $value['header_id'];
  }
  $vars['header_ids'] = $header_ids;
  $vars['agenda_hour_class'] = 'calendar-agenda-hour';
  $first_column_width = 10;
  if (empty($view->styleInfo
    ->getCustomGroupByTimes())) {
    $vars['agenda_hour_class'] .= ' calendar-agenda-no-hours';
    $first_column_width = 1;
  }
  $vars['item_count'] = $item_count;
  $vars['by_hour_count'] = $by_hour_count;
}