6 * Displays Printer-friendly versions of Drupal pages.
8 * This is the core module of the PF package, with the Drupal hooks
9 * and other administrative functions.
12 define('PRINT_PATH', 'print');
14 define('PRINT_LOGO_OPTIONS_DEFAULT', 1);
15 define('PRINT_LOGO_URL_DEFAULT', '');
16 define('PRINT_FOOTER_OPTIONS_DEFAULT', 1);
17 define('PRINT_FOOTER_USER_DEFAULT', '');
18 define('PRINT_CSS_DEFAULT', '');
19 define('PRINT_URLS_DEFAULT', 1);
20 define('PRINT_COMMENTS_DEFAULT', 0);
21 define('PRINT_NEWWINDOW_DEFAULT', 1);
23 define('PRINT_HTML_LINK_POS_DEFAULT', 'link');
24 define('PRINT_HTML_SHOW_LINK_DEFAULT', 1);
25 define('PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT', 0);
26 define('PRINT_HTML_NODE_LINK_PAGES_DEFAULT', '');
27 define('PRINT_HTML_LINK_CLASS_DEFAULT', 'print-page');
28 define('PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT', 1);
29 define('PRINT_HTML_SYS_LINK_PAGES_DEFAULT', '');
30 define('PRINT_HTML_LINK_USE_ALIAS_DEFAULT', 0);
31 define('PRINT_HTML_BOOK_LINK_DEFAULT', 1);
32 define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
33 define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);
35 define('PRINT_SOURCEURL_ENABLED_DEFAULT', 1);
36 define('PRINT_SOURCEURL_DATE_DEFAULT', 0);
37 define('PRINT_SOURCEURL_FORCENODE_DEFAULT', 0);
39 define('PRINT_ROBOTS_NOINDEX_DEFAULT', 1);
40 define('PRINT_ROBOTS_NOFOLLOW_DEFAULT', 1);
41 define('PRINT_ROBOTS_NOARCHIVE_DEFAULT', 0);
43 define('PRINT_TYPE_SHOW_LINK_DEFAULT', 1);
44 define('PRINT_TYPE_COMMENT_LINK_DEFAULT', 0);
46 define('PRINT_ALLOW_NORMAL_LINK', 1);
47 define('PRINT_ALLOW_BOOK_LINK', 2);
50 * Implementation of hook_perm().
52 function print_perm() {
54 'access print' => array(
55 'title' => t('Access the printer-friendly page'),
56 'description' => t('View the printer-friendly pages and the links to them in the original pages.'),
58 'administer print' => array(
59 'title' => t('Administer the module'),
60 'description' => t('Perform maintenance tasks for the print module.'),
66 * Implementation of hook_theme().
68 function print_theme() {
70 'print_format_link' => array(
71 'arguments' => array(),
73 'print_text' => array(
74 'arguments' => array(),
80 * Implementation of hook_menu().
82 function print_menu() {
85 $items[PRINT_PATH
] = array(
86 'title' => 'Printer-friendly',
87 'page callback' => 'print_controller_html',
88 'access arguments' => array('access print'),
89 'type' => MENU_CALLBACK
,
91 $items['admin/settings/print'] = array(
92 'title' => 'Printer-friendly pages',
93 'description' => 'Adds a printer-friendly version link to content and administrative pages.',
94 'page callback' => 'drupal_get_form',
95 'page arguments' => array('print_html_settings'),
96 'access arguments' => array('administer print'),
98 $items['admin/settings/print/html'] = array(
99 'title' => 'Web page',
101 'type' => MENU_DEFAULT_LOCAL_TASK
,
103 $items['admin/settings/print/common'] = array(
104 'title' => 'Settings',
105 'page callback' => 'drupal_get_form',
106 'page arguments' => array('print_main_settings'),
107 'access arguments' => array('administer print'),
109 'type' => MENU_LOCAL_TASK
,
116 * Implementation of hook_block().
118 function print_block($op = 'list', $delta = 0, $edit = array()) {
121 $block[$delta]['info'] = t('Printer, e-mail and PDF versions');
129 $nid = preg_replace('!^node/!', '', $_GET['q']);
130 if (is_numeric($nid)) {
131 $node = node_load(array('nid' => $nid));
136 $funcs = get_defined_functions();
137 $block['content'] = '';
138 foreach ($funcs['user'] as
$func) {
139 if (preg_match('!^print.*?_insert_link$!', $func)) {
140 $link = $func(NULL
, $node);
142 $block['content'] .
= $link .
'<br />';
152 * Implementation of hook_link().
154 function print_link($type, $node = NULL
, $teaser = FALSE
) {
155 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT
=> PRINT_HTML_LINK_POS_DEFAULT
));
156 $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT
);
157 $allowed_type = print_link_allowed(array('type' => $type, 'node' => $node, 'teaser' => $teaser));
158 if (($allowed_type === PRINT_ALLOW_NORMAL_LINK
) && !empty($print_html_link_pos['link'])) {
159 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
161 $format = theme('print_format_link');
164 if ($type == 'comment') {
165 $query_arr['comment'] = $node->cid
;
167 $query = print_query_string_encode($query_arr, array('q'));
168 if (empty($query)) $query = NULL
;
170 if ($print_html_link_use_alias) {
171 $path = drupal_get_path_alias('node/'.
$node->nid
);
177 $links['print_html'] = array(
178 'href' => PRINT_PATH .
'/'.
$path,
179 'title' => $format['text'],
180 'attributes' => $format['attributes'],
181 'html' => $format['html'],
193 * Implementation of hook_link_alter().
195 function print_link_alter(&$links, $node) {
196 foreach ($links as
$module => $link) {
197 if (strpos($module, 'book_printer') !== FALSE
) {
198 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT
);
200 if ($print_html_book_link) {
201 $format = theme('print_format_link');
202 $format['attributes']['title'] = $link['attributes']['title'];
204 $links[$module]['href'] = PRINT_PATH .
'/'.
$link['href'];
205 $links[$module]['attributes'] = $format['attributes'];
212 * Implementation of hook_help().
214 function print_help($path, $arg) {
216 case
'admin/help#print':
217 // Return a line-break version of the module README
218 return filter_filter('process', 1, NULL
, file_get_contents(drupal_get_path('module', 'print') .
'/README.txt') );
221 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT
=> PRINT_HTML_LINK_POS_DEFAULT
));
222 if ((preg_match('!^node/!i', $path) == 0) &&
223 !(empty($print_html_link_pos['link']) && empty($print_html_link_pos['corner']))) {
224 static
$output = FALSE
;
226 if ($output === FALSE
) {
229 $link = print_insert_link();
231 return "<span class='print-syslink'>$link</span>";
238 * Implementation of hook_nodeapi_view().
240 function print_nodeapi_view(&$node, $teaser, $page) {
241 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT
=> PRINT_HTML_LINK_POS_DEFAULT
));
242 if (($teaser === FALSE
) && !empty($print_html_link_pos['corner']) &&
243 (preg_match('!^print!i', $_GET['q']) == 0)) {
244 $link = print_insert_link(NULL
, $node);
246 $node->content
['print_link'] = array(
247 '#markup' => "<span class='print-link'>$link</span>",
255 * Implementation of hook_form_alter().
257 function print_form_alter(&$form, $form_state, $form_id) {
258 // Add the node-type settings option to activate the printer-friendly version link
259 if ($form_id == 'node_type_form') {
260 $form['print'] = array(
261 '#type' => 'fieldset',
262 '#title' => t('Printer, e-mail and PDF versions'),
263 '#collapsible' => TRUE
,
264 '#collapsed' => TRUE
,
267 $form['print']['print_display'] = array(
268 '#type' => 'checkbox',
269 '#title' => t('Show printer-friendly version link'),
270 '#default_value' => variable_get('print_display_'.
$form['#node_type']->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
),
271 '#description' => t('Displays the link to a printer-friendly version of the content. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
273 $form['print']['print_display_comment'] = array(
274 '#type' => 'checkbox',
275 '#title' => t('Show printer-friendly version link in individual comments'),
276 '#default_value' => variable_get('print_display_comment_'.
$form['#node_type']->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
),
277 '#description' => t('Displays the link to a printer-friendly version of the comment. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
283 * Auxiliary function to fill the Printer-friendly link attributes
286 * text to displayed by the link when hovering over it with the mouse
288 * class attribute to be used in the link
290 * if TRUE opens the target page in a new window
292 * array of formatted attributes
294 function print_fill_attributes($title = '', $class = '', $new_window = FALSE
) {
295 $print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT
);
296 $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT
);
298 $attributes = array();
299 $attributes['title'] = $title;
300 if (!empty($class)) {
301 $attributes['class'] = $class;
305 switch ($print_newwindow) {
307 $attributes['target'] = '_blank';
310 $attributes['onclick'] = 'window.open(this.href); return false';
314 if (!empty($print_robots_noindex)) {
315 $attributes['rel'] = 'nofollow';
321 * Auxiliary function to set the link text and html flag
324 * type of link: 0 or 1 for a text-only link, 2 for icon-only and 3 for
327 * text to be displayed on the link to the printer-friendly page
329 * path to the icon file
331 * array with the link text and html flag
333 function _print_format_link_aux($type = 0, $text = '', $img = '') {
338 $text = theme('image', $img, $text, $text, array('class' => 'print-icon'));
341 $text = theme('image', $img, $text, $text, array('class' => 'print-icon print-icon-margin')) .
$text;
349 return array('text' => $text,
355 * Format the Printer-friendly link
358 * array of formatted attributes
361 function theme_print_format_link() {
362 $print_html_link_class = variable_get('print_html_link_class', PRINT_HTML_LINK_CLASS_DEFAULT
);
363 $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT
);
364 $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT
);
366 $text = t('Printer-friendly version');
367 $img = drupal_get_path('module', 'print') .
'/icons/print_icon.gif';
368 $title = t('Display a printer-friendly version of this page.');
369 $class = strip_tags($print_html_link_class);
370 $new_window = $print_html_new_window;
371 $format = _print_format_link_aux($print_html_show_link, $text, $img);
373 return array('text' => $format['text'],
374 'html' => $format['html'],
375 'attributes' => print_fill_attributes($title, $class, $new_window),
380 * Define the strings displayed by the module in the printer-friendly template
383 * array with the used text strings
386 function theme_print_text() {
387 return array('retrieved' => '',
397 * Auxiliary function to display a formatted Printer-friendly link
399 * Function made available so that developers may call this function from
400 * their defined pages/blocks.
403 * path of the original page (optional). If not specified, the current URL
406 * an optional node object, to be used in defining the path, if used, the
407 * path argument is irrelevant
409 * string with the HTML link to the printer-friendly page
411 function print_insert_link($path = NULL
, $node = NULL
) {
412 if ($node !== NULL
) {
414 $path = 'node/'.
$nid;
415 $allowed_type = print_link_allowed(array('node' => $node));
418 if ($path === NULL
) {
419 $nid = preg_replace('!^node/!', '', $_GET['q']);
425 $allowed_type = print_link_allowed(array('path' => $path));
430 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
431 $path = 'book/export/html/'.
$nid;
434 if (variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT
)) {
435 $path = drupal_get_path_alias($path);
441 $path = PRINT_PATH .
'/'.
$path;
442 $query = print_query_string_encode($_GET, array('q'));
450 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
451 $format = theme('print_format_link');
452 return '<span class="print_html">'.
l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE
, 'html' => $format['html'])) .
'</span>';
460 * Determine if the current page is enabled according to the visibility settings
463 * current visibility settings:
464 * 0 for show on every page except the listed pages
465 * 1 for show on only the listed pages
469 * TRUE if it is enabled, FALSE otherwise
471 function _print_page_match($visibility, $pages) {
473 $path = drupal_get_path_alias($_GET['q']);
474 $page_match = drupal_match_path($path, $pages);
475 if ($path != $_GET['q']) {
476 $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
479 return !($visibility xor
$page_match);
487 * Determine a the link to the PF version is allowed depending on all possible settings
490 * array containing the possible parameters:
491 * teaser, node, type, path
493 * FALSE if not allowed
494 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
495 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
497 function print_link_allowed($args) {
498 if (!empty($args['teaser']) || !user_access('access print')) {
499 // If showing only the teaser or the user is not allowed or link is disabled
502 if (!empty($args['path'])) {
503 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
504 if (is_numeric($nid)) {
505 $args['node'] = node_load(array('nid' => $nid));
508 if (!empty($args['node'])) {
509 static
$node_type = FALSE
;
511 $node = $args['node'];
512 if ($node_type === FALSE
) {
513 if (isset($node->type
)) {
514 $node_type = $node->type
;
521 $print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT
);
522 $print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT
);
524 if (!empty($node->printing
) ||
525 !_print_page_match($print_html_node_link_visibility, $print_html_node_link_pages)) {
526 // Page not in visibility list or we are working!
529 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
530 // Link is for a comment, return the configured setting
531 return variable_get('print_display_comment_'.
$node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
535 if (isset($node_type) &&
536 !variable_get('print_display_'.
$node_type, PRINT_TYPE_SHOW_LINK_DEFAULT
)) {
537 // Link for this node type is disabled
540 elseif (isset($node->book
)) {
542 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT
);
543 if (!$print_html_book_link || !user_access('access printer-friendly version')) {
544 // Book link is disabled
548 return PRINT_ALLOW_BOOK_LINK
;
552 return PRINT_ALLOW_NORMAL_LINK
;
558 $print_html_sys_link_visibility = variable_get('print_html_sys_link_visibility', PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT
);
559 $print_html_sys_link_pages = variable_get('print_html_sys_link_pages', PRINT_HTML_SYS_LINK_PAGES_DEFAULT
);
561 return _print_page_match($print_html_sys_link_visibility, $print_html_sys_link_pages);
566 * Parse an array into a valid urlencoded query string.
567 * Modified from drupal_query_string_encode to prevent re-encoding of
571 * The array to be processed e.g. $_GET
573 * The array filled with keys to be excluded.
575 * urlencoded string which can be appended to/as the URL query string
577 function print_query_string_encode($query, $exclude = array(), $parent = '') {
579 foreach ($query as
$key => $value) {
581 $key = $parent .
'['.
$key .
']';
584 if (in_array($key, $exclude)) {
588 if (is_array($value)) {
589 $params[] = print_query_string_encode($value, $exclude, $key);
592 $params[] = $key .
'='.
rawurlencode($value);
596 return implode('&', $params);