You are here

function theme_archive_block_calendar in Archive 5

Same name and namespace in other branches
  1. 6 archive.module \theme_archive_block_calendar()
  2. 7.2 archive.module \theme_archive_block_calendar()
  3. 7 archive.module \theme_archive_block_calendar()

Returns a single month as a calendar grid TODO: take the archive logic out to allow better theme-overloading of this Pass days with post numbers to this function and have this function only create the calendar

1 theme call to theme_archive_block_calendar()
archive_block in ./archive.module
Implementation of hook_block().

File

./archive.module, line 344

Code

function theme_archive_block_calendar($timestamp) {
  $the_date = explode(' ', format_date($timestamp, 'custom', 'F Y n t'));
  $title = $the_date[0] . ' ' . $the_date[1];
  $year = $the_date[1];
  $month = $the_date[2];
  $num_days = (int) $the_date[3];

  // Get the number of days for the previous month for adding those dates
  // to the block calendar. Store the month/year for the previous month
  // so that we can find and link to archive pages in those extra days.
  // We don't need to get days for the next month as we'll never need to fit
  // the entire month into the block.
  $prev_month = $month == 1 ? 12 : $month - 1;
  $prev_year = $month == 1 ? $year - 1 : $year;
  $num_days_prev = cal_days_in_month(CAL_GREGORIAN, $month - 1, $year);
  $next_month = $month == 12 ? 1 : $month + 1;
  $next_year = $month == 12 ? $year + 1 : $year;
  $date = _archive_date('all', $year, $month);
  $month_title = '';
  if ($date->months[$month]) {
    $month_title = l($title, _archive_url('all', $year, $month), array(
      'title' => format_plural($date->months[format_date($timestamp, 'custom', 'n')], '1 post', '@count posts'),
    ));
  }
  else {
    $month_title = $title;
  }

  // Build the week starting with
  $first_day_of_week = variable_get('date_first_day', 0);
  $week = array(
    t('Sun'),
    t('Mon'),
    t('Tue'),
    t('Wed'),
    t('Thu'),
    t('Fri'),
    t('Sat'),
  );
  $day_headers = array();
  for ($i = $first_day_of_week; $i < $first_day_of_week + 7; $i++) {
    $day_headers[] = $week[$i % 7];
  }

  // this is inefficient but necessary to determine the start of the month:
  // We use gmdate the first time so that we don't apply the user's timezone twice
  list($start_year, $start_month) = explode(' ', format_date($timestamp, 'custom', 'Y m'));
  $start = gmmktime(0, 0, 0, (int) $start_month, 1, (int) $start_year);
  $weekday = gmdate('w', $start) - $first_day_of_week;
  $days_row = array();

  // From http://www.theadminzone.com/forums/showthread.php?t=17490
  for ($i = 1 - $weekday; $i <= ceil(($weekday + $num_days) / 7) * 7; $i++) {
    if ($i > 0) {
      if (array_key_exists($i, $date->days)) {
        $days_row[] = l($i, _archive_url('all', $year, $month, $i), array(
          'title' => format_plural($date->days[$i], '1 post', '@count posts'),
        ));
      }
      else {
        if ($i <= $num_days) {
          $days_row[] = $i;
        }
        else {
          $curr_cal_date = $i - $num_days;
          if (isset($date->next_month_days[$curr_cal_date])) {
            $data = l($curr_cal_date, _archive_url('all', $next_year, $next_month, $curr_cal_date), array(
              'title' => format_plural($date->next_month_days[$curr_cal_date], '1 post', '@count posts'),
            ));
          }
          else {
            $data = $curr_cal_date;
          }
          $days_row[] = array(
            'data' => $data,
            'class' => 'out-of-month',
          );
        }
      }

      // Add the week table row we just created if we've finished it
      if (($i + $weekday) % 7 == 0) {
        $rows[] = $days_row;
        $days_row = array();
      }
    }
    else {
      $curr_cal_date = $num_days_prev + $i;
      if (isset($date->prev_month_days[$curr_cal_date])) {
        $data = l($curr_cal_date, _archive_url('all', $prev_year, $prev_month, $curr_cal_date), array(
          'title' => format_plural($date->prev_month_days[$curr_cal_date], '1 post', '@count posts'),
        ));
      }
      else {
        $data = $curr_cal_date;
      }
      $days_row[] = array(
        'data' => $data,
        'class' => 'out-of-month',
      );
    }
  }
  return theme('table', $day_headers, $rows, array(), $month_title);
}