Project

General

Profile

Paste
Download (3.69 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / print / print_epub / print_epub.pages.inc @ 76bdcd04

1
<?php
2

    
3
/**
4
 * @file
5
 * Generates the EPUB versions of the pages.
6
 *
7
 * This file is included by the print_epub module and includes the
8
 * functions that interface with the EPUB generation packages.
9
 *
10
 * @ingroup print
11
 */
12

    
13
module_load_include('inc', 'print', 'print.pages');
14

    
15
/**
16
 * Generate a EPUB version of the printer-friendly page.
17
 *
18
 * @see print_controller()
19
 * @see _print_epub_domepub()
20
 * @see _print_epub_tcepub()
21
 */
22
function print_epub_controller() {
23
  // Disable caching for generated EPUBs, as Drupal doesn't ouput the proper
24
  // headers from the cache.
25
  $GLOBALS['conf']['cache'] = FALSE;
26

    
27
  $args = func_get_args();
28
  $path = filter_xss(implode('/', $args));
29
  $cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
30

    
31
  // Handle the query.
32
  $query = $_GET;
33
  unset($query['q']);
34

    
35
  $node = NULL;
36
  if (!empty($path)) {
37
    if ($alias = drupal_lookup_path('source', $path)) {
38
      // Alias.
39
      $path_arr = explode('/', $alias);
40
      $node = node_load($path_arr[1]);
41
    }
42
    elseif (ctype_digit($args[0])) {
43
      // Normal nid.
44
      $node = node_load($args[0]);
45
    }
46

    
47
    $epub_filename = variable_get('print_epub_filename', PRINT_EPUB_FILENAME_DEFAULT);
48
    if (!empty($epub_filename) && !empty($node)) {
49
      $epub_filename = token_replace($epub_filename, array('node' => $node), array('clear' => TRUE));
50
    }
51
    else {
52
      $epub_filename = token_replace($epub_filename, array('site'), array('clear' => TRUE));
53
      if (empty($epub_filename)) {
54
        // If empty, use a fallback solution.
55
        $epub_filename = str_replace('/', '_', $path);
56
      }
57
    }
58
  }
59
  else {
60
    $epub_filename = 'page';
61
  }
62

    
63
  if (function_exists('transliteration_clean_filename')) {
64
    $epub_filename = transliteration_clean_filename($epub_filename, language_default('language'));
65
  }
66

    
67
  drupal_alter('print_epub_filename', $epub_filename, $path);
68

    
69
  $epub = print_epub_generate_path($path, $query, $cid, $epub_filename . '.epub');
70
  if ($epub == NULL) {
71
    drupal_goto($path);
72
    exit;
73
  }
74

    
75
  $nodepath = (isset($node->nid)) ? 'node/' . $node->nid : drupal_get_normal_path($path);
76
  db_merge('print_epub_page_counter')
77
    ->key(array('path' => $nodepath))
78
    ->fields(array(
79
      'totalcount' => 1,
80
      'timestamp' => REQUEST_TIME,
81
    ))
82
    ->expression('totalcount', 'totalcount + 1')
83
    ->execute();
84

    
85
  drupal_exit();
86
}
87

    
88
/**
89
 * Gennerate a EPUB for a given Drupal path.
90
 *
91
 * @param string $path
92
 *   path of the page to convert to EPUB.
93
 * @param array $query
94
 *   (Optional) array of key/value pairs as used in the url() function for the
95
 *   query.
96
 * @param int $cid
97
 *   (Optional) comment ID of the comment to render.
98
 * @param string $epub_filename
99
 *   (Optional) filename of the generated EPUB.
100
 * @param string $view_mode
101
 *   (Optional) view mode to be used when rendering the content.
102
 *
103
 * @return string|null
104
 *   generated EPUB page, or NULL in case of error
105
 *
106
 * @see print_epub_controller()
107
 */
108
function print_epub_generate_path($path, $query = NULL, $cid = NULL, $epub_filename = NULL, $view_mode = PRINT_VIEW_MODE) {
109
  $link = print_epub_print_link();
110
  $node = print_controller($path, $link['format'], $cid, $view_mode);
111
  if ($node) {
112
    $html = theme('print', array(
113
      'node' => $node,
114
      'query' => $query,
115
      'expand_css' => TRUE,
116
      'format' => $link['format'],
117
    ));
118

    
119
    $meta = array(
120
      'node' => $node,
121
      'url' => url(drupal_get_path_alias(empty($node->nid) ? $node->path : "node/$node->nid"), array('absolute' => TRUE)),
122
    );
123
    if (isset($node->name)) {
124
      $meta['name'] = $node->name;
125
    }
126
    if (isset($node->title)) {
127
      $meta['title'] = $node->title;
128
    }
129

    
130
    return print_epub_generate_html($html, $meta, $epub_filename);
131
  }
132
  else {
133
    return NULL;
134
  }
135
}