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