You are here

function theme_epub_formatter_default in Epub 7

1 theme call to theme_epub_formatter_default()
epub_field_formatter_view in includes/epub.field.inc
Implements hook_field_formatter_view().

File

includes/epub.field.inc, line 251

Code

function theme_epub_formatter_default($variables) {
  $file_url = file_create_url($variables['file']->uri);
  $filename = $variables['file']->filename;
  $fid = 'fid-' . $variables['file']->fid;
  if (file_entity_access('download', $variables['file'])) {
    $download_link = t('Download: ') . l($filename, $file_url);
  }
  $dir = 'public://epub_content/' . $variables['file']->fid;
  $file = drupal_realpath($variables['file']->uri);
  $output = $download_link . t('<p>This eBook can\'t be opened.</p>');
  $scan = file_scan_directory($dir, '/.*\\.opf$/');
  $opf = array_shift($scan);
  if (isset($opf)) {
    $opfXML = simplexml_load_file($opf->uri);
    $namespaces = $opfXML
      ->getDocNamespaces();
    $opfXML
      ->registerXPathNamespace('opf', 'http://www.idpf.org/2007/opf');
    $items = array();
    foreach ($opfXML
      ->xpath('/opf:package/opf:spine/opf:itemref') as $key => $value) {
      $attributes = $value
        ->attributes();
      $id = (string) $attributes['idref'];
      $items[$key] = file_create_url(epub_get_item($dir, $id));
    }
    $query = drupal_get_query_parameters();
    if (isset($query['page'])) {
      $current_page = $query['page'];
    }
    else {
      $current_page = 0;
    }
    $total = count($items);
    pager_default_initialize($total, 1);
    $iframe = array(
      '#type' => 'html_tag',
      '#tag' => 'iframe',
      '#value' => '',
      '#attributes' => array(
        'class' => array(
          'epub',
          'ebook',
        ),
        'id' => array(
          $fid,
        ),
        'webkitallowfullscreen' => '',
        'mozallowfullscreen' => '',
        'allowfullscreen' => '',
        'name' => 'epub',
        'width' => $variables['width'] ? $variables['width'] : '100%',
        'height' => $variables['height'] ? $variables['height'] : '100%',
        'src' => $items[$current_page],
      ),
    );
    $output = $download_link . render($iframe) . theme('pager');
  }
  drupal_add_css(drupal_get_path('module', 'epub') . '/css/epub.css');
  drupal_add_js(drupal_get_path('module', 'epub') . '/js/epub.js');
  if ($variables['height'] == 'auto') {
    drupal_add_js('Drupal.behaviors.epub.resizeIframe();', array(
      'type' => 'inline',
      'scope' => 'footer',
    ));
  }
  return '<div id="ebook" class="epub">' . $output . '</div>';
}