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