print.module 10.0 KB

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