Same filename and directory in other branches
  1. 4.6.x modules/archive.module

Displays a calendar to navigate old content.

File

modules/archive.module
View source
<?php

/**
 * @file
 * Displays a calendar to navigate old content.
 */

/**
 * Implementation of hook_help().
 */
function archive_help($section) {
  switch ($section) {
    case 'admin/help#archive':
      $output = '<p>' . t('The archive page allows content to be viewed by date.  It also provides a monthly calendar view that users can use to navigate through content.') . '</p>';
      $output .= '<p>' . t('To view the archive by date, select the date in the calendar.  Administrators can enable the <em>browse archives</em> block in block administration to allow users to browse by calendar.  Clicking on a date in the monthly calendar view shows the content for that date.  Users can navigate to different months using arrows beside the month\'s name in the calendar display.   The current date will be highlighted in the calendar.') . '</p>';
      $output .= t('<p>You can</p>
<ul>
<li>view your <a href="%archive">archive by day</a>.</li>
<li>enable the <em>browse archives</em> block at <a href="%admin-block">administer &gt;&gt; block</a>.</li>
</ul>
', array(
        '%archive' => url('archive'),
        '%admin-block' => url('admin/block'),
      ));
      $output .= '<p>' . t('For more information please read the configuration and customization handbook <a href="%archive">Archive page</a>.', array(
        '%archive' => 'http://drupal.org/handbook/modules/archive/',
      )) . '</p>';
      return $output;
    case 'admin/modules#description':
      return t('Displays a calendar for navigating older content.');
  }
}

/**
 * Implementation of hook_menu().
 */
function archive_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'archive',
      'title' => t('archives'),
      'access' => user_access('access content'),
      'callback' => 'archive_page',
      'type' => MENU_SUGGESTED_ITEM,
    );
  }
  return $items;
}

/**
 * Implementation of hook_block().
 *
 * Generates a calendar for the current month, with links to the archives
 * for each day.
 */
function archive_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Calendar to browse archives');
    return $blocks;
  }
  else {
    if ($op == 'view' && user_access('access content')) {
      $block['subject'] = t('Browse archives');
      $block['content'] = archive_calendar();
      return $block;
    }
  }
}

/**
 * Generates a monthly calendar, for display in the archive block.
 */
function archive_calendar() {
  global $user;

  // Extract today's date:
  $start_of_today = mktime(0, 0, 0, date('n', time()), date('d', time()), date('Y', time())) + $user->timezone;
  $end_of_today = mktime(23, 59, 59, date('n', time()), date('d', time()), date('Y', time())) + $user->timezone;

  // Extract the requested date:
  if (arg(0) == 'archive' && arg(3)) {
    $year = arg(1);
    $month = arg(2);
    $day = arg(3);
    $requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
  }
  else {
    $year = date('Y', time());
    $month = date('n', time());
    $day = date('d', time());
    $requested = $end_of_today + $user->timezone;
  }
  $start_of_month = mktime(0, 0, 0, $month, 1, $year);

  // Extract first day of the month:
  $first = date('w', $start_of_month);

  // Extract last day of the month:
  $last = date('t', $start_of_month);
  $end_of_month = mktime(23, 59, 59, $month, $last, $year);
  $cache = cache_get("archive:calendar:{$day}-{$month}-{$year}");
  if (!empty($cache)) {
    return $cache->data;
  }

  // Calculate previous and next months dates and check for shorter months (28/30 days)
  $prevmonth = mktime(23, 59, 59, $month - 1, 1, $year);
  $prev = mktime(23, 59, 59, $month - 1, min(date('t', $prevmonth), $day), $year);
  $nextmonth = mktime(23, 59, 59, $month + 1, 1, $year);
  $next = mktime(23, 59, 59, $month + 1, min(date('t', $nextmonth), $day), $year);
  $sql = 'SELECT n.nid, n.created FROM {node} n WHERE n.status = 1 AND n.created > %d AND n.created < %d ORDER BY n.created';
  $sql = db_rewrite_sql($sql);
  $result = db_query($sql, $start_of_month, $end_of_month);
  $days_with_posts = array();
  while ($day_with_post = db_fetch_object($result)) {
    $daynum = date('j', $day_with_post->created + $user->timezone);
    if (isset($days_with_posts[$daynum])) {
      $days_with_posts[$daynum]++;
    }
    else {
      $days_with_posts[$daynum] = 1;
    }
  }

  // Generate calendar header:
  $output .= "\n<!-- calendar -->\n";
  $output .= '<div class="calendar">';
  $output .= '<table summary="' . t('A calendar to browse the archives') . ".\">\n";
  $output .= ' <caption>' . l('«', 'archive/' . date('Y/m/d', $prev), array(
    'title' => t('Previous month'),
  )) . ' ' . format_date($requested, 'custom', 'F') . date(' Y', $requested) . ' ' . ($nextmonth <= time() ? l('»', 'archive/' . date('Y/m/d', $next), array(
    'title' => t('Next month'),
  )) : ' ') . "</caption>\n";

  // First day of week (0 => Sunday, 1 => Monday, ...)
  $weekstart = variable_get('date_first_day', 0);

  // Last day of week
  $weekstart - 1 == -1 ? $lastday = 6 : ($lastday = $weekstart - 1);

  // Generate the days of the week:
  $firstcolumn = mktime(0, 0, 0, 3, 20 + $weekstart, 1994);
  $output .= " <tr class=\"header-week\">\n";
  $days = array(
    t('Sunday') => t('Su'),
    t('Monday') => t('Mo'),
    t('Tuesday') => t('Tu'),
    t('Wednesday') => t('We'),
    t('Thursday') => t('Th'),
    t('Friday') => t('Fr'),
    t('Saturday') => t('Sa'),
  );
  if ($weekstart) {
    $days = array_merge(array_slice($days, $weekstart), array_slice($days, 0, $weekstart));
  }
  foreach ($days as $fullname => $name) {
    $output .= ' <th abbr="' . $fullname . '">' . $name . "</th>\n";
  }
  $output .= "</tr>\n";

  // Initialize temporary variables:
  $nday = 1;
  $sday = $first;

  // Loop through all the days of the month:
  while ($nday <= $last) {

    // Set up blank days for first week of the month (allowing individual blank day styling):
    if ($first != $weekstart) {
      $blankdays = ($first - $weekstart + 7) % 7;
      $output .= " <tr class=\"row-week\">" . str_repeat("<td class=\"day-blank\">&nbsp;</td>\n", $blankdays);
      $first = $weekstart;
    }

    // Start every week on a new line:
    if ($sday == $weekstart) {
      $output .= " <tr class=\"row-week\">\n";
    }

    // Print one cell:
    $date = mktime(0, 0, 0, $month, $nday, $year) + $user->timezone;
    if (isset($days_with_posts[$nday])) {
      $daytext = l($nday, "archive/{$year}/{$month}/{$nday}", array(
        "title" => format_plural($days_with_posts[$nday], "1 post", "%count posts"),
      ));
      $dayclass = 'day-link';
    }
    else {
      $daytext = $nday;
      $dayclass = 'day-normal';
    }
    if ($date == $requested) {
      $output .= "  <td class=\"day-selected\">{$daytext}</td>\n";
    }
    else {
      if ($date == $start_of_today) {
        $output .= "  <td class=\"day-today\">{$daytext}</td>\n";
      }
      else {
        if ($date > $end_of_today) {
          $output .= "  <td class=\"day-future\">{$daytext}</td>\n";
        }
        else {
          $output .= "  <td class=\"{$dayclass}\">{$daytext}</td>\n";
        }
      }
    }

    // Start every week on a new line:
    if ($sday == $lastday) {
      $output .= " </tr>\n";
    }

    // Update temporary variables:
    $sday++;
    $sday = $sday % 7;
    $nday++;
  }

  // Complete the calendar (allowing individual blank day styling):
  if ($sday != $weekstart) {
    $end = (7 - $sday + $weekstart) % 7;
    $output .= str_repeat("<td class=\"day-blank\">&nbsp;</td>\n", $end) . "</tr>\n";
  }
  $output .= "</table></div>\n\n";
  cache_set("archive:calendar:{$day}-{$month}-{$year}", $output, CACHE_TEMPORARY);
  return $output;
}

/**
 * Menu callback; lists all nodes posted on a given date.
 */
function archive_page($year = 0, $month = 0, $day = 0) {
  global $user;
  $date = mktime(0, 0, 0, $month, $day, $year) - $user->timezone;
  $date_end = mktime(0, 0, 0, $month, $day + 1, $year) - $user->timezone;

  // Display form.
  $output = archive_browse_form($year, $month, $day);
  if ($year && $month && $day) {

    // Fetch nodes for the selected date, if one was specified.
    $sql = db_rewrite_sql('SELECT n.nid, n.created FROM {node} n WHERE n.status = 1 AND n.created > %d AND n.created < %d ORDER BY n.created');
    $countsql = db_rewrite_sql('SELECT count(n.nid) FROM {node} n WHERE n.status = 1 AND n.created > %d AND n.created < %d ORDER BY n.created');
    $result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $countsql, $date, $date_end);
    if (db_num_rows($result) > 0) {
      while ($nid = db_fetch_object($result)) {
        $output .= node_view(node_load($nid->nid), 1);
      }
      $output .= theme('pager');
    }
    else {
      $output .= theme('box', t('No posts found.'), '');
    }
  }
  else {
    $output .= theme('box', t('No posts found.'), '');
  }
  return $output;
}

/**
 * Generate a form that retrieves archives for a certain date.
 */
function archive_browse_form($year, $month, $day) {

  // Prepare the values of the form fields.
  $years = drupal_map_assoc(range(2000, date('Y')));
  $months = array(
    1 => t('January'),
    2 => t('February'),
    3 => t('March'),
    4 => t('April'),
    5 => t('May'),
    6 => t('June'),
    7 => t('July'),
    8 => t('August'),
    9 => t('September'),
    10 => t('October'),
    11 => t('November'),
    12 => t('December'),
  );
  $days = drupal_map_assoc(range(1, 31));
  $form['year'] = array(
    '#type' => 'select',
    '#default_value' => $year ? $year : date('Y'),
    '#options' => $years,
  );
  $form['month'] = array(
    '#type' => 'select',
    '#default_value' => $month ? $month : date('m'),
    '#options' => $months,
  );
  $form['day'] = array(
    '#type' => 'select',
    '#default_value' => $day ? $day : date('d'),
    '#options' => $days,
  );
  $form['show'] = array(
    '#type' => 'submit',
    '#value' => t('Show'),
  );
  return drupal_get_form('archive_browse_form', $form);
}

/**
 * Process archive browse form submission.
 */
function archive_browse_form_submit($form_id, $form_values) {
  return 'archive/' . $form_values['year'] . '/' . $form_values['month'] . '/' . $form_values['day'];
}

/**
 * Form theme function; displays the archive date navigation form inline.
 */
function theme_archive_browse_form($form) {
  $output = '<div class="container-inline archive">' . form_render($form) . '</div>';
  return $output;
}

Functions

Namesort descending Description
archive_block Implementation of hook_block().
archive_browse_form Generate a form that retrieves archives for a certain date.
archive_browse_form_submit Process archive browse form submission.
archive_calendar Generates a monthly calendar, for display in the archive block.
archive_help Implementation of hook_help().
archive_menu Implementation of hook_menu().
archive_page Menu callback; lists all nodes posted on a given date.
theme_archive_browse_form Form theme function; displays the archive date navigation form inline.