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