print_epub.module 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * @file
  4. * Displays Printer-friendly versions of Drupal pages.
  5. *
  6. * @ingroup print
  7. */
  8. define('PRINT_EPUB_EPUB_TOOL_DEFAULT', 0);
  9. define('PRINT_EPUB_IMAGES_VIA_FILE_DEFAULT', 0);
  10. define('PRINT_EPUB_FILENAME_DEFAULT', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]');
  11. /**
  12. * Implements hook_print_link().
  13. */
  14. function print_epub_print_link() {
  15. return array(
  16. 'format' => 'epub',
  17. 'text' => t('EPUB version'),
  18. 'description' => t('Display a EPUB version of this page.'),
  19. 'path' => 'printepub',
  20. 'class' => 'print-epub',
  21. 'icon' => 'epub_icon.png',
  22. 'module' => 'print_epub',
  23. );
  24. }
  25. /**
  26. * Implements hook_permission().
  27. */
  28. function print_epub_permission() {
  29. return array(
  30. 'access EPUB version' => array(
  31. 'title' => t('Access the EPUB version'),
  32. 'description' => t('View the EPUB versions and the links to them in the original pages.'),
  33. ),
  34. );
  35. }
  36. /**
  37. * Implements hook_menu().
  38. */
  39. function print_epub_menu() {
  40. $link = print_epub_print_link();
  41. $items = array();
  42. $items[$link['path']] = array(
  43. 'title' => 'Printer-friendly EPUB',
  44. 'page callback' => 'print_epub_controller',
  45. 'access arguments' => array('access EPUB version'),
  46. 'type' => MENU_CALLBACK,
  47. 'file' => 'print_epub.pages.inc',
  48. );
  49. $items[$link['path'] . '/' . $link['path']] = array(
  50. 'access callback' => FALSE,
  51. );
  52. $items['admin/config/user-interface/print/epub'] = array(
  53. 'title' => 'EPUB',
  54. 'description' => 'Configure the settings of the EPUB generation functionality.',
  55. 'page callback' => 'drupal_get_form',
  56. 'page arguments' => array('print_epub_settings'),
  57. 'access arguments' => array('administer print'),
  58. 'weight' => 3,
  59. 'type' => MENU_LOCAL_TASK,
  60. 'file' => 'print_epub.admin.inc',
  61. );
  62. $items['admin/config/user-interface/print/epub/options'] = array(
  63. 'title' => 'Options',
  64. 'weight' => -1,
  65. 'type' => MENU_DEFAULT_LOCAL_TASK,
  66. );
  67. return $items;
  68. }
  69. /**
  70. * Implements hook_variable_info().
  71. */
  72. function print_epub_variable_info($options) {
  73. $link = print_epub_print_link();
  74. $variable['print_epub_link_text'] = array(
  75. 'title' => t('EPUB version'),
  76. 'description' => t('Text used in the link to the EPUB version.'),
  77. 'type' => 'string',
  78. 'default' => t($link['text']),
  79. );
  80. return $variable;
  81. }
  82. /**
  83. * Implements hook_block_info().
  84. */
  85. function print_epub_block_info() {
  86. $block['print_epub-top']['info'] = t('Most EPUBed');
  87. $block['print_epub-top']['cache'] = DRUPAL_CACHE_GLOBAL;
  88. return $block;
  89. }
  90. /**
  91. * Implements hook_block_view().
  92. */
  93. function print_epub_block_view($delta = 0) {
  94. $block = array();
  95. switch ($delta) {
  96. case 'print_epub-top':
  97. $block['subject'] = t('Most EPUBd');
  98. $result = db_query_range("SELECT path FROM {print_epub_page_counter} LEFT JOIN {node} n ON path = CONCAT('node/', n.nid) WHERE status <> 0 OR status IS NULL ORDER BY totalcount DESC", 0, 3)
  99. ->fetchAll();
  100. if (count($result)) {
  101. $items = array();
  102. foreach ($result as $obj) {
  103. $items[] = l(_print_get_title($obj->path), $obj->path);
  104. }
  105. $block['content'] = theme('item_list', array('items' => $items, 'type' => 'ul'));
  106. }
  107. break;
  108. }
  109. return $block;
  110. }
  111. /**
  112. * Implements hook_node_delete().
  113. */
  114. function print_epub_node_delete($node) {
  115. db_delete('print_epub_page_counter')
  116. ->condition('path', 'node/' . $node->nid)
  117. ->execute();
  118. }
  119. /**
  120. * Auxiliary function to display a formatted EPUB version link.
  121. *
  122. * Function made available so that developers may call this function from
  123. * their defined pages/blocks.
  124. *
  125. * @param string $path
  126. * path to be used in the link. If not specified, the current URL is used.
  127. * @param object $node
  128. * node object, to be used in checking node access. If the path argument is
  129. * not provided, the path used will be node/nid.
  130. * @param string $location
  131. * Where in the page where the link is being inserted ('link', 'corner',
  132. * 'block', 'help').
  133. *
  134. * @return bool
  135. * string with the HTML link to the printer-friendly page
  136. *
  137. * @ingroup print_api
  138. */
  139. function print_epub_insert_link($path = NULL, $node = NULL, $location = '') {
  140. if (function_exists('print_ui_insert_link')) {
  141. return print_ui_insert_link(print_epub_print_link(), array(
  142. 'path' => $path,
  143. 'node' => $node,
  144. 'location' => $location,
  145. ));
  146. }
  147. else {
  148. return FALSE;
  149. }
  150. }
  151. /**
  152. * Check if the link to the EPUB version is allowed depending on the settings.
  153. *
  154. * @param array $args
  155. * Array containing the possible parameters:
  156. * view_mode, node, type, path.
  157. *
  158. * @return bool
  159. * FALSE if not allowed, TRUE otherwise
  160. */
  161. function print_epub_link_allowed($args) {
  162. $print_epub_epub_tool = variable_get('print_epub_epub_tool', PRINT_EPUB_EPUB_TOOL_DEFAULT);
  163. return (user_access('access EPUB version') && (!empty($print_epub_epub_tool)));
  164. }
  165. /**
  166. * Generate a EPUB version of the provided HTML.
  167. *
  168. * @param string $html
  169. * HTML content of the EPUB.
  170. * @param array $meta
  171. * Meta information to be used in the EPUB
  172. * - url: original URL
  173. * - name: author's name
  174. * - title: Page title
  175. * - node: node object.
  176. * @param string $filename
  177. * (optional) Filename of the generated EPUB.
  178. *
  179. * @return string|null
  180. * generated EPUB page, or NULL in case of error
  181. *
  182. * @see print_epub_controller()
  183. *
  184. * @ingroup print_api
  185. */
  186. function print_epub_generate_html($html, $meta, $filename = NULL) {
  187. $epub_tool = explode('|', variable_get('print_epub_epub_tool', PRINT_EPUB_EPUB_TOOL_DEFAULT));
  188. module_load_include('inc', $epub_tool[0], $epub_tool[0] . '.pages');
  189. $function = $epub_tool[0] . '_print_epub_generate';
  190. if (function_exists($function)) {
  191. return $function($html, $meta, $filename);
  192. }
  193. return NULL;
  194. }
  195. /**
  196. * Implements hook_views_api().
  197. */
  198. function print_epub_views_api() {
  199. return array(
  200. 'api' => 2.0,
  201. 'path' => drupal_get_path('module', 'print_epub'),
  202. );
  203. }