Use type-default settings if a node is being created/updated leaving them unspecified
[project/print.git] / print.module
CommitLineData
1aaa7127 1<?php
623692c4 2// $Id$
1aaa7127 3
c4122ca5 4/**
64ad3057
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
623692c4
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.
64ad3057
JV
36 *
37 * @ingroup print
c4122ca5
DN
38 */
39
9383096e
JV
40define('PRINT_PATH', 'print');
41
6ea54f45
JV
42define('PRINT_HTML_FORMAT', 'html');
43
9383096e
JV
44define('PRINT_LOGO_OPTIONS_DEFAULT', 1);
45define('PRINT_LOGO_URL_DEFAULT', '');
905534de
JV
46define('PRINT_FOOTER_OPTIONS_DEFAULT', 1);
47define('PRINT_FOOTER_USER_DEFAULT', '');
9383096e
JV
48define('PRINT_CSS_DEFAULT', '');
49define('PRINT_URLS_DEFAULT', 1);
b54ad9f8 50define('PRINT_URLS_ANCHORS_DEFAULT', 0);
9383096e
JV
51define('PRINT_COMMENTS_DEFAULT', 0);
52define('PRINT_NEWWINDOW_DEFAULT', 1);
53
54define('PRINT_HTML_LINK_POS_DEFAULT', 'link');
7b050ea9 55define('PRINT_HTML_LINK_TEASER_DEFAULT', 0);
9383096e
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);
905534de 61define('PRINT_HTML_SYS_LINK_PAGES_DEFAULT', '');
d2994558 62define('PRINT_HTML_LINK_USE_ALIAS_DEFAULT', 0);
9383096e
JV
63define('PRINT_HTML_BOOK_LINK_DEFAULT', 1);
64define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
65define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);
413d30ed 66define('PRINT_HTML_WINDOWCLOSE_DEFAULT', 1);
9383096e
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);
6ea54f45 78define('PRINT_TYPE_URLLIST_DEFAULT', 1);
9383096e
JV
79
80define('PRINT_ALLOW_NORMAL_LINK', 1);
81define('PRINT_ALLOW_BOOK_LINK', 2);
e09bb0e2 82define('PRINT_TYPE_FIELDS_WEIGHT', 30);
35c110c4 83
ddb9ae21
JV
84require_once(drupal_get_path('module', 'print') .'/print.admin.inc');
85require_once(drupal_get_path('module', 'print') .'/print.pages.inc');
86
87/**
7e166782 88 * Implementation of hook_perm().
449d2abc
JV
89 */
90function print_perm() {
d39d0535 91 return array('access print', 'administer print', 'node-specific print configuration', 'use PHP for link visibility');
449d2abc
JV
92}
93
94/**
7e166782 95 * Implementation of hook_menu().
1aaa7127
MW
96 */
97function print_menu($may_cache) {
98 $items = array();
99
4bbc29ad
CP
100 if ($may_cache) {
101 $items[] = array(
35c110c4 102 'path' => PRINT_PATH,
449d2abc 103 'title' => t('Printer-friendly'),
ddb9ae21 104 'callback' => 'print_controller_html',
449d2abc 105 'access' => user_access('access print'),
7e166782 106 'type' => MENU_CALLBACK,
4bbc29ad 107 );
5808585e
CP
108 $items[] = array(
109 'path' => 'admin/settings/print',
2e711b51 110 'title' => t('Printer, e-mail and PDF versions'),
7e166782 111 'description' => t('Adds a printer-friendly version link to content and administrative pages.'),
5808585e 112 'callback' => 'drupal_get_form',
9383096e
JV
113 'callback arguments' => array('print_html_settings'),
114 'access' => user_access('administer print'),
115 );
116 $items[] = array(
117 'path' => 'admin/settings/print/html',
118 'title' => t('Web page'),
119 'weight' => 1,
120 'type' => MENU_DEFAULT_LOCAL_TASK,
121 );
122 $items[] = array(
2e711b51
JV
123 'path' => 'admin/settings/print/html/options',
124 'title' => t('Options'),
125 'weight' => 1,
126 'type' => MENU_DEFAULT_LOCAL_TASK,
127 );
128 $items[] = array(
129 'path' => 'admin/settings/print/html/strings',
130 'title' => t('Text strings'),
131 'callback' => 'drupal_get_form',
132 'callback arguments' => array('print_html_strings_settings'),
f6b25c27 133 'access' => user_access('administer print'),
2e711b51
JV
134 'weight' => 2,
135 'type' => MENU_LOCAL_TASK,
136 );
137 $items[] = array(
9383096e
JV
138 'path' => 'admin/settings/print/common',
139 'title' => t('Settings'),
140 'callback' => 'drupal_get_form',
449d2abc
JV
141 'callback arguments' => array('print_main_settings'),
142 'access' => user_access('administer print'),
9383096e
JV
143 'weight' => 10,
144 'type' => MENU_LOCAL_TASK,
4bbc29ad 145 );
2e711b51
JV
146 $items[] = array(
147 'path' => 'admin/settings/print/common/options',
148 'title' => t('Options'),
149 'weight' => 1,
150 'type' => MENU_DEFAULT_LOCAL_TASK,
151 );
152 $items[] = array(
153 'path' => 'admin/settings/print/common/strings',
154 'title' => t('Text strings'),
155 'callback' => 'drupal_get_form',
156 'callback arguments' => array('print_main_strings_settings'),
f6b25c27 157 'access' => user_access('administer print'),
2e711b51
JV
158 'weight' => 2,
159 'type' => MENU_LOCAL_TASK,
160 );
4bbc29ad 161 }
1aaa7127
MW
162
163 return $items;
164}
165
1aaa7127 166/**
d8ea68f9
JV
167 * Implementation of hook_block().
168 */
169function print_block($op = 'list', $delta = 0, $edit = array()) {
170 switch ($op) {
171 case 'list':
6ea54f45
JV
172 $block[0]['info'] = t('Printer, e-mail and PDF versions');
173 $block[1]['info'] = t('Most printed');
d8ea68f9
JV
174 return $block;
175 break;
176 case 'configure':
177 return '';
178 case 'save':
179 return;
180 case 'view':
6ea54f45
JV
181 switch ($delta) {
182 case 0:
d8ea68f9
JV
183 $nid = preg_replace('!^node/!', '', $_GET['q']);
184 if (is_numeric($nid)) {
45074750 185 $node = node_load($nid);
d8ea68f9
JV
186 }
187 else {
188 $node = NULL;
189 }
190 $funcs = get_defined_functions();
191 $block['content'] = '';
192 foreach ($funcs['user'] as $func) {
193 if (preg_match('!^print.*?_insert_link$!', $func)) {
194 $link = $func(NULL, $node);
195 if (!empty($link)) {
1c1c6d23 196 $block['content'] .= $link;
d8ea68f9
JV
197 }
198 }
199 }
6ea54f45
JV
200 break;
201 case 1:
568392c9 202 $block['subject'] = t('Most printed');
6ea54f45
JV
203 $result = db_query_range("SELECT path FROM {print_page_counter} ORDER BY totalcount DESC", 0, 3);
204 if (db_affected_rows()) {
205 $block['content'] = '<div class="item-list"><ul>';
206 while ($obj = db_fetch_object($result)) {
207 $block['content'] .= '<li>'. l(_print_get_title($obj->path), $obj->path) .'</li>';
208 }
209 $block['content'] .= '</ul></div>';
6ea54f45
JV
210 }
211 break;
212 }
d8ea68f9
JV
213 return $block;
214 break;
215 }
216}
217
218/**
1aaa7127
MW
219 * Implementation of hook_link().
220 */
324fcda3 221function print_link($type, $node = NULL, $teaser = FALSE) {
0a85b443
JV
222 // Prevent links from being shown for event nodes when in calendar view
223 if (preg_match('!^event_node!', $type)) {
224 return;
225 }
226
9383096e 227 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
d2994558 228 $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT);
9383096e 229 $allowed_type = print_link_allowed(array('type' => $type, 'node' => $node, 'teaser' => $teaser));
8b2d4a33 230 if (($allowed_type === PRINT_ALLOW_NORMAL_LINK) && !isset($node->parent) && !empty($print_html_link_pos['link'])) {
0a9dfc3a 231 drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css');
449d2abc 232 $links = array();
f260a4dc 233 $format = theme('print_format_link');
449d2abc 234
7e166782 235 $query_arr = $_GET;
f5544516 236 if ($type == 'comment') {
7e166782 237 $query_arr['comment'] = $node->cid;
f5544516 238 }
0f71eefe 239 $query = print_query_string_encode($query_arr, array('q'));
38943962 240 if (empty($query)) $query = NULL;
f5544516 241
d2994558
JV
242 if ($print_html_link_use_alias) {
243 $path = drupal_get_path_alias('node/'. $node->nid);
244 }
245 else {
246 $path = $node->nid;
247 }
248
cd7c92cc
JV
249 $links['print_html'] = array(
250 'href' => PRINT_PATH .'/'. $path,
251 'title' => $format['text'],
252 'attributes' => $format['attributes'],
253 'html' => $format['html'],
254 'query' => $query,
255 );
449d2abc
JV
256
257 return $links;
d72878de 258 }
da1e18e0 259 else {
6100cd9f 260 return;
449d2abc
JV
261 }
262}
263
6100cd9f 264/**
6c53882a
JV
265 * Implementation of hook_link_alter().
266 */
267function print_link_alter(&$node, &$links) {
7e166782
JV
268 foreach ($links as $module => $link) {
269 if (strpos($module, 'book_printer') !== FALSE) {
9383096e 270 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
6c53882a 271
9383096e 272 if ($print_html_book_link) {
cb33c7e4
JV
273 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
274
275 if (!empty($print_html_link_pos['link'])) {
f260a4dc 276 $format = theme('print_format_link');
9d395e7c 277
8b2d4a33
JV
278 switch ($print_html_book_link) {
279 case 1:
280 $path = $link['href'];
281 break;
282 case 2:
283 $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT);
abd96759 284 $path = ($print_html_link_use_alias && isset($node->path)) ? $node->path : $node->nid;
8b2d4a33
JV
285 break;
286 }
287
288 $links[$module] = array(
289 'href' => PRINT_PATH .'/'. $path,
290 'title' => $format['text'],
291 'attributes' => $format['attributes'],
292 'html' => $format['html'],
293 );
cb33c7e4
JV
294 }
295 else {
296 unset($links[$module]);
297 }
af8779e8
JV
298 }
299 }
6c53882a
JV
300 }
301}
302
303/**
7e166782 304 * Implementation of hook_help().
6100cd9f 305 */
51d2f757 306function print_help($path) {
623692c4
JV
307 switch ($path) {
308 case 'admin/help#print':
309 // Return a line-break version of the module README
9383096e 310 return filter_filter('process', 2, NULL, file_get_contents(drupal_get_path('module', 'print') .'/README.txt') );
623692c4
JV
311 }
312
9383096e 313 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
0e41addb 314 if ((preg_match('!^node/\d+$!', $path) == 0) && !(empty($print_html_link_pos['link']) && empty($print_html_link_pos['corner']))) {
6c7157ab
JV
315 static $output = FALSE;
316
317 if ($output === FALSE) {
318 $output = TRUE;
95076d9f 319
d8ea68f9
JV
320 $link = print_insert_link();
321 if ($link) {
322 return "<span class='print-syslink'>$link</span>";
323 }
0ebd8e40 324 }
0ebd8e40
JV
325 }
326}
327
6100cd9f 328/**
9383096e
JV
329 * Implementation of hook_nodeapi().
330 */
331function print_nodeapi(&$node, $op = 'view', $teaser, $page) {
332 switch ($op) {
333 case 'view':
6ea54f45 334 // Insert content corner links
ac6670fd 335 if ((!$teaser) && !isset($node->printing)) {
e09bb0e2 336 $node->content['print_links'] = array(
1f76df5a
JV
337 '#prefix' => '<span class="print-link">',
338 '#value' => '',
339 '#suffix' => '</span>',
e09bb0e2
JV
340 '#weight' => -101,
341 );
9383096e 342 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
0e2f03b1 343 if (!empty($print_html_link_pos['corner'])) {
1f76df5a 344 $node->content['print_links']['#value'] .= print_insert_link(NULL, $node);
9383096e 345 }
0e2f03b1 346 }
6a20a915 347 break;
149e2ffa 348 case 'load':
6a20a915 349 _print_set_node_fields($node);
6ea54f45 350 break;
91a30d8f 351 case 'insert':
6ea54f45 352 case 'update':
6b632dfd 353 if (user_access('administer print') || user_access('node-specific print configuration')) {
3acee930
JV
354 if ($node->print_display === NULL) $node->print_display = variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
355 if ($node->print_display_comment === NULL) $node->print_display_comment = variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
356 if ($node->print_display_urllist === NULL) $node->print_display_urllist = variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
357
358 _print_node_conf_modify($node->nid, $node->print_display, $node->print_display_comment, $node->print_display_urllist);
6b632dfd 359 }
6ea54f45
JV
360 break;
361 case 'delete':
362 db_query("DELETE FROM {print_node_conf} WHERE nid = %d", $node->nid);
abd96759 363 db_query("DELETE FROM {print_page_counter} WHERE path = 'node/%d'", $node->nid);
6ea54f45 364 break;
9383096e
JV
365 }
366}
367
368/**
7e166782 369 * Implementation of hook_form_alter().
6100cd9f 370 */
449d2abc
JV
371function print_form_alter($form_id, &$form) {
372 // Add the node-type settings option to activate the printer-friendly version link
d39d0535
JV
373 if ((user_access('administer print') || user_access('node-specific print configuration')) && (($form_id == 'node_type_form') ||
374 (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id))) {
9383096e
JV
375 $form['print'] = array(
376 '#type' => 'fieldset',
40cbc88e 377 '#title' => t('Printer, e-mail and PDF versions'),
9383096e
JV
378 '#collapsible' => TRUE,
379 '#collapsed' => TRUE,
b45dc6a5 380 '#weight' => PRINT_TYPE_FIELDS_WEIGHT,
9383096e
JV
381 );
382
6ea54f45
JV
383 $form['print']['label'] = array(
384 '#type' => 'markup',
385 '#value' => '<p><strong>'. t('Printer-friendly version') .'</strong></p>',
386 );
387
9383096e 388 $form['print']['print_display'] = array(
449d2abc 389 '#type' => 'checkbox',
6ea54f45 390 '#title' => t('Show link'),
449d2abc 391 );
9383096e 392 $form['print']['print_display_comment'] = array(
449d2abc 393 '#type' => 'checkbox',
6ea54f45 394 '#title' => t('Show link in individual comments'),
449d2abc 395 );
6ea54f45
JV
396 $form['print']['print_display_urllist'] = array(
397 '#type' => 'checkbox',
398 '#title' => t('Show Printer-friendly URLs list'),
399 );
400
401 if ($form_id == 'node_type_form') {
402 $form['print']['print_display']['#default_value'] = variable_get('print_display_'. $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
403 $form['print']['print_display_comment']['#default_value'] = variable_get('print_display_comment_'. $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
404 $form['print']['print_display_urllist']['#default_value'] = variable_get('print_display_urllist_'. $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
405 }
406 else {
2e711b51 407 $node = $form['#node'];
35c25611
JV
408 $form['print']['print_display']['#default_value'] = isset($node->print_display) ? $node->print_display : variable_get('print_display_'. $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
409 $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);
410 $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);
6ea54f45
JV
411 }
412 }
413}
414
415/**
6a20a915
JV
416 * Auxiliary function to assign the per-node settings to the node object fields
417 *
418 * @param $node
419 * node to be modified
420 */
421function _print_set_node_fields(&$node) {
30502d83
JV
422 if (isset($node->nid)) {
423 $res = db_fetch_object(db_query("SELECT link, comments, url_list FROM {print_node_conf} WHERE nid = %d", $node->nid));
424 }
425 else {
426 $res = FALSE;
427 }
35c25611
JV
428 $node->print_display = $res ? intval($res->link) : variable_get('print_display_'. $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
429 $node->print_display_comment = $res ? intval($res->comments) : variable_get('print_display_comment_'. $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
430 $node->print_display_urllist = $res ? intval($res->url_list) : variable_get('print_display_urllist_'. $node->type, PRINT_TYPE_URLLIST_DEFAULT);
6a20a915
JV
431}
432
433/**
6ea54f45
JV
434 * Auxiliary function to discover a given page's title
435 *
436 * @param $path
437 * path of the page being identified
438 * @return
439 * string with the page's title
440 */
441function _print_get_title($path) {
442 $path = drupal_get_normal_path($path);
443 $nid = preg_replace('!^node/!', '', $path);
444 if (is_numeric($nid)) {
445 $res = db_fetch_object(db_query("SELECT title FROM {node} WHERE nid = %d", $nid));
446 return $res->title;
447 }
448 else {
449 $res = db_fetch_object(db_query("SELECT title FROM {menu} WHERE path = '%s'", $path));
36ed3738
JV
450 if ($res) {
451 return $res->link_title;
452 } else {
453 return NULL;
454 }
6ea54f45
JV
455 }
456}
457
458/**
459 * Update the print_node_conf table to reflect the given attributes
460 * If updating to the default values, delete the record.
461 *
462 * @param $nid
463 * value of the nid field (primary key)
464 * @param $link
465 * value of the link field (0 or 1)
466 * @param $comments
467 * value of the comments field (0 or 1)
468 * @param $url_list
469 * value of the url_list field (0 or 1)
470 */
471function _print_node_conf_modify($nid, $link, $comments, $url_list) {
6ea54f45
JV
472 db_query("UPDATE {print_node_conf} SET link = %d, comments = %d, url_list = %d WHERE nid = %d", $link, $comments, $url_list, $nid);
473 if (!db_affected_rows()) {
474 db_query("INSERT INTO {print_node_conf} (nid, link, comments, url_list) VALUES (%d, %d, %d, %d)", $nid, $link, $comments, $url_list);
475 }
449d2abc 476}
1aaa7127 477
9d395e7c
JV
478/**
479 * Auxiliary function to fill the Printer-friendly link attributes
480 *
9f9f3f56
JV
481 * @param $title
482 * text to displayed by the link when hovering over it with the mouse
483 * @param $class
484 * class attribute to be used in the link
485 * @param $new_window
486 * if TRUE opens the target page in a new window
623692c4
JV
487 * @return
488 * array of formatted attributes
9d395e7c 489 */
9f9f3f56 490function print_fill_attributes($title = '', $class = '', $new_window = FALSE) {
9383096e
JV
491 $print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT);
492 $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
f260a4dc 493
9f9f3f56
JV
494 $attributes = array();
495 $attributes['title'] = $title;
178d5650
JV
496 if (!empty($class)) {
497 $attributes['class'] = $class;
498 }
9f9f3f56
JV
499
500 if ($new_window) {
9383096e 501 switch ($print_newwindow) {
9f9f3f56
JV
502 case 0:
503 $attributes['target'] = '_blank';
504 break;
505 case 1:
506 $attributes['onclick'] = 'window.open(this.href); return false';
507 break;
508 }
6100cd9f 509 }
9383096e 510 if (!empty($print_robots_noindex)) {
070db63a
JV
511 $attributes['rel'] = 'nofollow';
512 }
9f9f3f56
JV
513 return $attributes;
514}
ddb9ae21 515
9f9f3f56
JV
516/**
517 * Auxiliary function to set the link text and html flag
518 *
519 * @param $type
520 * type of link: 0 or 1 for a text-only link, 2 for icon-only and 3 for
521 * both text and icon
522 * @param $text
523 * text to be displayed on the link to the printer-friendly page
524 * @param $img
525 * path to the icon file
526 * @return
527 * array with the link text and html flag
528 */
529function _print_format_link_aux($type = 0, $text = '', $img = '') {
ddb9ae21 530 if ($type >= 2) {
ddb9ae21
JV
531 $html = TRUE;
532 switch ($type) {
533 case 2:
d8ea68f9 534 $text = theme('image', $img, $text, $text, array('class' => 'print-icon'));
ddb9ae21
JV
535 break;
536 case 3:
d8ea68f9 537 $text = theme('image', $img, $text, $text, array('class' => 'print-icon print-icon-margin')) . $text;
ddb9ae21
JV
538 break;
539 }
540 }
541 else {
542 $html = FALSE;
543 }
95076d9f 544
ddb9ae21
JV
545 return array('text' => $text,
546 'html' => $html,
ddb9ae21 547 );
f260a4dc 548}
6100cd9f 549
623692c4
JV
550/**
551 * Format the Printer-friendly link
552 *
553 * @return
554 * array of formatted attributes
555 * @ingroup themeable
556 */
f260a4dc 557function theme_print_format_link() {
9383096e
JV
558 $print_html_link_class = variable_get('print_html_link_class', PRINT_HTML_LINK_CLASS_DEFAULT);
559 $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
560 $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
d1ec4d6f 561 $print_html_link_text = filter_xss(variable_get('print_html_link_text', t('Printer-friendly version')));
9383096e 562
f2015a87 563 $img = drupal_get_path('module', 'print') .'/icons/print_icon.gif';
ddb9ae21 564 $title = t('Display a printer-friendly version of this page.');
9383096e
JV
565 $class = strip_tags($print_html_link_class);
566 $new_window = $print_html_new_window;
568392c9 567 $format = _print_format_link_aux($print_html_show_link, $print_html_link_text, $img);
ddb9ae21
JV
568
569 return array('text' => $format['text'],
570 'html' => $format['html'],
9f9f3f56 571 'attributes' => print_fill_attributes($title, $class, $new_window),
7e166782 572 );
6100cd9f
JV
573}
574
623692c4 575/**
9d395e7c
JV
576 * Auxiliary function to display a formatted Printer-friendly link
577 *
623692c4
JV
578 * Function made available so that developers may call this function from
579 * their defined pages/blocks.
580 *
581 * @param $path
95076d9f 582 * path of the original page (optional). If not specified, the current URL
623692c4 583 * is used
d8ea68f9
JV
584 * @param $node
585 * an optional node object, to be used in defining the path, if used, the
586 * path argument is irrelevant
623692c4
JV
587 * @return
588 * string with the HTML link to the printer-friendly page
9d395e7c 589 */
d8ea68f9
JV
590function print_insert_link($path = NULL, $node = NULL) {
591 if ($node !== NULL) {
592 $nid = $node->nid;
593 $path = 'node/'. $nid;
594 $allowed_type = print_link_allowed(array('node' => $node));
595 }
596 else {
f260a4dc 597 if ($path === NULL) {
d8ea68f9
JV
598 $nid = preg_replace('!^node/!', '', $_GET['q']);
599 $path = $_GET['q'];
600 }
601 else {
602 $nid = NULL;
603 }
604 $allowed_type = print_link_allowed(array('path' => $path));
605 }
d2994558 606
d8ea68f9
JV
607 if ($allowed_type) {
608 if ($nid !== NULL) {
609 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
610 $path = 'book/export/html/'. $nid;
d2994558
JV
611 }
612 else {
d8ea68f9
JV
613 if (variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT)) {
614 $path = drupal_get_path_alias($path);
615 }
616 else {
617 $path = $nid;
618 }
d2994558
JV
619 }
620 $path = PRINT_PATH .'/'. $path;
0f71eefe 621 $query = print_query_string_encode($_GET, array('q'));
66361a60
JV
622 if (empty($query)) {
623 $query = NULL;
1d7d7174 624 }
f260a4dc 625 }
d2994558
JV
626 else {
627 $query = NULL;
628 }
d8ea68f9 629 drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css');
f260a4dc 630 $format = theme('print_format_link');
cd7c92cc 631 return '<span class="print_html">'. l($format['text'], $path, $format['attributes'], $query, NULL, TRUE, $format['html']) .'</span>';
df79a5a0 632 }
d8ea68f9
JV
633 else {
634 return FALSE;
635 }
6100cd9f 636}
6ee36e8a
JV
637
638/**
639 * Check if a path matches any pattern in a set of patterns.
640 *
641 * @param $path
642 * The path to match.
643 * @param $patterns
644 * String containing a set of patterns separated by \n, \r or \r\n.
645 *
646 * @return
647 * Boolean value: TRUE if the path matches a pattern, FALSE otherwise.
648 */
649function _print_match_path($path, $patterns) {
650 static $regexps;
651
652 if (!isset($regexps[$patterns])) {
653 $regexps[$patterns] = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($patterns, '/')) .')$/';
654 }
655 return preg_match($regexps[$patterns], $path);
656}
0f71eefe
JV
657
658/**
82993ef8 659 * Determine if the provided page is enabled according to the visibility settings
9383096e
JV
660 *
661 * @param $visibility
662 * current visibility settings:
663 * 0 for show on every page except the listed pages
664 * 1 for show on only the listed pages
665 * @param $pages
666 * list of pages
667 * @return
668 * TRUE if it is enabled, FALSE otherwise
669 */
82993ef8 670function _print_page_match($visibility, $path, $pages) {
9383096e 671 if ($pages) {
d90ea3ba
JV
672 if ($visibility == 2) {
673 return drupal_eval($pages);
674 }
82993ef8 675 $alias = drupal_get_path_alias($path);
9383096e 676 $page_match = _print_match_path($path, $pages);
82993ef8
JV
677 if ($alias != $path) {
678 $page_match = $page_match || _print_match_path($alias, $pages);
9383096e
JV
679 }
680
681 return !($visibility xor $page_match);
682 }
683 else {
6d82f395 684 return !$visibility;
9383096e
JV
685 }
686}
687
688/**
689 * Determine a the link to the PF version is allowed depending on all possible settings
690 *
691 * @param $args
692 * array containing the possible parameters:
693 * teaser, node, type, path
694 * @return
695 * FALSE if not allowed
696 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
697 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
698 */
699function print_link_allowed($args) {
7b050ea9
JV
700 if ((!empty($args['teaser']) && !variable_get('print_html_link_teaser', PRINT_HTML_LINK_TEASER_DEFAULT))
701 || !user_access('access print')) {
702 // If the teaser link is disabled or the user is not allowed
9383096e
JV
703 return FALSE;
704 }
d8ea68f9
JV
705 if (!empty($args['path'])) {
706 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
707 if (is_numeric($nid)) {
45074750 708 $args['node'] = node_load($nid);
d8ea68f9
JV
709 }
710 }
0a9dfc3a 711 if (!empty($args['node'])) {
32418b7e 712 static $node_type = '';
caac7c2b
JV
713
714 $node = $args['node'];
32418b7e
JV
715 if (isset($node->type)) {
716 $node_type = $node->type;
9383096e
JV
717 }
718 // Node
719 $print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT);
720 $print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT);
721
82993ef8 722 if (!_print_page_match($print_html_node_link_visibility, "node/". $node->nid, $print_html_node_link_pages)) {
abd96759 723 // Page not in visibility list
9383096e
JV
724 return FALSE;
725 }
0a9dfc3a 726 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
9383096e 727 // Link is for a comment, return the configured setting
6ea54f45 728 $res = db_fetch_object(db_query("SELECT comments FROM {print_node_conf} WHERE nid = %d", $node->nid));
35c25611
JV
729 $print_display_comment = $res ? intval($res->comments) : variable_get('print_display_comment_'. $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
730 if ($print_display_comment) {
6ea54f45
JV
731 return PRINT_ALLOW_NORMAL_LINK;
732 }
9383096e
JV
733 }
734 else {
735 // Node link
35c25611
JV
736 if (!$node->print_display) {
737 // Link for this node is disabled
9383096e
JV
738 return FALSE;
739 }
905534de 740 elseif (isset($node->parent)) {
9383096e
JV
741 // Node is a book;
742 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
8b2d4a33
JV
743 switch ($print_html_book_link) {
744 case 1:
745 if (user_access('see printer-friendly version')) {
746 return PRINT_ALLOW_BOOK_LINK;
747 }
748 break;
749 case 2:
750 return PRINT_ALLOW_NORMAL_LINK;
9383096e
JV
751 }
752 }
753 else {
754 return PRINT_ALLOW_NORMAL_LINK;
755 }
756 }
757 }
758 else {
759 // 'System' page
760 $print_html_sys_link_visibility = variable_get('print_html_sys_link_visibility', PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT);
761 $print_html_sys_link_pages = variable_get('print_html_sys_link_pages', PRINT_HTML_SYS_LINK_PAGES_DEFAULT);
762
82993ef8 763 return _print_page_match($print_html_sys_link_visibility, $_GET['q'], $print_html_sys_link_pages);
9383096e 764 }
8b2d4a33 765 return FALSE;
9383096e
JV
766}
767
768/**
0f71eefe
JV
769 * Parse an array into a valid urlencoded query string.
770 * Modified from drupal_query_string_encode to prevent re-encoding of
771 * encoded original.
772 *
773 * @param $query
774 * The array to be processed e.g. $_GET
775 * @param $exclude
776 * The array filled with keys to be excluded.
777 * @return
778 * urlencoded string which can be appended to/as the URL query string
779 */
0a9dfc3a 780function print_query_string_encode($query, $exclude = array(), $parent = '') {
0f71eefe
JV
781 $params = array();
782 foreach ($query as $key => $value) {
0a9dfc3a
JV
783 if ($parent) {
784 $key = $parent .'['. $key .']';
785 }
786
0f71eefe
JV
787 if (in_array($key, $exclude)) {
788 continue;
789 }
0a9dfc3a
JV
790
791 if (is_array($value)) {
792 $params[] = print_query_string_encode($value, $exclude, $key);
793 }
794 else {
795 $params[] = $key .'='. rawurlencode($value);
796 }
0f71eefe 797 }
0a9dfc3a 798
0f71eefe
JV
799 return implode('&', $params);
800}