Skip to content
print.pages.inc 23.5 KiB
Newer Older
João Ventura's avatar
João Ventura committed
<?php

João Ventura's avatar
João Ventura committed
/**
 * @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.
João Ventura's avatar
João Ventura committed

$_print_urls = PRINT_URLS_DEFAULT;

João Ventura's avatar
João Ventura committed
/**
 * Generate an HTML version of the printer-friendly page
 *
 * @see print_controller()
 */
João Ventura's avatar
João Ventura committed
function print_controller_html() {
  $path = filter_xss(implode('/', $args));
  $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL;
João Ventura's avatar
João Ventura committed
  $link = print_print_link();
  $node = print_controller($path, $link['format'], $cid);
  if ($node) {
    // Handle the query
    $query = $_GET;
    unset($query['q']);

    $html = theme('print', array('node' => $node, 'query' => $query, 'format' => $link['format']));
    drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
    drupal_send_headers();
    $nodepath = (isset($node->nid)) ? 'node/' . $node->nid : drupal_get_normal_path($path);
    db_merge('print_page_counter')
      ->key(array('path' => $nodepath))
      ->fields(array(
João Ventura's avatar
João Ventura committed
        'totalcount' => 1,
        'timestamp' => REQUEST_TIME,
      ->expression('totalcount', 'totalcount + 1')
      ->execute();
João Ventura's avatar
João Ventura committed
}

/**
 * Select the print generator function based on the page type
João Ventura's avatar
João Ventura committed
 *
 * Depending on the type of node, this functions chooses the appropriate
 * generator function.
 *
João Ventura's avatar
João Ventura committed
 * @param string $path
João Ventura's avatar
João Ventura committed
 * @param string $format
 *   format of the page being generated
João Ventura's avatar
João Ventura committed
 * @param int $cid
João Ventura's avatar
João Ventura committed
 *   comment ID of the individual comment to be rendered
 * @param string $view_mode
 *   (optional) view mode to be used when rendering the content
João Ventura's avatar
João Ventura committed
 *
 * @return object
 *   node-like object to be used in the print template
 *
João Ventura's avatar
João Ventura committed
 * @see _print_generate_node()
 * @see _print_generate_path()
 * @see _print_generate_book()
João Ventura's avatar
João Ventura committed
 * @see print_preprocess_print()
João Ventura's avatar
João Ventura committed
 */
function print_controller($path, $format, $cid = NULL, $view_mode = PRINT_VIEW_MODE) {
  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);
    if (($path === $ref) || empty($path)) {
      $path = variable_get('site_frontpage', 'node');
  if ($alias = drupal_lookup_path('source', $path)) {
João Ventura's avatar
João Ventura committed
    // Indirect call with print/alias
    // If there is a path alias with these arguments, generate a printer-friendly version for it
João Ventura's avatar
João Ventura committed
  }
  $parts = explode('/', $path);
  if (($parts[0] == 'node') && (count($parts) > 1) && ctype_digit($parts[1])) {
    array_shift($parts);
    $path = implode('/', $parts);
  }
  $revision_view = preg_match('!^[\d]*/revisions/[\d]*/view$!', $path);
  if (ctype_digit($parts[0]) && ((count($parts) == 1) || $revision_view)) {
    $vid = $revision_view ? $parts[2] : NULL;
    $node = _print_generate_node($path, $format, $vid, $cid, $view_mode);
João Ventura's avatar
João Ventura committed
  }
  else {
    $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
João Ventura's avatar
João Ventura committed
    if ($ret == 1) {
      // This is a book PF page link, handle trough the book handling functions
João Ventura's avatar
João Ventura committed
      $node = _print_generate_book($matches[1], $format);
João Ventura's avatar
João Ventura committed
    }
    else {
      // If no content node was found, handle the page printing with the 'printable' engine
João Ventura's avatar
João Ventura committed
      $node = _print_generate_path($path, $format);
  return $node;
}

/**
 * Implements hook_preprocess_HOOK().
 */
function print_preprocess_print(&$variables) {
  global $language;

  $node = $variables['node'];
  $format = $variables['format'];
  $path = drupal_get_path_alias(empty($node->nid) ? $node->path : "node/$node->nid");

  static $hooks = NULL;
  if (!isset($hooks)) {
    drupal_theme_initialize();
    $hooks = theme_get_registry();
  }

  $variables['page']['#show_messages'] = FALSE;

  // Stolen from theme() so that ALL preprocess functions are called
  $hook = 'page';
  $info = $hooks[$hook];
  if (isset($info['preprocess functions']) || isset($info['process functions'])) {
    $variables['theme_hook_suggestions'] = array();
    foreach (array('preprocess functions', 'process functions') as $phase) {
      if (!empty($info[$phase])) {
        foreach ($info[$phase] as $processor_function) {
          if (function_exists($processor_function)) {
            // We don't want a poorly behaved process function changing $hook.
            $hook_clone = $hook;
            $processor_function($variables, $hook_clone);
          }
        }
      }
    }
  }

  $logo_url = FALSE;
  switch (variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT)) {
    case 1: // theme's
      $logo_url = theme_get_setting('logo');
      break;
    case 2: // user-specifed
      $logo_url = strip_tags(variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT));
      break;
  }
João Ventura's avatar
João Ventura committed
  $logo_url = preg_replace('!^' . base_path() . '!', '', $logo_url);
  $variables['print_logo']                     = $logo_url ? theme('image', array('path' => $logo_url, 'alt' => variable_get('site_name', 'Drupal'), 'attributes' => array('class' => 'print-logo', 'id' => 'logo'))) : NULL;

  $variables['print_node']                     = $node;
  $variables['content']                  = $node->content;
  $variables['scripts']                  = drupal_get_js();
  $variables['footer_scripts']           = drupal_get_js('footer');
  $variables['sourceurl_enabled']        = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT);
  $variables['url']                      = url($path, array('absolute' => TRUE, 'query' => $variables['query']));
  $variables['source_url']               = url(variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT) ? drupal_get_normal_path($path) : $path, array('alias' => TRUE, 'absolute' => TRUE, 'query' => $variables['query']));
  $variables['cid']                      = isset($node->cid) ? $node->cid : NULL;
  $variables['print_title']                    = check_plain($node->title);
  $variables['head']                     = drupal_get_html_head();
  $variables['robots_meta']              = _print_robots_meta_generator();
  $variables['css']                      = _print_css_generator($variables['expand_css']);

  if (variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT) && ($format == 'html')) {
    drupal_add_js('misc/drupal.js', array('weight' => JS_LIBRARY));

    $window_close = (variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT) && variable_get('print_html_windowclose', PRINT_HTML_WINDOWCLOSE_DEFAULT)) ? 'window.close();' : '';
    $variables['sendtoprinter'] = '<script type="text/javascript">(function ($) { Drupal.behaviors.print = {attach: function(context) {$(window).load(function() {window.print();' . $window_close . '})}}})(jQuery);</script>';
  }

  $type = (isset($node->type)) ? $node->type : '';
  $nid = (isset($node->nid)) ? $node->nid : '';

  $variables['theme_hook_suggestions'] = array();
  $variables['theme_hook_suggestions'][] = "print__node__{$type}";
  $variables['theme_hook_suggestions'][] = "print__node__{$type}__{$nid}";
  $variables['theme_hook_suggestions'][] = "print__{$format}";
  $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}";
  $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}__{$nid}";
}

/**
João Ventura's avatar
João Ventura committed
 * Returns HTML for the published line of the print template.
 *
 * @param array $vars
 *   An empty associative array
 *
 * @return string
 *   HTML text with the published line
 *
 * @ingroup themeable
João Ventura's avatar
João Ventura committed
 * @ingroup print_themeable
 */
function theme_print_published($vars) {
  global $base_url;

  $published_site = variable_get('site_name', 0);
  return $published_site ? t('Published on %site_name', array('%site_name' => $published_site)) . ' (' . l($base_url, $base_url) . ')' : '';
}

/**
João Ventura's avatar
João Ventura committed
 * Returns HTML for the breadcrumb line of the print template.
 *
 * @param array $vars
 *   An associative array containing:
 *   - $node: the node object
 *
 * @return string
 *   HTML text with the breadcrumb
 *
 * @ingroup themeable
João Ventura's avatar
João Ventura committed
 * @ingroup print_themeable
 */
function theme_print_breadcrumb($vars) {
  $node = $vars['node'];

  $path = empty($node->nid) ? $node->path : "node/$node->nid";
  menu_set_active_item($path);
  $breadcrumb = drupal_get_breadcrumb();
  if (!empty($breadcrumb)) {
    $breadcrumb[] = menu_get_active_title();
    return filter_xss(implode(' > ', $breadcrumb));
  }
  else {
João Ventura's avatar
João Ventura committed
 * Returns HTML for the footer of the print template.
 *
 * @param array $vars
 *   An empty associative array
 *
 * @return string
 *   HTML text with the footer
 *
 * @ingroup themeable
João Ventura's avatar
João Ventura committed
 * @ingroup print_themeable
 */
function theme_print_footer($vars) {
  $footer = '';

  switch (variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT)) {
    case 1: // theme's
      $footer_blocks = block_get_blocks_by_region('footer');
      $footer = variable_get('site_footer', FALSE) . "\n" . drupal_render($footer_blocks);
      break;
    case 2: // user-specifed
      $footer = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT);
      break;
  }
  // Delete the contextual links
  $footer = preg_replace('!\s*<div class="contextual-links-wrapper">.*?</div>!sim', '', $footer);

João Ventura's avatar
João Ventura committed
 * Returns HTML for the source URL line of the print template.
 *
 * @param array $vars
 *   An associative array containing:
 *   - $url: the URL to the full node view.
 *   - $node: the node object.
 *   - $cid; comment ID of the comment to display.
 *
 * @return string
 *   HTML text with the footer
 *
 * @ingroup themeable
João Ventura's avatar
João Ventura committed
 * @ingroup print_themeable
 */
function theme_print_sourceurl($vars) {
  $sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT);
  $url = is_int($vars['cid']) ? $vars['url'] . '#comment-' . $vars['cid'] : $vars['url'];

  $output = '<strong>' . t('Source URL');
  if ($sourceurl_date && isset($vars['node'])) {
    $output .= ' (';
    $date = format_date($vars['node']->changed, 'short');

    $output .= empty($vars['node']->nid) ? t('retrieved on !date', array('!date' => $date)) : t('modified on !date', array('!date' => $date));

    $output .= ')';
  }
  $output .= ':</strong> ' . $url;

  return $output;
}

/**
João Ventura's avatar
João Ventura committed
 * Returns HTML for the URL list of the print template.
 *
 * @param array $vars
 *   An empty associative array
 *
 * @return string
 *   HTML text with the URL list
 *
 * @ingroup themeable
João Ventura's avatar
João Ventura committed
 * @ingroup print_themeable
 */
function theme_print_url_list($vars) {
  global $_print_urls;

  // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module
  if (!empty($_print_urls)) {
    $urls = _print_friendly_urls();
    $max = count($urls);
    $url_list = '';
    foreach ($urls as $key => $url) {
      $url_list .= '[' . ($key + 1) . '] ' . check_plain($url) . "<br />\n";
    }
    if (!empty($url_list)) {
      return "<p><strong>" . t('Links') . "</strong><br />$url_list</p>";
    }
    else {
      return '';
    }
  }
João Ventura's avatar
João Ventura committed
 * Generates a robots meta tag to tell them what they may index
João Ventura's avatar
João Ventura committed
 *
João Ventura's avatar
João Ventura committed
 * @return string
 *   meta robots tag
João Ventura's avatar
João Ventura committed
 */
function _print_robots_meta_generator() {
  $robots_meta = array();

  if (variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT)) {
João Ventura's avatar
João Ventura committed
    $robots_meta[] = 'noindex';
  }
  if (variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT)) {
João Ventura's avatar
João Ventura committed
    $robots_meta[] = 'nofollow';
  }
  if (variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT)) {
João Ventura's avatar
João Ventura committed
    $robots_meta[] = 'noarchive';
  }

    return '<meta name="robots" content=' . implode(', ', $robots_meta) . ' />';
João Ventura's avatar
João Ventura committed
  }
  else {
 * Generates the CSS directive to include in the printer-friendly version
 *
João Ventura's avatar
João Ventura committed
 * @param bool $expand
 *   If TRUE, the provided CSS will be expanded, instead of given as a list
 *   of includes.
João Ventura's avatar
João Ventura committed
 *
João Ventura's avatar
João Ventura committed
 * @return string
 *   applicable CSS
João Ventura's avatar
João Ventura committed
 */
function _print_css_generator($expand = FALSE) {
  $print_css = variable_get('print_css', PRINT_CSS_DEFAULT);
João Ventura's avatar
João Ventura committed

    drupal_add_css(strtr($print_css, array('%t' => drupal_get_path('theme', variable_get('theme_default')))));
João Ventura's avatar
João Ventura committed
  }
  else {
João Ventura's avatar
João Ventura committed
    drupal_add_css(drupal_get_path('module', 'print') . '/css/print.css');
João Ventura's avatar
João Ventura committed
  }
João Ventura's avatar
João Ventura committed
  $drupal_css = drupal_add_css();
  if (!variable_get('print_keep_theme_css', PRINT_KEEP_THEME_CSS_DEFAULT)) {
    foreach ($drupal_css as $key => $css_file) {
      if ($css_file['group'] == CSS_THEME) {
      // Unset the theme's CSS
        unset($drupal_css[$key]);
      }
  // Expand the CSS if requested
  if ($expand) {
    $css_files = array_keys($drupal_css);
João Ventura's avatar
João Ventura committed
    foreach ($css_files as $filename) {
      if (file_exists($filename)) {
        $style .= file_get_contents($filename, TRUE);
    return "<style type='text/css' media='all'>$style</style>\n";
João Ventura's avatar
João Ventura committed
  }
  else {
João Ventura's avatar
João Ventura committed
 * Callback function for the preg_replace_callback for URL-capable patterns
João Ventura's avatar
João Ventura committed
 *
João Ventura's avatar
João Ventura committed
 * Manipulate URLs to make them absolute in the URLs list, and add a [n]
 * footnote marker.
João Ventura's avatar
João Ventura committed
 * @param array $matches
João Ventura's avatar
João Ventura committed
 *   array with the matched tag patterns, usually <a...>+text+</a>
João Ventura's avatar
João Ventura committed
 *
 * @return string
 *   tag with re-written URL and, if applicable, the [n] index to the URL list
João Ventura's avatar
João Ventura committed
 */
function _print_rewrite_urls($matches) {
  global $base_url, $base_root, $_print_urls;
João Ventura's avatar
João Ventura committed

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

João Ventura's avatar
João Ventura committed
  // first, split the html into the different tag attributes
  $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
  $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  foreach ($attribs as $key => $value) {
    $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
João Ventura's avatar
João Ventura committed

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

      if (empty($url)) {
        // If URL is empty, use current_url
        $path = explode('/', $_GET['q']);
        unset($path[0]);
        $path = implode('/', $path);
        if (ctype_digit($path)) {
          $path = "node/$path";
        }
        // Printer-friendly URLs is on, so we need to make it absolute
        $newurl = url($path, array('fragment' => drupal_substr($url, 1), 'absolute' => TRUE));
      }
      elseif (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', html_entity_decode($url))) {
João Ventura's avatar
João Ventura committed
        // URL is absolute, do nothing
João Ventura's avatar
João Ventura committed
      }
      elseif (strpos(html_entity_decode($url), '//') === 0) {
        // URL is 'almost absolute', but it does not contain protocol; replace with base_path protocol
        $newurl = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . ":" . $url;
        $matches[1] = str_replace($url, $newurl, $matches[1]);
      }
João Ventura's avatar
João Ventura committed
      else {
João Ventura's avatar
João Ventura committed
          // URL is an anchor tag
          if ($include_anchors && (!empty($_print_urls))) {
            $path = explode('/', $_GET['q']);
            unset($path[0]);
            $path = implode('/', $path);
João Ventura's avatar
João Ventura committed
              $path = "node/$path";
            }
            // Printer-friendly URLs is on, so we need to make it absolute
João Ventura's avatar
João Ventura committed
            $newurl = url($path, array('fragment' => drupal_substr($url, 1), 'absolute' => TRUE));
João Ventura's avatar
João Ventura committed
          }
          // Because base href is the original page, change the link to
          // still be usable inside the print page
          $matches[1] = str_replace($url, base_path() . $_GET['q'] . $url, $matches[1]);
João Ventura's avatar
João Ventura committed
        }
        else {
          // URL is relative, convert it into absolute URL
João Ventura's avatar
João Ventura committed
            // If it starts with '/' just append it to the server name
João Ventura's avatar
João Ventura committed
          }
          elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
            // If it starts with ?q=, just prepend with the base URL
João Ventura's avatar
João Ventura committed
          }
          else {
            $newurl = url(trim($url, '/'), array('absolute' => TRUE));
João Ventura's avatar
João Ventura committed
          }
          $matches[1] = str_replace($url, $newurl, $matches[1]);
João Ventura's avatar
João Ventura committed
  $ret = '<' . $matches[1] . '>';
João Ventura's avatar
João Ventura committed
  if (count($matches) == 4) {
    $ret .= $matches[2] . $matches[3];
    if ((!empty($_print_urls)) && (isset($newurl))) {
      $ret .= ' <span class="print-footnote">[' . _print_friendly_urls(trim($newurl)) . ']</span>';
João Ventura's avatar
João Ventura committed
    }
  }

  return $ret;
}

/**
 * Auxiliary function to store the Printer-friendly URLs list as static.
 *
João Ventura's avatar
João Ventura committed
 * @param string $url
João Ventura's avatar
João Ventura committed
 *   absolute URL to be inserted in the list
João Ventura's avatar
João Ventura committed
 *
 * @return array
João Ventura's avatar
João Ventura committed
 *   list of URLs previously stored if $url is 0, or the current count
João Ventura's avatar
João Ventura committed
 *   otherwise.
João Ventura's avatar
João Ventura committed
 */
function _print_friendly_urls($url = 0) {
  static $urls = array();
  if ($url !== 0) {
João Ventura's avatar
João Ventura committed
    $url_idx = array_search($url, $urls);
    if ($url_idx !== FALSE) {
      return ($url_idx + 1);
    }
    else {
      $urls[] = $url;
      return count($urls);
    }
  }
João Ventura's avatar
João Ventura committed
  $ret = $urls;
  $urls = array();
  return $ret;
/**
 * Check URL list settings for this node
 *
João Ventura's avatar
João Ventura committed
 * @param object $node
 *   node object
João Ventura's avatar
João Ventura committed
 * @param string $format
 *   format of the page being generated
João Ventura's avatar
João Ventura committed
 *
 * @return bool
 *   TRUE if URL list should be displayed, FALSE otherwise
 */
João Ventura's avatar
João Ventura committed
function _print_url_list_enabled($node, $format) {
  if (!isset($node->type)) {
João Ventura's avatar
João Ventura committed
    $node_urllist = variable_get('print_' . $format . '_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
João Ventura's avatar
João Ventura committed
    $node_urllist = isset($node->{'print_' . $format . '_display_urllist'}) ? $node->{'print_' . $format . '_display_urllist'} : variable_get('print_' . $format . '_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
  }

  // Get value of Printer-friendly URLs setting
  return (variable_get('print_urls', PRINT_URLS_DEFAULT) && ($node_urllist));
João Ventura's avatar
João Ventura committed
/**
João Ventura's avatar
João Ventura committed
 * Prepare a Printer-friendly-ready node body for content nodes
João Ventura's avatar
João Ventura committed
 * @param int $nid
João Ventura's avatar
João Ventura committed
 *   node ID of the node to be rendered into a printer-friendly page
João Ventura's avatar
João Ventura committed
 * @param string $format
 *   format of the page being generated
 * @param int $vid
 *   (optional) revision ID of the node to use
João Ventura's avatar
João Ventura committed
 * @param int $cid
 *   (optional) comment ID of the individual comment to be rendered
 * @param string $view_mode
 *   (optional) view mode to be used when rendering the content
João Ventura's avatar
João Ventura committed
 *
 * @return object
 *   filled node-like object to be used in the print template
João Ventura's avatar
João Ventura committed
 */
function _print_generate_node($nid, $format, $vid = NULL, $cid = NULL, $view_mode = PRINT_VIEW_MODE) {
  global $_print_urls;

  if (!isset($langcode)) {
    $langcode = $GLOBALS['language_content']->language;
  }
João Ventura's avatar
João Ventura committed
  // We can take a node id
  $node = node_load($nid, $vid);
  if (!$node) {
    // Node not found
    drupal_not_found();
João Ventura's avatar
João Ventura committed
    // Access is denied
João Ventura's avatar
João Ventura committed
  }
  drupal_set_title($node->title);
João Ventura's avatar
João Ventura committed

  if ($cid === NULL) {
    // Adapted (simplified) version of node_view
João Ventura's avatar
João Ventura committed
    // Render the node content
    // Disable the links area
    unset($node->content['links']);
    $build = $node->content;
    unset($node->content);
  if (function_exists('comment_node_page_additions') &&
      (($cid != NULL) || (variable_get('print_comments', PRINT_COMMENTS_DEFAULT)))) {
João Ventura's avatar
João Ventura committed
    // Print only the requested comment (or if $cid is NULL, all of them)

    $comments = comment_node_page_additions($node);
    if (!empty($comments)) {
      unset($comments['comment_form']);
      foreach ($comments['comments'] as $key => &$comment) {
        if (is_numeric($key)) {
          if (($cid != NULL) && ($key != $cid)) {
            unset($comments['comments'][$key]);
          }
          else {
            unset($comment['links']);
          }
João Ventura's avatar
João Ventura committed
  }
    '#node' => $node,
    '#view_mode' => $view_mode,
    '#language' => $langcode,
  $type = 'node';
  drupal_alter(array('node_view', 'entity_view'), $build, $type);
João Ventura's avatar
João Ventura committed
  // Get rid of any links before the content
  $parts = explode('<div class="content', $content, 2);
  if (count($parts) == 2) {
    $pattern = '!(.*?)<a [^>]*?>(.*?)</a>(.*?)!mis';
    $parts[0] = preg_replace($pattern, '$1$2$3', $parts[0]);
    $content = implode('<div class="content', $parts);
  }
João Ventura's avatar
João Ventura committed
  // Check URL list settings
  $_print_urls = _print_url_list_enabled($node, $format);

João Ventura's avatar
João Ventura committed
  // Convert the a href elements
  $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
  $content = preg_replace_callback($pattern, '_print_rewrite_urls', $content);
João Ventura's avatar
João Ventura committed

João Ventura's avatar
João Ventura committed

João Ventura's avatar
João Ventura committed
  return $node;
João Ventura's avatar
João Ventura committed
 * Prepare a Printer-friendly-ready node body for non-content pages
João Ventura's avatar
João Ventura committed
 * @param string $path
João Ventura's avatar
João Ventura committed
 *   path of the node to be rendered into a printer-friendly page
João Ventura's avatar
João Ventura committed
 * @param string $format
 *   format of the page being generated
João Ventura's avatar
João Ventura committed
 *
 * @return object
 *   filled node-like object to be used in the print template
João Ventura's avatar
João Ventura committed
 */
João Ventura's avatar
João Ventura committed
function _print_generate_path($path, $format) {
  global $_print_urls;

  // Handle node tabs
  $parts = explode('/', $path);
  if (ctype_digit($parts[0]) && (count($parts) > 1)) {
João Ventura's avatar
João Ventura committed
  $path = drupal_get_normal_path($path);

  menu_set_active_item($path);
  // Adapted from index.php.
  $node = new stdClass();
  $node->content = menu_execute_active_handler($path, FALSE);
  if (is_array($node->content)) {
    $node->content = drupal_render($node->content);
João Ventura's avatar
João Ventura committed

  if (is_int($node->content)) {
    switch ($node->content) {
  $node->title = drupal_get_title();
  $node->path = $path;
João Ventura's avatar
João Ventura committed
  // Delete any links area
  $node->content = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->content);
João Ventura's avatar
João Ventura committed

  // Delete the contextual links also
  $node->content = preg_replace('!\s*<div class="contextual-links-wrapper">.*?</div>!sim', '', $node->content);

João Ventura's avatar
João Ventura committed
  // Check URL list settings
  $_print_urls = _print_url_list_enabled($node, $format);

João Ventura's avatar
João Ventura committed
  // Convert the a href elements
  $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
  $node->content = preg_replace_callback($pattern, '_print_rewrite_urls', $node->content);
João Ventura's avatar
João Ventura committed

João Ventura's avatar
João Ventura committed
  return $node;
João Ventura's avatar
João Ventura committed
 * Prepare a Printer-friendly-ready node body for book pages
João Ventura's avatar
João Ventura committed
 * @param int $nid
João Ventura's avatar
João Ventura committed
 *   node ID of the node to be rendered into a printer-friendly page
João Ventura's avatar
João Ventura committed
 * @param string $format
 *   format of the page being generated
João Ventura's avatar
João Ventura committed
 *
 * @return object
 *   filled node-like object to be used in the print template
João Ventura's avatar
João Ventura committed
 */
João Ventura's avatar
João Ventura committed
function _print_generate_book($nid, $format) {
  global $_print_urls;

  $node = node_load($nid);
  if (!$node) {
    // Node not found
    drupal_not_found();
  }
  elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
João Ventura's avatar
João Ventura committed
    // Access is denied
João Ventura's avatar
João Ventura committed
  }

  $tree = book_menu_subtree_data($node->book);
  $node->content = book_export_traverse($tree, 'book_node_export');
João Ventura's avatar
João Ventura committed

João Ventura's avatar
João Ventura committed
  // Check URL list settings
  $_print_urls = _print_url_list_enabled($node, $format);

João Ventura's avatar
João Ventura committed
  // Convert the a href elements
  $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
  $node->content = preg_replace_callback($pattern, '_print_rewrite_urls', $node->content);
João Ventura's avatar
João Ventura committed

João Ventura's avatar
João Ventura committed
  return $node;
João Ventura's avatar
João Ventura committed
}