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