Fix content corner problem and other minor fixes
[project/print.git] / print.pages.inc
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Contains the functions to generate Printer-friendly pages.
7 *
8 * This file is included by the core PF module, and includes all the
9 * functions necessary to generate a PF version of the original page
10 * in HTML format.
11 */
12
13 $_print_urls = PRINT_URLS_DEFAULT;
14
15 /**
16 * Generate an HTML version of the printer-friendly page
17 *
18 * @see print_controller()
19 * @see _print_get_template()
20 */
21 function print_controller_html() {
22 $args = func_get_args();
23 // Remove the print/ prefix
24 $path = implode('/', $args);
25 $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
26
27 $print = print_controller($path, $cid, PRINT_HTML_FORMAT);
28 if ($print !== FALSE) {
29 $node = $print['node'];
30 include_once(_print_get_template(PRINT_HTML_FORMAT, $print['type']));
31
32 $nodepath = drupal_get_normal_path($node->path);
33 db_query("UPDATE {print_page_counter} SET totalcount = totalcount + 1, timestamp = %d WHERE path = '%s'", time(), $nodepath);
34 // If we affected 0 rows, this is the first time viewing the node.
35 if (!db_affected_rows()) {
36 // We must create a new row to store counters for the new node.
37 db_query("INSERT INTO {print_page_counter} (path, totalcount, timestamp) VALUES ('%s', 1, %d)", $nodepath, time());
38 }
39 }
40 }
41
42 /**
43 * Select the print generator function based on the page type
44 *
45 * Depending on the type of node, this functions chooses the appropriate
46 * generator function.
47 *
48 * @param $path
49 * path of the original page
50 * @param $cid
51 * comment ID of the individual comment to be rendered
52 * @param $format
53 * format of the page being generated
54 * @param $teaser
55 * if set to TRUE, outputs only the node's teaser
56 * @param $message
57 * optional sender's message (used by the send e-mail module)
58 * @return
59 * array with the fields to be used in the template
60 * @see _print_generate_node()
61 * @see _print_generate_path()
62 * @see _print_generate_book()
63 */
64 function print_controller($path, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
65 if (empty($path)) {
66 // If no path was provided, let's try to generate a page for the referer
67 global $base_url;
68
69 $ref = referer_uri();
70 $path = preg_replace("!^$base_url/!", '', $ref);
71 if ($path === $ref) {
72 $path = '';
73 }
74 }
75 if (!is_numeric($path)) {
76 // Indirect call with print/alias
77 // If there is a path alias with these arguments, generate a printer-friendly version for it
78 $path = drupal_get_normal_path($path);
79 $ret = preg_match('!^node/(.*)!i', $path, $matches);
80 if ($ret == 1) {
81 $path = $matches[1];
82 }
83 }
84 $parts = explode('/', $path);
85 if (is_numeric($parts[0])) {
86 $print = _print_generate_node($path, $cid, $format, $teaser, $message);
87 }
88 else {
89 $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
90 if ($ret == 1) {
91 // This is a book PF page link, handle trough the book handling functions
92 $print = _print_generate_book($matches[1], $format, $teaser, $message);
93 }
94 else {
95 // If no content node was found, handle the page printing with the 'printable' engine
96 $print = _print_generate_path($path, $format, $teaser, $message);
97 }
98 }
99
100 return $print;
101 }
102
103 /**
104 * Generates a robots meta tag to tell them what they may index
105 *
106 * @return
107 * string with the meta robots tag
108 */
109 function _print_robots_meta_generator() {
110 $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
111 $print_robots_nofollow = variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT);
112 $print_robots_noarchive = variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT);
113 $robots_meta = array();
114
115 if (!empty($print_robots_noindex)) {
116 $robots_meta[] = 'noindex';
117 }
118 if (!empty($print_robots_nofollow)) {
119 $robots_meta[] = 'nofollow';
120 }
121 if (!empty($print_robots_noarchive)) {
122 $robots_meta[] = 'noarchive';
123 }
124
125 if (count($robots_meta) > 0) {
126 $robots_meta = implode(', ', $robots_meta);
127 $robots_meta = "<meta name='robots' content='$robots_meta' />\n";
128 }
129 else {
130 $robots_meta = '';
131 }
132
133 return $robots_meta;
134 }
135
136 /**
137 * Post-processor that fills the array for the template with common details
138 *
139 * @param $node
140 * generated node with a printer-friendly node body
141 * @param $message
142 * optional sender's message (used by the send e-mail module)
143 * @param $cid
144 * id of current comment being generated (NULL when not generating
145 * an individual comment)
146 * @return
147 * array with the fields to be used in the template
148 */
149 function _print_var_generator($node, $message = NULL, $cid = NULL) {
150 global $base_url, $_print_urls;
151
152 $path = empty($node->nid) ? $node->path : "node/$node->nid";
153
154 // print module settings
155 $print_css = variable_get('print_css', PRINT_CSS_DEFAULT);
156 $print_logo_options = variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT);
157 $print_logo_url = variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT);
158 $print_html_sendtoprinter = variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT);
159 $print_sourceurl_enabled = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT);
160 $print_sourceurl_forcenode = variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT);
161 $print_sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT);
162 $print_footer_options = variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT);
163 $print_footer_user = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT);
164
165 $print['language'] = $GLOBALS['locale'];
166 $print['title'] = $node->title;
167 $print['head'] = drupal_get_html_head();
168 $print['scripts'] = drupal_get_js();
169 $print['robots_meta'] = _print_robots_meta_generator();
170 $print['url'] = url($path, NULL, NULL, TRUE);
171 $print['base_href'] = "<base href='". $print['url'] ."' />\n";
172 $print['favicon'] = theme_get_setting('toggle_favicon') ? "<link rel='shortcut icon' href='". theme_get_setting('favicon') ."' type='image/x-icon' />\n" : '';
173
174 if (!empty($print_css)) {
175 $replace_pairs = array('%b' => base_path(), '%t' => path_to_theme());
176 $user_css = strip_tags(strtr($print_css, $replace_pairs));
177 }
178 else {
179 drupal_add_css(drupal_get_path('module', 'print') .'/css/print.css');
180 }
181 $drupal_css = drupal_add_css();
182 foreach ($drupal_css as $key => $types) {
183 // Unset the theme's CSS
184 $drupal_css[$key]['theme'] = array();
185 }
186
187 // If we are sending a message via e-mail, the CSS must be embedded
188 if (!empty($message)) {
189 $style = '';
190 $css_files = array();
191 foreach ($drupal_css as $types) {
192 foreach ($types as $values) {
193 $css_files = array_merge($css_files, array_keys($values));
194 }
195 }
196 if (!empty($print_css)) {
197 // Convert to a local path, by removing the base_path
198 $pattern = '!^'. base_path() .'!';
199 $css_files[] = preg_replace($pattern, '', $user_css);
200 }
201 foreach ($css_files as $filename) {
202 $res = file_get_contents($filename, TRUE);
203 if ($res != FALSE) {
204 $style .= $res;
205 }
206 }
207 $print['css'] = "<style type='text/css' media='all'>$style</style>\n";
208 }
209 else {
210 $print['css'] = drupal_get_css($drupal_css);
211 if (!empty($print_css)) {
212 $print['css'] .= "<link type='text/css' rel='stylesheet' media='all' href='$user_css' />\n";
213 }
214 }
215
216 $print['sendtoprinter'] = $print_html_sendtoprinter ? ' onload="window.print();"' : '';
217
218 switch ($print_logo_options) {
219 case 0: // none
220 $logo_url = 0;
221 break;
222 case 1: // theme's
223 $logo_url = theme_get_setting('logo');
224 break;
225 case 2: // user-specifed
226 $logo_url = strip_tags($print_logo_url);
227 break;
228 }
229 $print['logo'] = $logo_url ? "<img class='print-logo' src='$logo_url' alt='' />\n" : '';
230
231 switch ($print_footer_options) {
232 case 0: // none
233 $footer = '';
234 break;
235 case 1: // theme's
236 $footer = filter_xss_admin(variable_get('site_footer', FALSE)) ."\n". theme('blocks', 'footer');
237 $logo_url = theme_get_setting('logo');
238 break;
239 case 2: // user-specifed
240 $footer = $print_footer_user;
241 break;
242 }
243 $print['footer_message'] = $footer;
244
245 $published_site = variable_get('site_name', 0);
246 if ($published_site) {
247 $print_text_published = variable_get('print_text_published', t('Published on %site_name'));
248 $published = t($print_text_published, array('%site_name' => $published_site));
249 $print['site_name'] = $published .' ('. l($base_url, $base_url) .')';
250 }
251 else {
252 $print['site_name'] = '';
253 }
254
255 if ($print_sourceurl_enabled == 1) {
256 /* Grab and format the src URL */
257 if (empty($print_sourceurl_forcenode)) {
258 $url = $print['url'];
259 }
260 else {
261 $url = $base_url .'/'. (((bool)variable_get('clean_url', '0')) ? '' : '?q=') . $path;
262 }
263 if (is_int($cid)) {
264 $url .= '#comment-$cid';
265 }
266 $retrieved_date = format_date(time(), 'small');
267 $print_text_retrieved = variable_get('print_text_retrieved', t('retrieved on %date'));
268 $retrieved = t($print_text_retrieved, array('%date' => $retrieved_date));
269 $print['printdate'] = $print_sourceurl_date ? " ($retrieved)" : '';
270
271 $source_url = variable_get('print_text_source_url', t('Source URL'));
272 $print['source_url'] = '<strong>'. $source_url . $print['printdate'] .':</strong> '. l($url, $url);
273 }
274 else {
275 $print['source_url'] = '';
276 }
277
278 if (isset($node->type)) {
279 $node_type = $node->type;
280
281 if (theme_get_setting("toggle_node_info_$node_type")) {
282 $print_text_by = variable_get('print_text_by', t('By %author'));
283 $by_author = ($node->name ? $node->name : variable_get('anonymous', t('Anonymous')));
284 $print['submitted'] = t($print_text_by, array('%author' => $by_author));
285
286 $print_text_created = variable_get('print_text_created', t('Created %date'));
287 $created_datetime = format_date($node->created, 'small');
288 $print['created'] = t($print_text_created, array('%date' => $created_datetime));
289 }
290 else {
291 $print['submitted'] = '';
292 $print['created'] = '';
293 }
294
295 $print['type'] = $node->type;
296 }
297 else {
298 $print['submitted'] = '';
299 $print['created'] = '';
300 $print['type'] = '';
301 }
302
303 menu_set_active_item($path);
304 $breadcrumb = drupal_get_breadcrumb();
305 if (!empty($breadcrumb)) {
306 $breadcrumb[] = menu_get_active_title();
307 $print['breadcrumb'] = implode(' > ', $breadcrumb);
308 }
309 else {
310 $print['breadcrumb'] = '';
311 }
312
313 // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
314 $print['pfp_links'] = '';
315 if (!empty($_print_urls)) {
316 $urls = _print_friendly_urls();
317 $max = count($urls);
318 $pfp_links = '';
319 if ($max) {
320 for ($i = 0; $i < $max; $i++) {
321 $pfp_links .= '['. ($i + 1) .'] '. $urls[$i] ."<br />\n";
322 }
323 $links = variable_get('print_text_links', t('Links'));
324 $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>";
325 }
326 }
327
328 if (module_exists('taxonomy')) {
329 $terms = taxonomy_link('taxonomy terms', $node);
330 $print['taxonomy'] = theme('links', $terms);
331 }
332
333 $print['content'] = $node->body;
334 $print['node'] = $node;
335 $print['message'] = $message;
336
337 return $print;
338 }
339
340 /**
341 * Callback function for the preg_replace_callback for URL-capable patterns
342 *
343 * Manipulate URLs to make them absolute in the URLs list, and to add a
344 * [n] footnote marker.
345 *
346 * @param $matches
347 * array with the matched tag patterns, usually <a...>+text+</a>
348 * @return
349 * tag with re-written URL and when appropriate the [n] index to the
350 * URL list
351 */
352 function _print_rewrite_urls($matches) {
353 global $base_url, $base_root, $_print_urls;
354
355 // first, split the html into the different tag attributes
356 $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
357 $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
358 foreach ($attribs as $key => $value) {
359 $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
360 }
361
362 $size = count($attribs);
363 for ($i=1; $i < $size; $i++) {
364 // If the attribute is href or src, we may need to rewrite the URL in the value
365 if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
366 $url = trim($urls[1], " \t\n\r\0\x0B\"'");
367
368 if (strpos($url, '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', $url)) {
369 // URL is absolute, do nothing
370 $newurl = urldecode($url);
371 }
372 else {
373 if ($url[0] == '#') {
374 // URL is an anchor tag
375 if (!empty($_print_urls)) {
376 $path = explode('/', $_GET['q']);
377 unset($path[0]);
378 $path = implode('/', $path);
379 if (is_numeric($path)) {
380 $path = "node/$path";
381 }
382 // Printer-friendly URLs is on, so we need to make it absolute
383 $newurl = url($path, NULL, substr(urldecode($url), 1), TRUE);
384 }
385 // Because base href is the original page, change the link to
386 // still be usable inside the print page
387 $matches[1] = str_replace($url, $_GET['q'] . $url, $matches[1]);
388 }
389 else {
390 // URL is relative, convert it into absolute URL
391 if ($url[0] == '/') {
392 // If it starts with '/' just append it to the server name
393 $newurl = $base_root .'/'. trim(urldecode($url), '/');
394 }
395 elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
396 // If it starts with ?q=, just prepend with the base URL
397 $newurl = $base_url .'/'. trim(urldecode($url), '/');
398 }
399 else {
400 $newurl = url(trim(urldecode($url), '/'), NULL, NULL, TRUE);
401 }
402 $matches[1] = str_replace($url, $newurl, $matches[1]);
403 }
404 }
405 }
406 }
407
408 $ret = '<'. $matches[1] .'>';
409 if (count($matches) == 4) {
410 $ret .= $matches[2] . $matches[3];
411 if ((!empty($_print_urls)) && (isset($newurl))) {
412 $ret .= ' <span class="print-footnote">['. _print_friendly_urls(trim(stripslashes($newurl))) .']</span>';
413 }
414 }
415
416 return $ret;
417 }
418
419 /**
420 * Auxiliary function to store the Printer-friendly URLs list as static.
421 *
422 * @param $url
423 * absolute URL to be inserted in the list
424 * @return
425 * list of URLs previously stored if $url is 0, or the current count
426 * otherwise.
427 */
428 function _print_friendly_urls($url = 0) {
429 static $urls = array();
430 if ($url) {
431 $url_idx = array_search($url, $urls);
432 if ($url_idx !== FALSE) {
433 return ($url_idx + 1);
434 }
435 else {
436 $urls[] = $url;
437 return count($urls);
438 }
439 }
440 $ret = $urls;
441 $urls = array();
442 return $ret;
443 }
444
445 /**
446 * Choose most appropriate template
447 *
448 * Auxiliary function to resolve the most appropriate template trying to find
449 * a content specific template in the theme or module dir before falling back
450 * on a generic template also in those dirs.
451 *
452 * @param format
453 * format of the PF page being rendered (html, pdf, etc.)
454 * @param $type
455 * name of the node type being rendered in a PF page
456 * @return
457 * filename of the most suitable template
458 */
459 function _print_get_template($format = NULL, $type = NULL) {
460 $filenames = array();
461 // First try to find a template defined both for the format and then the type
462 if (!empty($format) && !empty($type)) {
463 $filenames[] = "print_$format.node-$type.tpl.php";
464 }
465 // Then only for the format
466 if (!empty($format)) {
467 $filenames[] = "print_$format.tpl.php";
468 }
469 // If the node type is known, then try to find that type's template file
470 if (!empty($type)) {
471 $filenames[] = "print.node-$type.tpl.php";
472 $filenames[] = "print.$type.tpl.php";
473 }
474 // Finally search for a generic template file
475 $filenames[] = 'print.tpl.php';
476
477 foreach ($filenames as $value) {
478 // First in the theme directory
479 $file = drupal_get_path('theme', $GLOBALS['theme_key']) .'/'. $value;
480 if (file_exists($file)) {
481 return $file;
482 }
483 // Then in the module directory
484 $file = drupal_get_path('module', 'print') .'/'. $value;
485 if (file_exists($file)) {
486 return $file;
487 }
488 }
489 }
490
491 /**
492 * Check URL list settings for this node
493 *
494 * @param node
495 * node object
496 * @param $format
497 * format of the page being generated
498 * @return
499 * TRUE if URL list should be displayed, FALSE otherwise
500 */
501 function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT) {
502 switch ($format) {
503 case PRINT_HTML_FORMAT:
504 $node_urllist = isset($node->print_display_urllist) ? $node->print_display_urllist : TRUE;
505 $fmt = '';
506 break;
507 case PRINT_MAIL_FORMAT:
508 $node_urllist = isset($node->print_mail_display_urllist) ? $node->print_mail_display_urllist : TRUE;
509 $fmt = $format .'_';
510 break;
511 case PRINT_PDF_FORMAT:
512 $node_urllist = isset($node->print_pdf_display_urllist) ? $node->print_pdf_display_urllist : TRUE;
513 $fmt = $format .'_';
514 break;
515 }
516 if (!isset($node_urllist)) $node_urllist = TRUE;
517
518 // Get value of Printer-friendly URLs setting
519 return (variable_get('print_urls', PRINT_URLS_DEFAULT) && ($node_urllist) &&
520 variable_get('print_'. $fmt .'display_urllist_'. $node->type, PRINT_TYPE_URLLIST_DEFAULT));
521 }
522
523 /**
524 * Prepare a Printer-friendly-ready node body for content nodes
525 *
526 * @param $nid
527 * node ID of the node to be rendered into a printer-friendly page
528 * @param $cid
529 * comment ID of the individual comment to be rendered
530 * @param $format
531 * format of the page being generated
532 * @param $teaser
533 * if set to TRUE, outputs only the node's teaser
534 * @param $message
535 * optional sender's message (used by the send e-mail module)
536 * @return
537 * filled array ready to be used in the template
538 */
539 function _print_generate_node($nid, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
540 global $_print_urls;
541
542 // We can take a node id
543 $node = node_load(array('nid' => $nid));
544 if (!$node) {
545 // Node not found
546 drupal_not_found();
547 return FALSE;
548 }
549 elseif (!node_access('view', $node)) {
550 // Access is denied
551 drupal_access_denied();
552 return FALSE;
553 }
554 drupal_set_title($node->title);
555
556 //alert other modules that we are generating a printer-friendly page, so they can choose to show/hide info
557 $node->printing = TRUE;
558 // Turn off Pagination by the Paging module
559 unset($node->pages);
560 unset($node->pages_count);
561 unset($node->page_count);
562
563 if ($cid === NULL) {
564 // Adapted (simplified) version of node_view
565 //Render the node content
566 $node = node_build_content($node, $teaser, TRUE);
567 // Disable fivestar widget output
568 unset($node->content['fivestar_widget']);
569 // Disable service links module output
570 unset($node->content['service_links']);
571
572 $content = drupal_render($node->content);
573 if ($teaser) {
574 $node->teaser = $content;
575 unset($node->body);
576 }
577 else {
578 $node->body = $content;
579 unset($node->teaser);
580 }
581 }
582
583 $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT);
584
585 if (function_exists('comment_render') && (($cid != NULL) || ($print_comments))) {
586 //Print only the requested comment (or if $cid is NULL, all of them)
587 $comments = comment_render($node, $cid);
588
589 //Remove the comment forms
590 $comments = preg_replace('!<form.*?id="comment-.*?">.*?</form>!sim', '', $comments);
591 //Remove the 'Post new comment' title
592 $comments = preg_replace('!<h2.*?>Post new comment</h2>!', '', $comments);
593 //Remove the comment title hyperlink
594 $comments = preg_replace('!(<h3.*?>)(<a.*?>)(.*?)</a>(</h3>)!', '$1$3$4', $comments);
595 //Remove the comment author link
596 $pattern = '!(<span class="submitted">)(.*?)<a.*?>(.*?)</a>(</span>)!sim';
597 if (preg_match($pattern, $comments)) {
598 $comments = preg_replace($pattern , '$1$2$3$4', $comments);
599 }
600 //Remove the comment links
601 $comments = preg_replace('!\s*<ul class="links">.*?</ul>!sim', '', $comments);
602 if ($cid != NULL) {
603 // Single comment requested, output only the comment
604 unset($node->body);
605 }
606 $node->body .= $comments;
607 }
608
609 node_invoke_nodeapi($node, 'alter', $teaser, TRUE);
610
611 if ($teaser) {
612 $node->body = $node->teaser;
613 unset($node->teaser);
614 }
615
616 //Check URL list settings
617 $_print_urls = _print_url_list_enabled($node, $format);
618
619 // Convert the a href elements
620 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
621 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
622
623 init_theme();
624
625 $print = _print_var_generator($node, $message, $cid);
626
627 return $print;
628 }
629
630 /**
631 * Prepare a Printer-friendly-ready node body for non-content pages
632 *
633 * @param $path
634 * path of the node to be rendered into a printer-friendly page
635 * @param $format
636 * format of the page being generated
637 * @param $teaser
638 * if set to TRUE, outputs only the node's teaser
639 * @param $message
640 * optional sender's message (used by the send e-mail module)
641 * @return
642 * filled array ready to be used in the template
643 */
644 function _print_generate_path($path, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
645 global $_print_urls;
646
647 $path = drupal_get_normal_path($path);
648 $_GET['q'] = $path;
649 _menu_append_contextual_items();
650
651 menu_set_active_item($path);
652 // Adapted from index.php.
653 $node = new stdClass();
654 $node->body = menu_execute_active_handler();
655 $node->title = drupal_get_title();
656 $node->path = $path;
657
658 // It may happen that a drupal_not_found is called in the above call
659 if (preg_match('/404 Not Found/', drupal_get_headers()) == 1) {
660 return FALSE;
661 }
662
663 if (is_int($node->body)) {
664 switch ($node->body) {
665 case MENU_NOT_FOUND:
666 drupal_not_found();
667 return FALSE;
668 break;
669 case MENU_ACCESS_DENIED:
670 drupal_access_denied();
671 return FALSE;
672 break;
673 }
674 }
675
676 // Delete any links area
677 $node->body = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body);
678
679 //Check URL list settings
680 $_print_urls = _print_url_list_enabled($node, $format);
681
682 // Convert the a href elements
683 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
684 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
685
686 init_theme();
687
688 $print = _print_var_generator($node, $message);
689
690 return $print;
691 }
692
693
694 /**
695 * Prepare a Printer-friendly-ready node body for book pages
696 *
697 * @param $nid
698 * node ID of the node to be rendered into a printer-friendly page
699 * @param $format
700 * format of the page being generated
701 * @param $teaser
702 * if set to TRUE, outputs only the node's teaser
703 * @param $message
704 * optional sender's message (used by the send e-mail module)
705 * @return
706 * filled array ready to be used in the template
707 */
708 function _print_generate_book($nid, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
709 global $_print_urls;
710
711 $node = node_load(array('nid' => $nid));
712 if (!$node) {
713 // Node not found
714 drupal_not_found();
715 return FALSE;
716 }
717 elseif (!node_access('view', $node) || (!user_access('see printer-friendly version'))) {
718 // Access is denied
719 drupal_access_denied();
720 return FALSE;
721 }
722
723 $depth = count(book_location($node)) + 1;
724 $node->body = book_recurse($nid, $depth, '_print_node_visitor_html_pre', 'book_node_visitor_html_post');
725
726 //Check URL list settings
727 $_print_urls = _print_url_list_enabled($node, $format);
728
729 // Convert the a href elements
730 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
731 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
732
733 init_theme();
734
735 $print = _print_var_generator($node, $message);
736 // The title is already displayed by the book_recurse, so avoid duplication
737 $print['title'] = '';
738
739 return $print;
740 }
741
742 /**
743 * Generates printer-friendly HTML for a node. This function is a 'pre-node' visitor function for book_recurse()
744 *
745 * My own version of book_node_visitor_html_pre so that CCK pages in book nodes
746 * come out right
747 *
748 * @param $node
749 * the node to generate output for.
750 * @param $depth
751 * the depth of the given node in the hierarchy. This is used only for
752 * generating output.
753 * @param $nid
754 * the node id (nid) of the given node. This is used only for generating
755 * output.
756 * @return
757 * the HTML generated for the given node.
758 */
759 function _print_node_visitor_html_pre($node, $depth, $nid) {
760 $node = node_build_content($node, FALSE, TRUE);
761
762 unset($node->content['book_navigation']);
763
764 $output .= "<div id='node-$node->nid' class='section-$depth'>\n";
765 $output .= "<h1 class='book-heading'>". check_plain($node->title) ."</h1>\n";
766 $output .= drupal_render($node->content);
767
768 return $output;
769 }