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