2002-01-31 20:28:39 +00:00
<?php
// $Id$
2004-08-21 06:42:38 +00:00
/**
* @file
* Displays a calendar to navigate old content.
*/
2004-05-11 20:10:14 +00:00
/**
* Implementation of hook_help().
*/
2003-08-05 18:33:39 +00:00
function archive_help($section) {
switch ($section) {
2004-06-18 15:04:37 +00:00
case 'admin/modules#description':
2004-05-11 20:10:14 +00:00
return t('Displays a calendar to navigate old content.');
2003-08-05 18:33:39 +00:00
}
}
2002-06-01 21:57:29 +00:00
2004-05-11 20:10:14 +00:00
/**
2004-06-18 15:04:37 +00:00
* Generates a monthly calendar, for display in the archive block.
2004-05-11 20:10:14 +00:00
*/
2003-01-06 19:51:01 +00:00
function archive_calendar($original = 0) {
2003-06-22 08:46:34 +00:00
global $user;
2004-05-11 20:10:14 +00:00
$edit = $_POST['edit'];
2002-01-31 20:28:39 +00:00
2003-01-06 19:51:01 +00:00
// Extract today's date:
2004-10-08 11:29:31 +00:00
$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;
2002-01-31 20:28:39 +00:00
2003-01-06 19:51:01 +00:00
// Extract the requested date:
2004-05-11 20:10:14 +00:00
if ($edit['year'] && $edit['month'] && $edit['day']) {
$year = $edit['year'];
$month = $edit['month'];
$day = $edit['day'];
2003-06-07 06:08:52 +00:00
2003-06-22 08:46:34 +00:00
$requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
2003-06-07 06:08:52 +00:00
}
2004-05-11 20:10:14 +00:00
else if (arg(0) == 'archive' && arg(3)) {
2003-01-06 19:51:01 +00:00
$year = arg(1);
$month = arg(2);
$day = arg(3);
2002-01-31 20:28:39 +00:00
2003-06-22 08:46:34 +00:00
$requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
2003-01-06 19:51:01 +00:00
}
else {
2004-05-11 20:10:14 +00:00
$year = date('Y', time());
$month = date('n', time());
$day = date('d', time());
2002-01-31 20:28:39 +00:00
2003-06-22 08:46:34 +00:00
$requested = $end_of_today + $user->timezone;
2003-01-06 19:51:01 +00:00
}
2002-01-31 20:28:39 +00:00
2003-06-05 21:34:56 +00:00
$start_of_month = mktime(0, 0, 0, $month, 1, $year);
2002-01-31 20:28:39 +00:00
// Extract first day of the month:
2004-05-11 20:10:14 +00:00
$first = date('w', $start_of_month);
2002-01-31 20:28:39 +00:00
// Extract last day of the month:
2004-05-11 20:10:14 +00:00
$last = date('t', $start_of_month);
2003-06-05 21:34:56 +00:00
2003-10-07 18:16:41 +00:00
$end_of_month = mktime(23, 59, 59, $month, $last, $year);
2003-06-05 21:34:56 +00:00
2004-01-06 18:30:10 +00:00
$cache = cache_get("archive:calendar:$day-$month-$year");
2003-06-05 21:34:56 +00:00
if (!empty($cache)) {
return $cache->data;
}
2002-01-31 20:28:39 +00:00
// Calculate previous and next months dates and check for shorter months (28/30 days)
$prevmonth = mktime(23, 59, 59, $month - 1, 1, $year);
2004-05-11 20:10:14 +00:00
$prev = mktime(23, 59, 59, $month - 1, min(date('t', $prevmonth), $day), $year);
2002-01-31 20:28:39 +00:00
$nextmonth = mktime(23, 59, 59, $month + 1, 1, $year);
2004-05-11 20:10:14 +00:00
$next = mktime(23, 59, 59, $month + 1, min(date('t', $nextmonth), $day), $year);
2002-01-31 20:28:39 +00:00
2004-12-20 22:01:17 +00:00
$result = db_query('SELECT DISTINCT(n.nid), n.created FROM {node} n '. node_access_join_sql() .' WHERE n.status = 1 AND n.created > %d AND n.created < %d AND '. node_access_where_sql() .' ORDER BY n.created', $start_of_month, $end_of_month);
2003-06-05 21:34:56 +00:00
$days_with_posts = array();
while ($day_with_post = db_fetch_object($result)) {
2004-05-11 20:10:14 +00:00
$days_with_posts[] = date('j', $day_with_post->created + $user->timezone);
2003-06-05 21:34:56 +00:00
}
$days_with_posts = array_unique($days_with_posts);
2002-01-31 20:28:39 +00:00
// Generate calendar header:
$output .= "\n<!-- calendar -->\n";
2004-05-11 20:10:14 +00:00
$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";
2003-01-21 22:50:03 +00:00
// First day of week (0 => Sunday, 1 => Monday, ...)
2004-09-12 17:18:36 +00:00
$weekstart = variable_get('date_first_day', 0);
2003-01-21 22:50:03 +00:00
// Last day of week
2003-10-07 18:16:41 +00:00
($weekstart - 1 == -1) ? $lastday = 6 : $lastday = $weekstart - 1;
2002-01-31 20:28:39 +00:00
// Generate the days of the week:
2003-01-21 22:50:03 +00:00
$firstcolumn = mktime(0, 0, 0, 3, 20 + $weekstart, 1994);
2002-01-31 20:28:39 +00:00
2004-05-05 18:19:23 +00:00
$output .= " <tr class=\"header-week\">\n";
2004-12-14 06:46:08 +00:00
$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'));
2004-05-05 18:19:23 +00:00
if ($weekstart) {
$days = array_merge(array_slice($days, $weekstart), array_slice($days, 0, $weekstart));
}
2004-05-08 07:17:47 +00:00
2004-12-14 06:46:08 +00:00
foreach ($days as $fullname => $name) {
2004-05-11 20:10:14 +00:00
$output .= ' <th abbr="'. $fullname .'">'. $name . "</th>\n";
2002-01-31 20:28:39 +00:00
}
$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:
2003-01-21 22:50:03 +00:00
if ($first != $weekstart) {
$blankdays = ($first - $weekstart + 7) % 7;
2003-05-31 12:42:02 +00:00
$output .= " <tr class=\"row-week\"><td class=\"day-blank\" colspan=\"$blankdays\"> </td>\n";
2003-01-21 22:50:03 +00:00
$first = $weekstart;
2002-01-31 20:28:39 +00:00
}
// Start every week on a new line:
2003-01-21 22:50:03 +00:00
if ($sday == $weekstart) {
2003-06-08 19:26:26 +00:00
$output .= " <tr class=\"row-week\">\n";
2002-01-31 20:28:39 +00:00
}
// Print one cell:
2003-06-22 08:46:34 +00:00
$date = mktime(0, 0, 0, $month, $nday, $year) + $user->timezone;
2003-06-05 21:34:56 +00:00
if (in_array($nday, $days_with_posts)) {
$daytext = l($nday, "archive/$year/$month/$nday");
2004-05-11 20:10:14 +00:00
$dayclass = 'day-link';
2003-06-05 21:34:56 +00:00
}
else {
2004-05-05 18:19:23 +00:00
$daytext = $nday;
2004-05-11 20:10:14 +00:00
$dayclass = 'day-normal';
2003-06-05 21:34:56 +00:00
}
2003-01-06 19:51:01 +00:00
if ($date == $requested) {
2003-06-05 21:34:56 +00:00
$output .= " <td class=\"day-selected\">$daytext</td>\n";
2003-05-31 12:42:02 +00:00
}
else if ($date == $start_of_today) {
2003-06-05 21:34:56 +00:00
$output .= " <td class=\"day-today\">$daytext</td>\n";
2002-01-31 20:28:39 +00:00
}
2003-05-31 12:42:02 +00:00
else if ($date > $end_of_today) {
2003-06-05 21:34:56 +00:00
$output .= " <td class=\"day-future\">$daytext</td>\n";
2002-01-31 20:28:39 +00:00
}
else {
2003-06-16 17:17:51 +00:00
$output .= " <td class=\"$dayclass\">$daytext</td>\n";
2002-01-31 20:28:39 +00:00
}
// Start every week on a new line:
2003-01-21 22:50:03 +00:00
if ($sday == $lastday) {
2002-01-31 20:28:39 +00:00
$output .= " </tr>\n";
}
// Update temporary variables:
$sday++;
$sday = $sday % 7;
$nday++;
}
// Complete the calendar:
2003-01-21 22:50:03 +00:00
if ($sday != $weekstart) {
$end = (7 - $sday + $weekstart) % 7;
2003-05-31 12:42:02 +00:00
$output .= " <td class=\"day-blank\" colspan=\"$end\"> </td>\n </tr>\n";
2002-01-31 20:28:39 +00:00
}
2003-05-31 12:42:02 +00:00
$output .= "</table></div>\n\n";
2002-01-31 20:28:39 +00:00
2004-09-15 20:34:35 +00:00
cache_set("archive:calendar:$day-$month-$year", $output, CACHE_TEMPORARY);
2003-06-05 21:34:56 +00:00
2002-01-31 20:28:39 +00:00
return $output;
}
2004-05-11 20:10:14 +00:00
/**
* 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');
2002-10-26 15:17:26 +00:00
return $blocks;
}
2004-10-31 07:34:47 +00:00
else if ($op == 'view' && user_access('access content')) {
2004-07-07 20:18:22 +00:00
$block['subject'] = t('Browse archives');
$block['content'] = archive_calendar();
return $block;
2002-10-26 15:17:26 +00:00
}
2002-01-31 20:28:39 +00:00
}
2004-06-18 15:04:37 +00:00
/**
* Implementation of hook_menu().
*/
2004-09-16 07:17:56 +00:00
function archive_menu($may_cache) {
2004-06-18 15:04:37 +00:00
$items = array();
2004-09-16 07:17:56 +00:00
if ($may_cache) {
$items[] = array('path' => 'archive', 'title' => t('archives'),
'access' => user_access('access content'),
'callback' => 'archive_page',
'type' => MENU_SUGGESTED_ITEM);
}
2004-06-18 15:04:37 +00:00
return $items;
}
2004-05-11 20:10:14 +00:00
/**
2004-05-17 22:00:06 +00:00
* Menu callback; lists all nodes posted on a given date.
2004-05-11 20:10:14 +00:00
*/
function archive_page($year = 0, $month = 0, $day = 0) {
global $user;
2003-05-13 18:36:38 +00:00
2004-05-11 20:10:14 +00:00
$output = '';
$op = $_POST['op'];
$edit = $_POST['edit'];
2002-01-31 20:28:39 +00:00
2004-05-11 20:10:14 +00:00
if ($op == t('Show')) {
$year = $edit['year'];
$month = $edit['month'];
$day = $edit['day'];
2004-02-15 20:09:46 +00:00
}
2002-01-31 20:28:39 +00:00
2004-02-15 20:09:46 +00:00
$date = mktime(0, 0, 0, $month, $day, $year) - $user->timezone;
$date_end = mktime(0, 0, 0, $month, $day + 1, $year) - $user->timezone;
2002-01-31 20:28:39 +00:00
2004-05-11 20:10:14 +00:00
// Prepare the values of the form fields.
2004-02-15 20:09:46 +00:00
$years = drupal_map_assoc(range(2000, 2005));
2004-05-11 20:10:14 +00:00
$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'));
2004-02-15 20:09:46 +00:00
$days = drupal_map_assoc(range(0, 31));
2002-01-31 20:28:39 +00:00
2004-05-11 20:10:14 +00:00
$start = '<div class="container-inline">';
$start .= form_select('', 'year', ($year ? $year : date('Y')), $years). form_select('', 'month', ($month ? $month : date('m')), $months) . form_select('', 'day', ($day ? $day : date('d')), $days) . form_submit(t('Show'));
$start .= '</div>';
2004-02-15 20:09:46 +00:00
$output .= form($start);
2002-01-31 20:28:39 +00:00
2004-02-15 20:09:46 +00:00
if ($year && $month && $day) {
2004-05-11 20:10:14 +00:00
// Fetch nodes for the selected date, if one was specified.
2004-09-08 15:38:26 +00:00
$result = db_query_range('SELECT DISTINCT(n.nid), n.created FROM {node} n '. node_access_join_sql() .' WHERE n.status = 1 AND n.created > %d AND n.created < %d AND '. node_access_where_sql() .' ORDER BY n.created', $date, $date_end, 0, 20);
2002-01-31 20:28:39 +00:00
2004-02-15 20:09:46 +00:00
while ($nid = db_fetch_object($result)) {
2004-05-11 20:10:14 +00:00
$output .= node_view(node_load(array('nid' => $nid->nid)), 1);
2003-01-06 19:51:01 +00:00
}
2002-01-31 20:28:39 +00:00
}
2004-05-11 20:10:14 +00:00
print theme('page', $output);
2002-01-31 20:28:39 +00:00
}
2003-01-21 22:50:03 +00:00
?>