<?php

/**
 * @file
 * pdf reader module file.
 */

/**
 * Implements hook_permission().
 */
function pdf_reader_permission() {
  return array(
    'administer pdf reader' => array(
      'title' => t('Administer PDF Reader'),
      'description' => t('Perform maintenance tasks for PDF Reader'),
    ),
  );
}

/**
 * Implement hook_field_formatter_info().
 */
function pdf_reader_field_formatter_info() {
  return array(
    'pdf_reader_file' => array(
      'label' => t('PDF Reader'),
      'field types' => array('file', 'text'),
      'settings' => array(
        'pdf_width' => 600,
        'pdf_height' => 780,
        'renderer' => 'google',
        'download' => FALSE,
        'colorbox' => FALSE,
        'colorbox_link_text' => '',
      )
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function pdf_reader_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  $element['pdf_width'] = array(
    '#title' => t('Width'),
    '#type' => 'textfield',
    '#default_value' => $settings['pdf_width'],
  );

  $element['pdf_height'] = array(
    '#title' => t('Height'),
    '#type' => 'textfield',
    '#default_value' => $settings['pdf_height'],
  );

  $element['renderer'] = array(
    '#title' => t('Renderer'),
    '#type' => 'select',
    '#options' => array(
      'google' => 'Google Viewer',
      'direct' => 'Direct Embed',
      'pdf-js' => 'pdf.js'),
    '#default_value' => $settings['renderer'],
  );

  $element['download'] = array(
    '#title' => t('Show download link'),
    '#type' => 'checkbox',
    '#default_value' => $settings['download'],
  );

  if (module_exists('colorbox')) {
    variable_set('colorbox_inline', 1);
  }

  $element['colorbox'] = array(
    '#title' => t('Display in colorbox (colorbox module must be installed).'),
    '#type' => 'checkbox',
    '#disabled' => !module_exists('colorbox'),
    '#default_value' => $settings['colorbox'],
  );

  $element['colorbox_link_text'] = array(
    '#title' => t('Colorbox link'),
    '#type' => 'textfield',
    '#disabled' => !module_exists('colorbox'),
    '#default_value' => $settings['colorbox_link_text'],
  );

  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function pdf_reader_field_formatter_settings_summary($field, $instance, $view_mode) {
  $renderers = array(
    'google' => 'Google Viewer',
    'direct' => 'Direct Embed',
    'pdf-js' => 'pdf.js');
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  $summary = 'Size: ' . $settings['pdf_width'] . 'x' . $settings['pdf_height']
      . ' Using: ' . $renderers[$settings['renderer']]
      . '<br> Download Link: ' . ($settings['download'] == 1 ? 'YES' : 'NO')
      . ' Use colorbox: ' . ($settings['colorbox'] == 1 ? 'YES' : 'NO')
      . ' Colorbox link text: ' . $settings['colorbox_link_text'];
  return $summary;
}

/**
 * Implements hook_field_formatter_view().
 */
function pdf_reader_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $element = array();

  switch ($display['type']) {
    case 'pdf_reader_file':
      foreach ($items as $delta => $item) {
        $element[$delta]['#type'] = 'markup';
        $element[$delta]['#markup'] = theme('pdf_reader', array('file' => $item, 'settings' => $settings));
      }
      break;
  }

  return $element;
}

/**
 * Implements hook_theme().
 *
 * Use our theme function.
 */
function pdf_reader_theme($existing, $type, $theme, $path) {
  return array(
    'pdf_reader' => array(
      'variables' => array('file' => NULL, 'settings' => NULL)
    ),
    'pdf_js' => array(
      'template' => 'pdf_js',
      'path' => drupal_get_path('module', 'pdf_reader') . '/theme'
    )
  );
}

/**
 * Theme the field with pdf reader
 */
function theme_pdf_reader($variables) {
  $file_url = isset($variables['file']['uri']) ? file_create_url($variables['file']['uri']) : $variables['file']['value'];

  switch ($variables['settings']['renderer']) {
    case 'google':
      $output = '<iframe id="pdf_reader" src="//docs.google.com/viewer?embedded=true&url='
          . urlencode($file_url)
          . '" width="' . $variables['settings']['pdf_width']
          . '" height="' . $variables['settings']['pdf_height']
          . '" scrolling="no'
          . '" style="border: none;"></iframe>';
      break;

    case 'direct':
      $output = '<object id="pdf_reader" data="' . $file_url . '#view=Fit' . '" '
          . 'type="application/pdf' . '" '
          . 'width="' . $variables['settings']['pdf_width'] . '" '
          . 'height="' . $variables['settings']['pdf_height'] . '">'
          . '<embed src="' . $file_url . '#view=Fit' . '"'
          . 'width="' . $variables['settings']['pdf_width'] . '" '
          . 'height="' . $variables['settings']['pdf_height'] . '" '
          . 'type="application/pdf">'
          . '<p>' . t('It appears your Web browser is not configured to display PDF files. ')
          . l(t('Download adobe Acrobat '), 'http://www.adobe.com/products/reader.html')
          . ' ' . t('or') . ' ' . l(t('click here to download the PDF file.'), $file_url) . '</p>'
          . '</embed></object>';
      break;

    case 'pdf-js':
      _pdf_reader_load_pdf_js($file_url);
      $output = theme('pdf_js');
      break;
  }

  if ($variables['settings']['download'] == TRUE) {
    $output .= l(t('Click here to download the PDF file.'), $file_url, array('attributes' => array('class' => 'pdf-reader-download-link')));
  }

  if ($variables['settings']['colorbox'] == TRUE) {
    $html = $output;

    $options = array(
      'attributes' => array('class' => array('colorbox-inline', 'show')),
      'query' => array(
        'width' => $variables['settings']['pdf_width'],
        'height' => $variables['settings']['pdf_height'],
        'inline' => 'true',
      ),
      'fragment' => 'pdf_reader',
    );

    $output = l(t($variables['settings']['colorbox_link_text']), $file_url, $options);

    $output .= '<div style="display:none">' . $html . '</div>';
  }

  return $output;
}

function _pdf_reader_load_pdf_js($file_url) {
  $path = drupal_get_path('module', 'pdf_reader');
  $d_path = base_path() . $path;

  drupal_add_js("$path/js/pdf.js");
  drupal_add_js("PDFJS.workerSrc = '$d_path/js/pdf.js'", 'inline');
  drupal_add_js("$path/js/l10n.js");
  drupal_add_js("$path/js/compatibility.js");
  drupal_add_js("$path/js/viewer.js");
  drupal_add_css("$path/css/pdf_reader-viewer.css");

  drupal_add_js('kDefaultURL = "' . $file_url . '";', 'inline');
}
