Skip to content
print.module 9.9 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.
require_once(drupal_get_path('module', 'print') .'/print.admin.inc');
require_once(drupal_get_path('module', 'print') .'/print.pages.inc');

/**
 * Default values of the print_settings variable
João Ventura's avatar
João Ventura committed
 *
 * @return
 *   array with the values
 */
function print_settings_default() {
  return array('show_link' => 1,
               'link_class' => 'print-page',
               'show_sys_link' => 1,
               'book_link' => 1,
               'new_window' => 0,
               'logo_url' => '',
               'css' => '',
               'urls' => 1,
               'comments' => 0,
               'newwindow' => 1,
               'sendtoprinter' => 0,
              );
}

/**
 * Default values of the print_sourceurl_settings variable
João Ventura's avatar
João Ventura committed
 *
 * @return
 *   array with the values
 */
function print_sourceurl_settings_default() {
  return array('enabled' => 1, 'date' => 0, 'forcenode' => 0);
}

/**
 * Default values of the print_robot_settings variable
João Ventura's avatar
João Ventura committed
 *
 * @return
 *   array with the values
 */
function print_robot_settings_default() {
  return array('noindex' => 1, 'nofollow' => 1, 'noarchive' => 0, 'nocache' => 0);
}

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

 */
function print_menu($may_cache) {
  $items = array();

Chad Phillips's avatar
Chad Phillips committed
  if ($may_cache) {
    $items[] = array(
      'title' => t('Printer-friendly'),
      'callback' => 'print_controller_html',
      'access' => user_access('access print'),
Chad Phillips's avatar
Chad Phillips committed
    );
    $items[] = array(
      'path' => 'admin/settings/print',
      'title' => t('Printer-friendly Pages'),
      'description' => t('Adds a printer-friendly version link to content and administrative pages.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('print_main_settings'),
      'access'  => user_access('administer print'),
Chad Phillips's avatar
Chad Phillips committed
    );
  }
function print_link($type, $node = NULL, $teaser = FALSE) {
  $print_settings = variable_get('print_settings', print_settings_default());

  if (isset($node->type)) {
    $node_type = $node->type;
    $print_display_comment = variable_get('print_display_comment', 0);
    $print_display = variable_get('print_display_'. $node->type, 1);
  }
  else {
    $node_type = '';
    $print_display_comment = variable_get('print_display_comment', 0);
  // No link is shown for several motives...
  if ( !($teaser) && ($node_type != 'book') && (!isset($node->parent)) &&
      (!(module_exists('visibility_api')) || visibility_api_access('print')) &&
      ($print_settings['show_link']) && user_access('access print') &&
      (($type == 'comment' && $print_display_comment) ||
    $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'],
/**
 * Implementation of hook_link_alter().
 */
function print_link_alter(&$node, &$links) {
  foreach ($links as $module => $link) {
    if (strpos($module, 'book_printer') !== FALSE) {
      $print_settings = variable_get('print_settings', print_settings_default());
      if ($print_settings['book_link']) {
        $format = theme('print_format_link');
        $format['attributes']['title'] = $link['attributes']['title'];
        $links[$module]['href'] = PRINT_PATH ."/". $link['href'];
        $links[$module]['attributes'] = $format['attributes'];
      }
    }
João Ventura's avatar
João Ventura committed
function print_help($path) {
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', 2, NULL, file_get_contents(drupal_get_path('module', 'print') ."/README.txt") );
  }

  $print_settings = variable_get('print_settings', print_settings_default());

  if (($print_settings['show_link']) && ($print_settings['show_sys_link']) &&
      (!module_exists('visibility_api') || visibility_api_access('print')) &&
      user_access('access print') && (preg_match("/^node\//i", $path) == 0)) {
    static $output = FALSE;

    if ($output === FALSE) {
      $output = TRUE;
      return print_insert_link();
function print_form_alter($form_id, &$form) {
  // Add the node-type settings option to activate the printer-friendly version link
    $form['workflow']['print_display'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show printer-friendly version link'),
      '#default_value' => variable_get('print_display_'. $form['#node_type']->type, 1),
      '#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' ))),
    );
  elseif ($form_id == 'comment_admin_settings') {
    $form['viewing_options']['print_display_comment'] = array(
      '#type' => 'checkbox',
João Ventura's avatar
João Ventura committed
      '#title' => t('Show printer-friendly version link in individual comments'),
      '#default_value' => variable_get('print_display_comment', 0),
João Ventura's avatar
João Ventura committed
      '#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' ))),
/**
 * 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_settings = variable_get('print_settings', print_settings_default());
  $robots_settings = variable_get('print_robot_settings', print_robot_settings_default());
  $attributes = array();
  $attributes['title'] = $title;
  if (!empty($class)) {
    $attributes['class'] = $class;
  }

  if ($new_window) {
    switch ($print_settings['newwindow']) {
    case 0:
      $attributes['target'] = '_blank';
      break;
    case 1:
      $attributes['onclick'] = 'window.open(this.href); return false';
      break;
    }
  if (!empty($robots_settings['nofollow'])) {
    $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('style' => 'margin-right: 4px;vertical-align: middle'));
    $html = TRUE;
    switch ($type) {
    case 2:
      $text = $img;
      break;
    case 3:
      break;
    }
  }
  else {
    $html = FALSE;
  }
  
  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
 */
function theme_print_format_link() {
  $print_settings = variable_get('print_settings', print_settings_default());
  $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_settings['link_class']);
  $new_window = $print_settings['new_window'];
  $format = _print_format_link_aux($print_settings['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
 */
  return array('retrieved' => "",
               'sourceURL' => "",
               'published' => "",
               'by' => "",
               'created' => "",
               'links' => "",
/**
 * 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
 *   path of the original page (optional). If not specified, the current URL 
 *   is used
 * @return
 *   string with the HTML link to the printer-friendly page
function print_insert_link($path = NULL) {
  if (user_access('access print')) {
    if ($path === NULL) {
      $path = PRINT_PATH ."/". $_GET['q'];
      $query = drupal_query_string_encode($_GET, array('q'));
      if (empty($query)) {
        $query = NULL;
    }
    $format = theme('print_format_link');
    return '<span class="print">'. l($format['text'], $path, $format['attributes'], $query, NULL, TRUE, $format['html']) .'</span>';