<?php
/* $Id: print_pdf.module,v 1.1.2.12 2008/05/23 11:50:57 jcnventura Exp $ */

/**
 * @file
 * Display printer-friendly versions of Drupal pages
 */

define("PRINTPDF_PATH", "printpdf");

/**
 * Default values of the print_pdf_settings variable
 */
function print_pdf_settings_default() {
  return array('show_link' => 0,
               'link_class' => 'print-pdf',
               'show_sys_link' => 0,
               'book_link' => 1,
               'pdf_tool' => 0,
               'content_disposition' => 2,
               'paper_size' => 'A4',
               'page_orientation' => 'portrait',
              );
}

//*******************************************************************
// Drupal Hooks
//*******************************************************************

/**
 * Implementation of hook_theme().
 */
function print_pdf_theme() {
  return array(
    'print_pdf_format_link' => array(
      'arguments' => array(),
    ),
  );
}

/**
 * Implementation of hook_menu().
 */
function print_pdf_menu() {
  $items = array();

  $items[PRINTPDF_PATH] = array(
    'title' => 'Printer-friendly PDF',
    'page callback' => 'print_pdf_controller',
    'access arguments' => array('access print'),
    'type' => MENU_CALLBACK,
    'file' => 'print.pdf.inc',
  );
  $items['admin/settings/print/pdf'] = array(
    'title' => 'PDF',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('print_pdf_settings'),
    'access arguments'  => array('administer print'),
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
    'file' => 'print_pdf.admin.inc',
  );

  return $items;
}

/**
 * Implementation of hook_link().
 */
function print_pdf_link($type, $node = NULL, $teaser = FALSE) {
  $print_pdf_settings = variable_get('print_pdf_settings', print_pdf_settings_default());
  static $print_pdf_display_comment = FALSE;

  if (($print_pdf_display_comment == FALSE) && (isset($node->type))) {
    $node_type = $node->type;
    $print_pdf_display_comment = variable_get('print_pdf_display_comment_'. $node->type, 0);
    $print_pdf_display = variable_get('print_pdf_display_'. $node->type, 1);
  }
  else {
    $node_type = '';
    $print_pdf_display = 1;
  }

  if (!$teaser) {
    $links = array();
    $format = theme('print_pdf_format_link');
    // Show book link
    if (($print_pdf_settings['book_link']) && user_access('access printer-friendly version') &&
        (($node_type == 'book') || isset($node->book))) {

      $links['book_pdf'] = array('href' => PRINTPDF_PATH ."/book/export/html/". $node->nid,
                                 'title' => strip_tags($format['text']),
                                 'attributes' => $format['attributes'],
                                );

      return $links;
    }
    // No link is shown for several motives...
    elseif (($node_type != 'book') && (!isset($node->book)) &&
      ($print_pdf_settings['show_link']) && user_access('access print') &&
      (($type == 'comment' && $print_pdf_display_comment) ||
      ($type == 'node' && $print_pdf_display))) {

      $query_arr = $_GET;
      if ($type == 'comment') {
        $query_arr['comment'] = $node->cid;
      }
      $query = drupal_query_string_encode($query_arr, array('q'));

      $links['print_pdf'] = array('href' => PRINTPDF_PATH ."/". $node->nid,
                                  'title' => $format['text'],
                                  'attributes' => $format['attributes'],
                                  'html' => $format['html'],
                                  'query' => $query,
                                 );

      return $links;
    }
  }
  return;
}

/**
 * Implementation of hook_help().
 */
function print_pdf_help($path, $arg) {
  $print_pdf_settings = variable_get('print_pdf_settings', print_pdf_settings_default());

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

    if ($output === FALSE) {
      $output = TRUE;
    
      return print_pdf_insert_link();
    }
  }
}

/**
 * Implementation of hook_form_alter().
 */
function print_pdf_form_alter(&$form, $form_state, $form_id) {
  // Add the node-type settings option to activate the PDF version link
  if ($form_id == 'node_type_form') {
    $form['workflow']['print_pdf_display'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show PDF version link'),
      '#default_value' => variable_get('print_pdf_display_'. $form['#node_type']->type, 1),
      '#description' => t('Displays the link to a PDF version of the content. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
    );
    $form['comment']['print_pdf_display_comment'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show PDF version link in individual comments'),
      '#default_value' => variable_get('print_pdf_display_comment_'. $form['#node_type']->type, 0),
      '#description' => t('Displays the link to a PDF version of the comment. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
    );
  }
}

/**
 * Format the PDF version link
 *
 * @return
 *   array of formatted attributes
 * @ingroup themeable
 */
function theme_print_pdf_format_link() {
  $print_pdf_settings = variable_get('print_pdf_settings', print_pdf_settings_default());
  $text = t('PDF version');
  $img = base_path() . drupal_get_path('module', 'print') .'/icons/pdf_icon.gif';
  $title = t('Display a PDF version of this page.');
  $class = $print_pdf_settings['link_class'];
  $new_window = ($print_pdf_settings['content_disposition'] == 1);
  $format = _print_format_link_aux($print_pdf_settings['show_link'], $text, $img);

  return array('text' => $format['text'],
               'html' => $format['html'],
               'attributes' => print_fill_attributes($title, $class, $new_window),
              );
}

/**
 * Auxiliary function to display a formatted Printer-friendly link
 *
 * @return string
 */
function print_pdf_insert_link($path = NULL) {
  if (user_access('access print')) {
    if ($path === NULL) {
      $path = PRINTPDF_PATH ."/". $_GET['q'];
      $query = drupal_query_string_encode($_GET, array('q'));
      if (empty($query)) {
        $query = NULL;
      }
    }
    $format = theme('print_pdf_format_link');
    return '<span class="print">'. l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) .'</span>';
  }
}
