/[drupal]/contributions/modules/archive/archive.pages.inc
ViewVC logotype

Contents of /contributions/modules/archive/archive.pages.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.22 - (show annotations) (download) (as text)
Tue May 6 18:45:26 2008 UTC (18 months, 2 weeks ago) by susurrus
Branch: MAIN
CVS Tags: HEAD
Changes since 1.21: +3 -2 lines
File MIME type: text/x-php
[#243918] by destined
  - Use human-readable node type name in page title
1 <?php
2 // $Id: archive.pages.inc,v 1.21 2008/05/06 18:42:49 susurrus Exp $
3
4 /**
5 * Fetch nodes for the selected date, or current date if none selected.
6 *
7 * @param $year
8 * Number of year.
9 * @param $month
10 * Number of month.
11 * @param $day
12 * Number of day.
13 * @return
14 * A string with the themed page.
15 */
16 function archive_page($type = 'all', $year = 0, $month = 0, $day = 0) {
17
18 // Make sure all values are secure.
19 $day = (int) $day;
20 $year = (int) $year;
21 $month = (int) $month;
22 if ($month < 0 || $month > 12) {
23 // Ensure that we have a proper array index later.
24 $month = 0;
25 }
26 if (!_archive_validate_type($type)) {
27 $type = 'all';
28 }
29
30 drupal_set_title(theme('archive_page_title', $type, $year, $month, $day));
31
32 // Check that there're nodes we can display.
33 $nodes = db_result(db_query(db_rewrite_sql('SELECT COUNT(1) FROM {node} n WHERE n.status = 1 AND n.type IN (\''. implode('\',\'', variable_get('archive_type_filters', array())) .'\')')));
34 if (!$nodes && $type == 'all' && $year == 0 && $month == 0 && $day == 0) {
35 return t('No content found.');
36 }
37
38 drupal_add_css(drupal_get_path('module', 'archive') .'/archive.css');
39 $date = _archive_date($type, $year, $month, $day);
40 $output = theme('archive_navigation', $type, $date);
41 $nodes = variable_get('default_nodes_main', 10);
42 $query = _archive_query($type, $date);
43 $result = pager_query(db_rewrite_sql(array_shift($query)), $nodes, 0, null, $query);
44
45 $found_rows = false;
46 $node_date = 0;
47 while ($o = db_fetch_object($result)) {
48 $node = node_load($o->nid);
49 $node_created = $node->created + $date->tz;
50 // Determine which separators are needed
51 $separators = array('year' => 0,
52 'month' => 0,
53 'day' => 0);
54 if (!$year) {
55 $created_year = date('Y', $node_created);
56 $last_year = date('Y', $node_date);
57 if ($created_year != $last_year) {
58 $separators['year'] = 1;
59 }
60 }
61 // Print month separaters
62 if (!$month) {
63 $created_month = date('n', $node_created);
64 $last_month = date('n', $node_date);
65 if ($created_month != $last_month) {
66 $separators['month'] = 1;
67 }
68 }
69 // Print day separaters
70 if (!$day) {
71 $created_day = date('j', $node_created);
72 $last_day = date('j', $node_date);
73 if ($created_day != $last_day) {
74 $separators['day'] = 1;
75 }
76 }
77 $output .= theme('archive_separator', $node_created, $separators);
78 $output .= node_view($node, true);
79 $found_rows = true;
80 $node_date = $node->created + $date->tz;
81 }
82 if ($found_rows) {
83 $output .= theme('pager', null, $nodes);
84 }
85 // Handle URLs that are incorrectly typed and try to parse info out of them
86 else {
87 if (isset($date->days[$date->day])) {
88 drupal_goto(_archive_url($type, $date->year, $date->month, $date->day));
89 }
90 else if (isset($date->months[$date->month])) {
91 drupal_goto(_archive_url($type, $date->year, $date->month));
92 }
93 else if (isset($date->years[$date->year])) {
94 drupal_goto(_archive_url($type, $date->year));
95 }
96 else if ($_GET['q'] != ($url = _archive_url($type))){
97 drupal_goto($url);
98 }
99 }
100
101 return $output;
102 }
103
104 /**
105 * Builds an archive SQL query with its parameters for the specified date.
106 *
107 * @param $date
108 * A date object obtained from _archive_date().
109 * @return
110 * An array of (query, param_start, param_end).
111 */
112 function _archive_query($type, $date) {
113 // Confine the display interval to only one day
114 if ($date->day) {
115 $start = gmmktime(0, 0, 0, $date->month, $date->day, $date->year);
116 $end = gmmktime(0, 0, 0, $date->month, $date->day + 1, $date->year);
117 }
118 // Confine the display interval to one month
119 else if ($date->month) {
120 $start = gmmktime(0, 0, 0, $date->month, 1, $date->year);
121 $end = gmmktime(0, 0, 0, $date->month + 1, 1, $date->year);
122 }
123 // Confine the display interval to one year
124 else if ($date->year) {
125 $start = gmmktime(0, 0, 0, 1, 1, $date->year);
126 $end = gmmktime(0, 0, 0, 1, 1, $date->year + 1);
127 }
128 else {
129 $start = 0;
130 $end = 0;
131 }
132
133 // Grab limits on node types if exist
134 $final_types = _archive_types_sql_string($type);
135
136 // Allow viewing all nodes, not just nodes by year
137 if ($start && $end) {
138 return array('SELECT n.nid, n.type FROM {node} n WHERE n.status = 1 '. $final_types .'AND n.created >= %d AND n.created < %d ORDER BY n.created DESC', $start - $date->tz, $end - $date->tz);
139 }
140 else {
141 return array('SELECT n.nid, n.type FROM {node} n WHERE n.status = 1 '. $final_types .'ORDER BY n.created DESC');
142 }
143 }
144
145 /**
146 * Returns the different node types that have nodes.
147 *
148 * @param $date
149 * A date object obtained from _archive_date().
150 * @return
151 * An array of node-types to number of posts of that type.
152 */
153 function _archive_node_types($date) {
154
155 $types = variable_get('archive_type_filters', array());
156
157 // Confine the display interval to only one day
158 if ($date->day) {
159 $start = gmmktime(0, 0, 0, $date->month, $date->day, $date->year);
160 $end = gmmktime(0, 0, 0, $date->month, $date->day + 1, $date->year);
161 }
162 // Confine the display interval to one month
163 else if ($date->month) {
164 $start = gmmktime(0, 0, 0, $date->month, 1, $date->year);
165 $end = gmmktime(0, 0, 0, $date->month + 1, 1, $date->year);
166 }
167 // Confine the display interval to one year
168 else if ($date->year) {
169 $start = gmmktime(0, 0, 0, 1, 1, $date->year);
170 $end = gmmktime(0, 0, 0, 1, 1, $date->year + 1);
171 }
172 else {
173 $start = 0;
174 $end = 0;
175 }
176 if ($start && $end) {
177 $result = db_query(db_rewrite_sql('SELECT t.type, t.name, COUNT(n.nid) AS node_count FROM {node} n INNER JOIN {node_type} t ON t.type = n.type WHERE n.status = 1 AND t.type IN (\''. join($types, '\', \'') .'\') AND n.created BETWEEN %d AND %d GROUP BY n.type ORDER BY n.created'), $start - $date->tz, $end - $date->tz);
178 }
179 else {
180 $result = db_query(db_rewrite_sql('SELECT t.type, t.name, COUNT(n.nid) AS node_count FROM {node} n INNER JOIN {node_type} t ON t.type = n.type WHERE n.status = 1 AND t.type IN (\''. join($types, '\', \'') .'\') GROUP BY n.type ORDER BY n.created'));
181 }
182
183 $n_types = array();
184 while ($row = db_fetch_array($result)) {
185 $n_types[$row['type']] = array('count' => $row['node_count'],
186 'name' => $row['name']);
187 }
188
189 ksort($n_types);
190 return $n_types;
191 }
192
193 function theme_archive_page_title($type, $year, $month, $day) {
194
195 // Set the page title according to content that we're viewing.
196 $title = t('Archive');
197 $month_names = array('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
198 if ($day) {
199 $title .= ' - '. t($month_names[$month]) .' '. $day .', '. $year;
200 }
201 else if ($month) {
202 $title .= ' - '. t($month_names[$month]) .' '. $year;
203 }
204 else if ($year) {
205 $title .= ' - '. $year;
206 }
207 if ($type != 'all') {
208 $type_name = db_result(db_query('SELECT name FROM {node_type} WHERE type = \'%s\'', $type));
209 $title .= ' - '. ($type_name ? t($type_name) : $type);
210 }
211
212 return $title;
213 }
214
215 /**
216 * Theme the archive navigation with years, months and dates by default.
217 *
218 * @ingroup themeable
219 */
220 function theme_archive_navigation($type, $date) {
221 $output = "<div id=\"archive-container\"><dl><dt>". t('Date') ."</dt><dd>\n";
222 $output .= theme('archive_navigation_years', $type, $date);
223 if (_archive_validate_date($date->year)) {
224 $output .= theme('archive_navigation_months', $type, $date);
225 }
226 if (_archive_validate_date($date->year, $date->month)) {
227 $output .= theme('archive_navigation_days', $type, $date);
228 }
229 $output .= "</dd>";
230
231 // Only display node type filter if more than one node type represented
232 if (sizeof(_archive_node_types($date)) > 1) {
233 $output .= "<dt>". t('Type') ."</dt><dd>\n";
234 $output .= theme('archive_navigation_node_types', $type, $date);
235 $output .= "</dd>";
236 }
237 $output .= "</dl></div>\n";
238 return $output;
239 }
240
241 /**
242 * Theme the list of years for the archive navigation.
243 *
244 * @ingroup themeable
245 */
246 function theme_archive_navigation_years($type, $date) {
247 $output = "<ul id=\"archive-years\">\n";
248
249 $all_count = 0;
250 foreach ($date->years as $year_count) {
251 $all_count += $year_count;
252 }
253
254 $output .= '<li'. ($date->year?'':' class="selected"') .'>'. l(t('All'), _archive_url($type), array('attributes' => array('title' => format_plural($all_count, '1 post', '@count posts')))) ."</li>\n";
255 foreach ($date->years as $year => $year_count) {
256 $class = '';
257 if ($year == $date->year) {
258 $class = ' class="selected"';
259 }
260 $output .= '<li'. $class .'>'. l($year, _archive_url($type, $year), array('attributes' => array('title' => format_plural($year_count, '1 post', '@count posts')))) ."</li>\n";
261 }
262 $output .= "</ul>\n";
263
264 return $output;
265 }
266
267 /**
268 * Theme the list of months for the archive navigation.
269 *
270 * @ingroup themeable
271 */
272 function theme_archive_navigation_months($type, $date) {
273 $output = "<ul id=\"archive-months\">\n";
274
275 $all_count = 0;
276 foreach ($date->months as $month) {
277 $all_count += $month;
278 }
279
280 $output .= '<li'. ($date->month?'':' class="selected"') .'>'. l(t('All'), _archive_url($type, $date->year), array('attributes' => array('title' => format_plural($all_count, '1 post', '@count posts')))) ."</li>\n";
281 $curr_month = date('n', time());
282 $curr_year = date('Y', time());
283 foreach (range(1, 12) as $month) {
284 $posts = !empty($date->months[$month]) ? $date->months[$month] : 0;
285 $class = '';
286 if ($month == $date->month) {
287 $class = ' class="selected"';
288 }
289 else if ($curr_year == $date->year && $month > $curr_month) {
290 $class = ' class="future"';
291 }
292 $month_names = array('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
293 $output .= "<li$class>". ($posts > 0 ? l(t($month_names[$month]), _archive_url($type, $date->year, $month), array('attributes' => array('title' => format_plural($posts, "1 post", "@count posts")))) : t($month_names[$month])) ."</li>\n";
294 }
295 $output .= "</ul>\n";
296 return $output;
297 }
298
299 /**
300 * Theme the list of days for the archive navigation.
301 *
302 * @ingroup themeable
303 */
304 function theme_archive_navigation_days($type, $date) {
305 $output = "<ul id=\"archive-days\">\n";
306
307 $all_count = 0;
308 foreach ($date->days as $day) {
309 $all_count += $day;
310 }
311
312 $output .= '<li'. ($date->day?'':' class="selected"') .'>'. l(t('All'), _archive_url($type, $date->year, $date->month), array('attributes' => array('title' => format_plural($all_count, '1 post', '@count posts')))) ."</li>\n";
313 $curr_month = date('n', time());
314 $curr_year = date('Y', time());
315 $curr_day = date('j', time());
316
317 $day_stop = gmdate('t', gmmktime(0, 0, 0, $date->month, 1, $date->year));
318 for ($day = 1; $day <= $day_stop; $day++) {
319 $posts = array_key_exists($day, $date->days) ? $date->days[$day] : 0;
320 $class = '';
321 if ($day == $date->day) {
322 $class = ' class="selected"';
323 }
324 else if ($curr_year == $date->year && $curr_month == $date->month && $day > $curr_day) {
325 $class = ' class="future"';
326 }
327 $output .= "<li$class>". ($posts ? l($day, _archive_url($type, $date->year, $date->month, $day), array('attributes' => array("title" => format_plural($posts, "1 post", "@count posts")))) : $day) ."</li>\n";
328 }
329 $output .= "</ul>\n";
330 return $output;
331 }
332
333 /**
334 * Theme the list of node types for the archives.
335 *
336 * @ingroup themeable
337 */
338 function theme_archive_navigation_node_types($type, $date) {
339
340 $output = "<ul id=\"archive-node_types\">\n";
341 $types_count = _archive_node_types($date);
342
343 $all_count = 0;
344 foreach ($types_count as $t) {
345 $all_count += $t['count'];
346 }
347
348 $output .= '<li'. ($type && $type != 'all'?'':' class="selected"') .'>'. l(t('All'), _archive_url('all', $date->year, $date->month, $date->day), array('attributes' => array('title' => format_plural($all_count, '1 post', '@count posts')))) ."</li>\n";
349 foreach ($types_count as $ft_key => $ft_value) {
350 if (!$ft_value['count']) {
351 continue;
352 }
353 $class = ($ft_key == $type ? ' class="selected"' : '');
354 $name = $ft_value['name'];
355 if ($types_count[$ft_key]['count'] > 0) {
356 $output .= "<li$class>". l($name, _archive_url($ft_key, $date->year, $date->month, $date->day), array('attributes' => array("title" => format_plural($types_count[$ft_key]['count'], "1 post", "@count posts")))) ."</li>\n";
357 }
358 else {
359 $output .= "<li$class>$name</li>\n";
360 }
361 }
362 $output .= "</ul>\n";
363 return $output;
364 }
365
366 /**
367 * Theme the date separators between nodes of different year/month/day.
368 *
369 * @param $date_created
370 * A UNIX timestamp.
371 * @param $separators
372 * An array with 'year', 'month', and 'day' keys. A value of 1 for any
373 * of those keys means a transition for that unit of time.
374 * Ex. array('year' => 0, 'month' => 1, 'day' => 1)
375 * ^ Means the month has transitioned
376 *
377 * @ingroup themeable
378 */
379 function theme_archive_separator($date_created, $separators) {
380 $date_sep = '';
381 if ($separators['year'] && $separators['month'] && $separators['day']) {
382 $date_sep = format_date($date_created, 'custom', 'F jS, Y');
383 }
384 else if ($separators['month'] && $separators['day']) {
385 $date_sep = format_date($date_created, 'custom', 'F jS');
386 }
387 else if ($separators['day']) {
388 $date_sep = format_date($date_created, 'custom', 'F jS');
389 }
390
391 return '<h3>'. $date_sep .'</h3>';
392 }

  ViewVC Help
Powered by ViewVC 1.1.2