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