/[drupal]/contributions/modules/archive/archive.module
ViewVC logotype

Contents of /contributions/modules/archive/archive.module

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


Revision 1.37 - (show annotations) (download) (as text)
Tue May 6 18:41:01 2008 UTC (18 months, 2 weeks ago) by susurrus
Branch: MAIN
CVS Tags: HEAD
Changes since 1.36: +3 -5 lines
File MIME type: text/x-php
Initial port to D7 and works completely
1 <?php
2 // $Id: archive.module,v 1.36 2008/04/28 01:59:57 susurrus Exp $
3
4 /**
5 * Implementation of hook_help().
6 */
7 function archive_help($path, $arg) {
8 switch ($path) {
9 case 'admin/help#archive':
10 $output = '<p>'. t('The archive module allows your users to browse your content by its type and date of publication. The archive module interface, available at the path <a href="@archive">archive</a>, presents simple controls for selecting a year, month or day. All available posts published within this range are displayed beneath the control. Administrators may select the content types to be displayed. The archive module adds an <em>Archives</em> menu item to the default <em>Navigation</em> menu (the item is disabled by default), and displays the current month and year within an optional block.', array('@archive' => url('archive'))) .'</p>';
11 $output .= '<p>'. t('You can') .'</p>';
12 $output .= '<ul><li>'. t('view your <a href="@archive">archive page</a>.', array('@archive' => url('archive'))) .'</li>';
13 $output .= '<li>'. t('enable, position and configure the <em>Current calendar month with archive links</em> block at the <a href="@blocks">blocks administration page</a>.', array('@blocks' => url('admin/build/block'))) .'</li>';
14 $output .= '<li>'. t('enable or disable the <em>Archives</em> menu item on the <a href="@menu-administration">navigation menu administration page</a>.', array('@menu-administration' => url('admin/build/menu-customize/navigation'))) .'</li></ul>';
15 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@handbook-page">Archive module</a>.', array('@handbook-page' => 'http://drupal.org/handbook/modules/archive/')) .'</p>';
16 return $output;
17 }
18 }
19
20 /**
21 * Implementation of hook_menu().
22 */
23 function archive_menu() {
24 $items = array();
25 $items['archive'] = array(
26 'title' => 'Archives',
27 'access arguments' => array('access content'),
28 'page callback' => 'archive_page',
29 'type' => MENU_SUGGESTED_ITEM
30 );
31 $items['admin/settings/archive'] = array(
32 'title' => 'Archives',
33 'description' => 'Select the content types listed in the archives.',
34 'page callback' => 'drupal_get_form',
35 'page arguments' => array('archive_admin_settings'),
36 'access arguments' => array('access administration pages'),
37 'type' => MENU_NORMAL_ITEM
38 );
39
40 return $items;
41 }
42
43 /**
44 * Implementation of hook_theme().
45 */
46 function archive_theme() {
47 return array(
48 'archive_block_calendar' => array(
49 'arguments' => array('timestamp' => 0)
50 ),
51 'archive_page_title' => array(
52 'arguments' => array('type' => '', 'year' => NULL, 'month' => NULL, 'day' => NULL)
53 ),
54 'archive_navigation' => array(
55 'arguments' => array('type' => '', 'date' => NULL)
56 ),
57 'archive_navigation_days' => array(
58 'arguments' => array('type' => '', 'date' => NULL)
59 ),
60 'archive_navigation_months' => array(
61 'arguments' => array('type' => '', 'date' => NULL)
62 ),
63 'archive_navigation_node_types' => array(
64 'arguments' => array('type' => '', 'date' => NULL)
65 ),
66 'archive_navigation_years' => array(
67 'arguments' => array('type' => '', 'date' => NULL)
68 ),
69 'archive_separator' => array(
70 'arguments' => array('date_created' => 0, 'separators' => array())
71 ),
72 );
73 }
74
75 /**
76 * Returns a single month as a calendar grid.
77 *
78 * @todo
79 * Take the archive logic out to allow better theme-overloading
80 * of this function.
81 */
82 function theme_archive_block_calendar($timestamp) {
83 $the_date = explode(' ', format_date($timestamp, 'custom', 'F Y n t'));
84 $title = $the_date[0] .' '. $the_date[1];
85 $year = $the_date[1];
86 $month = $the_date[2];
87 $num_days = (int)$the_date[3];
88
89 $date = _archive_date('all', $year, $month);
90
91 $month_title = '';
92 if (in_array($month, $date->months)) {
93 $month_title = l($title, _archive_url('all', $year, $month), array('title' => format_plural($date->months[format_date($timestamp, 'custom', 'n')], '1 post', '@count posts')));
94 }
95 else {
96 $month_title = $title;
97 }
98
99 $rows[] = array(
100 array('data' => $month_title, 'colspan' => 7, 'style' => 'text-align:center'),
101 );
102
103 // Build the week starting with
104 $first_day_of_week = variable_get('date_first_day', 0);
105 $week = array(t('Sun'), t('Mon'), t('Tue'), t('Wed'), t('Thu'), t('Fri'), t('Sat'));
106 $day_titles = array();
107 for ($i = $first_day_of_week; $i < $first_day_of_week + 7; $i++) {
108 $day_titles[] = $week[$i%7];
109 }
110 $rows[] = $day_titles;
111
112 // Grab the first day of the month using the user's timezone
113 list($start_year, $start_month) = explode(' ', format_date($timestamp, 'custom', 'Y m'));
114 $start = gmmktime( 0, 0, 0, (int)$start_month, 1, (int)$start_year);
115 $weekday = gmdate('w', $start) - $first_day_of_week;
116 $days_row = array();
117
118 // From http://www.theadminzone.com/forums/showthread.php?t=17490
119 for ($i = 1-$weekday;$i <= ceil(($weekday+$num_days)/7)*7;$i++) {
120 if ($i > 0) {
121 if (array_key_exists($i, $date->days)) {
122 $days_row[] = l($i, _archive_url('all', $year, $month, $i),
123 array('attributes' => array('title' => format_plural($date->days[$i], '1 post', '@count posts'))));
124 }
125 else if ($i <= $num_days) {
126 $days_row[] = $i;
127 }
128 else {
129 $days_row[] = '';
130 }
131
132 // Add the week table row we just created if we've finished it
133 if (($i+$weekday)%7 == 0) {
134 $rows[] = $days_row;
135 $days_row = array();
136 }
137 }
138 else {
139 $days_row[] = '';
140 }
141 }
142 return theme('table', array(), $rows);
143 }
144
145
146 /**
147 * Implementation of hook_block().
148 */
149 function archive_block($op = 'list', $delta = 0) {
150 if ($op == 'list') {
151 $blocks[0] = array('info' => t('Current calendar month with archive links'));
152 return $blocks;
153 }
154 else if ($op == 'view') {
155 $block = array(
156 'subject' => t('Archives'),
157 'content' => theme('archive_block_calendar', time()),
158 );
159 return $block;
160 }
161 }
162
163 /**
164 * Generate an archive URL based on the $y, $m and $d provided.
165 *
166 * @param $type
167 * The node type to use if valid.
168 * @param $y
169 * The year to use if valid.
170 * @param $m
171 * The month to use if valid.
172 * @param $d
173 * The day to use if valid.
174 * @return
175 * A string with the generated archive URL.
176 */
177 function _archive_url($type, $y = 0, $m = 0, $d = 0) {
178 $url = 'archive';
179 if (_archive_validate_type($type)) {
180 $url .= '/'. $type;
181 }
182 else {
183 $url .= '/all';
184 }
185
186 if (_archive_validate_date($y, $m, $d)) {
187 $url .= '/'. $y .'/'. $m .'/'. $d;
188 }
189 else if (_archive_validate_date($y, $m)) {
190 $url .= '/'. $y .'/'. $m;
191 }
192 else if (_archive_validate_date($y)) {
193 $url .= '/'. $y;
194 }
195 return $url;
196 }
197
198 /**
199 * Parses the current URL and populates an archive date object.
200 *
201 * @param $year
202 * Number of year.
203 * @param $month
204 * Number of month.
205 * @param $day
206 * Number of day.
207 * @return
208 * A date object with GMT date values and a timezone value.
209 */
210 function _archive_date($type, $year = 0, $month = 0, $day = 0) {
211 $date = new stdClass();
212 $date->tz = _archive_get_timezone();
213
214 $date->year = 0;
215 $date->month = 0;
216 $date->day = 0;
217
218 if (_archive_validate_date($year, $month, $day)) {
219 $date->year = $year;
220 $date->month = $month;
221 $date->day = $day;
222 }
223 else if (_archive_validate_date($year, $month)) {
224 $date->year = $year;
225 $date->month = $month;
226 }
227 else if (_archive_validate_date($year)) {
228 $date->year = $year;
229 }
230
231 $post_counts = _archive_post_count($type, $date);
232 $date->years = $post_counts['years'];
233 ksort($date->years);
234 $date->months = $post_counts['months'];
235 ksort($date->months);
236 $date->days = $post_counts['days'];
237 ksort($date->days);
238
239 return $date;
240 }
241
242 /**
243 * Determine timezone to use for the dates (from format_date).
244 *
245 * @return
246 * Timezone offset to use in time operations.
247 */
248 function _archive_get_timezone() {
249 global $user;
250 if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
251 return $user->timezone;
252 }
253 else {
254 return variable_get('date_default_timezone', 0);
255 }
256 }
257
258 function _archive_types_sql_string($type) {
259 // Validate type and specify node types to include
260 $final_types = '';
261 if (_archive_validate_type($type) && $type != 'all') {
262 $final_types = $type;
263 }
264 else {
265 $types = variable_get('archive_type_filters', array());
266 // If no checkboxes selected
267 if (!array_key_exists('0', $types)) {
268 foreach ($types as $key => $value) {
269 if (!$value) {
270 unset($types[$key]);
271 }
272 }
273 $final_types = implode('\', \'', array_keys($types));
274 }
275 }
276 if (strlen($final_types) > 0) {
277 $final_types = 'AND n.type IN (\''. $final_types .'\') ';
278 }
279 return $final_types;
280 }
281
282 /**
283 * Check if given year, month and date combination is valid for the archive.
284 *
285 * @param $year
286 * The year to check.
287 * @param $month
288 * The month to check.
289 * @param $day
290 * The day to check.
291 * @return
292 * TRUE or FALSE.
293 */
294 function _archive_validate_date($year, $month = NULL, $day = NULL) {
295 $valid = FALSE;
296 if ($year && $year <= format_date(time(), 'custom', 'Y')) {
297 if (!is_null($month)) {
298 if (!is_null($day)) {
299 if ($last = gmdate('t', gmmktime(0, 0, 0, $month, 1, $year))) {
300 $valid = (0 < $day && $day <= $last);
301 }
302 }
303 else if (0 < $month && $month <= 12) {
304 $valid = TRUE;
305 }
306 }
307 else {
308 $valid = TRUE;
309 }
310 }
311 return $valid;
312 }
313
314 /**
315 * Check if given node type is valid for the archive.
316 *
317 * @param $type
318 * The type to check.
319 * @return
320 * TRUE or FALSE.
321 */
322 function _archive_validate_type($type) {
323 $types = variable_get('archive_type_filters', array());
324 return in_array($type, $types);
325 }
326
327 /**
328 * Returns the range of dates with nodes.
329 *
330 * @param $type
331 * The object that is being filtered.
332 * @param $date
333 * A date object returned from _archive_date().
334 * @return
335 * An array of the (first year with posts, last year with posts).
336 */
337 function _archive_post_count($type, $date) {
338 $final_types = _archive_types_sql_string($type);
339 $node_query = db_query(db_rewrite_sql('SELECT n.uid, n.created FROM {node} n WHERE n.status = 1 '. $final_types));
340
341 $with_posts = array('years' => array(), 'months' => array(), 'days' => array());
342 while ($o = db_fetch_object($node_query)) {
343 $node_date = explode(' ', format_date($o->created, 'custom', 'Y n j'));
344 $with_posts['years'][$node_date[0]] = array_key_exists($node_date[0], $with_posts['years'])?$with_posts['years'][$node_date[0]]+1:1;
345 if ($date->year && $node_date[0] == $date->year) {
346 $with_posts['months'][$node_date[1]] = array_key_exists($node_date[1], $with_posts['months'])?$with_posts['months'][$node_date[1]]+1:1;
347 if ($date->month && $node_date[1] == $date->month) {
348 $with_posts['days'][$node_date[2]] = array_key_exists($node_date[2], $with_posts['days'])?$with_posts['days'][$node_date[2]]+1:1;
349 }
350 }
351 }
352 return $with_posts;
353 }

  ViewVC Help
Powered by ViewVC 1.1.2