timezone; $start_of_today = mktime(0, 0, 0, date('n', $offset), date('d', $offset), date('Y', $offset)) + $user->timezone; $end_of_today = mktime(23, 59, 59, date('n', $offset), date('d', $offset), date('Y', $offset)) + $user->timezone; // Extract the requested date: if ($edit['year'] && $edit['month'] && $edit['day']) { $year = $edit['year']; $month = $edit['month']; $day = $edit['day']; $requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone; } else 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); $result = db_query("SELECT created FROM {node} WHERE status = 1 AND created > $start_of_month AND created < $end_of_month"); $days_with_posts = array(); while ($day_with_post = db_fetch_object($result)) { $days_with_posts[] = date('j', $day_with_post->created + $user->timezone); } $days_with_posts = array_unique($days_with_posts); // Generate calendar header: $output .= "\n\n"; $output .= '
'; $output .= '\n"; $output .= ' \n"; // First day of week (0 => Sunday, 1 => Monday, ...) $weekstart = variable_get('default_firstday', 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 .= " \n"; $days = array(t('Su') => t('Sunday'), t('Mo') => t('Monday'), t('Tu') => t('Tuesday'), t('We') => t('Wednesday'), t('Th') => t('Thursday'), t('Fr') => t('Friday'), t('Sa') => t('Saturday')); if ($weekstart) { $days = array_merge(array_slice($days, $weekstart), array_slice($days, 0, $weekstart)); } foreach ($days as $name => $fullname) { $output .= ' \n"; } $output .= "\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: if ($first != $weekstart) { $blankdays = ($first - $weekstart + 7) % 7; $output .= " \n"; $first = $weekstart; } // Start every week on a new line: if ($sday == $weekstart) { $output .= " \n"; } // Print one cell: $date = mktime(0, 0, 0, $month, $nday, $year) + $user->timezone; if (in_array($nday, $days_with_posts)) { $daytext = l($nday, "archive/$year/$month/$nday"); $dayclass = 'day-link'; } else { $daytext = $nday; $dayclass = 'day-normal'; } if ($date == $requested) { $output .= " \n"; } else if ($date == $start_of_today) { $output .= " \n"; } else if ($date > $end_of_today) { $output .= " \n"; } else { $output .= " \n"; } // Start every week on a new line: if ($sday == $lastday) { $output .= " \n"; } // Update temporary variables: $sday++; $sday = $sday % 7; $nday++; } // Complete the calendar: if ($sday != $weekstart) { $end = (7 - $sday + $weekstart) % 7; $output .= " \n \n"; } $output .= "
'. 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'))) : ' ') ."
'. $name . "
 
$daytext$daytext$daytext$daytext
 
\n\n"; cache_set("archive:calendar:$day-$month-$year", $output, 1); return $output; } /** * 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 (user_access('access content')) { $block['subject'] = t('Browse archives'); $block['content'] = archive_calendar(); return $block; } } /** * Implementation of hook_link(). */ function archive_link($type) { $links = array(); if ($type == 'page' && user_access('access content')) { $links[] = l(t('archives'), 'archive', array('title' => t('Read the older content in our archive.'))); } return $links; } /** * Implementation of hook_menu(). */ function archive_menu() { $items = array(); $items[] = array('path' => 'archive', 'title' => t('archives'), 'access' => user_access('access content'), 'callback' => 'archive_page', 'type' => MENU_SUGGESTED_ITEM); $items[] = array('path' => 'archive/browse', 'title' => t('browse'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10); $items[] = array('path' => 'archive/configure', 'title' => t('configure'), 'access' => user_access('administer site configuration'), 'callback' => 'archive_configure', 'type' => MENU_LOCAL_TASK); return $items; } /** * Menu callback; lists all nodes posted on a given date. */ function archive_page($year = 0, $month = 0, $day = 0) { global $user; $output = ''; $op = $_POST['op']; $edit = $_POST['edit']; if ($op == t('Show')) { $year = $edit['year']; $month = $edit['month']; $day = $edit['day']; } $date = mktime(0, 0, 0, $month, $day, $year) - $user->timezone; $date_end = mktime(0, 0, 0, $month, $day + 1, $year) - $user->timezone; // Prepare the values of the form fields. $years = drupal_map_assoc(range(2000, 2005)); $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(0, 31)); $start = '
'; $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 .= '
'; $output .= form($start); if ($year && $month && $day) { // Fetch nodes for the selected date, if one was specified. $result = db_query_range('SELECT DISTINCT(n.nid) 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); while ($nid = db_fetch_object($result)) { $output .= node_view(node_load(array('nid' => $nid->nid)), 1); } } print theme('page', $output); } function archive_configure() { if ($_POST) { system_settings_save(); } $output = form_select(t('First day of week'), 'default_firstday', variable_get('default_firstday', 0), array(0 => t('Sunday'), 1 => t('Monday'), 2 => t('Tuesday'), 3 => t('Wednesday'), 4 => t('Thursday'), 5 => t('Friday'), 6 => t('Saturday')), t('The first day of the week. By changing this value you choose how the calendar block is rendered.')); print theme('page', system_settings_form($output)); } ?>