Updated Spanish translation by gusgsm1
[project/print.git] / print.pages.inc
CommitLineData
ddb9ae21 1<?php
623692c4 2// $Id$
ddb9ae21 3
623692c4
JV
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 */
ddb9ae21 12
6ea54f45
JV
13$_print_urls = PRINT_URLS_DEFAULT;
14
623692c4
JV
15/**
16 * Generate an HTML version of the printer-friendly page
17 *
18 * @see print_controller()
19 * @see _print_get_template()
20 */
ddb9ae21 21function print_controller_html() {
9383096e 22 $args = func_get_args();
9f9f3f56 23 // Remove the print/ prefix
9383096e 24 $path = implode('/', $args);
2e0d9e3d 25 $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
9f9f3f56 26
6ea54f45 27 $print = print_controller($path, $cid, PRINT_HTML_FORMAT);
f0855c19
JV
28 if ($print !== FALSE) {
29 $node = $print['node'];
6ea54f45
JV
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 }
f0855c19 39 }
ddb9ae21
JV
40}
41
42/**
9f9f3f56 43 * Select the print generator function based on the page type
623692c4
JV
44 *
45 * Depending on the type of node, this functions chooses the appropriate
46 * generator function.
47 *
9f9f3f56
JV
48 * @param $path
49 * path of the original page
50 * @param $cid
51 * comment ID of the individual comment to be rendered
6ea54f45
JV
52 * @param $format
53 * format of the page being generated
9383096e
JV
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)
623692c4
JV
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()
ddb9ae21 63 */
6ea54f45 64function print_controller($path, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
e9f23a49
JV
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 }
9f9f3f56 75 if (!is_numeric($path)) {
ddb9ae21
JV
76 // Indirect call with print/alias
77 // If there is a path alias with these arguments, generate a printer-friendly version for it
9f9f3f56 78 $path = drupal_get_normal_path($path);
9383096e 79 $ret = preg_match('!^node/(.*)!i', $path, $matches);
ddb9ae21 80 if ($ret == 1) {
9f9f3f56 81 $path = $matches[1];
ddb9ae21
JV
82 }
83 }
9383096e 84 $parts = explode('/', $path);
6ee36e8a 85 if (is_numeric($parts[0])) {
6ea54f45 86 $print = _print_generate_node($path, $cid, $format, $teaser, $message);
ddb9ae21
JV
87 }
88 else {
9383096e 89 $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
ddb9ae21
JV
90 if ($ret == 1) {
91 // This is a book PF page link, handle trough the book handling functions
6ea54f45 92 $print = _print_generate_book($matches[1], $format, $teaser, $message);
ddb9ae21
JV
93 }
94 else {
95 // If no content node was found, handle the page printing with the 'printable' engine
6ea54f45 96 $print = _print_generate_path($path, $format, $teaser, $message);
ddb9ae21
JV
97 }
98 }
99
100 return $print;
101}
102
ddb9ae21 103/**
623692c4 104 * Generates a robots meta tag to tell them what they may index
ddb9ae21 105 *
623692c4 106 * @return
95076d9f 107 * string with the meta robots tag
ddb9ae21
JV
108 */
109function _print_robots_meta_generator() {
9383096e
JV
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);
ddb9ae21
JV
113 $robots_meta = array();
114
9383096e 115 if (!empty($print_robots_noindex)) {
ddb9ae21
JV
116 $robots_meta[] = 'noindex';
117 }
9383096e 118 if (!empty($print_robots_nofollow)) {
ddb9ae21
JV
119 $robots_meta[] = 'nofollow';
120 }
9383096e 121 if (!empty($print_robots_noarchive)) {
ddb9ae21
JV
122 $robots_meta[] = 'noarchive';
123 }
ddb9ae21
JV
124
125 if (count($robots_meta) > 0) {
126 $robots_meta = implode(', ', $robots_meta);
9383096e 127 $robots_meta = "<meta name='robots' content='$robots_meta' />\n";
ddb9ae21
JV
128 }
129 else {
130 $robots_meta = '';
131 }
132
133 return $robots_meta;
134}
135
136/**
623692c4 137 * Post-processor that fills the array for the template with common details
ddb9ae21 138 *
623692c4
JV
139 * @param $node
140 * generated node with a printer-friendly node body
9383096e
JV
141 * @param $message
142 * optional sender's message (used by the send e-mail module)
623692c4 143 * @param $cid
95076d9f 144 * id of current comment being generated (NULL when not generating
623692c4
JV
145 * an individual comment)
146 * @return
147 * array with the fields to be used in the template
ddb9ae21 148 */
9383096e 149function _print_var_generator($node, $message = NULL, $cid = NULL) {
6ea54f45 150 global $base_url, $_print_urls;
ddb9ae21
JV
151
152 $path = empty($node->nid) ? $node->path : "node/$node->nid";
153
ddb9ae21 154 // print module settings
9383096e 155 $print_css = variable_get('print_css', PRINT_CSS_DEFAULT);
9383096e
JV
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);
905534de
JV
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);
9383096e
JV
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" : '';
ddb9ae21 173
9383096e 174 if (!empty($print_css)) {
37f3c1f9 175 $replace_pairs = array('%b' => base_path(), '%t' => path_to_theme());
905534de 176 $user_css = strip_tags(strtr($print_css, $replace_pairs));
ddb9ae21
JV
177 }
178 else {
905534de 179 drupal_add_css(drupal_get_path('module', 'print') .'/css/print.css');
ddb9ae21 180 }
905534de
JV
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();
9383096e
JV
185 }
186
187 // If we are sending a message via e-mail, the CSS must be embedded
188 if (!empty($message)) {
189 $style = '';
905534de
JV
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)) {
9383096e 197 // Convert to a local path, by removing the base_path
905534de
JV
198 $pattern = '!^'. base_path() .'!';
199 $css_files[] = preg_replace($pattern, '', $user_css);
200 }
201 foreach ($css_files as $filename) {
9383096e
JV
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";
4b7c4a49 208 }
9383096e 209 else {
905534de
JV
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";
9f9f3f56 213 }
9383096e 214 }
9f9f3f56 215
9383096e 216 $print['sendtoprinter'] = $print_html_sendtoprinter ? ' onload="window.print();"' : '';
95076d9f 217
9383096e
JV
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" : '';
cdd843e5 230
905534de
JV
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
cdd843e5 245 $published_site = variable_get('site_name', 0);
54fb3a40 246 if ($published_site) {
a623e75e
JV
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));
9383096e 249 $print['site_name'] = $published .' ('. l($base_url, $base_url) .')';
cdd843e5
JV
250 }
251 else {
9383096e 252 $print['site_name'] = '';
cdd843e5 253 }
ddb9ae21 254
9383096e 255 if ($print_sourceurl_enabled == 1) {
78cc5963 256 /* Grab and format the src URL */
9383096e
JV
257 if (empty($print_sourceurl_forcenode)) {
258 $url = $print['url'];
78cc5963
JV
259 }
260 else {
9f9f3f56 261 $url = $base_url .'/'. (((bool)variable_get('clean_url', '0')) ? '' : '?q=') . $path;
78cc5963 262 }
2e0d9e3d 263 if (is_int($cid)) {
9383096e 264 $url .= '#comment-$cid';
78cc5963 265 }
cdd843e5 266 $retrieved_date = format_date(time(), 'small');
a623e75e
JV
267 $print_text_retrieved = variable_get('print_text_retrieved', t('retrieved on %date'));
268 $retrieved = t($print_text_retrieved, array('%date' => $retrieved_date));
9383096e 269 $print['printdate'] = $print_sourceurl_date ? " ($retrieved)" : '';
cdd843e5 270
a623e75e 271 $source_url = variable_get('print_text_source_url', t('Source URL'));
9383096e 272 $print['source_url'] = '<strong>'. $source_url . $print['printdate'] .':</strong> '. l($url, $url);
ddb9ae21
JV
273 }
274 else {
9383096e 275 $print['source_url'] = '';
ddb9ae21
JV
276 }
277
278 if (isset($node->type)) {
279 $node_type = $node->type;
cdd843e5 280
6d82f395 281 if (theme_get_setting("toggle_node_info_$node_type")) {
a623e75e 282 $print_text_by = variable_get('print_text_by', t('By %author'));
9f9f3f56 283 $by_author = ($node->name ? $node->name : variable_get('anonymous', t('Anonymous')));
a623e75e 284 $print['submitted'] = t($print_text_by, array('%author' => $by_author));
cdd843e5 285
a623e75e 286 $print_text_created = variable_get('print_text_created', t('Created %date'));
9f9f3f56 287 $created_datetime = format_date($node->created, 'small');
a623e75e 288 $print['created'] = t($print_text_created, array('%date' => $created_datetime));
9f9f3f56
JV
289 }
290 else {
9383096e
JV
291 $print['submitted'] = '';
292 $print['created'] = '';
9f9f3f56 293 }
cdd843e5 294
9383096e 295 $print['type'] = $node->type;
ddb9ae21
JV
296 }
297 else {
9383096e
JV
298 $print['submitted'] = '';
299 $print['created'] = '';
300 $print['type'] = '';
ddb9ae21
JV
301 }
302
ddb9ae21
JV
303 menu_set_active_item($path);
304 $breadcrumb = drupal_get_breadcrumb();
305 if (!empty($breadcrumb)) {
306 $breadcrumb[] = menu_get_active_title();
9383096e 307 $print['breadcrumb'] = implode(' > ', $breadcrumb);
ddb9ae21
JV
308 }
309 else {
9383096e 310 $print['breadcrumb'] = '';
ddb9ae21
JV
311 }
312
313 // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
905534de 314 $print['pfp_links'] = '';
6ea54f45 315 if (!empty($_print_urls)) {
ddb9ae21
JV
316 $urls = _print_friendly_urls();
317 $max = count($urls);
cdd843e5 318 $pfp_links = '';
ddb9ae21
JV
319 if ($max) {
320 for ($i = 0; $i < $max; $i++) {
cdd843e5 321 $pfp_links .= '['. ($i + 1) .'] '. $urls[$i] ."<br />\n";
ddb9ae21 322 }
a623e75e 323 $links = variable_get('print_text_links', t('Links'));
9383096e 324 $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>";
ddb9ae21
JV
325 }
326 }
327
328 if (module_exists('taxonomy')) {
329 $terms = taxonomy_link('taxonomy terms', $node);
9383096e 330 $print['taxonomy'] = theme('links', $terms);
ddb9ae21
JV
331 }
332
9383096e
JV
333 $print['content'] = $node->body;
334 $print['node'] = $node;
335 $print['message'] = $message;
ddb9ae21
JV
336
337 return $print;
338}
339
340/**
623692c4 341 * Callback function for the preg_replace_callback for URL-capable patterns
ddb9ae21 342 *
95076d9f 343 * Manipulate URLs to make them absolute in the URLs list, and to add a
623692c4 344 * [n] footnote marker.
95076d9f 345 *
623692c4
JV
346 * @param $matches
347 * array with the matched tag patterns, usually <a...>+text+</a>
348 * @return
95076d9f 349 * tag with re-written URL and when appropriate the [n] index to the
623692c4 350 * URL list
ddb9ae21
JV
351 */
352function _print_rewrite_urls($matches) {
6ea54f45 353 global $base_url, $base_root, $_print_urls;
ddb9ae21 354
ddb9ae21 355 // first, split the html into the different tag attributes
9df2504f
JV
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 }
ddb9ae21 361
9383096e
JV
362 $size = count($attribs);
363 for ($i=1; $i < $size; $i++) {
ddb9ae21 364 // If the attribute is href or src, we may need to rewrite the URL in the value
9df2504f 365 if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
9383096e 366 $url = trim($urls[1], " \t\n\r\0\x0B\"'");
ddb9ae21 367
6d82f395 368 if (strpos($url, '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', $url)) {
ddb9ae21
JV
369 // URL is absolute, do nothing
370 $newurl = urldecode($url);
371 }
372 else {
9383096e 373 if ($url[0] == '#') {
ddb9ae21 374 // URL is an anchor tag
6ea54f45 375 if (!empty($_print_urls)) {
9383096e
JV
376 $path = explode('/', $_GET['q']);
377 unset($path[0]);
378 $path = implode('/', $path);
ddb9ae21
JV
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
9383096e 391 if ($url[0] == '/') {
ddb9ae21 392 // If it starts with '/' just append it to the server name
9383096e 393 $newurl = $base_root .'/'. trim(urldecode($url), '/');
ddb9ae21 394 }
6d82f395
JV
395 elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
396 // If it starts with ?q=, just prepend with the base URL
9383096e 397 $newurl = $base_url .'/'. trim(urldecode($url), '/');
ddb9ae21
JV
398 }
399 else {
9383096e 400 $newurl = url(trim(urldecode($url), '/'), NULL, NULL, TRUE);
ddb9ae21
JV
401 }
402 $matches[1] = str_replace($url, $newurl, $matches[1]);
403 }
404 }
405 }
406 }
407
ddb9ae21
JV
408 $ret = '<'. $matches[1] .'>';
409 if (count($matches) == 4) {
410 $ret .= $matches[2] . $matches[3];
6ea54f45 411 if ((!empty($_print_urls)) && (isset($newurl))) {
ddb9ae21
JV
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 *
623692c4
JV
422 * @param $url
423 * absolute URL to be inserted in the list
95076d9f
JV
424 * @return
425 * list of URLs previously stored if $url is 0, or the current count
623692c4 426 * otherwise.
ddb9ae21
JV
427 */
428function _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 }
9383096e
JV
440 $ret = $urls;
441 $urls = array();
442 return $ret;
ddb9ae21
JV
443}
444
445/**
623692c4 446 * Choose most appropriate template
95076d9f 447 *
ddb9ae21
JV
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 *
9383096e
JV
452 * @param format
453 * format of the PF page being rendered (html, pdf, etc.)
623692c4
JV
454 * @param $type
455 * name of the node type being rendered in a PF page
456 * @return
457 * filename of the most suitable template
ddb9ae21 458 */
9383096e 459function _print_get_template($format = NULL, $type = NULL) {
9f9f3f56 460 $filenames = array();
9383096e
JV
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 }
9f9f3f56
JV
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
9383096e 475 $filenames[] = 'print.tpl.php';
9f9f3f56
JV
476
477 foreach ($filenames as $value) {
ddb9ae21 478 // First in the theme directory
9383096e 479 $file = drupal_get_path('theme', $GLOBALS['theme_key']) .'/'. $value;
9f9f3f56
JV
480 if (file_exists($file)) {
481 return $file;
ddb9ae21
JV
482 }
483 // Then in the module directory
9383096e 484 $file = drupal_get_path('module', 'print') .'/'. $value;
9f9f3f56
JV
485 if (file_exists($file)) {
486 return $file;
ddb9ae21 487 }
ddb9ae21 488 }
ddb9ae21
JV
489}
490
ddb9ae21 491/**
6ea54f45
JV
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 */
501function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT) {
502 switch ($format) {
503 case PRINT_HTML_FORMAT:
6a20a915 504 $node_urllist = isset($node->print_display_urllist) ? $node->print_display_urllist : TRUE;
6ea54f45
JV
505 $fmt = '';
506 break;
507 case PRINT_MAIL_FORMAT:
6a20a915 508 $node_urllist = isset($node->print_mail_display_urllist) ? $node->print_mail_display_urllist : TRUE;
6ea54f45
JV
509 $fmt = $format .'_';
510 break;
511 case PRINT_PDF_FORMAT:
6a20a915 512 $node_urllist = isset($node->print_pdf_display_urllist) ? $node->print_pdf_display_urllist : TRUE;
6ea54f45
JV
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/**
623692c4 524 * Prepare a Printer-friendly-ready node body for content nodes
95076d9f 525 *
623692c4
JV
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
6ea54f45
JV
530 * @param $format
531 * format of the page being generated
9383096e
JV
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)
623692c4
JV
536 * @return
537 * filled array ready to be used in the template
ddb9ae21 538 */
6ea54f45
JV
539function _print_generate_node($nid, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
540 global $_print_urls;
541
ddb9ae21
JV
542 // We can take a node id
543 $node = node_load(array('nid' => $nid));
ce5a12cb
JV
544 if (!$node) {
545 // Node not found
546 drupal_not_found();
f0855c19 547 return FALSE;
ce5a12cb
JV
548 }
549 elseif (!node_access('view', $node)) {
ddb9ae21 550 // Access is denied
ce5a12cb 551 drupal_access_denied();
f0855c19 552 return FALSE;
ddb9ae21
JV
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
ddb9ae21 563 if ($cid === NULL) {
ce5a12cb 564 // Adapted (simplified) version of node_view
ddb9ae21 565 //Render the node content
9383096e 566 $node = node_build_content($node, $teaser, TRUE);
ddb9ae21 567 // Disable fivestar widget output
9383096e 568 unset($node->content['fivestar_widget']);
ddb9ae21 569 // Disable service links module output
9383096e 570 unset($node->content['service_links']);
95076d9f 571
ce5a12cb
JV
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 }
ddb9ae21
JV
581 }
582
9383096e 583 $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT);
ddb9ae21 584
9383096e 585 if (function_exists('comment_render') && (($cid != NULL) || ($print_comments))) {
ddb9ae21
JV
586 //Print only the requested comment (or if $cid is NULL, all of them)
587 $comments = comment_render($node, $cid);
95076d9f 588
ddb9ae21 589 //Remove the comment forms
9383096e 590 $comments = preg_replace('!<form.*?id="comment-.*?">.*?</form>!sim', '', $comments);
ddb9ae21 591 //Remove the 'Post new comment' title
b88aa721 592 $comments = preg_replace('!<h2.*?>'. t('Post new comment') .'</h2>!', '', $comments);
ddb9ae21 593 //Remove the comment title hyperlink
b88aa721 594 $comments = preg_replace('!(<h3.*?>.*?)<a.*?>(.*?)</a>(.*?</h3>)!i', '$1$2$3', $comments);
ddb9ae21 595 //Remove the comment author link
b88aa721 596 $pattern = '!(<(?:span|div) class="submitted">.*?)<a.*?>(.*?)</a>(.*?</(?:span|div)>)!sim';
ddb9ae21 597 if (preg_match($pattern, $comments)) {
b88aa721 598 $comments = preg_replace($pattern , '$1$2$3', $comments);
ddb9ae21
JV
599 }
600 //Remove the comment links
9383096e 601 $comments = preg_replace('!\s*<ul class="links">.*?</ul>!sim', '', $comments);
ddb9ae21
JV
602 if ($cid != NULL) {
603 // Single comment requested, output only the comment
604 unset($node->body);
605 }
606 $node->body .= $comments;
607 }
608
ce5a12cb 609 node_invoke_nodeapi($node, 'alter', $teaser, TRUE);
ddb9ae21 610
6a20a915
JV
611 if ($teaser) {
612 $node->body = $node->teaser;
613 unset($node->teaser);
614 }
615
6ea54f45
JV
616 //Check URL list settings
617 $_print_urls = _print_url_list_enabled($node, $format);
618
ddb9ae21 619 // Convert the a href elements
9383096e
JV
620 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
621 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
ddb9ae21
JV
622
623 init_theme();
624
9383096e 625 $print = _print_var_generator($node, $message, $cid);
ddb9ae21
JV
626
627 return $print;
628}
629
630/**
623692c4 631 * Prepare a Printer-friendly-ready node body for non-content pages
95076d9f 632 *
623692c4
JV
633 * @param $path
634 * path of the node to be rendered into a printer-friendly page
6ea54f45
JV
635 * @param $format
636 * format of the page being generated
9383096e
JV
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)
623692c4
JV
641 * @return
642 * filled array ready to be used in the template
ddb9ae21 643 */
6ea54f45
JV
644function _print_generate_path($path, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
645 global $_print_urls;
646
ddb9ae21
JV
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();
f2015a87 655 $node->title = drupal_get_title();
ddb9ae21
JV
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) {
f0855c19 660 return FALSE;
ddb9ae21
JV
661 }
662
9383096e
JV
663 if (is_int($node->body)) {
664 switch ($node->body) {
665 case MENU_NOT_FOUND:
ce5a12cb 666 drupal_not_found();
f0855c19 667 return FALSE;
9383096e
JV
668 break;
669 case MENU_ACCESS_DENIED:
ce5a12cb 670 drupal_access_denied();
f0855c19 671 return FALSE;
9383096e
JV
672 break;
673 }
ddb9ae21
JV
674 }
675
676 // Delete any links area
9383096e 677 $node->body = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body);
ddb9ae21 678
6ea54f45
JV
679 //Check URL list settings
680 $_print_urls = _print_url_list_enabled($node, $format);
681
ddb9ae21 682 // Convert the a href elements
9383096e
JV
683 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
684 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
ddb9ae21
JV
685
686 init_theme();
687
9383096e 688 $print = _print_var_generator($node, $message);
ddb9ae21
JV
689
690 return $print;
691}
692
693
694/**
623692c4 695 * Prepare a Printer-friendly-ready node body for book pages
95076d9f 696 *
623692c4
JV
697 * @param $nid
698 * node ID of the node to be rendered into a printer-friendly page
6ea54f45
JV
699 * @param $format
700 * format of the page being generated
9383096e
JV
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)
623692c4
JV
705 * @return
706 * filled array ready to be used in the template
ddb9ae21 707 */
6ea54f45
JV
708function _print_generate_book($nid, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
709 global $_print_urls;
710
ddb9ae21 711 $node = node_load(array('nid' => $nid));
ce5a12cb
JV
712 if (!$node) {
713 // Node not found
714 drupal_not_found();
f0855c19 715 return FALSE;
ce5a12cb
JV
716 }
717 elseif (!node_access('view', $node) || (!user_access('see printer-friendly version'))) {
ddb9ae21 718 // Access is denied
ce5a12cb 719 drupal_access_denied();
f0855c19 720 return FALSE;
ddb9ae21
JV
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
6ea54f45
JV
726 //Check URL list settings
727 $_print_urls = _print_url_list_enabled($node, $format);
728
ddb9ae21 729 // Convert the a href elements
9383096e
JV
730 $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
731 $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
ddb9ae21
JV
732
733 init_theme();
734
9383096e 735 $print = _print_var_generator($node, $message);
ddb9ae21 736 // The title is already displayed by the book_recurse, so avoid duplication
9383096e 737 $print['title'] = '';
ddb9ae21
JV
738
739 return $print;
740}
741
742/**
623692c4 743 * Generates printer-friendly HTML for a node. This function is a 'pre-node' visitor function for book_recurse()
95076d9f 744 *
ddb9ae21
JV
745 * My own version of book_node_visitor_html_pre so that CCK pages in book nodes
746 * come out right
623692c4
JV
747 *
748 * @param $node
749 * the node to generate output for.
750 * @param $depth
95076d9f 751 * the depth of the given node in the hierarchy. This is used only for
623692c4
JV
752 * generating output.
753 * @param $nid
95076d9f 754 * the node id (nid) of the given node. This is used only for generating
623692c4
JV
755 * output.
756 * @return
757 * the HTML generated for the given node.
ddb9ae21
JV
758 */
759function _print_node_visitor_html_pre($node, $depth, $nid) {
760 $node = node_build_content($node, FALSE, TRUE);
761
9383096e 762 unset($node->content['book_navigation']);
ddb9ae21 763
9383096e
JV
764 $output .= "<div id='node-$node->nid' class='section-$depth'>\n";
765 $output .= "<h1 class='book-heading'>". check_plain($node->title) ."</h1>\n";
ddb9ae21
JV
766 $output .= drupal_render($node->content);
767
768 return $output;
769}