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

Contents of /contributions/modules/archiver/archiver.module

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


Revision 1.3 - (show annotations) (download) (as text)
Fri Apr 6 18:53:36 2007 UTC (2 years, 7 months ago) by lafo
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +64 -63 lines
File MIME type: text/x-php
Fixed incorrect ordering of nodes in the archiver listing.
1 <?php
2 // $Id$
3 define('ARCHIVER_PATH', drupal_get_path('module', 'archiver'));
4
5 /**
6 * Display help and module information
7 * @param section which section of the site we're displaying help
8 * @return help text for section
9 */
10 function archiver_help($section='') {
11 $output = '';
12 switch ($section) {
13 case "admin/modules#description":
14 $output = t("Allows archiving of older nodes (articles).");
15 break;
16 }
17 return $output;
18 }
19
20 /**
21 * Valid permissions for this module
22 * @return array An array of valid permissions for the onthisdate module
23 * display: allows listing of the archived nodes (q=archiver)
24 * manage: allows editing the archived/unarchived setting for nodes
25 * administer: grants access to the archiver settings
26 */
27 function archiver_perm() {
28 return array('browse archive', 'edit archive', 'configure archive' );
29 }
30
31 /**
32 * Generate HTML for the archiver block
33 * @param op the operation from the URL
34 * @param delta offset
35 * @returns block HTML
36 */
37 function archiver_block($op='list', $delta=0) {
38 // listing of blocks, such as on the admin/block page
39 if ($op == "list") {
40 $block[0]["info"] = t('Display the archive');
41 return $block;
42 }
43 // standard display of the content
44 else if ($op == "view") {
45 $block_content = "";
46 $block_content = archiver_generate_archive("block");
47 // set up the block
48 $block['subject'] = 'Archive';
49 $block['content'] = $block_content;
50 return $block;
51 }
52 else if ($op == "configure") {
53 return;
54 }
55
56 }
57
58 /**
59 * Display archive - page
60 **/
61 function archiver_display_archive() {
62 $page_content = '';
63 $page_content = archiver_generate_archive("page");
64 print theme("page", $page_content);
65 }
66
67 /* Generate HTML with articles read from DB. Various display settings may apply */
68 function archiver_generate_archive($context="page") {
69 $page_content = "";
70 $current_page = isset($_GET['page']) ? $_GET['page'] : 0;
71 $current_page = (int)$current_page;
72 $nodes_per_page = variable_get('archiver_max_nodes_per_page', 20);
73 $start_node = $current_page * $nodes_per_page;
74
75 $listed_items = array();
76 $listed_items = variable_get('archiver_listed_info', $listed_items);
77
78 //read all archived nodes
79 $archived_nodes = array();
80 $query = "SELECT nid FROM {archiver} ORDER BY created_on ASC LIMIT %d,%d";
81 $nids = db_query($query, $start_node, $nodes_per_page);
82 while($this_node = db_fetch_array($nids)) {
83 $query = "SELECT nid, title, created, type, uid FROM {node} WHERE nid=%d LIMIT 1";
84 $this_node = db_fetch_array(db_query($query, $this_node['nid']));
85 $this_year = date("Y", $this_node['created']);
86 $this_month = date("n", $this_node['created']);
87 $this_day = date("j", $this_node['created']);
88 $archived_nodes[$this_year][$this_month][$this_node['nid']]['title'] = $this_node['title'];
89 $archived_nodes[$this_year][$this_month][$this_node['nid']]['created'] = $this_node['created'];
90 $archived_nodes[$this_year][$this_month][$this_node['nid']]['nodetype'] = $this_node['type'];
91 $archived_nodes[$this_year][$this_month][$this_node['nid']]['author'] = $this_node['uid'];
92 }
93
94 $page_content = archiver_generate_html($archived_nodes);
95 return $page_content;
96 }
97
98 /*
99 * Here we forge html for the presentation.
100 * @$nodes is a structured array of archived nodes, defined in
101 * archiver_generate_archive
102 * TODO: employ some template engine to do this work
103 **/
104 function archiver_generate_html($nodes) {
105 // Define month names
106 $calendar_months = array(
107 1 => "January",
108 2 => "February",
109 3 => "March",
110 4 => "April",
111 5 => "May",
112 6 => "June",
113 7 => "July",
114 8 => "August",
115 9 => "September",
116 10 => "October",
117 11 => "November",
118 12 => "December",
119 );
120
121 // Define icon files paths
122 $path_to_icons = base_path().ARCHIVER_PATH;
123 $icon_files = array(
124 'story' => $path_to_icons . '/images/story.png',
125 'page' => $path_to_icons . '/images/page.png',
126 'image' => $path_to_icons . '/images/image.png',
127 'default' => $path_to_icons . '/images/default.png',
128 );
129
130 $node_displayed_info = check_plain(variable_get('archiver_displayed_details', ''));
131
132 $show_author = (substr_count($node_displayed_info, '%author') > 0) ? TRUE : FALSE;
133 $show_date = (substr_count($node_displayed_info, '%date') > 0) ? TRUE : FALSE;
134 $show_nodetype = (substr_count($node_displayed_info, '%nodetype') > 0) ? TRUE : FALSE;
135 $show_nodeicon = (substr_count($node_displayed_info, '%nodeicon') > 0) ? TRUE : FALSE;
136
137 $date_format = variable_get('archiver_date_format', '');
138 if ($date_format == "") $date_format = variable_get('date_format_short', '');
139
140 $html = '
141 <ul id="archiver_archive" class="clear-block">';
142
143 foreach($nodes as $year => $year_content) {
144 $html .= '
145 <li><h1>' . t("Year") . " " . $year . '</h1>
146 <ul>';
147 // process the year content
148 foreach($year_content as $month => $month_content) {
149 $html .= '
150 <li><h2>' . t($calendar_months[$month]) . '</h2>
151 <ul>';
152 // process the month content
153 foreach($month_content as $node_id=>$node_details) {
154 $this_node_displayed_info = $node_displayed_info;
155
156 //concatenate title
157 $max_title_length = variable_get('archiver_max_title_length', 50 );
158 if(strlen($node_details['title']) > $max_title_length) $node_title_short = substr($node_details['title'], 0, $max_title_length)."...";
159 else $node_title_short = $node_details['title'];
160
161 // replace parameter strings
162 if($show_author) {
163 // get author's name
164 $query = "SELECT name FROM {users} WHERE uid=%d LIMIT 1";
165 $result = db_fetch_array(db_query($query, $node_details['author']));
166 if(!$result = db_query($query, $node_details['author'])) {
167 $this_node_displayed_info = str_replace("%author", "unknown", $this_node_displayed_info);
168 } else {
169 $result = db_fetch_array($result);
170 $this_node_displayed_info = str_replace("%author", $result['name'], $this_node_displayed_info);
171 }
172 }
173
174 if($show_date) {
175 $this_node_displayed_info = str_replace("%date", date($date_format, $node_details['created']), $this_node_displayed_info);
176 }
177
178 if($show_nodetype) {
179 $this_node_displayed_info = str_replace("%nodetype", $node_details['nodetype'], $this_node_displayed_info);
180 }
181
182 if ($show_nodeicon) {
183 if (!array_key_exists($node_details['nodetype'], $icon_files)) $node_details['nodetype'] = 'default';
184 $this_node_displayed_info = str_replace("%nodeicon", '<img src="' . $icon_files[$node_details['nodetype']] . '" title="' . $node_details['nodetype'] . '" alt="' . $node_details['nodetype'] . '" />', $this_node_displayed_info);
185 }
186
187 $html .= '
188 <li><h3>'.l($node_title_short, 'node/'.$node_id, array('title' => $node_details['title'])).'</h3><span class="description archiver_nodeinfo">' . $this_node_displayed_info . '</span></li>';
189 }
190
191 $html .= '
192 </ul>
193 </li>';
194 }
195 $html .= '
196 </ul>
197 </li>';
198 }
199
200 $html .= "
201 </ul>
202 ";
203
204 $html .= archiver_generate_pager();
205
206 return $html;
207 }
208
209 function archiver_generate_pager() {
210 $output = "";
211 $query = "SELECT count(nid) AS count FROM {archiver}";
212 $node_count = db_fetch_array(db_query($query));
213 $node_count = $node_count['count'];
214 $nodes_per_page = variable_get('archiver_max_nodes_per_page', 20);
215 $page_count = ceil($node_count / $nodes_per_page);
216 if($page_count == 1) return "";
217 $current = isset($_GET['page']) ? $_GET['page'] : 0;
218 $current = (int)$current;
219 if ($current > $page_count-1) $current = 0;
220 $previous = $current - 1;
221 $next = $current + 1;
222 $last = $page_count-1;
223
224 $output = '
225 <div class="pager clear-block">
226 ';
227
228 if ($current != 0) {
229 $output .= l('« first', 'archiver', array('class'=>'pager-first active', 'title'=>'Go to first page') );
230 $output .= l('‹ previous', 'archiver', array('class'=>'pager-previous active', 'title'=>'Go to previous page'), 'page=' . $previous );
231 }
232 $output .= '<span class="pager-list">';
233
234 for($i = 0; $i < $page_count; $i++) {
235 $ii = $i + 1;
236 if ($i == $current) $output .= '<strong class="pager-current">' .$ii. '</strong>';
237 elseif ($i < $current) $output .= l($ii, 'archiver', array('class'=>'pager-previous active', 'title'=>'Go to page ' . $ii), 'page=' . $i );
238 elseif ($i > $current) $output .= l($ii, 'archiver', array('class'=>'pager-next active', 'title'=>'Go to page ' . $ii), 'page=' . $i );
239 }
240
241 $output .= '</span>';
242
243 if ($current < $page_count-1) {
244 $output .= l('next ›', 'archiver', array('class'=>'pager-next active', 'title'=>'Go to next page'), 'page=' . $next );
245 $output .= l('last »', 'archiver', array('class'=>'pager-last active', 'title'=>'Go to last page'), 'page=' . $last );
246 }
247
248 $output .= '</div>';
249 return $output;
250 }
251
252 /**
253 * Implementation of hook_form_alter()
254 * Adds "Archive this node" option to the node edit form and
255 * archiving controls to the admin/content page
256 *
257 */
258
259 function archiver_form_alter($form_id, &$form) {
260 $archiver_node_types = variable_get('archiver_node_types', array() );
261
262 if(isset($form['type']) &&
263 $form_id == $form['type']['#value'].'_node_form' &&
264 isset($form['#node']->nid) &&
265 in_array($form['#node']->type, $archiver_node_types, true) &&
266 user_access('edit archive')
267 ) {
268 $node = $form['#node'];
269 $node_is_archived = archiver_is_this_archived($node->nid);
270 $form['archive'] = array(
271 "#type" => 'fieldset',
272 "#title" => 'Archiving options',
273 "#collapsible" => 1,
274 "#collapsed" => !$node_is_archived,
275 "#weight" => 3,
276 );
277 $form['archive']['is_archived'] = array(
278 "#type" => "checkbox",
279 "#title" => "Archive this node",
280 "#default_value" => $node_is_archived,
281 );
282 }
283
284 if ($form_id == 'node_admin_nodes') {
285 // add 'archive'/'unarchive' link under the operations section of the admin node
286 // overview page (admin/content/node)
287 if ( !empty($form['operations']) ) {
288 foreach ( $form['operations'] as $nid => $title ) {
289 // only add archive link if archiving is enabled for this node type and node is not archived
290 if ( in_array(strtolower($form['name'][$nid]['#value']), $archiver_node_types) ) {
291 $form['operations'][$nid]['archiver_doit_link']['#value'] = archiver_doit_link($nid);
292 }
293 }
294 }
295
296 }
297
298 }
299
300 /**
301 * Returns HTML for the archive/unarchive link
302 */
303 function archiver_doit_link($nid) {
304 if(archiver_is_this_archived($nid)) $action = 'unarchive';
305 else $action = 'archive';
306
307 $link = l( ' '.$action, 'archiver/' . $action, array(), 'nid=' . $nid . '&destination=admin/content/node' );
308 return $link;
309 }
310
311 /**
312 * Implementation of hook_node_operation()
313 */
314 function archiver_node_operations() {
315 $operations = array(
316 'archive' => array(
317 'label' => t('Archive the selected posts'),
318 'callback' => 'archiver_operations_archive',
319 ),
320 'unarchive' => array(
321 'label' => t('Unarchive the selected posts'),
322 'callback' => 'archiver_operations_unarchive',
323 ),
324 );
325 return $operations;
326 }
327
328 /**
329 * Callback. Archives a single or multiple nodes
330 */
331 function archiver_operations_archive($nodes=array()) {
332 $destination = 'admin/content/node';
333 if(empty($nodes) and isset($_GET['nid']) ) $nodes[] = (int)$_GET['nid'];
334 if(empty($nodes)) drupal_goto($destination);
335
336
337 $wheres = '';
338 foreach($nodes as $nid) {
339 $wheres .= 'nid = ' . $nid . ' OR ';
340 }
341 $wheres = substr($wheres, 0, strlen($wheres)-4);
342 $nodes = db_query('SELECT nid, created FROM {node} WHERE ' .$wheres. ' LIMIT ' . count($nodes));
343
344 // get the dates of creation
345 $wheres = '';
346 $values = '';
347 while($this_node = db_fetch_array($nodes)) {
348 $wheres .= 'nid = ' . $this_node['nid'] . ' OR ';
349 $values .= "('" . $this_node['nid'] . "', '" . $this_node['created'] ."'), ";
350 }
351 $wheres = substr($wheres, 0, strlen($wheres)-4);
352 $values = substr($values, 0, strlen($values)-2);
353 $query = 'DELETE FROM {archiver} WHERE ' .$wheres. ' LIMIT ' . count($nodes);
354
355 db_query($query);
356 $query = 'INSERT INTO {archiver} (nid, created_on) VALUES ' . $values;
357 db_query($query);
358 drupal_goto($destination);
359 }
360
361 /**
362 * Callback. Unarchives a single or multiple nodes
363 * $nodes is array
364 */
365 function archiver_operations_unarchive($nodes=array()) {
366 $destination = 'admin/content/node';
367 if(empty($nodes) and isset($_GET['nid']) ) $nodes[] = (int)$_GET['nid'];
368 if(empty($nodes)) drupal_goto($destination);
369 $wheres = '';
370 foreach($nodes as $nid) {
371 $wheres .= 'nid = ' . $nid . ' OR ';
372 }
373 $wheres = substr($wheres, 0, strlen($wheres)-4);
374 $query = 'DELETE FROM {archiver} WHERE ' .$wheres. ' LIMIT ' . count($nodes);
375 db_query($query);
376 drupal_goto($destination);
377 }
378
379 /*
380 * Checks whether the passed node's id exists in the archiver table
381 */
382 function archiver_is_this_archived($nid) {
383 // is this node archived or not?
384 $sql = "SELECT COUNT(nid) AS count FROM {archiver} WHERE nid=%d";
385 $result = db_fetch_array(db_query($sql, $nid));
386 if($result['count'] > 0) return 1;
387 else return 0;
388 }
389
390
391 /*
392 * Perform archive/unarchive operation (requires the 'manage archiver' permission)
393 * This should happen whenever an existing node is updated
394 */
395 function archiver_nodeapi(&$node, $op) {
396 $types = array();
397 if (in_array($node->type, variable_get('archiver_node_types', $types), true) &&
398 user_access('edit archive') &&
399 $op == "update" ) {
400 $query = "DELETE FROM {archiver} WHERE nid = %d LIMIT 1";
401 db_query($query, $node->nid);
402 if($node->is_archived == 1) {
403 $query = "SELECT `created` FROM {node} WHERE `nid` = %d LIMIT 1";
404 $this_node = db_fetch_array(db_query($query, $node->nid));
405 $created_on = (int)$this_node['created'];
406 $query = "INSERT INTO {archiver} (`nid`,`created_on`) VALUES (%d, %d)";
407 $result = db_query($query, $node->nid, $created_on);
408 /*if(!$result) drupal_set_message("The node could not be archived.", "error");
409 else drupal_set_message("The node was moved to the archive.", "status");*/
410 } else {
411 // take it out from the archive = only delete from the table
412 }
413 }
414 }
415
416
417 /**
418 * Module configuration settings
419 * @return settings HTML or deny access
420 */
421 function archiver_admin_settings() {
422 // only administrators can access this module
423 if (!user_access("configure archive")) {
424 drupal_set_message(t('You have no privileges to access this page.'), "error");
425 return false;
426 }
427 /*$form['archiver_display_search'] = array(
428 '#type' => 'checkbox',
429 '#title' => t('Display search box (to be implemented)'),
430 '#default_value' => variable_get('archiver_displaysearch', 0),
431 '#description' => t("When checked, users can search within the archive."),
432 );*/
433 if(function_exists('node_get_types')){
434 $types = node_get_types('names');
435 $default_types = array_keys($types);
436 }
437
438 $form['archiver_general'] = array(
439 "#type" => 'fieldset',
440 "#title" => 'General settings',
441 "#collapsible" => 1,
442 "#collapsed" => 1,
443 "#weight" => -3,
444 );
445 $form['archiver_general']['archiver_node_types'] = array(
446 '#type' => 'checkboxes',
447 '#title' => t('Node types to archive'),
448 '#default_value' => variable_get('archiver_node_types', $default_types),
449 '#options' => $types,
450 '#description' => t("Allow these types of nodes to be archived."),
451 );
452 $form['archiver_display'] = array(
453 "#type" => 'fieldset',
454 "#title" => 'Display and format settings',
455 "#collapsible" => 1,
456 "#collapsed" => 1,
457 "#weight" => -2,
458 );
459 $form['archiver_display']['archiver_max_title_length'] = array(
460 '#type' => 'textfield',
461 '#title' => t('Maximum length of displayed node titles'),
462 '#default_value' => variable_get('archiver_max_title_length', 50),
463 '#size' => 5,
464 '#maxlength' => 3,
465 '#description' => t('Node titles on the archive listing will be trimmed down to this length.'),
466 );
467 $form['archiver_display']['archiver_max_nodes_per_page'] = array(
468 '#type' => 'textfield',
469 '#title' => t('Maximum number of nodes displayed on the archive page'),
470 '#default_value' => variable_get('archiver_max_nodes_per_page', 20),
471 '#size' => 5,
472 '#maxlength' => 5,
473 '#description' => t('If there is more nodes in the archive than this, the archive listing will be split into several pages.'),
474 );
475 $form['archiver_display']['archiver_displayed_details'] = array(
476 '#type' => 'textfield',
477 '#title' => t('Additional node details to display'),
478 '#default_value' => variable_get('archiver_displayed_details', ''),
479 '#size' => 100,
480 '#maxlength' => 100,
481 '#description' => t("This text will be displayed next to each node title in the archive listing. Available string parameters are %author, %date, %nodetype, %nodeicon. "),
482 );
483 $form['archiver_display']['archiver_date_format'] = array(
484 '#type' => 'textfield',
485 '#title' => t('Date format'),
486 '#default_value' => variable_get('archiver_date_format', '' ),
487 '#size' => 20,
488 '#maxlength' => 5,
489 '#description' => t("Format of %date displayed in the archive listing (you may ignore this if you do not display dates). If left blank, the system default will be used (if there is any). Consult " . l('the PHP manual', 'http://us3.php.net/manual/en/function.date.php') . " for available parameter strings."),
490 );
491 $form['archiver_hide'] = array(
492 "#type" => 'fieldset',
493 "#title" => 'Hide settings',
494 "#collapsible" => 1,
495 "#collapsed" => 1,
496 "#weight" => -1,
497 );
498 $form['archiver_hide']['archiver_remove_from_frontpage'] = array(
499 '#type' => 'checkbox',
500 '#title' => t('Remove archived nodes from the frontpage.'),
501 '#default_value' => variable_get('archiver_remove_from_frontpage', TRUE),
502 '#description' => t("If checked, archived nodes are no longer shown on the frontpage. To allow for this, node.module needs to be patched according to instructions from INSTALL.txt."),
503 );
504 $form['archiver_hide']['archiver_remove_from_categories'] = array(
505 '#type' => 'checkbox',
506 '#title' => t('Remove archived nodes from category listings.'),
507 '#default_value' => variable_get('archiver_remove_from_categories', TRUE),
508 '#description' => t("If checked, archived nodes are no longer shown in category listings. To allow for this, taxonomy.module needs to be patched according to instructions from INSTALL.txt"),
509 );
510
511 return system_settings_form($form);
512 }
513
514
515 /**
516 * Hook custom functions
517 **/
518 function archiver_menu() {
519 drupal_add_css(ARCHIVER_PATH .'/archiver.css');
520
521 $items = array();
522 $items[] = array('path' => 'archiver',
523 'title' => t('Archive'),
524 'callback' => 'archiver_display_archive',
525 'access' => user_access('browse archive'),
526 'type' => MENU_SUGGESTED_ITEM);
527 $items[] = array('path' => 'admin/settings/archiver',
528 'title' => t('Archiver'),
529 'description' => t('These settings control how archived nodes are displayed.'),
530 'callback' => 'drupal_get_form',
531 'callback arguments' => array('archiver_admin_settings'),
532 'access' => user_access('administer site configuration'),
533 'type' => MENU_NORMAL_ITEM, // optional
534 );
535 $items[] = array('path' => 'archiver/archive',
536 'title' => t('Archive a node'),
537 'callback' => 'archiver_operations_archive',
538 'callback arguments' => array(),
539 'access' => user_access('edit archive'),
540 'type' => MENU_CALLBACK, // optional
541 );
542 $items[] = array('path' => 'archiver/unarchive',
543 'title' => t('Unarchive a node'),
544 'callback' => 'archiver_operations_unarchive',
545 'callback arguments' => array(),
546 'access' => user_access('edit archive'),
547 'type' => MENU_CALLBACK, // optional
548 );
549 return $items;
550 }

  ViewVC Help
Powered by ViewVC 1.1.2