You are here

function template_preprocess_calendar_day in Calendar 8

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

Display a day view.

1 call to template_preprocess_calendar_day()
template_preprocess_calendar_day_overlap in ./calendar.theme.inc
Display a day overlap view.

File

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

Code

function template_preprocess_calendar_day(&$vars) {
  $vars['view']->styleInfo
    ->setShowWeekNumbers(FALSE);
  $view = $vars['view'];
  $rows = $vars['rows'];
  $item_count = 0;
  $by_hour_count = 0;

  /** @var \Drupal\calendar\CalendarStyleInfo $styleInfo */
  $styleInfo = $view->styleInfo;
  $grouping_field = !empty($styleInfo
    ->getCustomGroupByField()) ? $styleInfo
    ->getCustomGroupByField() : NULL;
  $display_overlap = !empty($styleInfo
    ->getThemeStyle()) && !empty($styleInfo
    ->getGroupByTimes());
  $vars['scroll_content'] = !empty($styleInfo
    ->getThemeStyle()) && $styleInfo
    ->getThemeStyle() == 1;
  if ($display_overlap) {
    $overlapped_items = [];
    $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';
    }
  }

  // If we're not grouping by time, move all items into the 'all day' array.
  if (empty($view->styleInfo
    ->getGroupByTimes())) {

    // Items are already grouped into times, so we need to process each time-group.
    foreach ($rows['items'] as $time => $items) {
      foreach ($items as $item) {
        $rows['all_day'][] = $item;
      }
    }
    $rows['items'] = [];
  }
  $columns = [];

  // Move all_day items into the right columns and render them.
  $grouped_items = [];
  foreach ($rows['all_day'] as $item) {
    if (!empty($item->rendered_fields[$grouping_field])) {
      $column = $item->rendered_fields[$grouping_field];
      if (!in_array($column, $columns)) {
        $columns[] = $column;
      }
    }
    else {
      $column = 'Items';
    }
    $grouped_items[$column][] = [
      '#theme' => 'calendar_item',
      '#view' => $view,
      '#rendered_fields' => $item
        ->getRenderedFields(),
      '#item' => $item,
    ];
    $item_count++;
  }
  $vars['rows']['all_day'] = $grouped_items;

  // Moved timed items into the right columns and render them.
  $start_times = $view->styleInfo
    ->getGroupByTimes();
  $show_empty_times = $view->styleInfo
    ->isShowEmptyTimes();
  $end_start_time = '23:59:59';
  $start_time = array_shift($start_times);
  $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
  $grouped_items = [];
  foreach ($rows['items'] as &$items) {

    /** @var \Drupal\calendar\CalendarEvent $item */
    foreach ($items as &$item) {
      $time = date_format($item
        ->getStartDate(), 'H:i:s');
      if (!empty($item->rendered_fields[$grouping_field])) {
        $column = $item->rendered_fields[$grouping_field];
        if (!in_array($column, $columns)) {
          $columns[] = $column;
        }
      }
      else {
        $column = 'Items';
      }

      // 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 ((!empty($show_empty_times) || $display_overlap) && !array_key_exists($start_time, $grouped_items)) {
          $grouped_items[$start_time]['values'] = [];
        }
        $start_time = $next_start_time;
        $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
      }
      $grouped_items[$start_time]['values'][$column][] = $item;
      if ($display_overlap) {
        $time_end = date_format($item
          ->getEndDate(), 'H:i:s');
        $item->time_start = $time;
        $item->time_end = $time_end;
        _calc_indents($overlapped_items, $time, $time_end, $item);
      }
      $item_count++;
      $by_hour_count++;
    }
  }

  // Finish out the day's time values if we want to see empty times.
  if (!empty($show_empty_times) || $display_overlap) {
    while ($start_time < $end_start_time && (!empty($start_time) || $display_overlap)) {
      if (empty($start_time)) {
        $start_times = $view->date_info->style_groupby_times;
        $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'] = [];
      }
      $start_time = $next_start_time;
      $next_start_time = count($start_times) ? array_shift($start_times) : $end_start_time;
    }
  }

  // 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' => $rows['#date'],
    ];
    $heading = template_preprocess_calendar_time_row_heading($variables);
    $grouped_items[$start_time]['hour'] = $heading['hour'];
    $grouped_items[$start_time]['ampm'] = $heading['ampm'];
    foreach ($grouped_items[$start_time]['values'] as $column => &$items) {
      foreach ($items as $index => &$item) {
        $group_time = NULL;
        $divisor = NULL;
        if ($display_overlap) {
          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->date_start, 'i') - $start_minute) / $divisor);
            $duration = round(($item->date_end
              ->format('U') - $item->date_start
              ->format('U')) / 60 / $divisor);
            $item->class = 'd_' . $duration . ' o_' . $offset . ' i_' . $item->indent . ' md_' . min($item->max_depth, 5);
          }
        }
        $grouped_items[$start_time]['values'][$column][$index] = [
          '#theme' => 'calendar_item',
          '#view' => $view,
          '#rendered_fields' => $item
            ->getRenderedFields(),
          '#item' => $item,
        ];
      }
    }
    $i++;
  }
  ksort($grouped_items);
  $vars['rows']['items'] = $grouped_items;
  if (empty($columns)) {
    $columns = [
      'Items',
    ];
  }
  $vars['columns'] = $columns;
  $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['first_column_width'] = $first_column_width;
  if (count($columns)) {
    $vars['column_width'] = round((100 - $first_column_width) / count($columns));
  }
  else {
    $vars['column_width'] = 100 - $first_column_width;
  }
  $vars['item_count'] = $item_count;
  $vars['by_hour_count'] = $by_hour_count;
  $vars['start_times'] = $view->styleInfo
    ->getGroupByTimes();
}