You are here

function _calc_indents in Calendar 7

Same name and namespace in other branches
  1. 8 calendar.theme.inc \_calc_indents()
  2. 6.2 calendar_multiday/theme/theme.inc \_calc_indents()
  3. 7.3 theme/theme.inc \_calc_indents()
  4. 7.2 calendar_multiday/theme/theme.inc \_calc_indents()

Indent items based off a nested tree structure of overlapping items

Parameters

array $overlapped_items: Tree of overlapped items

date $start: Start time of the event

date $end: End tiem of the event

array $item: The event to add to the tree

int $depth: Current depth of the tree

Return value

rc Returns an array with the max depth of the branch and whether an overlap occurred

2 calls to _calc_indents()
template_preprocess_calendar_day in calendar_multiday/theme/theme.inc
Display a day view.
template_preprocess_calendar_week in calendar_multiday/theme/theme.inc
Display a week view.

File

calendar_multiday/theme/theme.inc, line 941
Theme functions for the Calendar module.

Code

function _calc_indents(&$overlapped_items, $start, $end, &$item, $depth = 0) {

  // Are there any items at this depth?
  if (!empty($overlapped_items)) {

    // Iterate for each item as this depth and see if we overlap
    foreach ($overlapped_items as $index => &$entry) {

      // We search depth-first, so if there are children for this item, recurse into
      // each child tree looking for an overlap
      if (!empty($entry['children'])) {
        $rc = _calc_indents($entry['children'], $start, $end, $item, $depth + 1);

        // Was there an overlap in the child tree?
        if ($rc['overlap']) {
          if (is_object($entry['item'])) {
            $entry['item']->indent = _calc_indent($entry['depth'], $rc['max_depth']);
            $entry['item']->max_depth = $rc['max_depth'];
          }
          else {
            $entry['item']['indent'] = _calc_indent($entry['depth'], $rc['max_depth']);
            $entry['item']['max_depth'] = $rc['max_depth'];
          }

          // There was an overlap, pop out of this depth
          return $rc;
        }
      }

      // No, child overlap, so check if we overlap this item
      if ($start >= $entry['start'] && $start <= $entry['end']) {

        // We overlap, create an overlapping entry
        $entry['children'][] = array(
          'item' => &$item,
          'depth' => $depth + 1,
          'start' => $start,
          'end' => $end,
          'children' => array(),
        );
        if (is_object($entry['item'])) {
          $max_depth = max($entry['item']->max_depth, $depth + 1);
          $entry['item']->indent = _calc_indent($depth, $max_depth);
          $entry['item']->max_depth = $max_depth;
        }
        else {
          $max_depth = max($entry['item']['max_depth'], $depth + 1);
          $entry['item']['indent'] = _calc_indent($depth, $max_depth);
          $entry['item']['max_depth'] = $max_depth;
        }
        if (is_object($item)) {
          $item->indent = _calc_indent($depth + 1, $max_depth);
          $item->max_depth = $max_depth;
        }
        else {
          $item['indent'] = _calc_indent($depth + 1, $max_depth);
          $item['max_depth'] = $max_depth;
        }

        // We overlap, so pop out of this depth
        return array(
          'overlap' => TRUE,
          'max_depth' => $max_depth,
        );
      }
    }

    // If there are items at this depth, but no overlap, then return no overlap and pop
    // out of this depth
    if ($depth > 0) {
      return array(
        'overlap' => FALSE,
        'max_depth' => 0,
      );
    }
  }

  // No overlap at any depth, reset the array of overlaps
  if ($depth == 0) {
    reset($overlapped_items);
    $overlapped_items[0] = array(
      'item' => &$item,
      'depth' => $depth,
      'start' => $start,
      'end' => $end,
      'children' => array(),
    );
  }
  else {
    $overlapped_items[] = array(
      'item' => &$item,
      'depth' => $depth,
      'start' => $start,
      'end' => $end,
      'children' => array(),
    );
  }
  if (is_object($item)) {
    $item->indent = _calc_indent($depth, $depth);
    $item->max_depth = $depth;
  }
  else {
    $item['indent'] = _calc_indent($depth, $depth);
    $item['max_depth'] = $depth;
  }
  return array(
    'overlap' => FALSE,
    'max_depth' => $depth,
  );
}