d015cb580a927848f3b55e71f524d7da534ff956
[plewww.git] / modules / archive.module
1 <?php
2 // $Id: archive.module 144 2007-03-28 07:52:20Z thierry $
3
4 /**
5  * @file
6  * Displays a calendar to navigate old content.
7  */
8
9 /**
10  * Implementation of hook_help().
11  */
12 function archive_help($section) {
13   switch ($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>
18 <ul>
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 &gt;&gt; block</a>.</li>
21 </ul>
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>';
24       return $output;
25     case 'admin/modules#description':
26       return t('Displays a calendar for navigating older content.');
27   }
28 }
29
30 /**
31  * Implementation of hook_menu().
32  */
33 function archive_menu($may_cache) {
34   $items = array();
35
36   if ($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);
42   }
43   return $items;
44 }
45
46 /**
47  * Implementation of hook_block().
48  *
49  * Generates a calendar for the current month, with links to the archives
50  * for each day.
51  */
52 function archive_block($op = 'list', $delta = 0) {
53   if ($op == 'list') {
54     $blocks[0]['info'] = t('Calendar to browse archives');
55     return $blocks;
56   }
57   else if ($op == 'view' && user_access('access content')) {
58     $block['subject'] = t('Browse archives');
59     $block['content'] = archive_calendar();
60     return $block;
61   }
62 }
63
64 /**
65  * Generates a monthly calendar, for display in the archive block.
66  */
67 function archive_calendar() {
68   global $user;
69
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;
73
74   // Extract the requested date:
75   if (arg(0) == 'archive' && arg(3)) {
76     $year = arg(1);
77     $month = arg(2);
78     $day = arg(3);
79
80     $requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
81   }
82   else {
83     $year = date('Y', time());
84     $month  = date('n', time());
85     $day = date('d', time());
86
87     $requested = $end_of_today + $user->timezone;
88   }
89
90   $start_of_month = mktime(0, 0, 0, $month, 1, $year);
91
92   // Extract first day of the month:
93   $first = date('w', $start_of_month);
94
95   // Extract last day of the month:
96   $last = date('t', $start_of_month);
97
98   $end_of_month = mktime(23, 59, 59, $month, $last, $year);
99
100   $cache = cache_get("archive:calendar:$day-$month-$year");
101
102   if (!empty($cache)) {
103     return $cache->data;
104   }
105
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);
111
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);
115
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]++;
121     }
122     else {
123       $days_with_posts[$daynum] = 1;
124     }
125   }
126
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";
132
133   // First day of week (0 => Sunday, 1 => Monday, ...)
134   $weekstart = variable_get('date_first_day', 0);
135
136   // Last day of week
137   ($weekstart - 1 == -1) ? $lastday = 6 : $lastday = $weekstart - 1;
138
139   // Generate the days of the week:
140   $firstcolumn = mktime(0, 0, 0, 3, 20 + $weekstart, 1994);
141
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'));
144   if ($weekstart) {
145     $days = array_merge(array_slice($days, $weekstart), array_slice($days, 0, $weekstart));
146   }
147
148   foreach ($days as $fullname => $name) {
149     $output .= ' <th abbr="'. $fullname .'">'. $name . "</th>\n";
150   }
151   $output .= "</tr>\n";
152
153   // Initialize temporary variables:
154   $nday = 1;
155   $sday = $first;
156
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\">&nbsp;</td>\n", $blankdays);
163       $first = $weekstart;
164     }
165     // Start every week on a new line:
166     if ($sday == $weekstart) {
167       $output .= " <tr class=\"row-week\">\n";
168     }
169
170     // Print one cell:
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';
175     }
176     else {
177       $daytext = $nday;
178       $dayclass = 'day-normal';
179     }
180     if ($date == $requested) {
181       $output .= "  <td class=\"day-selected\">$daytext</td>\n";
182     }
183     else if ($date == $start_of_today) {
184       $output .= "  <td class=\"day-today\">$daytext</td>\n";
185     }
186     else if ($date > $end_of_today) {
187       $output .= "  <td class=\"day-future\">$daytext</td>\n";
188     }
189     else {
190       $output .= "  <td class=\"$dayclass\">$daytext</td>\n";
191     }
192
193     // Start every week on a new line:
194     if ($sday == $lastday) {
195       $output .=  " </tr>\n";
196     }
197
198     // Update temporary variables:
199     $sday++;
200     $sday = $sday % 7;
201     $nday++;
202   }
203
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\">&nbsp;</td>\n", $end) . "</tr>\n";
208   }
209
210   $output .= "</table></div>\n\n";
211
212   cache_set("archive:calendar:$day-$month-$year", $output, CACHE_TEMPORARY);
213
214   return $output;
215 }
216
217 /**
218  * Menu callback; lists all nodes posted on a given date.
219  */
220 function archive_page($year = 0, $month = 0, $day = 0) {
221   global $user;
222
223   $date = mktime(0, 0, 0, $month, $day, $year) - $user->timezone;
224   $date_end = mktime(0, 0, 0, $month, $day + 1, $year) - $user->timezone;
225
226   // Display form.
227   $output = archive_browse_form($year, $month, $day);
228
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);
237       }
238     }
239     else {
240       $output .= theme('box', t('No posts found.'), '');
241     }
242   }
243   else {
244     $output .= theme('box', t('No posts found.'), '');
245   }
246
247   return $output;
248 }
249
250 /**
251  * Generate a form that retrieves archives for a certain date.
252  */
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));
258
259   $form['year']  = array('#type' => 'select',
260     '#default_value' => ($year ? $year : date('Y')),
261     '#options' => $years,
262   );
263   $form['month'] = array('#type' => 'select',
264     '#default_value' => ($month ? $month : date('m')),
265     '#options' => $months,
266   );
267   $form['day']   = array('#type' => 'select',
268     '#default_value' => ($day ? $day : date('d')),
269     '#options' => $days,
270   );
271   $form['show']  = array('#type' => 'submit',
272     '#value' => t('Show'),
273   );
274
275   return drupal_get_form('archive_browse_form', $form);
276 }
277
278 /**
279  * Process archive browse form submission.
280  */
281 function archive_browse_form_submit($form_id, $form_values) {
282   return('archive/'. $form_values['year'] .'/'. $form_values['month'] .'/'. $form_values['day']);
283 }
284
285 /**
286  * Form theme function; displays the archive date navigation form inline.
287  */
288 function theme_archive_browse_form($form) {
289   $output = '<div class="container-inline archive">' . form_render($form) . '</div>';
290   return $output;
291 }