Issue #1281678 by Damien Tournoud: issue a single request in print_*_node_load().
[project/print.git] / print.module
1 <?php
2
3 /**
4 * @defgroup print Printer, e-mail and PDF versions
5 *
6 * Welcome to the print module developer's documentation. The interesting
7 * functions for themers are those that start with 'theme_'.
8 *
9 * - Printer-friendly pages
10 * - @link print.module Module main file @endlink
11 * - @link print.admin.inc Settings form @endlink
12 * - @link print.pages.inc HTML generation @endlink
13 * - @link print.install (Un)Install routines @endlink
14 * - @link print.tpl.php Page generation template @endlink
15 * - Send by e-mail
16 * - @link print_mail.module Module main file @endlink
17 * - @link print_mail.admin.inc Settings form @endlink
18 * - @link print_mail.inc Mail form and send mail routine @endlink
19 * - @link print_mail.install (Un)Install routines @endlink
20 * - PDF version
21 * - @link print_pdf.module Module main file @endlink
22 * - @link print_pdf.admin.inc Settings form @endlink
23 * - @link print_pdf.pages.inc PDF generation @endlink
24 * - @link print_pdf.class.inc Auxiliary PHP5 class @endlink
25 * - @link print_pdf.class_php4.inc Auxiliary PHP4 class @endlink
26 * - @link print_pdf.install (Un)Install routines @endlink
27 */
28
29 /**
30 * @file
31 * Displays Printer-friendly versions of Drupal pages.
32 *
33 * This is the core module of the PF package, with the Drupal hooks
34 * and other administrative functions.
35 *
36 * @ingroup print
37 */
38
39 define('PRINT_PATH', 'print');
40
41 define('PRINT_HTML_FORMAT', 'html');
42 define('PRINT_MAIL_FORMAT', 'mail');
43 define('PRINT_PDF_FORMAT', 'pdf');
44 define('PRINT_LOGO_OPTIONS_DEFAULT', 1);
45 define('PRINT_LOGO_URL_DEFAULT', '');
46 define('PRINT_FOOTER_OPTIONS_DEFAULT', 1);
47 define('PRINT_FOOTER_USER_DEFAULT', '');
48 define('PRINT_CSS_DEFAULT', '');
49 define('PRINT_URLS_DEFAULT', 1);
50 define('PRINT_URLS_ANCHORS_DEFAULT', 0);
51 define('PRINT_COMMENTS_DEFAULT', 0);
52 define('PRINT_NEWWINDOW_DEFAULT', 1);
53
54 define('PRINT_HTML_LINK_POS_DEFAULT', 'link');
55 define('PRINT_HTML_LINK_TEASER_DEFAULT', 0);
56 define('PRINT_HTML_SHOW_LINK_DEFAULT', 1);
57 define('PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT', 0);
58 define('PRINT_HTML_NODE_LINK_PAGES_DEFAULT', '');
59 define('PRINT_HTML_LINK_CLASS_DEFAULT', 'print-page');
60 define('PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT', 1);
61 define('PRINT_HTML_SYS_LINK_PAGES_DEFAULT', '');
62 define('PRINT_HTML_LINK_USE_ALIAS_DEFAULT', 0);
63 define('PRINT_HTML_BOOK_LINK_DEFAULT', 1);
64 define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
65 define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);
66 define('PRINT_HTML_WINDOWCLOSE_DEFAULT', 1);
67
68 define('PRINT_SOURCEURL_ENABLED_DEFAULT', 1);
69 define('PRINT_SOURCEURL_DATE_DEFAULT', 0);
70 define('PRINT_SOURCEURL_FORCENODE_DEFAULT', 0);
71
72 define('PRINT_ROBOTS_NOINDEX_DEFAULT', 1);
73 define('PRINT_ROBOTS_NOFOLLOW_DEFAULT', 1);
74 define('PRINT_ROBOTS_NOARCHIVE_DEFAULT', 0);
75
76 define('PRINT_TYPE_SHOW_LINK_DEFAULT', 1);
77 define('PRINT_TYPE_COMMENT_LINK_DEFAULT', 0);
78 define('PRINT_TYPE_URLLIST_DEFAULT', 1);
79 define('PRINT_TYPE_SYS_URLLIST_DEFAULT', 0);
80
81 define('PRINT_ALLOW_NORMAL_LINK', 1);
82 define('PRINT_ALLOW_BOOK_LINK', 2);
83 define('PRINT_TYPE_FIELDS_WEIGHT', 30);
84
85 /**
86 * Implements hook_permission().
87 */
88 function print_permission() {
89 return array(
90 'administer print' => array(
91 'title' => t('Administer the module'),
92 'description' => t('Perform maintenance tasks for the print module.'),
93 ),
94 'node-specific print configuration' => array(
95 'title' => t('Node-specific configuration'),
96 'description' => t('Enable access to the per-node settings.'),
97 ),
98 'access print' => array(
99 'title' => t('Access the printer-friendly page'),
100 'description' => t('View the printer-friendly pages and the links to them in the original pages.'),
101 ),
102 );
103 }
104
105 /**
106 * Implements hook_theme().
107 */
108 function print_theme() {
109 return array(
110 'print_format_link' => array(
111 'variables' => array(),
112 ),
113 'print_node' => array(
114 'variables' => array('node' => NULL, 'teaser' => FALSE, 'page' => FALSE, 'type' => PRINT_HTML_FORMAT),
115 'template' => 'print_node',
116 ),
117 'print' => array(
118 'variables' => array('print' => array(), 'type' => PRINT_HTML_FORMAT, 'node' => NULL),
119 'template' => 'print',
120 ),
121 );
122 }
123
124 /**
125 * Implements hook_preprocess_HOOK().
126 */
127 function print_preprocess_print_node(&$variables) {
128 static $hooks = NULL;
129 if (!isset($hooks)) {
130 drupal_theme_initialize();
131 $hooks = theme_get_registry();
132 }
133
134 //Stolen from theme() so that ALL preprocess functions are called
135 $hook = 'node';
136 $info = $hooks[$hook];
137 if (isset($info['preprocess functions']) || isset($info['process functions'])) {
138 $variables['theme_hook_suggestions'] = array();
139 foreach (array('preprocess functions', 'process functions') as $phase) {
140 if (!empty($info[$phase])) {
141 foreach ($info[$phase] as $processor_function) {
142 if (function_exists($processor_function)) {
143 // We don't want a poorly behaved process function changing $hook.
144 $hook_clone = $hook;
145 $processor_function($variables, $hook_clone);
146 }
147 }
148 }
149 }
150 }
151
152 $variables['node'] = $variables['elements']['#node'];
153 $type = $variables['node']->type;
154 $format = $variables['type'];
155 // template_preprocess_node($variables);
156
157 $variables['theme_hook_suggestions'][] = "node";
158 $variables['theme_hook_suggestions'][] = "node__{$type}";
159 $variables['theme_hook_suggestions'][] = "print_node";
160 // $variables['theme_hook_suggestions'][] = "print_node_{$format}";
161 // $variables['theme_hook_suggestions'][] = "print_node_{$format}.node__{$type}";
162 }
163
164 /**
165 * Implements hook_preprocess_HOOK().
166 */
167 function print_preprocess_print(&$variables) {
168 static $hooks = NULL;
169 if (!isset($hooks)) {
170 drupal_theme_initialize();
171 $hooks = theme_get_registry();
172 }
173
174 $variables['page']['#show_messages'] = FALSE;
175
176 //Stolen from theme() so that ALL preprocess functions are called
177 $hook = 'page';
178 $info = $hooks[$hook];
179 if (isset($info['preprocess functions']) || isset($info['process functions'])) {
180 $variables['theme_hook_suggestions'] = array();
181 foreach (array('preprocess functions', 'process functions') as $phase) {
182 if (!empty($info[$phase])) {
183 foreach ($info[$phase] as $processor_function) {
184 if (function_exists($processor_function)) {
185 // We don't want a poorly behaved process function changing $hook.
186 $hook_clone = $hook;
187 $processor_function($variables, $hook_clone);
188 }
189 }
190 }
191 }
192 }
193
194 $format = $variables['type'];
195 $type = (isset($variables['node']->type)) ? $variables['node']->type : '';
196 $nid = (isset($variables['node']->nid)) ? $variables['node']->nid : '';
197
198 // $variables['theme_hook_suggestions'][] = "print";
199 $variables['theme_hook_suggestions'][] = "print__node__{$type}";
200 $variables['theme_hook_suggestions'][] = "print__node__{$type}__{$nid}";
201 $variables['theme_hook_suggestions'][] = "print__{$format}";
202 $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}";
203 $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}__{$nid}";
204 }
205
206 /**
207 * Implements hook_menu().
208 */
209 function print_menu() {
210 $items = array();
211
212 $items[PRINT_PATH] = array(
213 'title' => 'Printer-friendly',
214 'page callback' => 'print_controller_html',
215 'access arguments' => array('access print'),
216 'type' => MENU_CALLBACK,
217 'file' => 'print.pages.inc',
218 );
219 $items[PRINT_PATH . '/' . PRINT_PATH] = array(
220 'access callback' => FALSE,
221 );
222 $items['admin/config/user-interface/print'] = array(
223 'title' => 'Printer, e-mail and PDF versions',
224 'description' => 'Adds a printer-friendly version link to content and administrative pages.',
225 'page callback' => 'drupal_get_form',
226 'page arguments' => array('print_html_settings'),
227 'access arguments' => array('administer print'),
228 'file' => 'print.admin.inc',
229 );
230 $items['admin/config/user-interface/print/html'] = array(
231 'title' => 'Web page',
232 'weight' => 1,
233 'type' => MENU_DEFAULT_LOCAL_TASK,
234 );
235 $items['admin/config/user-interface/print/html/options'] = array(
236 'title' => 'Options',
237 'weight' => 1,
238 'type' => MENU_DEFAULT_LOCAL_TASK,
239 );
240 $items['admin/config/user-interface/print/html/strings'] = array(
241 'title' => 'Text strings',
242 'page callback' => 'drupal_get_form',
243 'page arguments' => array('print_html_strings_settings'),
244 'access arguments' => array('administer print'),
245 'weight' => 2,
246 'type' => MENU_LOCAL_TASK,
247 'file' => 'print.admin.inc',
248 );
249 $items['admin/config/user-interface/print/common'] = array(
250 'title' => 'Settings',
251 'page callback' => 'drupal_get_form',
252 'page arguments' => array('print_main_settings'),
253 'access arguments' => array('administer print'),
254 'weight' => 10,
255 'type' => MENU_LOCAL_TASK,
256 'file' => 'print.admin.inc',
257 );
258 $items['admin/config/user-interface/print/common/options'] = array(
259 'title' => 'Options',
260 'weight' => 1,
261 'type' => MENU_DEFAULT_LOCAL_TASK,
262 );
263 $items['admin/config/user-interface/print/common/strings'] = array(
264 'title' => 'Text strings',
265 'page callback' => 'drupal_get_form',
266 'page arguments' => array('print_main_strings_settings'),
267 'access arguments' => array('administer print'),
268 'weight' => 2,
269 'type' => MENU_LOCAL_TASK,
270 'file' => 'print.admin.inc',
271 );
272
273 return $items;
274 }
275
276 /**
277 * Implements hook_block_info().
278 */
279 function print_block_info() {
280 $block['print-links']['info'] = t('Printer, e-mail and PDF versions');
281 $block['print-links']['cache'] = DRUPAL_CACHE_PER_PAGE;
282 $block['print-top']['info'] = t('Most printed');
283 $block['print-top']['cache'] = DRUPAL_CACHE_GLOBAL;
284 return $block;
285 }
286
287 /**
288 * Implements hook_block_view().
289 */
290 function print_block_view($delta = '') {
291 switch ($delta) {
292 case 'print-links':
293 $nid = preg_replace('!^node/!', '', $_GET['q']);
294 if (ctype_digit($nid)) {
295 $node = node_load($nid);
296 if (!node_access('view', $node)) {
297 // If the user doesn't have access to the node, don't show any links
298 $block['content'] == '';
299 return;
300 }
301 }
302 else {
303 $node = NULL;
304 }
305 $funcs = get_defined_functions();
306 $block['content'] = '';
307 foreach ($funcs['user'] as $func) {
308 if (preg_match('!^print.*?_insert_link$!', $func)) {
309 $link = $func(NULL, $node);
310 if (!empty($link)) {
311 $block['content'] .= $link;
312 }
313 }
314 }
315 break;
316 case 'print-top':
317 $block['subject'] = t('Most printed');
318 $result = db_query_range("SELECT path FROM {print_page_counter} ORDER BY totalcount DESC", 0, 3)
319 ->fetchAll();
320 if (count($result)) {
321 $block['content'] = '<div class="item-list"><ul>';
322 foreach ($result as $obj) {
323 $block['content'] .= '<li>' . l(_print_get_title($obj->path), $obj->path) . '</li>';
324 }
325 $block['content'] .= '</ul></div>';
326 }
327 break;
328 }
329 return $block;
330 }
331
332 /**
333 * Implements hook_node_view_alter().
334 */
335 function print_node_view_alter(&$build) {
336 if (isset($build['links']['book']['#links']['book_printer'])) {
337 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
338
339 if ($print_html_book_link) {
340 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
341
342 if (!empty($print_html_link_pos['link'])) {
343 $format = theme('print_format_link');
344
345 switch ($print_html_book_link) {
346 case 1:
347 $path = $build['links']['book']['#links']['book_printer']['href'];
348 break;
349 case 2:
350 $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT);
351 $path = ($print_html_link_use_alias && isset($node->path)) ? $node->path : $node->nid;
352 break;
353 }
354
355 $build['links']['book']['#links']['book_printer'] = array(
356 'href' => PRINT_PATH . '/' . $path,
357 'title' => $format['text'],
358 'attributes' => $format['attributes'],
359 'html' => $format['html'],
360 );
361 }
362 else {
363 unset($build['links']['book']['#links']['book_printer']);
364 }
365 }
366 }
367 }
368
369 /**
370 * Implements hook_help().
371 */
372 function print_help($path, $arg) {
373 switch ($path) {
374 case 'admin/help#print':
375 // Return a line-break version of the module README
376 return _filter_autop(file_get_contents(drupal_get_path('module', 'print') . '/README.txt'));
377 }
378
379 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
380 if (($path !== 'node/%') && !(empty($print_html_link_pos['link']) && empty($print_html_link_pos['corner']))) {
381 static $output = FALSE;
382
383 if ($output === FALSE) {
384 $output = TRUE;
385
386 $link = print_insert_link();
387 if ($link) {
388 return "<span class='print-syslink'>$link</span>";
389 }
390 }
391 }
392 }
393
394 /**
395 * Implements hook_node_view().
396 */
397 function print_node_view($node, $view_mode) {
398 $type = 'node';
399 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
400 $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT);
401 $allowed_type = print_link_allowed(array('type' => $type, 'node' => $node, 'view_mode' => $view_mode));
402 if (($allowed_type === PRINT_ALLOW_NORMAL_LINK) && !isset($node->book) && !empty($print_html_link_pos['link'])) {
403 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
404 $links = array();
405 $format = theme('print_format_link');
406
407 $query_arr = $_GET;
408 if ($type == 'comment') {
409 $query_arr['comment'] = $node->cid;
410 }
411 $query = print_query_string_encode($query_arr, array('q'));
412 if (empty($query)) $query = NULL;
413
414 if ($print_html_link_use_alias) {
415 $path = drupal_get_path_alias('node/' . $node->nid);
416 }
417 else {
418 $path = $node->nid;
419 }
420
421 $links['print_html'] = array(
422 'href' => PRINT_PATH . '/' . $path,
423 'title' => $format['text'],
424 'attributes' => $format['attributes'],
425 'html' => $format['html'],
426 'query' => $query,
427 );
428
429 $node->content['links']['print_html'] = array(
430 '#theme' => 'links',
431 '#links' => $links,
432 '#attributes' => array('class' => array('links', 'inline')),
433 );
434 }
435
436 if ($view_mode == 'full') {
437 // Insert content corner links
438 $node->content['print_links'] = array(
439 '#prefix' => '<span class="print-link">',
440 '#markup' => '',
441 '#suffix' => '</span>',
442 '#weight' => -101,
443 );
444 if (!empty($print_html_link_pos['corner'])) {
445 $node->content['print_links']['#markup'] .= print_insert_link(NULL, $node);
446 }
447 }
448 }
449
450 /**
451 * Implements hook_node_load().
452 */
453 function print_node_load($nodes, $types) {
454 $ids = array();
455 foreach ($nodes as $node) {
456 $ids[] = $node->nid;
457 }
458
459 $result = db_query('SELECT nid, link, comments, url_list FROM {print_node_conf} WHERE nid IN (:nids)', array(':nids' => $ids))->fetchAllAssoc('nid');
460
461 foreach ($nodes as $node) {
462 $node->print_display = isset($result[$node->nid]) ? intval($result[$node->nid]->link) : variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
463 $node->print_display_comment = isset($result[$node->nid]) ? intval($result[$node->nid]->comments) : variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
464 $node->print_display_urllist = isset($result[$node->nid]) ? intval($result[$node->nid]->url_list) : variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
465 }
466 }
467
468 /**
469 * Implements hook_node_insert().
470 */
471 function print_node_insert($node) {
472 if (user_access('administer print') || user_access('node-specific print configuration')) {
473 if (!isset($node->print_display) || $node->print_display === NULL) {
474 $node->print_display = variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
475 }
476 if (!isset($node->print_display_comment) || $node->print_display_comment === NULL) {
477 $node->print_display_comment = variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
478 }
479 if (!isset($node->print_display_urllist) || $node->print_display_urllist === NULL) {
480 $node->print_display_urllist = variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
481 }
482
483 _print_node_conf_modify($node->nid, $node->print_display, $node->print_display_comment, $node->print_display_urllist);
484 }
485 }
486
487 /**
488 * Implements hook_node_update().
489 */
490 function print_node_update($node) {
491 if (user_access('administer print') || user_access('node-specific print configuration')) {
492 if ($node->print_display === NULL) $node->print_display = variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
493 if ($node->print_display_comment === NULL) $node->print_display_comment = variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
494 if ($node->print_display_urllist === NULL) $node->print_display_urllist = variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
495
496 _print_node_conf_modify($node->nid, $node->print_display, $node->print_display_comment, $node->print_display_urllist);
497 }
498 }
499
500 /**
501 * Implements hook_node_delete().
502 */
503 function print_node_delete($node) {
504 db_delete('print_node_conf')
505 ->condition('nid', $node->nid)
506 ->execute();
507 db_delete('print_page_counter')
508 ->condition('path', 'node/' . $node->nid)
509 ->execute();
510 }
511
512 /**
513 * Implements hook_form_alter().
514 */
515 function print_form_alter(&$form, &$form_state, $form_id) {
516 // Add the node-type settings option to activate the printer-friendly version link
517 if ((user_access('administer print') || user_access('node-specific print configuration')) &&
518 (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
519 $form['print'] = array(
520 '#type' => 'fieldset',
521 '#title' => t('Printer, e-mail and PDF versions'),
522 '#collapsible' => TRUE,
523 '#collapsed' => TRUE,
524 '#weight' => PRINT_TYPE_FIELDS_WEIGHT,
525 '#group' => 'additional_settings',
526 );
527
528 $form['print']['label'] = array(
529 '#type' => 'markup',
530 '#markup' => '<p><strong>' . t('Printer-friendly version') . '</strong></p>',
531 );
532
533 $form['print']['print_display'] = array(
534 '#type' => 'checkbox',
535 '#title' => t('Show link'),
536 );
537 $form['print']['print_display_comment'] = array(
538 '#type' => 'checkbox',
539 '#title' => t('Show link in individual comments'),
540 );
541 $form['print']['print_display_urllist'] = array(
542 '#type' => 'checkbox',
543 '#title' => t('Show Printer-friendly URLs list'),
544 );
545
546 if ($form_id == 'node_type_form') {
547 $form['print']['print_display']['#default_value'] = variable_get('print_display_' . $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
548 $form['print']['print_display_comment']['#default_value'] = variable_get('print_display_comment_' . $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
549 $form['print']['print_display_urllist']['#default_value'] = variable_get('print_display_urllist_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
550 }
551 else {
552 $node = $form['#node'];
553 $form['print']['print_display']['#default_value'] = isset($node->print_display) ? $node->print_display : variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
554 $form['print']['print_display_comment']['#default_value'] = isset($node->print_display_comment) ? $node->print_display_comment : variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
555 $form['print']['print_display_urllist']['#default_value'] = isset($node->print_display_urllist) ? $node->print_display_urllist : variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
556 }
557 }
558 }
559
560 /**
561 * Implements hook_entity_info_alter().
562 */
563 function print_entity_info_alter(&$info) {
564 // Add the 'Print' view mode for nodes.
565 $info['node']['view modes'] += array(
566 'print' => array(
567 'label' => t('Print'),
568 'custom settings' => FALSE,
569 ),
570 );
571 }
572
573 /**
574 * Auxiliary function to discover a given page's title
575 *
576 * @param $path
577 * path of the page being identified
578 * @return
579 * string with the page's title
580 */
581 function _print_get_title($path) {
582 $path = drupal_get_normal_path($path);
583 $nid = preg_replace('!^node/!', '', $path);
584 if (ctype_digit($nid)) {
585 $res = db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $nid))
586 ->fetchField();
587 }
588 else {
589 $res = db_query("SELECT link_title FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $path))
590 ->fetchField();
591 }
592 return $res;
593 }
594
595 /**
596 * Update the print_node_conf table to reflect the given attributes
597 * If updating to the default values, delete the record.
598 *
599 * @param $nid
600 * value of the nid field (primary key)
601 * @param $link
602 * value of the link field (0 or 1)
603 * @param $comments
604 * value of the comments field (0 or 1)
605 * @param $url_list
606 * value of the url_list field (0 or 1)
607 */
608 function _print_node_conf_modify($nid, $link, $comments, $url_list) {
609 db_merge('print_node_conf')
610 ->key(array('nid' => $nid))
611 ->fields(array(
612 'link' => $link,
613 'comments' => $comments,
614 'url_list' => $url_list,
615 ))
616 ->execute();
617 }
618
619 /**
620 * Auxiliary function to fill the Printer-friendly link attributes
621 *
622 * @param $title
623 * text to displayed by the link when hovering over it with the mouse
624 * @param $class
625 * class attribute to be used in the link
626 * @param $new_window
627 * if TRUE opens the target page in a new window
628 * @return
629 * array of formatted attributes
630 */
631 function print_fill_attributes($title = '', $class = '', $new_window = FALSE) {
632 $print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT);
633 $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
634
635 $attributes = array();
636 $attributes['title'] = $title;
637 if (!empty($class)) {
638 $attributes['class'] = array($class);
639 }
640
641 if ($new_window) {
642 switch ($print_newwindow) {
643 case 0:
644 $attributes['target'] = '_blank';
645 break;
646 case 1:
647 $attributes['onclick'] = 'window.open(this.href); return false';
648 break;
649 }
650 }
651 if (!empty($print_robots_noindex)) {
652 $attributes['rel'] = 'nofollow';
653 }
654 return $attributes;
655 }
656
657 /**
658 * Auxiliary function to set the link text and html flag
659 *
660 * @param $type
661 * type of link: 0 or 1 for a text-only link, 2 for icon-only and 3 for
662 * both text and icon
663 * @param $text
664 * text to be displayed on the link to the printer-friendly page
665 * @param $img
666 * path to the icon file
667 * @return
668 * array with the link text and html flag
669 */
670 function _print_format_link_aux($type = 0, $text = '', $img = '') {
671 if ($type >= 2) {
672 $html = TRUE;
673 switch ($type) {
674 case 2:
675 $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon'))));
676 break;
677 case 3:
678 $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon', 'print-icon-margin')))) . $text;
679 break;
680 }
681 }
682 else {
683 $html = FALSE;
684 }
685
686 return array('text' => $text,
687 'html' => $html,
688 );
689 }
690
691 /**
692 * Format the Printer-friendly link
693 *
694 * @return
695 * array of formatted attributes
696 * @ingroup themeable
697 */
698 function theme_print_format_link() {
699 $print_html_link_class = variable_get('print_html_link_class', PRINT_HTML_LINK_CLASS_DEFAULT);
700 $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
701 $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
702 $print_html_link_text = filter_xss(variable_get('print_html_link_text', t('Printer-friendly version')));
703
704 $img = drupal_get_path('module', 'print') . '/icons/print_icon.gif';
705 $title = t('Display a printer-friendly version of this page.');
706 $class = strip_tags($print_html_link_class);
707 $new_window = $print_html_new_window;
708 $format = _print_format_link_aux($print_html_show_link, $print_html_link_text, $img);
709
710 return array('text' => $format['text'],
711 'html' => $format['html'],
712 'attributes' => print_fill_attributes($title, $class, $new_window),
713 );
714 }
715
716 /**
717 * Auxiliary function to display a formatted Printer-friendly link
718 *
719 * Function made available so that developers may call this function from
720 * their defined pages/blocks.
721 *
722 * @param $path
723 * path of the original page (optional). If not specified, the current URL
724 * is used
725 * @param $node
726 * an optional node object, to be used in defining the path, if used, the
727 * path argument is irrelevant
728 * @return
729 * string with the HTML link to the printer-friendly page
730 */
731 function print_insert_link($path = NULL, $node = NULL) {
732 if ($node !== NULL) {
733 $nid = $node->nid;
734 $path = 'node/' . $nid;
735 $allowed_type = print_link_allowed(array('node' => $node));
736 }
737 else {
738 if ($path === NULL) {
739 $nid = preg_replace('!^node/!', '', $_GET['q']);
740 $path = $_GET['q'];
741 }
742 else {
743 $nid = NULL;
744 }
745 $allowed_type = print_link_allowed(array('path' => $path));
746 }
747
748 if ($allowed_type) {
749 if ($nid !== NULL) {
750 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
751 $path = 'book/export/html/' . $nid;
752 }
753 else {
754 if (variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT)) {
755 $path = drupal_get_path_alias($path);
756 }
757 else {
758 $path = $nid;
759 }
760 }
761 $path = PRINT_PATH . '/' . $path;
762 $query = print_query_string_encode($_GET, array('q'));
763 if (empty($query)) {
764 $query = NULL;
765 }
766 }
767 else {
768 $query = NULL;
769 }
770 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
771 $format = theme('print_format_link');
772 return '<span class="print_html">' . l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) . '</span>';
773 }
774 else {
775 return FALSE;
776 }
777 }
778
779 /**
780 * Determine if the provided page is enabled according to the visibility settings
781 *
782 * @param $visibility
783 * current visibility settings:
784 * 0 for show on every page except the listed pages
785 * 1 for show on only the listed pages
786 * @param $pages
787 * list of pages
788 * @return
789 * TRUE if it is enabled, FALSE otherwise
790 */
791 function _print_page_match($visibility, $path, $pages) {
792 if ($pages) {
793 if ($visibility == 2) {
794 if (module_exists('php')) {
795 return php_eval($pages);
796 }
797 else {
798 return FALSE;
799 }
800 }
801 $alias = drupal_get_path_alias($path);
802 $page_match = drupal_match_path($path, $pages);
803 if ($alias != $path) {
804 $page_match = $page_match || drupal_match_path($alias, $pages);
805 }
806
807 return !($visibility xor $page_match);
808 }
809 else {
810 return !$visibility;
811 }
812 }
813
814 /**
815 * Determine a the link to the PF version is allowed depending on all possible settings
816 *
817 * @param $args
818 * array containing the possible parameters:
819 * teaser, node, type, path
820 * @return
821 * FALSE if not allowed
822 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
823 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
824 */
825 function print_link_allowed($args) {
826 $view_mode = isset($args['view_mode']) ? $args['view_mode'] : '';
827 if ((($view_mode == 'teaser') && !variable_get('print_html_link_teaser', PRINT_HTML_LINK_TEASER_DEFAULT))
828 || !in_array($view_mode, array('full', 'teaser', '')) || !user_access('access print')) {
829 // If the teaser link is disabled or the user is not allowed
830 return FALSE;
831 }
832 if (!empty($args['path'])) {
833 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
834 if (ctype_digit($nid)) {
835 $args['node'] = node_load($nid);
836 }
837 }
838 if (!empty($args['node'])) {
839 static $node_type = '';
840
841 $node = $args['node'];
842 if (isset($node->type)) {
843 $node_type = $node->type;
844 }
845 // Node
846 $print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT);
847 $print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT);
848
849 if (!_print_page_match($print_html_node_link_visibility, "node/" . $node->nid, $print_html_node_link_pages)) {
850 // Page not in visibility list
851 return FALSE;
852 }
853 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
854 // Link is for a comment, return the configured setting
855 // Cache this statically to avoid duplicate queries for every comment.
856 static $res = array();
857 if (!isset($res[$node->nid])) {
858 $res[$node->nid] = db_query("SELECT comments FROM {print_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
859 ->fetch();
860 }
861 $print_display_comment = $res ? intval($res[$node->nid]->comments) : variable_get('print_display_comment_' . $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
862 if ($print_display_comment) {
863 return PRINT_ALLOW_NORMAL_LINK;
864 }
865 }
866 else {
867 // Node link
868 if (!$node->print_display) {
869 // Link for this node is disabled
870 return FALSE;
871 }
872 elseif (isset($node->book)) {
873 // Node is a book;
874 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
875 switch ($print_html_book_link) {
876 case 1:
877 if (user_access('access printer-friendly version')) {
878 return PRINT_ALLOW_BOOK_LINK;
879 }
880 break;
881 case 2:
882 return PRINT_ALLOW_NORMAL_LINK;
883 }
884 }
885 else {
886 return PRINT_ALLOW_NORMAL_LINK;
887 }
888 }
889 }
890 else {
891 // 'System' page
892 $print_html_sys_link_visibility = variable_get('print_html_sys_link_visibility', PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT);
893 $print_html_sys_link_pages = variable_get('print_html_sys_link_pages', PRINT_HTML_SYS_LINK_PAGES_DEFAULT);
894
895 return _print_page_match($print_html_sys_link_visibility, $_GET['q'], $print_html_sys_link_pages);
896 }
897 return FALSE;
898 }
899
900 /**
901 * Parse an array into a valid urlencoded query string.
902 * Modified from drupal_query_string_encode to prevent re-encoding of
903 * encoded original.
904 *
905 * @param $query
906 * The array to be processed e.g. $_GET
907 * @param $exclude
908 * The array filled with keys to be excluded.
909 * @return
910 * urlencoded string which can be appended to/as the URL query string
911 */
912 function print_query_string_encode($query, $exclude = array(), $parent = '') {
913 $params = array();
914 foreach ($query as $key => $value) {
915 if ($parent) {
916 $key = $parent . '[' . $key . ']';
917 }
918
919 if (in_array($key, $exclude)) {
920 continue;
921 }
922
923 if (is_array($value)) {
924 $params[] = print_query_string_encode($value, $exclude, $key);
925 }
926 else {
927 $params[] = $key . '=' . rawurlencode($value);
928 }
929 }
930
931 return $params;
932 }
933
934 /**
935 * Implements hook_contextual_links_view_alter().
936 */
937 function print_contextual_links_view_alter(&$element, $items) {
938 // Hide contextual links, always
939 $element['#attributes']['class'][] = 'element-hidden';
940 }