2 // $Id: archive.module 144 2007-03-28 07:52:20Z thierry $
6 * Displays a calendar to navigate old content.
10 * Implementation of hook_help().
12 function archive_help($section) {
14 case 'admin/help#archive':
15 $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>';
16 $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>';
17 $output .= t('<p>You can</p>
19 <li>view your <a href="%archive">archive by day</a>.</li>
20 <li>enable the <em>browse archives</em> block at <a href="%admin-block">administer >> block</a>.</li>
22 ', array('%archive' => url('archive'), '%admin-block' => url('admin/block')));
23 $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>';
25 case 'admin/modules#description':
26 return t('Displays a calendar for navigating older content.');
31 * Implementation of hook_menu().
33 function archive_menu($may_cache) {
37 $items[] = array('path' => 'archive',
38 'title' => t('archives'),
39 'access' => user_access('access content'),
40 'callback' => 'archive_page',
41 'type' => MENU_SUGGESTED_ITEM);
47 * Implementation of hook_block().
49 * Generates a calendar for the current month, with links to the archives
52 function archive_block($op = 'list', $delta = 0) {
54 $blocks[0]['info'] = t('Calendar to browse archives');
57 else if ($op == 'view' && user_access('access content')) {
58 $block['subject'] = t('Browse archives');
59 $block['content'] = archive_calendar();
65 * Generates a monthly calendar, for display in the archive block.
67 function archive_calendar() {
70 // Extract today's date:
71 $start_of_today = mktime(0, 0, 0, date('n', time()), date('d', time()), date('Y', time())) + $user->timezone;
72 $end_of_today = mktime(23, 59, 59, date('n', time()), date('d', time()), date('Y', time())) + $user->timezone;
74 // Extract the requested date:
75 if (arg(0) == 'archive' && arg(3)) {
80 $requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
83 $year = date('Y', time());
84 $month = date('n', time());
85 $day = date('d', time());
87 $requested = $end_of_today + $user->timezone;
90 $start_of_month = mktime(0, 0, 0, $month, 1, $year);
92 // Extract first day of the month:
93 $first = date('w', $start_of_month);
95 // Extract last day of the month:
96 $last = date('t', $start_of_month);
98 $end_of_month = mktime(23, 59, 59, $month, $last, $year);
100 $cache = cache_get("archive:calendar:$day-$month-$year");
102 if (!empty($cache)) {
106 // Calculate previous and next months dates and check for shorter months (28/30 days)
107 $prevmonth = mktime(23, 59, 59, $month - 1, 1, $year);
108 $prev = mktime(23, 59, 59, $month - 1, min(date('t', $prevmonth), $day), $year);
109 $nextmonth = mktime(23, 59, 59, $month + 1, 1, $year);
110 $next = mktime(23, 59, 59, $month + 1, min(date('t', $nextmonth), $day), $year);
112 $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';
113 $sql = db_rewrite_sql($sql);
114 $result = db_query($sql, $start_of_month, $end_of_month);
116 $days_with_posts = array();
117 while ($day_with_post = db_fetch_object($result)) {
118 $daynum = date('j', $day_with_post->created + $user->timezone);
119 if (isset($days_with_posts[$daynum])) {
120 $days_with_posts[$daynum]++;
123 $days_with_posts[$daynum] = 1;
127 // Generate calendar header:
128 $output .= "\n<!-- calendar -->\n";
129 $output .= '<div class="calendar">';
130 $output .= '<table summary="'. t('A calendar to browse the archives') .".\">\n";
131 $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";
133 // First day of week (0 => Sunday, 1 => Monday, ...)
134 $weekstart = variable_get('date_first_day', 0);
137 ($weekstart - 1 == -1) ? $lastday = 6 : $lastday = $weekstart - 1;
139 // Generate the days of the week:
140 $firstcolumn = mktime(0, 0, 0, 3, 20 + $weekstart, 1994);
142 $output .= " <tr class=\"header-week\">\n";
143 $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'));
145 $days = array_merge(array_slice($days, $weekstart), array_slice($days, 0, $weekstart));
148 foreach ($days as $fullname => $name) {
149 $output .= ' <th abbr="'. $fullname .'">'. $name . "</th>\n";
151 $output .= "</tr>\n";
153 // Initialize temporary variables:
157 // Loop through all the days of the month:
158 while ($nday <= $last) {
159 // Set up blank days for first week of the month (allowing individual blank day styling):
160 if ($first != $weekstart) {
161 $blankdays = ($first - $weekstart + 7) % 7;
162 $output .= " <tr class=\"row-week\">" . str_repeat("<td class=\"day-blank\"> </td>\n", $blankdays);
165 // Start every week on a new line:
166 if ($sday == $weekstart) {
167 $output .= " <tr class=\"row-week\">\n";
171 $date = mktime(0, 0, 0, $month, $nday, $year) + $user->timezone;
172 if (isset($days_with_posts[$nday])) {
173 $daytext = l($nday, "archive/$year/$month/$nday", array("title" => format_plural($days_with_posts[$nday], "1 post", "%count posts")));
174 $dayclass = 'day-link';
178 $dayclass = 'day-normal';
180 if ($date == $requested) {
181 $output .= " <td class=\"day-selected\">$daytext</td>\n";
183 else if ($date == $start_of_today) {
184 $output .= " <td class=\"day-today\">$daytext</td>\n";
186 else if ($date > $end_of_today) {
187 $output .= " <td class=\"day-future\">$daytext</td>\n";
190 $output .= " <td class=\"$dayclass\">$daytext</td>\n";
193 // Start every week on a new line:
194 if ($sday == $lastday) {
195 $output .= " </tr>\n";
198 // Update temporary variables:
204 // Complete the calendar (allowing individual blank day styling):
205 if ($sday != $weekstart) {
206 $end = (7 - $sday + $weekstart) % 7;
207 $output .= str_repeat("<td class=\"day-blank\"> </td>\n", $end) . "</tr>\n";
210 $output .= "</table></div>\n\n";
212 cache_set("archive:calendar:$day-$month-$year", $output, CACHE_TEMPORARY);
218 * Menu callback; lists all nodes posted on a given date.
220 function archive_page($year = 0, $month = 0, $day = 0) {
223 $date = mktime(0, 0, 0, $month, $day, $year) - $user->timezone;
224 $date_end = mktime(0, 0, 0, $month, $day + 1, $year) - $user->timezone;
227 $output = archive_browse_form($year, $month, $day);
229 if ($year && $month && $day) {
230 // Fetch nodes for the selected date, if one was specified.
231 $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';
232 $sql = db_rewrite_sql($sql);
233 $result = db_query_range($sql, $date, $date_end, 0, 20);
234 if (db_num_rows($result) > 0) {
235 while ($nid = db_fetch_object($result)) {
236 $output .= node_view(node_load($nid->nid), 1);
240 $output .= theme('box', t('No posts found.'), '');
244 $output .= theme('box', t('No posts found.'), '');
251 * Generate a form that retrieves archives for a certain date.
253 function archive_browse_form($year, $month, $day) {
254 // Prepare the values of the form fields.
255 $years = drupal_map_assoc(range(2000, date('Y')));
256 $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'));
257 $days = drupal_map_assoc(range(1, 31));
259 $form['year'] = array('#type' => 'select',
260 '#default_value' => ($year ? $year : date('Y')),
261 '#options' => $years,
263 $form['month'] = array('#type' => 'select',
264 '#default_value' => ($month ? $month : date('m')),
265 '#options' => $months,
267 $form['day'] = array('#type' => 'select',
268 '#default_value' => ($day ? $day : date('d')),
271 $form['show'] = array('#type' => 'submit',
272 '#value' => t('Show'),
275 return drupal_get_form('archive_browse_form', $form);
279 * Process archive browse form submission.
281 function archive_browse_form_submit($form_id, $form_values) {
282 return('archive/'. $form_values['year'] .'/'. $form_values['month'] .'/'. $form_values['day']);
286 * Form theme function; displays the archive date navigation form inline.
288 function theme_archive_browse_form($form) {
289 $output = '<div class="container-inline archive">' . form_render($form) . '</div>';