print.pages.inc 22 KB
Newer Older
jcnventura's avatar
jcnventura committed
1 2
<?php

jcnventura's avatar
jcnventura committed
3 4 5 6 7 8 9
/**
 * @file
 * Contains the functions to generate Printer-friendly pages.
 *
 * This file is included by the core PF module, and includes all the
 * functions necessary to generate a PF version of the original page
 * in HTML format.
10 11
 *
 * @ingroup print
jcnventura's avatar
jcnventura committed
12
 */
jcnventura's avatar
jcnventura committed
13

jcnventura's avatar
jcnventura committed
14 15
$_print_urls = PRINT_URLS_DEFAULT;

jcnventura's avatar
jcnventura committed
16 17 18 19 20
/**
 * Generate an HTML version of the printer-friendly page
 *
 * @see print_controller()
 */
jcnventura's avatar
jcnventura committed
21
function print_controller_html() {
22 23
  $args = func_get_args();
  $path = implode('/', $args);
24
  $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
25

jcnventura's avatar
jcnventura committed
26
  $print = print_controller($path, $cid, PRINT_HTML_FORMAT);
27 28
  if ($print !== FALSE) {
    $node = $print['node'];
29
    $html = theme('print', array('print' => $print, 'type' => PRINT_HTML_FORMAT, 'node' => $node));
30
    drupal_send_headers();
31
    print $html;
jcnventura's avatar
jcnventura committed
32

33
    $nodepath = (isset($node->path) && is_string($node->path)) ? drupal_get_normal_path($node->path) : 'node/' . $path;
jcnventura's avatar
jcnventura committed
34 35 36 37 38 39
    db_merge('print_page_counter')
      ->key(array('path' => $nodepath))
      ->fields(array(
          'totalcount' => 1,
          'timestamp' => REQUEST_TIME,
      ))
40
      ->expression('totalcount', 'totalcount + 1')
jcnventura's avatar
jcnventura committed
41
      ->execute();
42
  }
jcnventura's avatar
jcnventura committed
43 44 45
}

/**
46
 * Select the print generator function based on the page type
jcnventura's avatar
jcnventura committed
47 48 49 50
 *
 * Depending on the type of node, this functions chooses the appropriate
 * generator function.
 *
51 52 53 54
 * @param $path
 *   path of the original page
 * @param $cid
 *   comment ID of the individual comment to be rendered
jcnventura's avatar
jcnventura committed
55 56
 * @param $format
 *   format of the page being generated
57 58
 * @param $teaser
 *   if set to TRUE, outputs only the node's teaser
59 60
 * @param $message
 *   optional sender's message (used by the send e-mail module)
jcnventura's avatar
jcnventura committed
61 62 63 64 65
 * @return
 *   array with the fields to be used in the template
 * @see _print_generate_node()
 * @see _print_generate_path()
 * @see _print_generate_book()
jcnventura's avatar
jcnventura committed
66
 */
jcnventura's avatar
jcnventura committed
67
function print_controller($path, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
68 69 70 71 72 73
  if (empty($path)) {
    // If no path was provided, let's try to generate a page for the referer
    global $base_url;

    $ref = $_SERVER['HTTP_REFERER'];
    $path = preg_replace("!^$base_url/!", '', $ref);
74 75
    if (($path === $ref) || empty($path)) {
      $path = variable_get('site_frontpage', 'node');
76 77
    }
  }
78
  if ($alias = drupal_lookup_path('source', $path)) {
jcnventura's avatar
jcnventura committed
79 80
    // Indirect call with print/alias
    // If there is a path alias with these arguments, generate a printer-friendly version for it
81
    $path = $alias;
jcnventura's avatar
jcnventura committed
82
  }
83
  $parts = explode('/', $path);
84
  if (($parts[0] == 'node') && (count($parts) > 1)) {
85 86 87
    array_shift($parts);
    $path = implode('/', $parts);
  }
88
  if (ctype_digit($parts[0]) && (count($parts) == 1)) {
jcnventura's avatar
jcnventura committed
89
    $print = _print_generate_node($path, $cid, $format, $teaser, $message);
jcnventura's avatar
jcnventura committed
90 91
  }
  else {
92
    $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
jcnventura's avatar
jcnventura committed
93 94
    if ($ret == 1) {
      // This is a book PF page link, handle trough the book handling functions
jcnventura's avatar
jcnventura committed
95
      $print = _print_generate_book($matches[1], $format, $teaser, $message);
jcnventura's avatar
jcnventura committed
96 97 98
    }
    else {
      // If no content node was found, handle the page printing with the 'printable' engine
jcnventura's avatar
jcnventura committed
99
      $print = _print_generate_path($path, $format, $teaser, $message);
jcnventura's avatar
jcnventura committed
100 101 102 103 104 105 106
    }
  }

  return $print;
}

/**
jcnventura's avatar
jcnventura committed
107
 * Generates a robots meta tag to tell them what they may index
jcnventura's avatar
jcnventura committed
108
 *
jcnventura's avatar
jcnventura committed
109
 * @return
jcnventura's avatar
jcnventura committed
110
 *   string with the meta robots tag
jcnventura's avatar
jcnventura committed
111 112
 */
function _print_robots_meta_generator() {
113 114 115
  $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
  $print_robots_nofollow = variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT);
  $print_robots_noarchive = variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT);
jcnventura's avatar
jcnventura committed
116 117
  $robots_meta = array();

118
  if (!empty($print_robots_noindex)) {
jcnventura's avatar
jcnventura committed
119 120
    $robots_meta[] = 'noindex';
  }
121
  if (!empty($print_robots_nofollow)) {
jcnventura's avatar
jcnventura committed
122 123
    $robots_meta[] = 'nofollow';
  }
124
  if (!empty($print_robots_noarchive)) {
jcnventura's avatar
jcnventura committed
125 126 127
    $robots_meta[] = 'noarchive';
  }

128 129
  if (count($robots_meta) > 0) {
    $robots_meta = implode(', ', $robots_meta);
130
    $robots_meta = "<meta name='robots' content='$robots_meta' />\n";
jcnventura's avatar
jcnventura committed
131 132 133 134 135 136 137 138 139
  }
  else {
    $robots_meta = '';
  }

  return $robots_meta;
}

/**
jcnventura's avatar
jcnventura committed
140
 * Post-processor that fills the array for the template with common details
jcnventura's avatar
jcnventura committed
141
 *
jcnventura's avatar
jcnventura committed
142 143
 * @param $node
 *   generated node with a printer-friendly node body
144 145
 * @param $message
 *   optional sender's message (used by the send e-mail module)
jcnventura's avatar
jcnventura committed
146
 * @param $cid
jcnventura's avatar
jcnventura committed
147
 *   id of current comment being generated (NULL when not generating
jcnventura's avatar
jcnventura committed
148 149 150
 *   an individual comment)
 * @return
 *   array with the fields to be used in the template
jcnventura's avatar
jcnventura committed
151
 */
152
function _print_var_generator($node, $message = NULL, $cid = NULL) {
jcnventura's avatar
jcnventura committed
153
  global $base_url, $language, $_print_urls;
jcnventura's avatar
jcnventura committed
154 155 156 157

  $path = empty($node->nid) ? $node->path : "node/$node->nid";

  // 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 165 166
  $print_sourceurl_enabled = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT);
  $print_sourceurl_forcenode = variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT);
  $print_sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT);
167 168
  $print_footer_options = variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT);
  $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['footer_scripts'] = drupal_get_js('footer');
178 179
  $print['robots_meta'] = _print_robots_meta_generator();
  $print['url'] = url($path, array('absolute' => TRUE));
jcnventura's avatar
jcnventura committed
180 181
  $print['base_href'] = "<base href='" . $print['url'] . "' />\n";
  $print['favicon'] = theme_get_setting('toggle_favicon') ? "<link rel='shortcut icon' href='" . theme_get_setting('favicon') . "' type='image/x-icon' />\n" : '';
jcnventura's avatar
jcnventura committed
182

183
  if (!empty($print_css)) {
184
    drupal_add_css(strtr($print_css, array('%t' => path_to_theme())));
jcnventura's avatar
jcnventura committed
185 186
  }
  else {
jcnventura's avatar
jcnventura committed
187
    drupal_add_css(drupal_get_path('module', 'print') . '/css/print.css');
jcnventura's avatar
jcnventura committed
188
  }
jcnventura's avatar
jcnventura committed
189
  $drupal_css = drupal_add_css();
190 191
  foreach ($drupal_css as $key => $css_file) {
    if ($css_file['group'] == CSS_THEME) {
jcnventura's avatar
jcnventura committed
192
    // Unset the theme's CSS
193 194
      unset($drupal_css[$key]);
    }
195 196 197 198
  }

  // If we are sending a message via e-mail, the CSS must be embedded
  if (!empty($message)) {
199
    $style = '';
200
    $css_files = array_keys($drupal_css);
jcnventura's avatar
jcnventura committed
201
    foreach ($css_files as $filename) {
jcnventura's avatar
jcnventura committed
202
      $res = file_exists($filename) ? file_get_contents($filename, TRUE) : FALSE;
203 204 205
      if ($res != FALSE) {
        $style .= $res;
      }
206
    }
207
    $print['css'] = "<style type='text/css' media='all'>$style</style>\n";
208 209
  }
  else {
jcnventura's avatar
jcnventura committed
210
    $print['css'] = drupal_get_css($drupal_css);
211
  }
jcnventura's avatar
jcnventura committed
212

213
  $window_close = ($print_html_new_window && $print_html_windowclose) ? 'window.close();' : '';
214
  $print['sendtoprinter'] = $print_html_sendtoprinter ? "<script type='text/javascript'>(function ($) { Drupal.behaviors.print = { attach: function(context) {window.print();$window_close}};}(jQuery));</script>" : '';
jcnventura's avatar
jcnventura committed
215

216 217 218 219 220 221 222 223 224 225 226
  switch ($print_logo_options) {
    case 0: // none
      $logo_url = 0;
      break;
    case 1: // theme's
      $logo_url = theme_get_setting('logo');
      break;
    case 2: // user-specifed
      $logo_url = strip_tags($print_logo_url);
      break;
  }
227 228
  $site_name = variable_get('site_name', 'Drupal');
  $print['logo'] = $logo_url ? "<img class='print-logo' src='" . check_url($logo_url) . "' alt='$site_name' id='logo' />\n" : '';
229

230 231 232 233 234
  switch ($print_footer_options) {
    case 0: // none
      $footer = '';
      break;
    case 1: // theme's
235 236
      $footer_blocks = block_get_blocks_by_region('footer');
      $footer = variable_get('site_footer', FALSE) . "\n" . drupal_render($footer_blocks);
237 238 239 240 241
      break;
    case 2: // user-specifed
      $footer = $print_footer_user;
      break;
  }
242
  $print['footer_message'] = filter_xss_admin($footer);
243

244
  $published_site = variable_get('site_name', 0);
245
  if ($published_site) {
246
    $print_text_published = filter_xss(variable_get('print_text_published', t('Published on %site_name')));
jcnventura's avatar
jcnventura committed
247
    $published = t($print_text_published, array('%site_name' => $published_site));
jcnventura's avatar
jcnventura committed
248
    $print['site_name'] = $published . ' (' . l($base_url, $base_url) . ')';
249 250
  }
  else {
251
    $print['site_name'] = '';
252
  }
jcnventura's avatar
jcnventura committed
253

254
  if ($print_sourceurl_enabled == 1) {
255
    /* Grab and format the src URL */
256 257
    if (empty($print_sourceurl_forcenode)) {
      $url = $print['url'];
258 259
    }
    else {
jcnventura's avatar
jcnventura committed
260
      $url = $base_url . '/' . (((bool)variable_get('clean_url', '0')) ? '' : '?q=') . $path;
261
    }
262
    if (is_int($cid)) {
263
      $url .= '#comment-$cid';
264
    }
jcnventura's avatar
jcnventura committed
265
    $retrieved_date = format_date(REQUEST_TIME, 'short');
266
    $print_text_retrieved = filter_xss(variable_get('print_text_retrieved', t('retrieved on %date')));
jcnventura's avatar
jcnventura committed
267
    $retrieved = t($print_text_retrieved, array('%date' => $retrieved_date));
268
    $print['printdate'] = $print_sourceurl_date ? " ($retrieved)" : '';
269

270
    $source_url = filter_xss(variable_get('print_text_source_url', t('Source URL')));
jcnventura's avatar
jcnventura committed
271
    $print['source_url'] = '<strong>' . $source_url . $print['printdate'] . ':</strong> ' . l($url, $url);
jcnventura's avatar
jcnventura committed
272 273
  }
  else {
274
    $print['source_url'] = '';
jcnventura's avatar
jcnventura committed
275 276
  }

277
  $print['type'] = (isset($node->type)) ? $node->type : '';
jcnventura's avatar
jcnventura committed
278

jcnventura's avatar
jcnventura committed
279 280 281 282
  menu_set_active_item($path);
  $breadcrumb = drupal_get_breadcrumb();
  if (!empty($breadcrumb)) {
    $breadcrumb[] = menu_get_active_title();
283
    $print['breadcrumb'] = filter_xss(implode(' > ', $breadcrumb));
jcnventura's avatar
jcnventura committed
284 285
  }
  else {
286
    $print['breadcrumb'] = '';
jcnventura's avatar
jcnventura committed
287 288 289
  }

  // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
jcnventura's avatar
jcnventura committed
290
  $print['pfp_links'] = '';
jcnventura's avatar
jcnventura committed
291
  if (!empty($_print_urls)) {
jcnventura's avatar
jcnventura committed
292 293
    $urls = _print_friendly_urls();
    $max = count($urls);
294
    $pfp_links = '';
jcnventura's avatar
jcnventura committed
295 296
    if ($max) {
      for ($i = 0; $i < $max; $i++) {
297
        $pfp_links .= '[' . ($i + 1) . '] ' . check_plain($urls[$i]) . "<br />\n";
jcnventura's avatar
jcnventura committed
298
      }
299
      $links = filter_xss(variable_get('print_text_links', t('Links')));
300
      $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>";
jcnventura's avatar
jcnventura committed
301 302 303
    }
  }

304 305
  $print['node'] = $node;
  $print['message'] = $message;
jcnventura's avatar
jcnventura committed
306 307 308 309 310

  return $print;
}

/**
jcnventura's avatar
jcnventura committed
311
 * Callback function for the preg_replace_callback for URL-capable patterns
jcnventura's avatar
jcnventura committed
312
 *
jcnventura's avatar
jcnventura committed
313
 * Manipulate URLs to make them absolute in the URLs list, and to add a
jcnventura's avatar
jcnventura committed
314
 * [n] footnote marker.
jcnventura's avatar
jcnventura committed
315
 *
jcnventura's avatar
jcnventura committed
316 317 318
 * @param $matches
 *   array with the matched tag patterns, usually <a...>+text+</a>
 * @return
jcnventura's avatar
jcnventura committed
319
 *   tag with re-written URL and when appropriate the [n] index to the
jcnventura's avatar
jcnventura committed
320
 *   URL list
jcnventura's avatar
jcnventura committed
321 322
 */
function _print_rewrite_urls($matches) {
jcnventura's avatar
jcnventura committed
323
  global $base_url, $base_root, $_print_urls;
jcnventura's avatar
jcnventura committed
324

325 326
  $include_anchors = variable_get('print_urls_anchors', PRINT_URLS_ANCHORS_DEFAULT);

jcnventura's avatar
jcnventura committed
327
  // first, split the html into the different tag attributes
328
  $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
329 330
  $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  foreach ($attribs as $key => $value) {
331
    $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
332
  }
jcnventura's avatar
jcnventura committed
333

334 335
  $size = count($attribs);
  for ($i=1; $i < $size; $i++) {
jcnventura's avatar
jcnventura committed
336
    // If the attribute is href or src, we may need to rewrite the URL in the value
337
    if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
338
      $url = trim($urls[1], " \t\n\r\0\x0B\"'");
jcnventura's avatar
jcnventura committed
339

340
      if (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', html_entity_decode($url))) {
jcnventura's avatar
jcnventura committed
341
        // URL is absolute, do nothing
342
        $newurl = $url;
jcnventura's avatar
jcnventura committed
343 344
      }
      else {
345
        if ($url[0] == '#') {
jcnventura's avatar
jcnventura committed
346
          // URL is an anchor tag
347
          if ($include_anchors && (!empty($_print_urls))) {
348 349 350
            $path = explode('/', $_GET['q']);
            unset($path[0]);
            $path = implode('/', $path);
351
            if (ctype_digit($path)) {
jcnventura's avatar
jcnventura committed
352 353 354
              $path = "node/$path";
            }
            // Printer-friendly URLs is on, so we need to make it absolute
jcnventura's avatar
jcnventura committed
355
            $newurl = url($path, array('fragment' => drupal_substr($url, 1), 'absolute' => TRUE));
jcnventura's avatar
jcnventura committed
356 357 358
          }
          // Because base href is the original page, change the link to
          // still be usable inside the print page
359
          $matches[1] = str_replace($url, base_path() . $_GET['q'] . $url, $matches[1]);
jcnventura's avatar
jcnventura committed
360 361 362
        }
        else {
          // URL is relative, convert it into absolute URL
363
          if ($url[0] == '/') {
jcnventura's avatar
jcnventura committed
364
            // If it starts with '/' just append it to the server name
365
            $newurl = $base_root . '/' . trim($url, '/');
jcnventura's avatar
jcnventura committed
366
          }
367 368
          elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
            // If it starts with ?q=, just prepend with the base URL
369
            $newurl = $base_url . '/' . trim($url, '/');
jcnventura's avatar
jcnventura committed
370 371
          }
          else {
372
            $newurl = url(trim($url, '/'), array('absolute' => TRUE));
jcnventura's avatar
jcnventura committed
373
          }
374
          $matches[1] = str_replace($url, $newurl, $matches[1]);
jcnventura's avatar
jcnventura committed
375 376 377 378 379
        }
      }
    }
  }

jcnventura's avatar
jcnventura committed
380
  $ret = '<' . $matches[1] . '>';
jcnventura's avatar
jcnventura committed
381 382
  if (count($matches) == 4) {
    $ret .= $matches[2] . $matches[3];
jcnventura's avatar
jcnventura committed
383
    if ((!empty($_print_urls)) && (isset($newurl))) {
384
      $ret .= ' <span class="print-footnote">[' . _print_friendly_urls(trim($newurl)) . ']</span>';
jcnventura's avatar
jcnventura committed
385 386 387 388 389 390 391 392 393
    }
  }

  return $ret;
}

/**
 * Auxiliary function to store the Printer-friendly URLs list as static.
 *
jcnventura's avatar
jcnventura committed
394 395
 * @param $url
 *   absolute URL to be inserted in the list
jcnventura's avatar
jcnventura committed
396 397
 * @return
 *   list of URLs previously stored if $url is 0, or the current count
jcnventura's avatar
jcnventura committed
398
 *   otherwise.
jcnventura's avatar
jcnventura committed
399 400 401 402 403 404 405 406 407 408 409 410 411
 */
function _print_friendly_urls($url = 0) {
  static $urls = array();
  if ($url) {
    $url_idx = array_search($url, $urls);
    if ($url_idx !== FALSE) {
      return ($url_idx + 1);
    }
    else {
      $urls[] = $url;
      return count($urls);
    }
  }
jcnventura's avatar
jcnventura committed
412 413 414
  $ret = $urls;
  $urls = array();
  return $ret;
jcnventura's avatar
jcnventura committed
415 416
}

jcnventura's avatar
jcnventura committed
417 418 419 420 421 422 423 424 425 426 427
/**
 * Check URL list settings for this node
 *
 * @param node
 *   node object
 * @param $format
 *   format of the page being generated
 * @return
 *   TRUE if URL list should be displayed, FALSE otherwise
 */
function _print_url_list_enabled($node, $format = PRINT_HTML_FORMAT) {
428
  if (!isset($node->type)) {
429 430 431 432 433 434 435 436 437 438 439 440 441
    switch ($format) {
      case PRINT_HTML_FORMAT:
        $node_urllist = variable_get('print_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
        break;
      case PRINT_MAIL_FORMAT:
        $node_urllist = variable_get('print_mail_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
        break;
      case PRINT_PDF_FORMAT:
        $node_urllist = variable_get('print_pdf_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
        break;
      default:
        $node_urllist = PRINT_TYPE_SYS_URLLIST_DEFAULT;
    }
442
  }
443 444 445 446 447 448 449 450 451 452 453 454 455 456
  else {
    switch ($format) {
      case PRINT_HTML_FORMAT:
        $node_urllist = isset($node->print_display_urllist) ? $node->print_display_urllist : variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
        break;
      case PRINT_MAIL_FORMAT:
        $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);
        break;
      case PRINT_PDF_FORMAT:
        $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);
        break;
      default:
        $node_urllist = PRINT_TYPE_URLLIST_DEFAULT;
    }
jcnventura's avatar
jcnventura committed
457 458 459
  }

  // Get value of Printer-friendly URLs setting
460
  return (variable_get('print_urls', PRINT_URLS_DEFAULT) && ($node_urllist));
jcnventura's avatar
jcnventura committed
461 462
}

jcnventura's avatar
jcnventura committed
463
/**
jcnventura's avatar
jcnventura committed
464
 * Prepare a Printer-friendly-ready node body for content nodes
jcnventura's avatar
jcnventura committed
465
 *
jcnventura's avatar
jcnventura committed
466 467 468 469
 * @param $nid
 *   node ID of the node to be rendered into a printer-friendly page
 * @param $cid
 *   comment ID of the individual comment to be rendered
jcnventura's avatar
jcnventura committed
470 471
 * @param $format
 *   format of the page being generated
472 473
 * @param $teaser
 *   if set to TRUE, outputs only the node's teaser
474 475
 * @param $message
 *   optional sender's message (used by the send e-mail module)
jcnventura's avatar
jcnventura committed
476 477
 * @return
 *   filled array ready to be used in the template
jcnventura's avatar
jcnventura committed
478
 */
jcnventura's avatar
jcnventura committed
479 480
function _print_generate_node($nid, $cid = NULL, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
  global $_print_urls;
481 482 483 484

  if (!isset($langcode)) {
    $langcode = $GLOBALS['language_content']->language;
  }
jcnventura's avatar
jcnventura committed
485

jcnventura's avatar
jcnventura committed
486
  // We can take a node id
487
  $node = node_load($nid);
488 489 490
  if (!$node) {
    // Node not found
    drupal_not_found();
491
    return FALSE;
492 493
  }
  elseif (!node_access('view', $node)) {
jcnventura's avatar
jcnventura committed
494
    // Access is denied
495
    drupal_access_denied();
496
    return FALSE;
jcnventura's avatar
jcnventura committed
497
  }
498
  drupal_set_title($node->title);
jcnventura's avatar
jcnventura committed
499

500 501
  $view_mode = $teaser ? 'teaser' : 'print';

jcnventura's avatar
jcnventura committed
502 503 504 505
  // Turn off Pagination by the Paging module
  unset($node->pages);
  unset($node->page_count);

506 507 508 509 510
  // Make this page a member of the original page's organic group
  if (function_exists('og_set_group_context') && isset($node->og_groups)) {
    og_set_group_context($node->og_groups);
  }

jcnventura's avatar
jcnventura committed
511
  if ($cid === NULL) {
512
    // Adapted (simplified) version of node_view
jcnventura's avatar
jcnventura committed
513
    //Render the node content
514 515
    node_build_content($node, $view_mode);

516 517
    // Disable the links area
    unset($node->content['links']);
jcnventura's avatar
jcnventura committed
518
    // Disable fivestar widget output
519
    unset($node->content['fivestar_widget']);
jcnventura's avatar
jcnventura committed
520
    // Disable service links module output
521
    unset($node->content['service_links']);
jcnventura's avatar
jcnventura committed
522

523 524
    $build = $node->content;
    unset($node->content);
jcnventura's avatar
jcnventura committed
525 526
  }

527
  $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT);
jcnventura's avatar
jcnventura committed
528

529
  if (function_exists('comment_node_page_additions') && (($cid != NULL) || ($print_comments))) {
jcnventura's avatar
jcnventura committed
530
    //Print only the requested comment (or if $cid is NULL, all of them)
531 532 533 534 535

    $comments = comment_node_page_additions($node);
    unset($comments['comment_form']);
    foreach ($comments['comments'] as &$comment) {
      unset($comment['links']);
jcnventura's avatar
jcnventura committed
536
    }
537 538

    $build['comments'] = $comments;
jcnventura's avatar
jcnventura committed
539
  }
jcnventura's avatar
jcnventura committed
540

541
  $build += array(
542
    '#theme' => 'node',
543 544
    '#node' => $node,
    '#view_mode' => $view_mode,
545
    '#language' => $langcode,
546
    '#print_format' => $format,
547
  );
548

549 550
  $type = 'node';
  drupal_alter(array('node_view', 'entity_view'), $build, $type);
jcnventura's avatar
jcnventura committed
551

552 553
  $content = render($build);

554 555 556 557
  //Get rid of the annoying D7 title link
  $pattern = '!(<h2 property="dc:title" datatype="">)\s*<a [^>]*?>(.*?)</a>\s*(</h2>)!mis';
  $content = preg_replace($pattern, '$1$2$3', $content);

jcnventura's avatar
jcnventura committed
558 559 560
  //Check URL list settings
  $_print_urls = _print_url_list_enabled($node, $format);

jcnventura's avatar
jcnventura committed
561
  // Convert the a href elements
562
  $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
563
  $content = preg_replace_callback($pattern, '_print_rewrite_urls', $content);
jcnventura's avatar
jcnventura committed
564

565
  $print = _print_var_generator($node, $message, $cid);
566
  $print['content'] = $content;
jcnventura's avatar
jcnventura committed
567 568 569 570 571

  return $print;
}

/**
jcnventura's avatar
jcnventura committed
572
 * Prepare a Printer-friendly-ready node body for non-content pages
jcnventura's avatar
jcnventura committed
573
 *
jcnventura's avatar
jcnventura committed
574 575
 * @param $path
 *   path of the node to be rendered into a printer-friendly page
jcnventura's avatar
jcnventura committed
576 577
 * @param $format
 *   format of the page being generated
578 579 580 581
 * @param $teaser
 *   if set to TRUE, outputs only the node's teaser
 * @param $message
 *   optional sender's message (used by the send e-mail module)
jcnventura's avatar
jcnventura committed
582 583
 * @return
 *   filled array ready to be used in the template
jcnventura's avatar
jcnventura committed
584
 */
jcnventura's avatar
jcnventura committed
585 586 587
function _print_generate_path($path, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
  global $_print_urls;

588 589
  // Handle node tabs
  $parts = explode('/', $path);
590
  if (ctype_digit($parts[0]) && (count($parts) > 1)) {
591 592 593
    $path = 'node/' . $path;
  }

jcnventura's avatar
jcnventura committed
594 595 596 597 598
  $path = drupal_get_normal_path($path);

  menu_set_active_item($path);
  // Adapted from index.php.
  $node = new stdClass();
599
  $node->body = menu_execute_active_handler($path, FALSE);
600 601 602
  if (is_array($node->body)) {
    $node->body = drupal_render($node->body);
  }
jcnventura's avatar
jcnventura committed
603

604 605 606
  if (is_int($node->body)) {
    switch ($node->body) {
      case MENU_NOT_FOUND:
607
        drupal_not_found();
608
        return FALSE;
609 610
        break;
      case MENU_ACCESS_DENIED:
611
        drupal_access_denied();
612
        return FALSE;
613 614
        break;
    }
jcnventura's avatar
jcnventura committed
615 616
  }

617 618
  $node->title = drupal_get_title();
  $node->path = $path;
619
  $node->changed = 0;
620

jcnventura's avatar
jcnventura committed
621
  // Delete any links area
622
  $node->body = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body);
jcnventura's avatar
jcnventura committed
623

jcnventura's avatar
jcnventura committed
624 625 626
  //Check URL list settings
  $_print_urls = _print_url_list_enabled($node, $format);

jcnventura's avatar
jcnventura committed
627
  // Convert the a href elements
628 629
  $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
  $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
jcnventura's avatar
jcnventura committed
630

631
  $print = _print_var_generator($node, $message);
632
  $print['content'] = $node->body;
jcnventura's avatar
jcnventura committed
633 634 635 636 637 638

  return $print;
}


/**
jcnventura's avatar
jcnventura committed
639
 * Prepare a Printer-friendly-ready node body for book pages
jcnventura's avatar
jcnventura committed
640
 *
jcnventura's avatar
jcnventura committed
641 642
 * @param $nid
 *   node ID of the node to be rendered into a printer-friendly page
jcnventura's avatar
jcnventura committed
643 644
 * @param $format
 *   format of the page being generated
645 646 647 648
 * @param $teaser
 *   if set to TRUE, outputs only the node's teaser
 * @param $message
 *   optional sender's message (used by the send e-mail module)
jcnventura's avatar
jcnventura committed
649 650
 * @return
 *   filled array ready to be used in the template
jcnventura's avatar
jcnventura committed
651
 */
jcnventura's avatar
jcnventura committed
652 653 654
function _print_generate_book($nid, $format = PRINT_HTML_FORMAT, $teaser = FALSE, $message = NULL) {
  global $_print_urls;

655
  $node = node_load($nid);
656 657 658
  if (!$node) {
    // Node not found
    drupal_not_found();
659
    return FALSE;
660 661
  }
  elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
jcnventura's avatar
jcnventura committed
662
    // Access is denied
663
    drupal_access_denied();
664
    return FALSE;
jcnventura's avatar
jcnventura committed
665 666 667 668 669
  }

  $tree = book_menu_subtree_data($node->book);
  $node->body = book_export_traverse($tree, 'book_node_export');

jcnventura's avatar
jcnventura committed
670 671 672
  //Check URL list settings
  $_print_urls = _print_url_list_enabled($node, $format);

jcnventura's avatar
jcnventura committed
673
  // Convert the a href elements
674 675
  $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
  $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body);
jcnventura's avatar
jcnventura committed
676

677
  $print = _print_var_generator($node, $message);
678 679
  $print['content'] = $node->body;

jcnventura's avatar
jcnventura committed
680
  // The title is already displayed by the book_recurse, so avoid duplication
681
  $print['title'] = '';
jcnventura's avatar
jcnventura committed
682 683 684

  return $print;
}