print_ui.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. <?php
  2. /**
  3. * @file
  4. * Printer-friendly pages User Interface module.
  5. *
  6. * This module handles the display of the printer-friendly sub-module links.
  7. *
  8. * @ingroup print
  9. */
  10. define('PRINT_UI_LINK_POS_DEFAULT', '{ "link": "link", "block": "block", "help": "help" }');
  11. define('PRINT_UI_LINK_TEASER_DEFAULT', 0);
  12. define('PRINT_UI_SHOW_LINK_DEFAULT', 1);
  13. define('PRINT_UI_SYS_LINK_VISIBILITY_DEFAULT', 1);
  14. define('PRINT_UI_SYS_LINK_PAGES_DEFAULT', '');
  15. define('PRINT_UI_LINK_USE_ALIAS_DEFAULT', 0);
  16. define('PRINT_UI_BOOK_LINK_DEFAULT', 1);
  17. define('PRINT_UI_ALLOW_NORMAL_LINK', 1);
  18. define('PRINT_UI_ALLOW_BOOK_LINK', 2);
  19. define('PRINT_UI_TYPE_FIELDS_WEIGHT', 30);
  20. define('PRINT_UI_TYPE_SHOW_LINK_DEFAULT', 1);
  21. define('PRINT_UI_TYPE_COMMENT_LINK_DEFAULT', 0);
  22. /**
  23. * Implements hook_permission().
  24. */
  25. function print_ui_permission() {
  26. return array(
  27. 'node-specific print configuration' => array(
  28. 'title' => t('Node-specific configuration'),
  29. 'description' => t('Enable access to the per-node settings.'),
  30. ),
  31. );
  32. }
  33. /**
  34. * Implements hook_theme().
  35. */
  36. function print_ui_theme() {
  37. return array(
  38. 'print_ui_format_link' => array(
  39. 'variables' => array('format' => '', 'location' => ''),
  40. ),
  41. 'print_ui_settings' => array(
  42. 'render element' => 'form',
  43. 'file' => 'print_ui.admin.inc',
  44. ),
  45. );
  46. }
  47. /**
  48. * Implements hook_menu().
  49. */
  50. function print_ui_menu() {
  51. $items = array();
  52. $items['admin/config/user-interface/print/ui'] = array(
  53. 'title' => 'Links',
  54. 'description' => 'Configure the print module links.',
  55. 'page callback' => 'drupal_get_form',
  56. 'page arguments' => array('print_ui_settings'),
  57. 'access arguments' => array('administer print'),
  58. 'weight' => 9,
  59. 'type' => MENU_LOCAL_TASK,
  60. 'file' => 'print_ui.admin.inc',
  61. );
  62. return $items;
  63. }
  64. /**
  65. * Implements hook_block_info().
  66. */
  67. function print_ui_block_info() {
  68. $block['print-links']['info'] = t('Printer, email and PDF versions');
  69. $block['print-links']['cache'] = DRUPAL_CACHE_PER_PAGE;
  70. return $block;
  71. }
  72. /**
  73. * Implements hook_block_view().
  74. */
  75. function print_ui_block_view($delta = '') {
  76. $block = array();
  77. switch ($delta) {
  78. case 'print-links':
  79. $nid = preg_replace('!^node/!', '', $_GET['q']);
  80. if (ctype_digit($nid)) {
  81. $node = node_load($nid);
  82. if (!node_access('view', $node)) {
  83. // If the user doesn't have access to the node, don't show any links
  84. $block['content'] = '';
  85. return;
  86. }
  87. }
  88. else {
  89. $node = NULL;
  90. }
  91. $block['content'] = '';
  92. foreach (module_implements('print_link') as $module) {
  93. $function = $module . '_print_link';
  94. if (function_exists($function)) {
  95. $link = call_user_func_array($function, array());
  96. $link_pos = variable_get('print_' . $link['format'] . '_link_pos', drupal_json_decode(PRINT_UI_LINK_POS_DEFAULT));
  97. if (!(empty($link_pos['block']))) {
  98. $links = print_ui_insert_link($link, array('node' => $node, 'location' => 'block'));
  99. if (!empty($links)) {
  100. $block['content'] .= $links;
  101. }
  102. }
  103. }
  104. }
  105. break;
  106. }
  107. return $block;
  108. }
  109. /**
  110. * Implements hook_help().
  111. */
  112. function print_ui_help($path, $arg) {
  113. $links = '';
  114. if (($path !== 'node/%') && ($path !== 'node/%/revisions/%/view')) {
  115. static $output = FALSE;
  116. if ($output === FALSE) {
  117. $output = TRUE;
  118. foreach (module_implements('print_link') as $module) {
  119. $function = $module . '_print_link';
  120. if (function_exists($function)) {
  121. $link = call_user_func_array($function, array());
  122. $link_pos = variable_get('print_' . $link['format'] . '_link_pos', drupal_json_decode(PRINT_UI_LINK_POS_DEFAULT));
  123. if (!empty($link_pos['help'])) {
  124. $links .= print_ui_insert_link($link, array('location' => 'help'));
  125. }
  126. }
  127. }
  128. if ($links) {
  129. return "<span class='print-syslink'>$links</span>";
  130. }
  131. }
  132. }
  133. }
  134. /**
  135. * Implements hook_node_view_alter().
  136. */
  137. function print_ui_node_view_alter(&$build) {
  138. if (isset($build['links']['book']['#links']['book_printer'])) {
  139. $book_link = variable_get('print_html_book_link', PRINT_UI_BOOK_LINK_DEFAULT);
  140. if ($book_link) {
  141. $link = print_print_link();
  142. $link_pos = variable_get('print_html_link_pos', drupal_json_decode(PRINT_UI_LINK_POS_DEFAULT));
  143. if (!empty($link_pos['link'])) {
  144. $format = theme('print_ui_format_link', array('format' => 'html', 'location' => 'link'));
  145. switch ($book_link) {
  146. case 1:
  147. $path = $build['links']['book']['#links']['book_printer']['href'];
  148. break;
  149. case 2:
  150. $link_use_alias = variable_get('print_html_link_use_alias', PRINT_UI_LINK_USE_ALIAS_DEFAULT);
  151. $path = ($link_use_alias && ($alias = drupal_lookup_path('alias', 'node/' . $build['#node']->nid))) ? $alias : $build['#node']->nid;
  152. break;
  153. }
  154. $build['links']['book']['#links']['book_printer'] = array(
  155. 'href' => $link['path'] . '/' . $path,
  156. 'title' => $format['text'],
  157. 'attributes' => $format['attributes'],
  158. 'html' => $format['html'],
  159. );
  160. }
  161. else {
  162. unset($build['links']['book']['#links']['book_printer']);
  163. }
  164. }
  165. }
  166. }
  167. /**
  168. * Implements hook_node_view().
  169. */
  170. function print_ui_node_view($node, $view_mode) {
  171. $corner_markup = '';
  172. foreach (module_implements('print_link') as $module) {
  173. $function = $module . '_print_link';
  174. if (function_exists($function)) {
  175. $link = call_user_func_array($function, array());
  176. $link_pos = variable_get('print_' . $link['format'] . '_link_pos', drupal_json_decode(PRINT_UI_LINK_POS_DEFAULT));
  177. $link_use_alias = variable_get('print_' . $link['format'] . '_link_use_alias', PRINT_UI_LINK_USE_ALIAS_DEFAULT);
  178. if (!preg_match('!^node/[\d]*/revisions/[\d]*/view$!', $_GET['q'])) {
  179. // Not a revision, use node->nid to build the path
  180. $path = (($link_use_alias) && ($alias = drupal_lookup_path('alias', 'node/' . $node->nid))) ? $alias : $node->nid;
  181. }
  182. else {
  183. // This is a node revision, replace only the node component
  184. $path = preg_replace('!^node/!', '', $_GET['q']);
  185. }
  186. $path = $link['path'] . '/' . $path;
  187. foreach (array('node', 'comment') as $type) {
  188. $allowed_type = print_ui_link_allowed($link, array('type' => $type, 'node' => $node, 'view_mode' => $view_mode));
  189. if ($allowed_type) {
  190. drupal_add_css(drupal_get_path('module', 'print_ui') . '/css/print_ui.theme.css');
  191. $links = array();
  192. $format = theme('print_ui_format_link', array('format' => $link['format'], 'location' => 'link'));
  193. // Show book link
  194. if ($allowed_type === PRINT_UI_ALLOW_BOOK_LINK) {
  195. $path = $link['path'] . '/book/export/html/' . $node->nid;
  196. $links['book_' . $link['format']] = array(
  197. 'href' => $path,
  198. 'title' => $format['text'],
  199. 'attributes' => $format['attributes'],
  200. 'html' => $format['html'],
  201. );
  202. }
  203. elseif ($allowed_type === PRINT_UI_ALLOW_NORMAL_LINK) {
  204. $links['print_' . $link['format']] = array(
  205. 'href' => $path,
  206. 'title' => $format['text'],
  207. 'attributes' => $format['attributes'],
  208. 'html' => $format['html'],
  209. 'query' => _print_ui_query_string_encode($_GET, array('q')),
  210. );
  211. }
  212. $link_content = array(
  213. '#theme' => 'links',
  214. '#links' => $links,
  215. '#attributes' => array('class' => array('links', 'inline')),
  216. );
  217. // If it's a node, and configured to show the link, but it's not the html version of a book then show it
  218. if (($type == 'node') && !empty($link_pos['link']) && !(isset($node->book) && ($link['format'] == 'html'))) {
  219. $node->content['links']['print_' . $link['format']] = $link_content;
  220. }
  221. elseif (($type == 'comment') && isset($node->content['comments']['comments'])) {
  222. foreach ($node->content['comments']['comments'] as $cid => $comment) {
  223. if (is_numeric($cid)) {
  224. $link_content['#links']['print_' . $link['format']]['query']['comment'] = $cid;
  225. $node->content['comments']['comments'][$cid]['links']['print_' . $link['format']] = $link_content;
  226. }
  227. }
  228. }
  229. }
  230. }
  231. if (!empty($link_pos['corner'])) {
  232. $corner_markup .= print_ui_insert_link($link, array('node' => $node, 'path' => $path, 'location' => 'corner'));
  233. }
  234. }
  235. }
  236. if (($view_mode == 'full') && (!empty($corner_markup))) {
  237. // Insert content corner links
  238. $node->content['print_links'] = array(
  239. '#prefix' => '<span class="print-link">',
  240. '#markup' => $corner_markup,
  241. '#suffix' => '</span>',
  242. '#weight' => -101,
  243. );
  244. }
  245. }
  246. /**
  247. * Implements hook_node_load().
  248. */
  249. function print_ui_node_load($nodes, $types) {
  250. $ids = array();
  251. foreach ($nodes as $node) {
  252. $ids[] = $node->nid;
  253. }
  254. foreach (module_implements('print_link') as $module) {
  255. $function = $module . '_print_link';
  256. if (function_exists($function)) {
  257. $link = call_user_func_array($function, array());
  258. $display = 'print_' . $link['format'] . '_display';
  259. $display_comment = 'print_' . $link['format'] . '_display_comment';
  260. $display_urllist = 'print_' . $link['format'] . '_display_urllist';
  261. $result = db_query('SELECT nid, link, comments, url_list FROM {' . $module . '_node_conf} WHERE nid IN (:nids)', array(':nids' => $ids))->fetchAllAssoc('nid');
  262. foreach ($nodes as $node) {
  263. $node->{$display} = isset($result[$node->nid]) ? intval($result[$node->nid]->link) : variable_get($display . '_' . $node->type, PRINT_UI_TYPE_SHOW_LINK_DEFAULT);
  264. $node->{$display_comment} = isset($result[$node->nid]) ? intval($result[$node->nid]->comments) : variable_get($display_comment . '_' . $node->type, PRINT_UI_TYPE_COMMENT_LINK_DEFAULT);
  265. $node->{$display_urllist} = isset($result[$node->nid]) ? intval($result[$node->nid]->url_list) : variable_get($display_urllist . '_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
  266. }
  267. }
  268. }
  269. }
  270. /**
  271. * Implements hook_node_insert().
  272. */
  273. function print_ui_node_insert($node) {
  274. return print_ui_node_update($node);
  275. }
  276. /**
  277. * Implements hook_node_update().
  278. */
  279. function print_ui_node_update($node) {
  280. if (user_access('administer print') || user_access('node-specific print configuration')) {
  281. foreach (module_implements('print_link') as $module) {
  282. $function = $module . '_print_link';
  283. if (function_exists($function)) {
  284. $link = call_user_func_array($function, array());
  285. $display = 'print_' . $link['format'] . '_display';
  286. $display_comment = 'print_' . $link['format'] . '_display_comment';
  287. $display_urllist = 'print_' . $link['format'] . '_display_urllist';
  288. if (!isset($node->{$display}) || $node->{$display} === NULL) {
  289. $node->{$display} = variable_get($display . '_' . $node->type, PRINT_UI_TYPE_SHOW_LINK_DEFAULT);
  290. }
  291. if (!isset($node->{$display_comment}) || $node->{$display_comment} === NULL) {
  292. $node->{$display_comment} = variable_get($display_comment . '_' . $node->type, PRINT_UI_TYPE_COMMENT_LINK_DEFAULT);
  293. }
  294. if (!isset($node->{$display_urllist}) || $node->{$display_urllist} === NULL) {
  295. $node->{$display_urllist} = variable_get($display_urllist . '_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
  296. }
  297. db_merge($module . '_node_conf')
  298. ->key(array('nid' => $node->nid))
  299. ->fields(array(
  300. 'link' => $node->{$display},
  301. 'comments' => $node->{$display_comment},
  302. 'url_list' => $node->{$display_urllist},
  303. ))
  304. ->execute();
  305. }
  306. }
  307. }
  308. }
  309. /**
  310. * Implements hook_node_delete().
  311. */
  312. function print_ui_node_delete($node) {
  313. foreach (module_implements('print_link') as $module) {
  314. $function = $module . '_print_link';
  315. if (function_exists($function)) {
  316. $link = call_user_func_array($function, array());
  317. db_delete($module . '_node_conf')
  318. ->condition('nid', $node->nid)
  319. ->execute();
  320. }
  321. }
  322. }
  323. /**
  324. * Implements hook_form_alter().
  325. */
  326. function print_ui_form_alter(&$form, &$form_state, $form_id) {
  327. // Add the node-type settings option to activate the printer-friendly version link
  328. if ((user_access('administer print') || user_access('node-specific print configuration')) &&
  329. (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
  330. $form['print'] = array(
  331. '#type' => 'fieldset',
  332. '#title' => t('Printer, email and PDF versions'),
  333. '#collapsible' => TRUE,
  334. '#collapsed' => TRUE,
  335. '#weight' => PRINT_UI_TYPE_FIELDS_WEIGHT,
  336. '#group' => 'additional_settings',
  337. );
  338. foreach (module_implements('print_link') as $module) {
  339. $function = $module . '_print_link';
  340. if (function_exists($function)) {
  341. $link = call_user_func_array($function, array());
  342. $form['print']['print_' . $link['format']] = array(
  343. '#type' => 'fieldset',
  344. '#title' => check_plain($link['text']),
  345. '#collapsible' => TRUE,
  346. );
  347. $display = 'print_' . $link['format'] . '_display';
  348. $display_comment = 'print_' . $link['format'] . '_display_comment';
  349. $display_urllist = 'print_' . $link['format'] . '_display_urllist';
  350. $form['print']['print_' . $link['format']][$display] = array(
  351. '#type' => 'checkbox',
  352. '#title' => t('Show link'),
  353. );
  354. $form['print']['print_' . $link['format']][$display_comment] = array(
  355. '#type' => 'checkbox',
  356. '#title' => t('Show link in individual comments'),
  357. );
  358. $form['print']['print_' . $link['format']][$display_urllist] = array(
  359. '#type' => 'checkbox',
  360. '#title' => t('Show Printer-friendly URLs list'),
  361. );
  362. if ($form_id == 'node_type_form') {
  363. $form['print']['print_' . $link['format']][$display]['#default_value'] = variable_get($display . '_' . $form['#node_type']->type, PRINT_UI_TYPE_SHOW_LINK_DEFAULT);
  364. $form['print']['print_' . $link['format']][$display_comment]['#default_value'] = variable_get($display_comment . '_' . $form['#node_type']->type, PRINT_UI_TYPE_COMMENT_LINK_DEFAULT);
  365. $form['print']['print_' . $link['format']][$display_urllist]['#default_value'] = variable_get($display_urllist . '_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
  366. }
  367. else {
  368. $node = $form['#node'];
  369. $form['print']['print_' . $link['format']][$display]['#default_value'] = isset($node->{$display}) ? $node->{$display} : variable_get($display . '_' . $node->type, PRINT_UI_TYPE_SHOW_LINK_DEFAULT);
  370. $form['print']['print_' . $link['format']][$display_comment]['#default_value'] = isset($node->{$display_comment}) ? $node->{$display_comment} : variable_get($display_comment . '_' . $node->type, PRINT_UI_TYPE_COMMENT_LINK_DEFAULT);
  371. $form['print']['print_' . $link['format']][$display_urllist]['#default_value'] = isset($node->{$display_urllist}) ? $node->{$display_urllist} : variable_get($display_urllist . '_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
  372. }
  373. }
  374. }
  375. }
  376. }
  377. /**
  378. * Auxiliary function to fill the Printer-friendly link attributes
  379. *
  380. * @param string $title
  381. * text to displayed by the link when hovering over it with the mouse
  382. * @param string $class
  383. * class attribute to be used in the link
  384. * @param bool $new_window
  385. * if TRUE opens the target page in a new window
  386. *
  387. * @return array
  388. * An associative array containing:
  389. * - title: text to be used when hovering over the link.
  390. * - class: CSS class of the link tag.
  391. * - target: used for opening a new window with the non-javascript method
  392. * - onclick: open a new window, with the javascript method
  393. * - rel: SEO-related attribute indicating that the printer-friendly version
  394. * should not be indexed by search engine robots.
  395. */
  396. function _print_ui_fill_attributes($title = '', $class = '', $new_window = FALSE) {
  397. $print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT);
  398. $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
  399. $attributes = array();
  400. $attributes['title'] = $title;
  401. if (!empty($class)) {
  402. $attributes['class'] = array($class);
  403. }
  404. if ($new_window) {
  405. switch ($print_newwindow) {
  406. case 0:
  407. $attributes['target'] = '_blank';
  408. break;
  409. case 1:
  410. $attributes['onclick'] = 'window.open(this.href); return false';
  411. break;
  412. }
  413. }
  414. if (!empty($print_robots_noindex)) {
  415. $attributes['rel'] = 'nofollow';
  416. }
  417. return $attributes;
  418. }
  419. /**
  420. * Format the Printer-friendly link
  421. *
  422. * @return array
  423. * An associative array containing:
  424. * - text: The content of the link
  425. * - html: TRUE if the text contains HTML tags, FALSE if it's plain text
  426. * - attributes: several attributes of the link tag (title, class, target,
  427. * onclick, rel)
  428. *
  429. * @see _print_ui_fill_attributes()
  430. * @ingroup themeable
  431. * @ingroup print_themeable
  432. */
  433. function theme_print_ui_format_link($vars) {
  434. $format = $vars['format'];
  435. foreach (module_implements('print_link') as $module) {
  436. $function = $module . '_print_link';
  437. if (function_exists($function)) {
  438. $link = call_user_func_array($function, array());
  439. if ($link['format'] == $format) {
  440. $link_class = variable_get('print_' . $link['format'] . '_link_class', $link['class']);
  441. $new_window = FALSE;
  442. $func = $module . '_print_new_window_alter';
  443. if (function_exists($func)) {
  444. $func($new_window, $link['format']);
  445. }
  446. $show_link = variable_get('print_' . $link['format'] . '_show_link', PRINT_UI_SHOW_LINK_DEFAULT);
  447. $link_text = filter_xss(variable_get('print_' . $link['format'] . '_link_text', $link['text']));
  448. if ($show_link >= 2) {
  449. $img = drupal_get_path('module', $module) . '/icons/' . $link['icon'];
  450. switch ($show_link) {
  451. case 2:
  452. $text = theme('image', array('path' => $img, 'width' => '16px', 'height' => '16px', 'alt' => $link_text, 'title' => $link_text, 'attributes' => array('class' => array('print-icon'))));
  453. break;
  454. case 3:
  455. $text = theme('image', array('path' => $img, 'width' => '16px', 'height' => '16px', 'alt' => $link_text, 'title' => $link_text, 'attributes' => array('class' => array('print-icon', 'print-icon-margin')))) . $link_text;
  456. break;
  457. }
  458. $html = TRUE;
  459. }
  460. else {
  461. $text = $link_text;
  462. $html = FALSE;
  463. }
  464. return array(
  465. 'text' => $text,
  466. 'html' => $html,
  467. 'attributes' => _print_ui_fill_attributes($link['description'], strip_tags($link_class), $new_window),
  468. );
  469. }
  470. }
  471. }
  472. }
  473. /**
  474. * Auxiliary function to display a formatted Printer-friendly link
  475. *
  476. * Function made available so that developers may call this function from
  477. * their defined pages/blocks.
  478. *
  479. * @param array $link
  480. * array returned by the hook_print_link() call.
  481. * @param array $args
  482. * Array of optional arguments:
  483. * - node: node object, to be used in checking node access. If the path
  484. * argument is not provided, the path used will be node/nid.
  485. * - path: path to be used in the link. If not specified, the current URL
  486. * is used.
  487. * - location: the location in the page where the link is being inserted
  488. * ('link', 'corner', 'block', 'help').
  489. *
  490. * @return string
  491. * string with the HTML link to the printer-friendly page
  492. */
  493. function print_ui_insert_link($link, $args = array()) {
  494. $node = isset($args['node']) ? $args['node'] : NULL;
  495. $path = isset($args['path']) ? $args['path'] : NULL;
  496. $location = isset($args['location']) ? $args['location'] : '';
  497. if ($node !== NULL) {
  498. $nid = $node->nid;
  499. if ($path === NULL) {
  500. $path = 'node/' . $nid;
  501. }
  502. $allowed_type = print_ui_link_allowed($link, array('node' => $node));
  503. }
  504. else {
  505. if ($path === NULL) {
  506. $nid = preg_replace('!^node/([\d]+)!', '$1', $_GET['q']);
  507. $path = $_GET['q'];
  508. }
  509. else {
  510. $nid = NULL;
  511. }
  512. $allowed_type = print_ui_link_allowed($link, array('path' => $path));
  513. }
  514. if ($allowed_type) {
  515. if ($nid !== NULL) {
  516. if ($allowed_type === PRINT_UI_ALLOW_BOOK_LINK) {
  517. $path = 'book/export/html/' . $nid;
  518. }
  519. else {
  520. if (variable_get('print_' . $link['format'] . '_link_use_alias', PRINT_UI_LINK_USE_ALIAS_DEFAULT) && ($alias = drupal_lookup_path('alias', $path))) {
  521. $path = $alias;
  522. }
  523. else {
  524. $path = $nid;
  525. }
  526. }
  527. $path = $link['path'] . '/' . $path;
  528. $query = _print_ui_query_string_encode($_GET, array('q'));
  529. }
  530. else {
  531. $query = NULL;
  532. }
  533. drupal_add_css(drupal_get_path('module', 'print_ui') . '/css/print_ui.theme.css');
  534. $format = theme('print_ui_format_link', array('format' => $link['format'], 'location' => $location));
  535. return '<span class="print_' . $link['format'] . '">' . l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) . '</span>';
  536. }
  537. else {
  538. return FALSE;
  539. }
  540. }
  541. /**
  542. * Check if the link to the PF version is allowed depending on the settings
  543. *
  544. * @param array $link
  545. * array returned by the hook_print_link() call.
  546. * @param array $args
  547. * array containing the possible parameters:
  548. * view_mode, node, type, path
  549. *
  550. * @return
  551. * FALSE if not allowed
  552. * PRINT_UI_ALLOW_NORMAL_LINK if a normal link is allowed
  553. * PRINT_UI_ALLOW_BOOK_LINK if a link is allowed in a book node
  554. */
  555. function print_ui_link_allowed($link, $args) {
  556. if (isset($args['view_mode'])) {
  557. $view_mode = $args['view_mode'];
  558. if ((($view_mode == 'teaser') && !variable_get('print_' . $link['format'] . '_link_teaser', PRINT_UI_LINK_TEASER_DEFAULT))
  559. || !in_array($view_mode, array('full', 'teaser'))) {
  560. // If the teaser link is disabled
  561. return FALSE;
  562. }
  563. }
  564. $link_allowed_func = $link['module'] . '_link_allowed';
  565. if (function_exists($link_allowed_func)) {
  566. if (!$link_allowed_func($args)) {
  567. // If the format-specific function disallows the link
  568. return FALSE;
  569. }
  570. }
  571. if (!empty($args['path'])) {
  572. $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
  573. if (ctype_digit($nid)) {
  574. $args['node'] = node_load($nid);
  575. }
  576. }
  577. if (!empty($args['node'])) {
  578. static $node_type = '';
  579. $node = $args['node'];
  580. if (isset($node->type)) {
  581. $node_type = $node->type;
  582. }
  583. // Node
  584. if (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
  585. // Link is for a comment, return the configured setting
  586. // Cache this statically to avoid duplicate queries for every comment.
  587. static $res = array();
  588. if (!isset($res[$link['format']][$node->nid])) {
  589. $res[$link['format']][$node->nid] = db_query("SELECT comments FROM {" . $link['module'] . "_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))->fetchField();
  590. }
  591. $display_comment = ($res && ($res[$link['format']][$node->nid] !== FALSE)) ? $res[$link['format']][$node->nid] : variable_get('print_' . $link['format'] . '_display_comment_' . $node_type, PRINT_UI_TYPE_COMMENT_LINK_DEFAULT);
  592. if ($display_comment) {
  593. return PRINT_UI_ALLOW_NORMAL_LINK;
  594. }
  595. }
  596. else {
  597. // Node link
  598. $display = 'print_' . $link['format'] . '_display';
  599. if (isset($node->{$display}) && !$node->{$display}) {
  600. // Link for this node is disabled
  601. return FALSE;
  602. }
  603. elseif (isset($node->book)) {
  604. // Node is a book;
  605. $book_link = variable_get('print_' . $link['format'] . '_book_link', PRINT_UI_BOOK_LINK_DEFAULT);
  606. switch ($book_link) {
  607. case 1:
  608. if (user_access('access printer-friendly version')) {
  609. return PRINT_UI_ALLOW_BOOK_LINK;
  610. }
  611. break;
  612. case 2:
  613. return PRINT_UI_ALLOW_NORMAL_LINK;
  614. }
  615. }
  616. else {
  617. return PRINT_UI_ALLOW_NORMAL_LINK;
  618. }
  619. }
  620. }
  621. else {
  622. // 'System' page
  623. $sys_link_visibility = variable_get('print_' . $link['format'] . '_sys_link_visibility', PRINT_UI_SYS_LINK_VISIBILITY_DEFAULT);
  624. $sys_link_pages = variable_get('print_' . $link['format'] . '_sys_link_pages', PRINT_UI_SYS_LINK_PAGES_DEFAULT);
  625. return _print_ui_page_match($sys_link_visibility, $_GET['q'], $sys_link_pages);
  626. }
  627. return FALSE;
  628. }
  629. /**
  630. * Check if the provided page is enabled according to the visibility settings
  631. *
  632. * @param int $visibility
  633. * current visibility settings:
  634. * 0 for show on every page except the listed pages
  635. * 1 for show on only the listed pages
  636. * @param string $path
  637. * current path
  638. * @param string $pages
  639. * list of pages
  640. *
  641. * @return bool
  642. * TRUE if it is enabled, FALSE otherwise
  643. */
  644. function _print_ui_page_match($visibility, $path, $pages) {
  645. if ($pages) {
  646. if ($visibility == 2) {
  647. if (module_exists('php')) {
  648. return php_eval($pages);
  649. }
  650. else {
  651. return FALSE;
  652. }
  653. }
  654. $alias = drupal_get_path_alias($path);
  655. $page_match = drupal_match_path($path, $pages);
  656. if ($alias != $path) {
  657. $page_match = $page_match || drupal_match_path($alias, $pages);
  658. }
  659. return !($visibility xor $page_match);
  660. }
  661. else {
  662. return !$visibility;
  663. }
  664. }
  665. /**
  666. * Parse an array into a valid urlencoded query string.
  667. *
  668. * Modified from drupal_query_string_encode to prevent re-encoding of
  669. * encoded original. (see #301192)
  670. *
  671. * @param array $query
  672. * The array to be processed e.g. $_GET
  673. * @param array $exclude
  674. * The array filled with keys to be excluded.
  675. * @param string $parent
  676. * The be used in recursive calls.
  677. *
  678. * @return string
  679. * urlencoded string which can be appended to/as the URL query string
  680. */
  681. function _print_ui_query_string_encode($query, $exclude = array(), $parent = '') {
  682. $params = array();
  683. foreach ($query as $key => $value) {
  684. if (in_array($key, $exclude, TRUE)) {
  685. continue;
  686. }
  687. if (is_array($value)) {
  688. $params[$key] = _print_ui_query_string_encode($value, $exclude, $key);
  689. }
  690. else {
  691. $params[$key] = $value;
  692. }
  693. }
  694. return empty($params) ? NULL : $params;
  695. }