print_epub.module 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. switch ($delta) {
  95. case 'print_epub-top':
  96. $block['subject'] = t('Most EPUBd');
  97. $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)
  98. ->fetchAll();
  99. if (count($result)) {
  100. $items = array();
  101. foreach ($result as $obj) {
  102. $items[] = l(_print_get_title($obj->path), $obj->path);
  103. }
  104. $block['content'] = theme('item_list', array('items' => $items, 'type' => 'ul'));
  105. }
  106. break;
  107. }
  108. return $block;
  109. }
  110. /**
  111. * Implements hook_requirements().
  112. */
  113. function print_epub_requirements($phase) {
  114. $requirements = array();
  115. $t = get_t();
  116. switch ($phase) {
  117. // At runtime, make sure that a EPUB generation tool is selected
  118. case 'runtime':
  119. $print_epub_epub_tool = variable_get('print_epub_epub_tool', PRINT_EPUB_EPUB_TOOL_DEFAULT);
  120. if (empty($print_epub_epub_tool)) {
  121. $requirements['print_epub_tool'] = array(
  122. 'title' => $t('Printer, email and EPUB versions - EPUB generation library'),
  123. 'value' => $t('No EPUB tool selected'),
  124. 'description' => $t('Please configure it in the !url.', array('!url' => l($t('EPUB settings page'), 'admin/config/user-interface/print/epub'))),
  125. 'severity' => REQUIREMENT_ERROR,
  126. );
  127. }
  128. else {
  129. $tool = explode('|', $print_epub_epub_tool);
  130. if (!is_file($tool[1]) || !is_readable($tool[1])) {
  131. $requirements['print_epub_tool'] = array(
  132. 'title' => $t('Printer, email and EPUB versions - EPUB generation library'),
  133. 'value' => $t('File not found'),
  134. 'description' => $t('The currently selected EPUB generation library (%file) is no longer accessible.', array('%file' => $tool[1])),
  135. 'severity' => REQUIREMENT_ERROR,
  136. );
  137. }
  138. }
  139. break;
  140. }
  141. return $requirements;
  142. }
  143. /**
  144. * Implements hook_node_delete().
  145. */
  146. function print_epub_node_delete($node) {
  147. db_delete('print_epub_page_counter')
  148. ->condition('path', 'node/' . $node->nid)
  149. ->execute();
  150. }
  151. /**
  152. * Auxiliary function to display a formatted EPUB version link
  153. *
  154. * Function made available so that developers may call this function from
  155. * their defined pages/blocks.
  156. *
  157. * @param string $path
  158. * path to be used in the link. If not specified, the current URL is used.
  159. * @param object $node
  160. * node object, to be used in checking node access. If the path argument is
  161. * not provided, the path used will be node/nid.
  162. * @param string $location
  163. * where in the page where the link is being inserted ('link', 'corner',
  164. * 'block', 'help').
  165. *
  166. * @return bool
  167. * string with the HTML link to the printer-friendly page
  168. *
  169. * @ingroup print_api
  170. */
  171. function print_epub_insert_link($path = NULL, $node = NULL, $location = '') {
  172. if (function_exists('print_ui_insert_link')) {
  173. return print_ui_insert_link(print_epub_print_link(), array('path' => $path, 'node' => $node, 'location' => $location));
  174. }
  175. else {
  176. return FALSE;
  177. }
  178. }
  179. /**
  180. * Check if the link to the EPUB version is allowed depending on the settings
  181. *
  182. * @param array $args
  183. * array containing the possible parameters:
  184. * view_mode, node, type, path
  185. *
  186. * @return bool
  187. * FALSE if not allowed, TRUE otherwise
  188. */
  189. function print_epub_link_allowed($args) {
  190. $print_epub_epub_tool = variable_get('print_epub_epub_tool', PRINT_EPUB_EPUB_TOOL_DEFAULT);
  191. return (user_access('access EPUB version') && (!empty($print_epub_epub_tool)));
  192. }
  193. /**
  194. * Generate a EPUB version of the provided HTML.
  195. *
  196. * @param string $html
  197. * HTML content of the EPUB
  198. * @param array $meta
  199. * Meta information to be used in the EPUB
  200. * - url: original URL
  201. * - name: author's name
  202. * - title: Page title
  203. * - node: node object
  204. * @param string $filename
  205. * (optional) Filename of the generated EPUB
  206. *
  207. * @return
  208. * generated EPUB page, or NULL in case of error
  209. *
  210. * @see print_epub_controller()
  211. *
  212. * @ingroup print_api
  213. */
  214. function print_epub_generate_html($html, $meta, $filename = NULL) {
  215. $epub_tool = explode('|', variable_get('print_epub_epub_tool', PRINT_EPUB_EPUB_TOOL_DEFAULT));
  216. module_load_include('inc', $epub_tool[0], $epub_tool[0] . '.pages');
  217. $function = $epub_tool[0] . '_print_epub_generate';
  218. if (function_exists($function)) {
  219. return $function($html, $meta, $filename);
  220. }
  221. return NULL;
  222. }
  223. /**
  224. * Implements hook_views_api().
  225. */
  226. function print_epub_views_api() {
  227. return array(
  228. 'api' => 2.0,
  229. 'path' => drupal_get_path('module', 'print_epub'),
  230. );
  231. }