Skip to content
print.module 13.5 KiB
Newer Older
João Ventura's avatar
João Ventura committed
// $Id$
João Ventura's avatar
João Ventura committed
 * Displays Printer-friendly versions of Drupal pages.
 *
 * This is the core module of the PF package, with the Drupal hooks
 * and other administrative functions.
define('PRINT_PATH', 'print');

define('PRINT_LOGO_URL_DEFAULT', '');
define('PRINT_CSS_DEFAULT', '');
define('PRINT_URLS_DEFAULT', 1);
define('PRINT_COMMENTS_DEFAULT', 0);
define('PRINT_NEWWINDOW_DEFAULT', 1);

define('PRINT_HTML_SHOW_LINK_DEFAULT', 1);
define('PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT', 0);
define('PRINT_HTML_NODE_LINK_PAGES_DEFAULT', '');
define('PRINT_HTML_LINK_CLASS_DEFAULT', 'print-page');
define('PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT', 1);
define('PRINT_HTML_SYS_LINK_PAGES_DEFAULT', '<front>');
define('PRINT_HTML_BOOK_LINK_DEFAULT', 1);
define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);

define('PRINT_SOURCEURL_ENABLED_DEFAULT', 1);
define('PRINT_SOURCEURL_DATE_DEFAULT', 0);
define('PRINT_SOURCEURL_FORCENODE_DEFAULT', 0);

define('PRINT_ROBOTS_NOINDEX_DEFAULT', 1);
define('PRINT_ROBOTS_NOFOLLOW_DEFAULT', 1);
define('PRINT_ROBOTS_NOARCHIVE_DEFAULT', 0);

define('PRINT_TYPE_SHOW_LINK_DEFAULT', 1);
define('PRINT_TYPE_COMMENT_LINK_DEFAULT', 0);
João Ventura's avatar
João Ventura committed

 */
function print_perm() {
  return array('access print', 'administer print');
}

João Ventura's avatar
João Ventura committed
/**
João Ventura's avatar
João Ventura committed
 */
function print_theme() {
  return array(
    'print_format_link' => array(
      'arguments' => array(),
    ),
    'print_text' => array(
      'arguments' => array(),
    ),
  );
}

function print_menu() {
  $items[PRINT_PATH] = array(
    'title' => 'Printer-friendly',
João Ventura's avatar
João Ventura committed
    'page callback' => 'print_controller_html',
    'access arguments' => array('access print'),
João Ventura's avatar
João Ventura committed
    'type' => MENU_CALLBACK,
    'file' => 'print.pages.inc',
  );
  $items['admin/settings/print'] = array(
    'title' => 'Printer-friendly pages',
    'description' => 'Adds a printer-friendly version link to content and administrative pages.',
    'page callback' => 'drupal_get_form',
João Ventura's avatar
João Ventura committed
    'page arguments' => array('print_html_settings'),
    'access arguments'  => array('administer print'),
    'file' => 'print.admin.inc',
  );
  $items['admin/settings/print/html'] = array(
    'title' => 'Web page',
    'weight' => 0,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/settings/print/common'] = array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('print_main_settings'),
    'access arguments'  => array('administer print'),
João Ventura's avatar
João Ventura committed
    'weight' => 10,
    'type' => MENU_LOCAL_TASK,
    'file' => 'print.admin.inc',
function print_link($type, $node = NULL, $teaser = FALSE) {
  $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
  $print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT);
  $print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT);
  static $print_display_comment = FALSE;
  if (($print_display_comment == FALSE) && (isset($node->type))) {
    $node_type = $node->type;
    $print_display_comment = variable_get('print_display_comment_'. $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
    $print_display = variable_get('print_display_'. $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
  }
  else {
    $node_type = '';
    $print_display = 1;
  }

  // No link is shown for several motives...
  if ( !($teaser) && ($node_type != 'book') && (!isset($node->book)) &&
      ($print_html_show_link) && user_access('access print') &&
      _print_page_match($print_html_node_link_visibility, $print_html_node_link_pages) &&
      (($type == 'comment' && $print_display_comment) ||
      ($type == 'node' && $print_display))) {
    $links = array();

João Ventura's avatar
João Ventura committed
    $format = theme('print_format_link');
    if ($type == 'comment') {
    $query = drupal_query_string_encode($query_arr, array('q'));
    if (empty($query)) $query = NULL;
    $links['print'] = array('href' => PRINT_PATH .'/'. $node->nid,
                            'title' => $format['text'],
                            'attributes' => $format['attributes'],
João Ventura's avatar
João Ventura committed
                            'html' => $format['html'],
João Ventura's avatar
João Ventura committed
  }
João Ventura's avatar
João Ventura committed
/**
 * Implementation of hook_link_alter().
 */
function print_link_alter(&$links, $node) {
  foreach ($links as $module => $link) {
    if (strpos($module, 'book_printer') !== FALSE) {
      $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
João Ventura's avatar
João Ventura committed

      if ($print_html_book_link) {
João Ventura's avatar
João Ventura committed
        $format = theme('print_format_link');
        $format['attributes']['title'] = $link['attributes']['title'];

        $links[$module]['href'] = PRINT_PATH .'/'. $link['href'];
João Ventura's avatar
João Ventura committed
        $links[$module]['attributes'] = $format['attributes'];
      }
    }
  }
}

 */
function print_help($path, $arg) {
João Ventura's avatar
João Ventura committed
  switch ($path) {
    case 'admin/help#print':
      // Return a line-break version of the module README
      return filter_filter('process', 1, NULL, file_get_contents(drupal_get_path('module', 'print') .'/README.txt') );
  $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
  $print_html_sys_link_visibility = variable_get('print_html_sys_link_visibility', PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT);
  $print_html_sys_link_pages = variable_get('print_html_sys_link_pages', PRINT_HTML_SYS_LINK_PAGES_DEFAULT);
  if (($print_html_show_link) && user_access('access print') &&
      _print_page_match($print_html_sys_link_visibility, $print_html_sys_link_pages) &&
      (preg_match('!^node/!i', $path) == 0)) {
    static $output = FALSE;

    if ($output === FALSE) {
      drupal_add_css(drupal_get_path('module', 'print') .'/printlinks.css');
      $output = TRUE;
      return '<span class="print-syslink">'. print_insert_link() .'</span>';
/**
 * Implementation of hook_nodeapi().
 */
function print_nodeapi(&$node, $op = 'view', $teaser, $page) {
  switch ($op) {
    case 'view':
      $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
      $print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT)
      $print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT);
      $print_display = variable_get('print_display_'. $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);

      if ((!$teaser) && (!$node->printing) && ($print_html_show_link) &&
          user_access('access print') && ($print_display) &&
          _print_page_match($print_html_node_link_visibility, $print_html_node_link_pages)) {
        drupal_add_css(drupal_get_path('module', 'print') .'/printlinks.css');
        $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
        if (($print_html_book_link) && user_access('access printer-friendly version') &&
            (($node->type == 'book') || isset($node->book))) {
          $path = PRINT_PATH .'/book/export/html/'. $node->nid;
        }
        $link = print_insert_link($path);
          '#value' => "<span class='print-link'>$link</span>",
 */
function print_form_alter(&$form, $form_state, $form_id) {
  // Add the node-type settings option to activate the printer-friendly version link
    $form['print'] = array(
      '#type' => 'fieldset',
      '#title' => t('Printer-friendly pages'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );

    $form['print']['print_display'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show printer-friendly version link'),
      '#default_value' => variable_get('print_display_'. $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT),
      '#description' => t('Displays the link to a printer-friendly version of the content. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
    );
    $form['print']['print_display_comment'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show printer-friendly version link in individual comments'),
      '#default_value' => variable_get('print_display_comment_'. $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT),
      '#description' => t('Displays the link to a printer-friendly version of the comment. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
    );
  }
João Ventura's avatar
João Ventura committed
 * Auxiliary function to fill the Printer-friendly link attributes
 * @param $title
 *   text to displayed by the link when hovering over it with the mouse
 * @param $class
 *   class attribute to be used in the link
 * @param $new_window
 *   if TRUE opens the target page in a new window
João Ventura's avatar
João Ventura committed
 * @return
 *   array of formatted attributes
function print_fill_attributes($title = '', $class = '', $new_window = FALSE) {
  $print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT);
  $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
  $attributes = array();
  $attributes['title'] = $title;
  if (!empty($class)) {
    $attributes['class'] = $class;
  }
    case 0:
      $attributes['target'] = '_blank';
      break;
    case 1:
      $attributes['onclick'] = 'window.open(this.href); return false';
      break;
    }
  if (!empty($print_robots_noindex)) {
João Ventura's avatar
João Ventura committed
    $attributes['rel'] = 'nofollow';
  return $attributes;
}
/**
 * Auxiliary function to set the link text and html flag
 *
 * @param $type
 *   type of link: 0 or 1 for a text-only link, 2 for icon-only and 3 for
 *   both text and icon
 * @param $text
 *   text to be displayed on the link to the printer-friendly page
 * @param $img
 *   path to the icon file
 * @return
 *   array with the link text and html flag
 */
function _print_format_link_aux($type = 0, $text = '', $img = '') {
  if ($type >= 2) {
    $img = theme('image', $img, $text, '', array('class' => 'print-icon'));
    $html = TRUE;
    switch ($type) {
    case 2:
      $text = $img;
      break;
    case 3:
      $text = $img .'&nbsp;'. $text;
  return array('text' => $text,
               'html' => $html,
João Ventura's avatar
João Ventura committed
/**
 * Format the Printer-friendly link
 *
 * @return
 *   array of formatted attributes
 * @ingroup themeable
 */
João Ventura's avatar
João Ventura committed
function theme_print_format_link() {
  $print_html_link_class = variable_get('print_html_link_class', PRINT_HTML_LINK_CLASS_DEFAULT);
  $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
  $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);

João Ventura's avatar
João Ventura committed
  $text = t('Printer-friendly version');
  $img = drupal_get_path('module', 'print') .'/icons/print_icon.gif';
  $title = t('Display a printer-friendly version of this page.');
  $class = strip_tags($print_html_link_class);
  $new_window = $print_html_new_window;
  $format = _print_format_link_aux($print_html_show_link, $text, $img);

  return array('text' => $format['text'],
               'html' => $format['html'],
               'attributes' => print_fill_attributes($title, $class, $new_window),
João Ventura's avatar
João Ventura committed
/**
 * Define the strings displayed by the module in the printer-friendly template
 *
 * @return
 *   array with the used text strings
 * @ingroup themeable
 */
João Ventura's avatar
João Ventura committed
function theme_print_text() {
  return array('retrieved' => '',
               'sourceURL' => '',
               'published' => '',
               'by' => '',
               'created' => '',
               'links' => '',
João Ventura's avatar
João Ventura committed
 * Auxiliary function to display a formatted Printer-friendly link
 *
João Ventura's avatar
João Ventura committed
 * Function made available so that developers may call this function from
 * their defined pages/blocks.
 *
 * @param $path
João Ventura's avatar
João Ventura committed
 *   path of the original page (optional). If not specified, the current URL
João Ventura's avatar
João Ventura committed
 *   is used
 * @return
 *   string with the HTML link to the printer-friendly page
João Ventura's avatar
João Ventura committed
function print_insert_link($path = NULL) {
  if (user_access('access print')) {
    if ($path === NULL) {
      $path = PRINT_PATH .'/'. $_GET['q'];
João Ventura's avatar
João Ventura committed
      $query = drupal_query_string_encode($_GET, array('q'));
      if (empty($query)) {
        $query = NULL;
      }
João Ventura's avatar
João Ventura committed
    $format = theme('print_format_link');
    return '<span class="print">'. l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) .'</span>';

/**
 * Determine if the current page is enabled according to the visibility settings
 *
 * @param $visibility
 *   current visibility settings: 
 *    0 for show on every page except the listed pages
 *    1 for show on only the listed pages
 * @param $pages
 *   list of pages
 * @return
 *   TRUE if it is enabled, FALSE otherwise
 */
function _print_page_match($visibility, $pages) {
  if ($pages) {
    $path = drupal_get_path_alias($_GET['q']);
    $page_match = drupal_match_path($path, $pages);
    if ($path != $_GET['q']) {
      $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
    }

    return !($visibility xor $page_match);
  }
  else {
    return TRUE;
  }
}