print.module 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * @file
  4. * Displays Printer-friendly versions of Drupal pages.
  5. *
  6. * This is the core module of the PF package, with the Drupal hooks
  7. * and other administrative functions.
  8. *
  9. * @ingroup print
  10. */
  11. define('PRINT_VIEW_MODE', 'print');
  12. define('PRINT_LOGO_OPTIONS_DEFAULT', 1);
  13. define('PRINT_LOGO_URL_DEFAULT', '');
  14. define('PRINT_FOOTER_OPTIONS_DEFAULT', 1);
  15. define('PRINT_FOOTER_USER_DEFAULT', '');
  16. define('PRINT_CSS_DEFAULT', '');
  17. define('PRINT_KEEP_THEME_CSS_DEFAULT', 0);
  18. define('PRINT_URLS_DEFAULT', 1);
  19. define('PRINT_URLS_ANCHORS_DEFAULT', 0);
  20. define('PRINT_COMMENTS_DEFAULT', 0);
  21. define('PRINT_NEWWINDOW_DEFAULT', 1);
  22. define('PRINT_TYPE_URLLIST_DEFAULT', 1);
  23. define('PRINT_TYPE_SYS_URLLIST_DEFAULT', 0);
  24. define('PRINT_TYPE_LINK_TEXT_ENABLED_DEFAULT', 0);
  25. define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
  26. define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);
  27. define('PRINT_HTML_WINDOWCLOSE_DEFAULT', 1);
  28. define('PRINT_SOURCEURL_ENABLED_DEFAULT', 1);
  29. define('PRINT_SOURCEURL_DATE_DEFAULT', 0);
  30. define('PRINT_SOURCEURL_FORCENODE_DEFAULT', 0);
  31. define('PRINT_ROBOTS_NOINDEX_DEFAULT', 1);
  32. define('PRINT_ROBOTS_NOFOLLOW_DEFAULT', 1);
  33. define('PRINT_ROBOTS_NOARCHIVE_DEFAULT', 0);
  34. define('PRINT_LIB_PATH', 'sites/all/libraries');
  35. /**
  36. * Implements hook_print_link().
  37. */
  38. function print_print_link() {
  39. return array(
  40. 'format' => 'html',
  41. 'text' => t('Printer-friendly version'),
  42. 'description' => t('Display a printer-friendly version of this page.'),
  43. 'path' => 'print',
  44. 'class' => 'print-page',
  45. 'icon' => 'print_icon.png',
  46. 'module' => 'print',
  47. );
  48. }
  49. /**
  50. * Implements hook_print_new_window_alter().
  51. */
  52. function print_print_new_window_alter(&$new_window, $format) {
  53. $new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
  54. }
  55. /**
  56. * Implements hook_permission().
  57. */
  58. function print_permission() {
  59. return array(
  60. 'administer print' => array(
  61. 'title' => t('Administer the module'),
  62. 'description' => t('Perform maintenance tasks for the print module.'),
  63. ),
  64. 'access print' => array(
  65. 'title' => t('Access the printer-friendly page'),
  66. 'description' => t('View the printer-friendly pages and the links to them in the original pages.'),
  67. ),
  68. );
  69. }
  70. /**
  71. * Implements hook_theme().
  72. */
  73. function print_theme() {
  74. return array(
  75. 'print' => array(
  76. 'variables' => array('node' => NULL, 'query' => NULL, 'format' => '', 'expand_css' => FALSE, 'message' => ''),
  77. 'template' => 'print',
  78. 'file' => 'print.pages.inc',
  79. ),
  80. 'print_published' => array(
  81. 'variables' => array(),
  82. 'file' => 'print.pages.inc',
  83. ),
  84. 'print_breadcrumb' => array(
  85. 'variables' => array('node' => NULL),
  86. 'file' => 'print.pages.inc',
  87. ),
  88. 'print_footer' => array(
  89. 'variables' => array(),
  90. 'file' => 'print.pages.inc',
  91. ),
  92. 'print_sourceurl' => array(
  93. 'variables' => array('url' => '', 'node' => NULL, 'cid' => NULL),
  94. 'file' => 'print.pages.inc',
  95. ),
  96. 'print_url_list' => array(
  97. 'variables' => array(),
  98. 'file' => 'print.pages.inc',
  99. ),
  100. );
  101. }
  102. /**
  103. * Implements hook_preprocess_HOOK().
  104. */
  105. function print_preprocess_node(&$variables) {
  106. if (($variables['elements']['#view_mode'] == PRINT_VIEW_MODE) && isset($variables['elements']['#print_format'])) {
  107. $type = $variables['elements']['#node']->type;
  108. $format = $variables['elements']['#print_format'];
  109. $nid = $variables['elements']['#node']->nid;
  110. $variables['theme_hook_suggestions'][] = "node__print";
  111. $variables['theme_hook_suggestions'][] = "node__print__{$format}";
  112. $variables['theme_hook_suggestions'][] = "node__print__{$format}__node__{$type}";
  113. $variables['theme_hook_suggestions'][] = "node__print__{$format}__node__{$nid}";
  114. }
  115. }
  116. /**
  117. * Implements hook_menu().
  118. */
  119. function print_menu() {
  120. $link = print_print_link();
  121. $items = array();
  122. $items[$link['path']] = array(
  123. 'title' => 'Printer-friendly',
  124. 'page callback' => 'print_controller_html',
  125. 'access arguments' => array('access print'),
  126. 'type' => MENU_CALLBACK,
  127. 'file' => 'print.pages.inc',
  128. );
  129. $items[$link['path'] . '/' . $link['path']] = array(
  130. 'access callback' => FALSE,
  131. );
  132. $items['admin/config/user-interface/print'] = array(
  133. 'title' => 'Printer, email and PDF versions',
  134. 'description' => 'Adds a printer-friendly version link to content and administrative pages.',
  135. 'page callback' => 'drupal_get_form',
  136. 'page arguments' => array('print_html_settings'),
  137. 'access arguments' => array('administer print'),
  138. 'file' => 'print.admin.inc',
  139. );
  140. $items['admin/config/user-interface/print/html'] = array(
  141. 'title' => 'Web page',
  142. 'weight' => 1,
  143. 'type' => MENU_DEFAULT_LOCAL_TASK,
  144. );
  145. $items['admin/config/user-interface/print/common'] = array(
  146. 'title' => 'Settings',
  147. 'description' => 'Settings for the common functionalities for all the print sub-modules.',
  148. 'page callback' => 'drupal_get_form',
  149. 'page arguments' => array('print_main_settings'),
  150. 'access arguments' => array('administer print'),
  151. 'weight' => 10,
  152. 'type' => MENU_LOCAL_TASK,
  153. 'file' => 'print.admin.inc',
  154. );
  155. return $items;
  156. }
  157. /**
  158. * Implements hook_variable_info().
  159. */
  160. function print_variable_info($options) {
  161. $link = print_print_link();
  162. $variable['print_html_link_text'] = array(
  163. 'title' => t('Printer-friendly version'),
  164. 'description' => t('Text used in the link to the printer-friendly version.'),
  165. 'type' => 'string',
  166. 'default' => t($link['text']),
  167. );
  168. return $variable;
  169. }
  170. /**
  171. * Implements hook_block_info().
  172. */
  173. function print_block_info() {
  174. $block['print-top']['info'] = t('Most printed');
  175. $block['print-top']['cache'] = DRUPAL_CACHE_GLOBAL;
  176. return $block;
  177. }
  178. /**
  179. * Implements hook_block_view().
  180. */
  181. function print_block_view($delta = '') {
  182. $block = array();
  183. switch ($delta) {
  184. case 'print-top':
  185. $block['subject'] = t('Most printed');
  186. $result = db_query_range("SELECT path FROM {print_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)
  187. ->fetchAll();
  188. if (count($result)) {
  189. $items = array();
  190. foreach ($result as $obj) {
  191. $items[] = l(_print_get_title($obj->path), $obj->path);
  192. }
  193. $block['content'] = theme('item_list', array('items' => $items, 'type' => 'ul'));
  194. }
  195. break;
  196. }
  197. return $block;
  198. }
  199. /**
  200. * Implements hook_help().
  201. */
  202. function print_help($path, $arg) {
  203. switch ($path) {
  204. case 'admin/help#print':
  205. // Return a line-break version of the module README
  206. return _filter_autop(file_get_contents(drupal_get_path('module', 'print') . '/README.txt'));
  207. }
  208. }
  209. /**
  210. * Implements hook_node_delete().
  211. */
  212. function print_node_delete($node) {
  213. db_delete('print_page_counter')
  214. ->condition('path', 'node/' . $node->nid)
  215. ->execute();
  216. }
  217. /**
  218. * Implements hook_entity_info_alter().
  219. */
  220. function print_entity_info_alter(&$info) {
  221. // Add the 'Print' view mode for nodes.
  222. $info['node']['view modes'] += array(
  223. PRINT_VIEW_MODE => array(
  224. 'label' => t('Print'),
  225. 'custom settings' => FALSE,
  226. ),
  227. );
  228. // Add the 'Print' view mode for field_collections
  229. if (module_exists('field_collection')) {
  230. $info['field_collection_item']['view modes'] += array(
  231. PRINT_VIEW_MODE => array(
  232. 'label' => t('Print'),
  233. 'custom settings' => FALSE,
  234. ),
  235. );
  236. }
  237. }
  238. /**
  239. * Auxiliary function to discover a given page's title
  240. *
  241. * @param string $path
  242. * path of the page being identified
  243. *
  244. * @return string
  245. * string with the page's title
  246. */
  247. function _print_get_title($path) {
  248. $path = drupal_get_normal_path($path);
  249. $nid = preg_replace('!^node/!', '', $path);
  250. if (ctype_digit($nid)) {
  251. return db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $nid))
  252. ->fetchField();
  253. }
  254. else {
  255. // Not a node, try to get title from the menu system
  256. $menu_item = menu_get_item($path);
  257. if (!empty($menu_item['title'])) {
  258. return $menu_item['title'];
  259. }
  260. elseif (drupal_substr($menu_item['page_callback'], 0, 6) == 'views_') {
  261. // It's a view, load the view to have access to the title
  262. $view = views_get_view($menu_item['page_arguments']['0']);
  263. return $view->get_title();
  264. }
  265. else {
  266. return NULL;
  267. }
  268. }
  269. }
  270. /**
  271. * Auxiliary function to display a formatted Printer-friendly link
  272. *
  273. * Function made available so that developers may call this function from
  274. * their defined pages/blocks.
  275. *
  276. * @param string $path
  277. * path to be used in the link. If not specified, the current URL is used.
  278. * @param object $node
  279. * node object, to be used in checking node access. If the path argument is
  280. * not provided, the path used will be node/nid.
  281. * @param string $location
  282. * where in the page where the link is being inserted ('link', 'corner',
  283. * 'block', 'help').
  284. *
  285. * @return string
  286. * HTML link to the printer-friendly page
  287. *
  288. * @ingroup print_api
  289. */
  290. function print_insert_link($path = NULL, $node = NULL, $location = '') {
  291. if (function_exists('print_ui_insert_link')) {
  292. return print_ui_insert_link(print_print_link(), array('path' => $path, 'node' => $node, 'location' => $location));
  293. }
  294. else {
  295. return FALSE;
  296. }
  297. }
  298. /**
  299. * Check if the link to the PF version is allowed depending on the settings
  300. *
  301. * @param array $args
  302. * array containing the possible parameters:
  303. * view_mode, node, type, path
  304. *
  305. * @return bool
  306. * FALSE if not allowed, TRUE otherwise
  307. */
  308. function print_link_allowed($args) {
  309. return (user_access('access print'));
  310. }
  311. /**
  312. * Implements hook_contextual_links_view_alter().
  313. */
  314. function print_contextual_links_view_alter(&$element, $items) {
  315. // Hide all contextual links
  316. if (preg_match('!^print!', $_GET['q'])) {
  317. unset($element['#links']);
  318. }
  319. }
  320. /**
  321. * Implements hook_views_api().
  322. */
  323. function print_views_api() {
  324. return array(
  325. 'api' => 2.0,
  326. 'path' => drupal_get_path('module', 'print'),
  327. );
  328. }