Issue #1123786: remove print links from RSS feeds.
[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);
8f1e9731 351 $allowed_type = print_link_allowed(array('type' => 'node', 'node' => $node, 'view_mode' => $view_mode));
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/**
17691f50 508 * Implements hook_entity_info_alter().
a924eea7 509 */
17691f50
JV
510function print_entity_info_alter(&$info) {
511 // Add the 'Print' view mode for nodes.
512 $info['node']['view modes'] += array(
a924eea7 513 'print' => array(
17691f50
JV
514 'label' => t('Print'),
515 'custom settings' => FALSE,
a924eea7
JV
516 ),
517 );
6cd6c868 518}
a924eea7
JV
519
520/**
855b243a
JV
521 * Auxiliary function to assign the per-node settings to the node object fields
522 *
523 * @param $node
524 * node to be modified
525 */
526function _print_set_node_fields(&$node) {
48692886
JV
527 if (isset($node->nid)) {
528 $res = db_query("SELECT link, comments, url_list FROM {print_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
fe25f54b 529 ->fetch();
48692886
JV
530 }
531 else {
532 $res = FALSE;
533 }
e646a8df
JV
534 $node->print_display = $res ? intval($res->link) : variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
535 $node->print_display_comment = $res ? intval($res->comments) : variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
536 $node->print_display_urllist = $res ? intval($res->url_list) : variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
855b243a
JV
537}
538
539/**
540 * Auxiliary function to discover a given page's title
541 *
542 * @param $path
543 * path of the page being identified
544 * @return
545 * string with the page's title
546 */
547function _print_get_title($path) {
548 $path = drupal_get_normal_path($path);
549 $nid = preg_replace('!^node/!', '', $path);
2d95fba8 550 if (ctype_digit($nid)) {
855b243a 551 $res = db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $nid))
fe25f54b 552 ->fetchField();
855b243a
JV
553 }
554 else {
555 $res = db_query("SELECT link_title FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $path))
fe25f54b 556 ->fetchField();
855b243a
JV
557 }
558 return $res;
559}
560
561/**
562 * Update the print_node_conf table to reflect the given attributes
563 * If updating to the default values, delete the record.
564 *
565 * @param $nid
566 * value of the nid field (primary key)
567 * @param $link
568 * value of the link field (0 or 1)
569 * @param $comments
570 * value of the comments field (0 or 1)
571 * @param $url_list
572 * value of the url_list field (0 or 1)
573 */
574function _print_node_conf_modify($nid, $link, $comments, $url_list) {
855b243a
JV
575 db_merge('print_node_conf')
576 ->key(array('nid' => $nid))
577 ->fields(array(
578 'link' => $link,
579 'comments' => $comments,
580 'url_list' => $url_list,
581 ))
582 ->execute();
1aaa7127
MW
583}
584
1aaa7127 585/**
b09f08f7 586 * Auxiliary function to fill the Printer-friendly link attributes
c4122ca5 587 *
f6972c51
JV
588 * @param $title
589 * text to displayed by the link when hovering over it with the mouse
590 * @param $class
591 * class attribute to be used in the link
fb70cd39
JV
592 * @param $new_window
593 * if TRUE opens the target page in a new window
1e156b5a
JV
594 * @return
595 * array of formatted attributes
c4122ca5 596 */
1caf9f9d 597function print_fill_attributes($title = '', $class = '', $new_window = FALSE) {
7d62e7c4
JV
598 $print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT);
599 $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
c4122ca5 600
f6972c51
JV
601 $attributes = array();
602 $attributes['title'] = $title;
c021eaef 603 if (!empty($class)) {
070225f6 604 $attributes['class'] = array($class);
c021eaef 605 }
f6972c51 606
1caf9f9d 607 if ($new_window) {
7d62e7c4 608 switch ($print_newwindow) {
1caf9f9d
JV
609 case 0:
610 $attributes['target'] = '_blank';
611 break;
612 case 1:
613 $attributes['onclick'] = 'window.open(this.href); return false';
614 break;
615 }
c4122ca5 616 }
7d62e7c4 617 if (!empty($print_robots_noindex)) {
b09f08f7 618 $attributes['rel'] = 'nofollow';
c4122ca5 619 }
f6972c51
JV
620 return $attributes;
621}
c67b9ed4 622
f6972c51
JV
623/**
624 * Auxiliary function to set the link text and html flag
625 *
626 * @param $type
627 * type of link: 0 or 1 for a text-only link, 2 for icon-only and 3 for
628 * both text and icon
629 * @param $text
630 * text to be displayed on the link to the printer-friendly page
631 * @param $img
632 * path to the icon file
633 * @return
634 * array with the link text and html flag
635 */
636function _print_format_link_aux($type = 0, $text = '', $img = '') {
c67b9ed4 637 if ($type >= 2) {
c67b9ed4
JV
638 $html = TRUE;
639 switch ($type) {
640 case 2:
070225f6 641 $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon'))));
c67b9ed4
JV
642 break;
643 case 3:
070225f6 644 $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon', 'print-icon-margin')))) . $text;
c67b9ed4
JV
645 break;
646 }
647 }
648 else {
649 $html = FALSE;
650 }
3483245d 651
c67b9ed4
JV
652 return array('text' => $text,
653 'html' => $html,
7e748d74 654 );
c4122ca5
DN
655}
656
1e156b5a
JV
657/**
658 * Format the Printer-friendly link
659 *
660 * @return
661 * array of formatted attributes
662 * @ingroup themeable
663 */
b09f08f7 664function theme_print_format_link() {
7d62e7c4
JV
665 $print_html_link_class = variable_get('print_html_link_class', PRINT_HTML_LINK_CLASS_DEFAULT);
666 $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
667 $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
937c5ab6 668 $print_html_link_text = filter_xss(variable_get('print_html_link_text', t('Printer-friendly version')));
7d62e7c4 669
fe25f54b 670 $img = drupal_get_path('module', 'print') . '/icons/print_icon.gif';
c67b9ed4 671 $title = t('Display a printer-friendly version of this page.');
7d62e7c4
JV
672 $class = strip_tags($print_html_link_class);
673 $new_window = $print_html_new_window;
855b243a 674 $format = _print_format_link_aux($print_html_show_link, $print_html_link_text, $img);
c67b9ed4
JV
675
676 return array('text' => $format['text'],
677 'html' => $format['html'],
1caf9f9d 678 'attributes' => print_fill_attributes($title, $class, $new_window),
7e748d74 679 );
cff994c6
JV
680}
681
1e156b5a 682/**
b09f08f7
JV
683 * Auxiliary function to display a formatted Printer-friendly link
684 *
1e156b5a
JV
685 * Function made available so that developers may call this function from
686 * their defined pages/blocks.
687 *
688 * @param $path
3483245d 689 * path of the original page (optional). If not specified, the current URL
1e156b5a 690 * is used
e60934b7
JV
691 * @param $node
692 * an optional node object, to be used in defining the path, if used, the
693 * path argument is irrelevant
1e156b5a
JV
694 * @return
695 * string with the HTML link to the printer-friendly page
cff994c6 696 */
e60934b7
JV
697function print_insert_link($path = NULL, $node = NULL) {
698 if ($node !== NULL) {
699 $nid = $node->nid;
fe25f54b 700 $path = 'node/' . $nid;
e60934b7
JV
701 $allowed_type = print_link_allowed(array('node' => $node));
702 }
703 else {
b09f08f7 704 if ($path === NULL) {
e60934b7
JV
705 $nid = preg_replace('!^node/!', '', $_GET['q']);
706 $path = $_GET['q'];
707 }
708 else {
709 $nid = NULL;
710 }
711 $allowed_type = print_link_allowed(array('path' => $path));
712 }
c0bede24 713
e60934b7
JV
714 if ($allowed_type) {
715 if ($nid !== NULL) {
716 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
fe25f54b 717 $path = 'book/export/html/' . $nid;
c0bede24
JV
718 }
719 else {
e60934b7
JV
720 if (variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT)) {
721 $path = drupal_get_path_alias($path);
722 }
723 else {
724 $path = $nid;
725 }
c0bede24 726 }
fe25f54b 727 $path = PRINT_PATH . '/' . $path;
973f5b72 728 $query = print_query_string_encode($_GET, array('q'));
b09f08f7
JV
729 if (empty($query)) {
730 $query = NULL;
731 }
cff994c6 732 }
730a39bb
JV
733 else {
734 $query = NULL;
735 }
fe25f54b 736 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
b09f08f7 737 $format = theme('print_format_link');
fe25f54b 738 return '<span class="print_html">' . l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) . '</span>';
cff994c6 739 }
e60934b7
JV
740 else {
741 return FALSE;
742 }
1aaa7127 743}
c8a584b9
JV
744
745/**
5d9572d7 746 * Determine if the provided page is enabled according to the visibility settings
c8a584b9
JV
747 *
748 * @param $visibility
973f5b72 749 * current visibility settings:
c8a584b9
JV
750 * 0 for show on every page except the listed pages
751 * 1 for show on only the listed pages
752 * @param $pages
753 * list of pages
754 * @return
755 * TRUE if it is enabled, FALSE otherwise
756 */
5d9572d7 757function _print_page_match($visibility, $path, $pages) {
c8a584b9 758 if ($pages) {
fc341830 759 if ($visibility == 2) {
fc6dbbc1
JV
760 if (module_exists('php')) {
761 return php_eval($pages);
762 }
763 else {
764 return FALSE;
765 }
fc341830 766 }
5d9572d7 767 $alias = drupal_get_path_alias($path);
c8a584b9 768 $page_match = drupal_match_path($path, $pages);
5d9572d7
JV
769 if ($alias != $path) {
770 $page_match = $page_match || drupal_match_path($alias, $pages);
c8a584b9
JV
771 }
772
773 return !($visibility xor $page_match);
774 }
775 else {
1c9c68be 776 return !$visibility;
c8a584b9
JV
777 }
778}
dcd3fc93
JV
779
780/**
781 * Determine a the link to the PF version is allowed depending on all possible settings
782 *
783 * @param $args
784 * array containing the possible parameters:
785 * teaser, node, type, path
786 * @return
787 * FALSE if not allowed
788 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
789 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
790 */
791function print_link_allowed($args) {
8f1e9731
JV
792 $view_mode = isset($args['view_mode']) ? $args['view_mode'] : '';
793 if ((($view_mode == 'teaser') && !variable_get('print_html_link_teaser', PRINT_HTML_LINK_TEASER_DEFAULT))
794 || !in_array($view_mode, array('full', 'teaser')) || !user_access('access print')) {
23cc13e2 795 // If the teaser link is disabled or the user is not allowed
dcd3fc93
JV
796 return FALSE;
797 }
e60934b7
JV
798 if (!empty($args['path'])) {
799 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
2d95fba8 800 if (ctype_digit($nid)) {
f0e49fdd 801 $args['node'] = node_load($nid);
e60934b7
JV
802 }
803 }
b698536b 804 if (!empty($args['node'])) {
06442272 805 static $node_type = '';
742db343
JV
806
807 $node = $args['node'];
06442272
JV
808 if (isset($node->type)) {
809 $node_type = $node->type;
dcd3fc93
JV
810 }
811 // Node
812 $print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT);
813 $print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT);
814
9c42c5ac 815 if (!_print_page_match($print_html_node_link_visibility, "node/" . $node->nid, $print_html_node_link_pages)) {
c8aee091 816 // Page not in visibility list
dcd3fc93
JV
817 return FALSE;
818 }
b698536b 819 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
dcd3fc93 820 // Link is for a comment, return the configured setting
8deb0c87
JV
821 // Cache this statically to avoid duplicate queries for every comment.
822 static $res = array();
823 if (!isset($res[$node->nid])) {
824 $res[$node->nid] = db_query("SELECT comments FROM {print_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
825 ->fetch();
826 }
827 $print_display_comment = $res ? intval($res[$node->nid]->comments) : variable_get('print_display_comment_' . $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
e646a8df 828 if ($print_display_comment) {
855b243a
JV
829 return PRINT_ALLOW_NORMAL_LINK;
830 }
dcd3fc93
JV
831 }
832 else {
833 // Node link
e646a8df
JV
834 if (!$node->print_display) {
835 // Link for this node is disabled
dcd3fc93
JV
836 return FALSE;
837 }
e02f16ac 838 elseif (isset($node->book)) {
dcd3fc93
JV
839 // Node is a book;
840 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
c64555f4
JV
841 switch ($print_html_book_link) {
842 case 1:
843 if (user_access('access printer-friendly version')) {
844 return PRINT_ALLOW_BOOK_LINK;
845 }
846 break;
847 case 2:
848 return PRINT_ALLOW_NORMAL_LINK;
dcd3fc93
JV
849 }
850 }
851 else {
852 return PRINT_ALLOW_NORMAL_LINK;
853 }
854 }
855 }
856 else {
857 // 'System' page
858 $print_html_sys_link_visibility = variable_get('print_html_sys_link_visibility', PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT);
859 $print_html_sys_link_pages = variable_get('print_html_sys_link_pages', PRINT_HTML_SYS_LINK_PAGES_DEFAULT);
860
5d9572d7 861 return _print_page_match($print_html_sys_link_visibility, $_GET['q'], $print_html_sys_link_pages);
dcd3fc93 862 }
c64555f4 863 return FALSE;
dcd3fc93 864}
973f5b72
JV
865
866/**
867 * Parse an array into a valid urlencoded query string.
868 * Modified from drupal_query_string_encode to prevent re-encoding of
869 * encoded original.
870 *
871 * @param $query
872 * The array to be processed e.g. $_GET
873 * @param $exclude
874 * The array filled with keys to be excluded.
875 * @return
876 * urlencoded string which can be appended to/as the URL query string
877 */
b9a69e89 878function print_query_string_encode($query, $exclude = array(), $parent = '') {
973f5b72
JV
879 $params = array();
880 foreach ($query as $key => $value) {
b9a69e89 881 if ($parent) {
fe25f54b 882 $key = $parent . '[' . $key . ']';
b9a69e89
JV
883 }
884
973f5b72
JV
885 if (in_array($key, $exclude)) {
886 continue;
887 }
b9a69e89
JV
888
889 if (is_array($value)) {
7a444aeb 890 $params[] = print_query_string_encode($value, $exclude, $key);
b9a69e89
JV
891 }
892 else {
fe25f54b 893 $params[] = $key . '=' . rawurlencode($value);
b9a69e89 894 }
973f5b72 895 }
b9a69e89 896
07ce01a7 897 return $params;
973f5b72 898}