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