You are here

function calendar_build_month in Calendar 7

Same name in this branch
  1. 7 includes/calendar.inc \calendar_build_month()
  2. 7 calendar_multiday/includes/calendar.inc \calendar_build_month()
Same name and namespace in other branches
  1. 5.2 calendar.inc \calendar_build_month()
  2. 6.2 includes/calendar.inc \calendar_build_month()
  3. 6.2 calendar_multiday/includes/calendar.inc \calendar_build_month()
  4. 7.2 includes/calendar.inc \calendar_build_month()
  5. 7.2 calendar_multiday/includes/calendar.inc \calendar_build_month()

Build one month.

2 calls to calendar_build_month()
calendar_build_calendar in includes/calendar.inc
Build calendar
calendar_build_calendar in calendar_multiday/includes/calendar.inc
Build calendar

File

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

Code

function calendar_build_month(&$curday, $view, $items) {
  $month = date_format($curday, 'n');
  $curday_date = date_format($curday, DATE_FORMAT_DATE);
  $weekdays = calendar_untranslated_days($items, $view);
  date_modify($curday, '-' . strval(date_format($curday, 'j') - 1) . ' days');
  $rows = array();
  do {
    $init_day = clone $curday;
    $today = date_format(date_now(date_default_timezone()), DATE_FORMAT_DATE);
    $month = date_format($curday, 'n');
    $week = date_week($curday_date);
    $first_day = variable_get('date_first_day', 0);
    $week_rows = calendar_build_week($curday, $view, $items, TRUE);
    $multiday_buckets = $week_rows['multiday_buckets'];
    $singleday_buckets = $week_rows['singleday_buckets'];
    $total_rows = $week_rows['total_rows'];

    // Theme each row
    $output = "";
    $final_day = clone $curday;
    $iehint = 0;
    $max_multirow_cnt = 0;
    $max_singlerow_cnt = 0;
    for ($i = 0; $i < intval($total_rows + 1); $i++) {
      $inner = "";

      // If we're displaying the week number, add it as the
      // first cell in the week.
      if ($i == 0 && !empty($view->date_info->style_with_weekno) && !in_array($view->date_info->granularity, array(
        'day',
        'week',
      ))) {
        $url = $view
          ->get_path() . '/' . $view->date_info->year . '-W' . $week;
        if (!empty($view->date_info->display_types['week'])) {
          $weekno = l($week, $url, array(
            'query' => !empty($view->date_info->append) ? $view->date_info->append : '',
          ));
        }
        else {

          // Do not link week numbers, if Week views are disabled.
          $weekno = $week;
        }
        $item = array(
          'entry' => $weekno,
          'colspan' => 1,
          'rowspan' => $total_rows + 1,
          'id' => $view->name . '-weekno-' . $curday_date,
          'class' => 'week',
        );
        $inner .= theme('calendar_month_col', array(
          'item' => $item,
        ));
      }
      $curday = clone $init_day;

      // move backwards to the first day of the week
      $day_wday = date_format($curday, 'w');
      date_modify($curday, '-' . strval((7 + $day_wday - $first_day) % 7) . ' days');
      for ($wday = 0; $wday < 7; $wday++) {
        $curday_date = date_format($curday, DATE_FORMAT_DATE);
        $class = strtolower($weekdays[$wday]);
        $item = NULL;
        $in_month = !($curday_date < $view->date_info->min_date_date || $curday_date > $view->date_info->max_date_date || date_format($curday, 'n') != $month);

        // Add the datebox
        if ($i == 0) {
          $variables = array(
            'date' => $curday_date,
            'view' => $view,
            'items' => $items,
            'selected' => $in_month ? count($multiday_buckets[$wday]) + count($singleday_buckets[$wday]) : FALSE,
          );
          $item = array(
            'entry' => theme('calendar_datebox', $variables),
            'colspan' => 1,
            'rowspan' => 1,
            'class' => 'date-box',
            'date' => $curday_date,
            'id' => $view->name . '-' . $curday_date . '-date-box',
          );
          $item['class'] .= ($curday_date == $today && $in_month ? ' today' : '') . ($curday_date < $today ? ' past' : '') . ($curday_date > $today ? ' future' : '');
        }
        else {
          $index = $i - 1;
          $multi_count = count($multiday_buckets[$wday]);

          // Process multiday buckets first.  If there is a multiday-bucket item in this row...
          if ($index < $multi_count) {

            // If this item is filled with either a blank or an entry...
            if ($multiday_buckets[$wday][$index]['filled']) {

              // Add item and add class
              $item = $multiday_buckets[$wday][$index];
              $item['class'] = 'multi-day';
              $item['date'] = $curday_date;

              // Is this an entry?
              if (!$multiday_buckets[$wday][$index]['avail']) {

                // If the item either starts or ends on today,
                // then add tags so we can style the borders
                if ($curday_date == $today && $in_month) {
                  $item['class'] .= ' starts-today';
                }

                // Calculate on which day of this week this item ends on..
                $end_day = clone $curday;
                $span = $item['colspan'] - 1;
                date_modify($end_day, '+' . $span . ' day');
                $endday_date = date_format($end_day, DATE_FORMAT_DATE);

                // If it ends today, add class
                if ($endday_date == $today && $in_month) {
                  $item['class'] .= ' ends-today';
                }
              }
            }

            // If this is an acutal entry, add classes regarding the state of the
            // item
            if ($multiday_buckets[$wday][$index]['avail']) {
              $item['class'] .= ' ' . $wday . ' ' . $index . ' no-entry ' . ($curday_date == $today && $in_month ? ' today' : '') . ($curday_date < $today ? ' past' : '') . ($curday_date > $today ? ' future' : '');
            }

            // Else, process the single day bucket - we only do this once per day
          }
          elseif ($index == $multi_count) {
            $single_day_cnt = 0;

            // If it's empty, add class
            if (count($singleday_buckets[$wday]) == 0) {
              $single_days = "&nbsp;";
              if ($max_multirow_cnt == 0) {
                $class = $multi_count > 0 ? 'single-day no-entry noentry-multi-day' : 'single-day no-entry';
              }
              else {
                $class = 'single-day';
              }
            }
            else {
              $single_days = "";
              foreach ($singleday_buckets[$wday] as $day) {
                foreach ($day as $event) {
                  $single_day_cnt++;
                  $single_days .= isset($event['more_link']) ? '<div class="calendar-more">' . $event['entry'] . '</div>' : $event['entry'];
                }
              }
              $class = 'single-day';
            }
            $rowspan = $total_rows - $index;

            // Add item...
            $item = array(
              'entry' => $single_days,
              'colspan' => 1,
              'rowspan' => $rowspan,
              'class' => $class,
              'date' => $curday_date,
              'id' => $view->name . '-' . $curday_date . '-' . $index,
            );

            // Hack for ie to help it properly space single day rows
            if ($rowspan > 1 && $in_month && $single_day_cnt > 0) {
              $max_multirow_cnt = max($max_multirow_cnt, $single_day_cnt);
            }
            else {
              $max_singlerow_cnt = max($max_singlerow_cnt, $single_day_cnt);
            }

            // If the singlerow is bigger than the multi-row, then null out
            // ieheight - I'm estimating that a single row is twice the size of
            // multi-row.  This is really the best that can be done with ie
            if ($max_singlerow_cnt >= $max_multirow_cnt || $max_multirow_cnt <= $multi_count / 2) {
              $iehint = 0;
            }
            elseif ($rowspan > 1 && $in_month && $single_day_cnt > 0) {
              $iehint = max($iehint, $rowspan - 1);

              // How many rows to adjust for?
            }

            // Set the class
            $item['class'] .= ($curday_date == $today && $in_month ? ' today' : '') . ($curday_date < $today ? ' past' : '') . ($curday_date > $today ? ' future' : '');
          }
        }

        // If there isn't an item, then add empty class
        if ($item != NULL) {
          if (!$in_month) {
            $item['class'] .= ' empty';
          }

          // Style this entry - it will be a <td>.
          $inner .= theme('calendar_month_col', array(
            'item' => $item,
          ));
        }
        date_modify($curday, '+1 day');
      }
      if ($i == 0) {
        $output .= theme('calendar_month_row', array(
          'inner' => $inner,
          'class' => 'date-box',
          'iehint' => $iehint,
        ));
      }
      elseif ($i == $total_rows) {
        $output .= theme('calendar_month_row', array(
          'inner' => $inner,
          'class' => 'single-day',
          'iehint' => $iehint,
        ));
        $iehint = 0;
        $max_singlerow_cnt = 0;
        $max_multirow_cnt = 0;
      }
      else {

        // Style all the columns into a row
        $output .= theme('calendar_month_row', array(
          'inner' => $inner,
          'class' => 'multi-day',
          'iehint' => 0,
        ));
      }
    }

    // End foreach
    $curday = $final_day;

    // Add the row into the row array....
    $rows[] = array(
      'data' => $output,
    );
    $curday_date = date_format($curday, DATE_FORMAT_DATE);
    $curday_month = date_format($curday, 'n');
  } while ($curday_month == $month && $curday_date <= $view->date_info->max_date_date);

  // Merge the day names in as the first row.
  $rows = array_merge(array(
    calendar_week_header($view),
  ), $rows);
  return $rows;
}