Update the language prefix handling in the local file access routine to D7.
[project/print.git] / print.module
1 <?php
2
3 /**
4 * @defgroup print Printer, email and PDF versions
5 *
6 * Welcome to the print module developer's documentation. The interesting
7 * functions for themers are those that start with 'theme_'.
8 *
9 * - Printer-friendly pages
10 * - @link print.module Module main file @endlink
11 * - @link print.admin.inc Settings form @endlink
12 * - @link print.pages.inc HTML generation @endlink
13 * - @link print.install (Un)Install routines @endlink
14 * - @link print.tpl.php Page generation template @endlink
15 * - Send by email
16 * - @link print_mail.module Module main file @endlink
17 * - @link print_mail.admin.inc Settings form @endlink
18 * - @link print_mail.inc Mail form and send mail routine @endlink
19 * - @link print_mail.install (Un)Install routines @endlink
20 * - PDF version
21 * - @link print_pdf.module Module main file @endlink
22 * - @link print_pdf.admin.inc Settings form @endlink
23 * - @link print_pdf.pages.inc PDF generation @endlink
24 * - @link print_pdf.class.inc Auxiliary PHP5 class @endlink
25 * - @link print_pdf.class_php4.inc Auxiliary PHP4 class @endlink
26 * - @link print_pdf.install (Un)Install routines @endlink
27 */
28
29 /**
30 * @file
31 * Displays Printer-friendly versions of Drupal pages.
32 *
33 * This is the core module of the PF package, with the Drupal hooks
34 * and other administrative functions.
35 *
36 * @ingroup print
37 */
38
39 define('PRINT_PATH', 'print');
40
41 define('PRINT_HTML_FORMAT', 'html');
42 define('PRINT_MAIL_FORMAT', 'mail');
43 define('PRINT_PDF_FORMAT', 'pdf');
44 define('PRINT_LOGO_OPTIONS_DEFAULT', 1);
45 define('PRINT_LOGO_URL_DEFAULT', '');
46 define('PRINT_FOOTER_OPTIONS_DEFAULT', 1);
47 define('PRINT_FOOTER_USER_DEFAULT', '');
48 define('PRINT_CSS_DEFAULT', '');
49 define('PRINT_KEEP_THEME_CSS_DEFAULT', 0);
50 define('PRINT_URLS_DEFAULT', 1);
51 define('PRINT_URLS_ANCHORS_DEFAULT', 0);
52 define('PRINT_COMMENTS_DEFAULT', 0);
53 define('PRINT_NEWWINDOW_DEFAULT', 1);
54
55 define('PRINT_HTML_LINK_POS_DEFAULT', 'link');
56 define('PRINT_HTML_LINK_TEASER_DEFAULT', 0);
57 define('PRINT_HTML_SHOW_LINK_DEFAULT', 1);
58 define('PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT', 0);
59 define('PRINT_HTML_NODE_LINK_PAGES_DEFAULT', '');
60 define('PRINT_HTML_LINK_CLASS_DEFAULT', 'print-page');
61 define('PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT', 1);
62 define('PRINT_HTML_SYS_LINK_PAGES_DEFAULT', '');
63 define('PRINT_HTML_LINK_USE_ALIAS_DEFAULT', 0);
64 define('PRINT_HTML_BOOK_LINK_DEFAULT', 1);
65 define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
66 define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);
67 define('PRINT_HTML_WINDOWCLOSE_DEFAULT', 1);
68
69 define('PRINT_SOURCEURL_ENABLED_DEFAULT', 1);
70 define('PRINT_SOURCEURL_DATE_DEFAULT', 0);
71 define('PRINT_SOURCEURL_FORCENODE_DEFAULT', 0);
72
73 define('PRINT_ROBOTS_NOINDEX_DEFAULT', 1);
74 define('PRINT_ROBOTS_NOFOLLOW_DEFAULT', 1);
75 define('PRINT_ROBOTS_NOARCHIVE_DEFAULT', 0);
76
77 define('PRINT_TYPE_SHOW_LINK_DEFAULT', 1);
78 define('PRINT_TYPE_COMMENT_LINK_DEFAULT', 0);
79 define('PRINT_TYPE_URLLIST_DEFAULT', 1);
80 define('PRINT_TYPE_SYS_URLLIST_DEFAULT', 0);
81
82 define('PRINT_ALLOW_NORMAL_LINK', 1);
83 define('PRINT_ALLOW_BOOK_LINK', 2);
84 define('PRINT_TYPE_FIELDS_WEIGHT', 30);
85
86 /**
87 * Implements hook_permission().
88 */
89 function print_permission() {
90 return array(
91 'administer print' => array(
92 'title' => t('Administer the module'),
93 'description' => t('Perform maintenance tasks for the print module.'),
94 ),
95 'node-specific print configuration' => array(
96 'title' => t('Node-specific configuration'),
97 'description' => t('Enable access to the per-node settings.'),
98 ),
99 'access print' => array(
100 'title' => t('Access the printer-friendly page'),
101 'description' => t('View the printer-friendly pages and the links to them in the original pages.'),
102 ),
103 );
104 }
105
106 /**
107 * Implements hook_theme().
108 */
109 function print_theme() {
110 return array(
111 'print_format_link' => array(
112 'variables' => array(),
113 ),
114 'print' => array(
115 'variables' => array('print' => array(), 'type' => PRINT_HTML_FORMAT, 'node' => NULL),
116 'template' => 'print',
117 ),
118 );
119 }
120
121 /**
122 * Implements hook_preprocess_HOOK().
123 */
124 function print_preprocess_node(&$variables) {
125 if ($variables['elements']['#view_mode'] == 'print') {
126 $type = $variables['elements']['#node']->type;
127 $format = $variables['elements']['#print_format'];
128 $nid = $variables['elements']['#node']->nid;
129
130 $variables['theme_hook_suggestions'][] = "node__print";
131 $variables['theme_hook_suggestions'][] = "node__print__{$format}";
132 $variables['theme_hook_suggestions'][] = "node__print__{$format}__node__{$type}";
133 $variables['theme_hook_suggestions'][] = "node__print__{$format}__node__{$nid}";
134 }
135 }
136
137 /**
138 * Implements hook_preprocess_HOOK().
139 */
140 function print_preprocess_print(&$variables) {
141 static $hooks = NULL;
142 if (!isset($hooks)) {
143 drupal_theme_initialize();
144 $hooks = theme_get_registry();
145 }
146
147 $variables['page']['#show_messages'] = FALSE;
148
149 // Stolen from theme() so that ALL preprocess functions are called
150 $hook = 'page';
151 $info = $hooks[$hook];
152 if (isset($info['preprocess functions']) || isset($info['process functions'])) {
153 foreach (array('preprocess functions', 'process functions') as $phase) {
154 if (!empty($info[$phase])) {
155 foreach ($info[$phase] as $processor_function) {
156 if (function_exists($processor_function)) {
157 // We don't want a poorly behaved process function changing $hook.
158 $hook_clone = $hook;
159 $processor_function($variables, $hook_clone);
160 }
161 }
162 }
163 }
164 }
165
166 $format = $variables['type'];
167 $type = (isset($variables['node']->type)) ? $variables['node']->type : '';
168 $nid = (isset($variables['node']->nid)) ? $variables['node']->nid : '';
169
170 $variables['theme_hook_suggestions'] = array();
171 $variables['theme_hook_suggestions'][] = "print__node__{$type}";
172 $variables['theme_hook_suggestions'][] = "print__node__{$type}__{$nid}";
173 $variables['theme_hook_suggestions'][] = "print__{$format}";
174 $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}";
175 $variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}__{$nid}";
176 }
177
178 /**
179 * Implements hook_menu().
180 */
181 function print_menu() {
182 $items = array();
183
184 $items[PRINT_PATH] = array(
185 'title' => 'Printer-friendly',
186 'page callback' => 'print_controller_html',
187 'access arguments' => array('access print'),
188 'type' => MENU_CALLBACK,
189 'file' => 'print.pages.inc',
190 );
191 $items[PRINT_PATH . '/' . PRINT_PATH] = array(
192 'access callback' => FALSE,
193 );
194 $items['admin/config/user-interface/print'] = array(
195 'title' => 'Printer, email and PDF versions',
196 'description' => 'Adds a printer-friendly version link to content and administrative pages.',
197 'page callback' => 'drupal_get_form',
198 'page arguments' => array('print_html_settings'),
199 'access arguments' => array('administer print'),
200 'file' => 'print.admin.inc',
201 );
202 $items['admin/config/user-interface/print/html'] = array(
203 'title' => 'Web page',
204 'weight' => 1,
205 'type' => MENU_DEFAULT_LOCAL_TASK,
206 );
207 $items['admin/config/user-interface/print/html/options'] = array(
208 'title' => 'Options',
209 'weight' => 1,
210 'type' => MENU_DEFAULT_LOCAL_TASK,
211 );
212 $items['admin/config/user-interface/print/html/strings'] = array(
213 'title' => 'Text strings',
214 'description' => 'Override the user-facing strings used in the printer-friendly version.',
215 'page callback' => 'drupal_get_form',
216 'page arguments' => array('print_html_strings_settings'),
217 'access arguments' => array('administer print'),
218 'weight' => 2,
219 'type' => MENU_LOCAL_TASK,
220 'file' => 'print.admin.inc',
221 );
222 $items['admin/config/user-interface/print/common'] = array(
223 'title' => 'Settings',
224 'description' => 'Settings for the common functionalities for all the print sub-modules.',
225 'page callback' => 'drupal_get_form',
226 'page arguments' => array('print_main_settings'),
227 'access arguments' => array('administer print'),
228 'weight' => 10,
229 'type' => MENU_LOCAL_TASK,
230 'file' => 'print.admin.inc',
231 );
232 $items['admin/config/user-interface/print/common/options'] = array(
233 'title' => 'Options',
234 'weight' => 1,
235 'type' => MENU_DEFAULT_LOCAL_TASK,
236 );
237 $items['admin/config/user-interface/print/common/strings'] = array(
238 'title' => 'Text strings',
239 'description' => 'Override the user-facing strings used by the print module.',
240 'page callback' => 'drupal_get_form',
241 'page arguments' => array('print_main_strings_settings'),
242 'access arguments' => array('administer print'),
243 'weight' => 2,
244 'type' => MENU_LOCAL_TASK,
245 'file' => 'print.admin.inc',
246 );
247
248 return $items;
249 }
250
251 /**
252 * Implements hook_block_info().
253 */
254 function print_block_info() {
255 $block['print-links']['info'] = t('Printer, email and PDF versions');
256 $block['print-links']['cache'] = DRUPAL_CACHE_PER_PAGE;
257 $block['print-top']['info'] = t('Most printed');
258 $block['print-top']['cache'] = DRUPAL_CACHE_GLOBAL;
259 return $block;
260 }
261
262 /**
263 * Implements hook_block_view().
264 */
265 function print_block_view($delta = '') {
266 switch ($delta) {
267 case 'print-links':
268 $nid = preg_replace('!^node/!', '', $_GET['q']);
269 if (ctype_digit($nid)) {
270 $node = node_load($nid);
271 if (!node_access('view', $node)) {
272 // If the user doesn't have access to the node, don't show any links
273 $block['content'] == '';
274 return;
275 }
276 }
277 else {
278 $node = NULL;
279 }
280 $block['content'] = '';
281 foreach (array('html' => 'print', 'mail' => 'print_mail', 'pdf' => 'print_pdf') as $format => $module) {
282 $link_pos = variable_get('print_' . $format . '_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
283
284 if (!(empty($link_pos['block']))) {
285 $func = $module . '_insert_link';
286
287 if (function_exists($func)) {
288 $links = $func(NULL, $node);
289 if (!empty($links)) {
290 $block['content'] .= $links;
291 }
292 }
293 }
294 }
295 break;
296 case 'print-top':
297 $block['subject'] = t('Most printed');
298 $result = db_query_range("SELECT path FROM {print_page_counter} LEFT JOIN {node} ON path = CONCAT('node/', node.nid) WHERE status <> 0 OR status IS NULL ORDER BY totalcount DESC", 0, 3)
299 ->fetchAll();
300 if (count($result)) {
301 $block['content'] = '<div class="item-list"><ul>';
302 foreach ($result as $obj) {
303 $block['content'] .= '<li>' . l(_print_get_title($obj->path), $obj->path) . '</li>';
304 }
305 $block['content'] .= '</ul></div>';
306 }
307 break;
308 }
309 return $block;
310 }
311
312 /**
313 * Implements hook_node_view_alter().
314 */
315 function print_node_view_alter(&$build) {
316 if (isset($build['links']['book']['#links']['book_printer'])) {
317 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
318
319 if ($print_html_book_link) {
320 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
321
322 if (!empty($print_html_link_pos['link'])) {
323 $format = theme('print_format_link');
324
325 switch ($print_html_book_link) {
326 case 1:
327 $path = $build['links']['book']['#links']['book_printer']['href'];
328 break;
329 case 2:
330 $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT);
331 $path = ($print_html_link_use_alias && ($alias = drupal_lookup_path('alias', 'node/' . $build['#node']->nid))) ? $alias : $build['#node']->nid;
332 break;
333 }
334
335 $build['links']['book']['#links']['book_printer'] = array(
336 'href' => PRINT_PATH . '/' . $path,
337 'title' => $format['text'],
338 'attributes' => $format['attributes'],
339 'html' => $format['html'],
340 );
341 }
342 else {
343 unset($build['links']['book']['#links']['book_printer']);
344 }
345 }
346 }
347 }
348
349 /**
350 * Implements hook_help().
351 */
352 function print_help($path, $arg) {
353 switch ($path) {
354 case 'admin/help#print':
355 // Return a line-break version of the module README
356 return _filter_autop(file_get_contents(drupal_get_path('module', 'print') . '/README.txt'));
357 }
358
359 if ($path !== 'node/%') {
360 static $output = FALSE;
361
362 if ($output === FALSE) {
363 $output = TRUE;
364
365 $link = print_insert_link();
366 if ($link) {
367 return "<span class='print-syslink'>$link</span>";
368 }
369 }
370 }
371 }
372
373 /**
374 * Implements hook_node_view().
375 */
376 function print_node_view($node, $view_mode) {
377 $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
378 $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT);
379
380 foreach (array('node', 'comment') as $type) {
381 $allowed_type = print_link_allowed(array('type' => $type, 'node' => $node, 'view_mode' => $view_mode));
382 if (($allowed_type === PRINT_ALLOW_NORMAL_LINK) && !isset($node->book) && !empty($print_html_link_pos['link'])) {
383 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
384 $links = array();
385 $format = theme('print_format_link');
386
387 $path = (($print_html_link_use_alias) && ($alias = drupal_lookup_path('alias', 'node/' . $node->nid))) ? $alias : $node->nid;
388
389 $links['print_html'] = array(
390 'href' => PRINT_PATH . '/' . $path,
391 'title' => $format['text'],
392 'attributes' => $format['attributes'],
393 'html' => $format['html'],
394 'query' => print_query_string_encode($_GET, array('q')),
395 );
396
397 $link_content = array(
398 '#theme' => 'links',
399 '#links' => $links,
400 '#attributes' => array('class' => array('links', 'inline')),
401 );
402
403 if ($type == 'node') {
404 $node->content['links']['print_html'] = $link_content;
405 }
406 elseif (($type == 'comment') && isset($node->content['comments']['comments'])) {
407 foreach ($node->content['comments']['comments'] as $cid => $comment) {
408 if (is_numeric($cid)) {
409 $link_content['#links']['print_html']['query']['comment'] = $cid;
410 $node->content['comments']['comments'][$cid]['links']['print_html'] = $link_content;
411 }
412 }
413 }
414 }
415 }
416
417 if ($view_mode == 'full') {
418 // Insert content corner links
419 $node->content['print_links'] = array(
420 '#prefix' => '<span class="print-link">',
421 '#markup' => '',
422 '#suffix' => '</span>',
423 '#weight' => -101,
424 );
425 if (!empty($print_html_link_pos['corner'])) {
426 $node->content['print_links']['#markup'] .= print_insert_link(NULL, $node);
427 }
428 }
429 }
430
431 /**
432 * Implements hook_node_load().
433 */
434 function print_node_load($nodes, $types) {
435 $ids = array();
436 foreach ($nodes as $node) {
437 $ids[] = $node->nid;
438 }
439
440 $result = db_query('SELECT nid, link, comments, url_list FROM {print_node_conf} WHERE nid IN (:nids)', array(':nids' => $ids))->fetchAllAssoc('nid');
441
442 foreach ($nodes as $node) {
443 $node->print_display = isset($result[$node->nid]) ? intval($result[$node->nid]->link) : variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
444 $node->print_display_comment = isset($result[$node->nid]) ? intval($result[$node->nid]->comments) : variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
445 $node->print_display_urllist = isset($result[$node->nid]) ? intval($result[$node->nid]->url_list) : variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
446 }
447 }
448
449 /**
450 * Implements hook_node_insert().
451 */
452 function print_node_insert($node) {
453 if (user_access('administer print') || user_access('node-specific print configuration')) {
454 if (!isset($node->print_display)) $node->print_display = variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
455 if (!isset($node->print_display_comment)) $node->print_display_comment = variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
456 if (!isset($node->print_display_urllist)) $node->print_display_urllist = variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
457
458 _print_node_conf_modify($node->nid, $node->print_display, $node->print_display_comment, $node->print_display_urllist);
459 }
460 }
461
462 /**
463 * Implements hook_node_update().
464 */
465 function print_node_update($node) {
466 if (user_access('administer print') || user_access('node-specific print configuration')) {
467 if (!isset($node->print_display)) $node->print_display = variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
468 if (!isset($node->print_display_comment)) $node->print_display_comment = variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
469 if (!isset($node->print_display_urllist)) $node->print_display_urllist = variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
470
471 _print_node_conf_modify($node->nid, $node->print_display, $node->print_display_comment, $node->print_display_urllist);
472 }
473 }
474
475 /**
476 * Implements hook_node_delete().
477 */
478 function print_node_delete($node) {
479 db_delete('print_node_conf')
480 ->condition('nid', $node->nid)
481 ->execute();
482 db_delete('print_page_counter')
483 ->condition('path', 'node/' . $node->nid)
484 ->execute();
485 }
486
487 /**
488 * Implements hook_form_alter().
489 */
490 function print_form_alter(&$form, &$form_state, $form_id) {
491 // Add the node-type settings option to activate the printer-friendly version link
492 if ((user_access('administer print') || user_access('node-specific print configuration')) &&
493 (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
494 $form['print'] = array(
495 '#type' => 'fieldset',
496 '#title' => t('Printer, email and PDF versions'),
497 '#collapsible' => TRUE,
498 '#collapsed' => TRUE,
499 '#weight' => PRINT_TYPE_FIELDS_WEIGHT,
500 '#group' => 'additional_settings',
501 );
502
503 $form['print']['label'] = array(
504 '#type' => 'markup',
505 '#markup' => '<p><strong>' . t('Printer-friendly version') . '</strong></p>',
506 );
507
508 $form['print']['print_display'] = array(
509 '#type' => 'checkbox',
510 '#title' => t('Show link'),
511 );
512 $form['print']['print_display_comment'] = array(
513 '#type' => 'checkbox',
514 '#title' => t('Show link in individual comments'),
515 );
516 $form['print']['print_display_urllist'] = array(
517 '#type' => 'checkbox',
518 '#title' => t('Show Printer-friendly URLs list'),
519 );
520
521 if ($form_id == 'node_type_form') {
522 $form['print']['print_display']['#default_value'] = variable_get('print_display_' . $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
523 $form['print']['print_display_comment']['#default_value'] = variable_get('print_display_comment_' . $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
524 $form['print']['print_display_urllist']['#default_value'] = variable_get('print_display_urllist_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
525 }
526 else {
527 $node = $form['#node'];
528 $form['print']['print_display']['#default_value'] = isset($node->print_display) ? $node->print_display : variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
529 $form['print']['print_display_comment']['#default_value'] = isset($node->print_display_comment) ? $node->print_display_comment : variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
530 $form['print']['print_display_urllist']['#default_value'] = isset($node->print_display_urllist) ? $node->print_display_urllist : variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
531 }
532 }
533 }
534
535 /**
536 * Implements hook_entity_info_alter().
537 */
538 function print_entity_info_alter(&$info) {
539 // Add the 'Print' view mode for nodes.
540 $info['node']['view modes'] += array(
541 'print' => array(
542 'label' => t('Print'),
543 'custom settings' => FALSE,
544 ),
545 );
546 }
547
548 /**
549 * Auxiliary function to discover a given page's title
550 *
551 * @param $path
552 * path of the page being identified
553 * @return
554 * string with the page's title
555 */
556 function _print_get_title($path) {
557 $path = drupal_get_normal_path($path);
558 $nid = preg_replace('!^node/!', '', $path);
559 if (ctype_digit($nid)) {
560 $res = db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $nid))
561 ->fetchField();
562 }
563 else {
564 $res = db_query("SELECT link_title FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $path))
565 ->fetchField();
566 }
567 return $res;
568 }
569
570 /**
571 * Modify the print_node_conf_table
572 *
573 * Update the print_node_conf table to reflect the given attributes.
574 * If updating to the default values, delete the record.
575 *
576 * @param $nid
577 * value of the nid field (primary key)
578 * @param $link
579 * value of the link field (0 or 1)
580 * @param $comments
581 * value of the comments field (0 or 1)
582 * @param $url_list
583 * value of the url_list field (0 or 1)
584 */
585 function _print_node_conf_modify($nid, $link, $comments, $url_list) {
586 db_merge('print_node_conf')
587 ->key(array('nid' => $nid))
588 ->fields(array(
589 'link' => $link,
590 'comments' => $comments,
591 'url_list' => $url_list,
592 ))
593 ->execute();
594 }
595
596 /**
597 * Auxiliary function to fill the Printer-friendly link attributes
598 *
599 * @param $title
600 * text to displayed by the link when hovering over it with the mouse
601 * @param $class
602 * class attribute to be used in the link
603 * @param $new_window
604 * if TRUE opens the target page in a new window
605 * @return
606 * array of formatted attributes
607 */
608 function print_fill_attributes($title = '', $class = '', $new_window = FALSE) {
609 $print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT);
610 $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
611
612 $attributes = array();
613 $attributes['title'] = $title;
614 if (!empty($class)) {
615 $attributes['class'] = array($class);
616 }
617
618 if ($new_window) {
619 switch ($print_newwindow) {
620 case 0:
621 $attributes['target'] = '_blank';
622 break;
623 case 1:
624 $attributes['onclick'] = 'window.open(this.href); return false';
625 break;
626 }
627 }
628 if (!empty($print_robots_noindex)) {
629 $attributes['rel'] = 'nofollow';
630 }
631 return $attributes;
632 }
633
634 /**
635 * Auxiliary function to set the link text and html flag
636 *
637 * @param $type
638 * type of link: 0 or 1 for a text-only link, 2 for icon-only and 3 for
639 * both text and icon
640 * @param $text
641 * text to be displayed on the link to the printer-friendly page
642 * @param $img
643 * path to the icon file
644 * @return
645 * array with the link text and html flag
646 */
647 function _print_format_link_aux($type = 0, $text = '', $img = '') {
648 if ($type >= 2) {
649 $html = TRUE;
650 switch ($type) {
651 case 2:
652 $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon'))));
653 break;
654 case 3:
655 $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon', 'print-icon-margin')))) . $text;
656 break;
657 }
658 }
659 else {
660 $html = FALSE;
661 }
662
663 return array('text' => $text,
664 'html' => $html,
665 );
666 }
667
668 /**
669 * Format the Printer-friendly link
670 *
671 * @return
672 * array of formatted attributes
673 * @ingroup themeable
674 */
675 function theme_print_format_link() {
676 $print_html_link_class = variable_get('print_html_link_class', PRINT_HTML_LINK_CLASS_DEFAULT);
677 $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
678 $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
679 $print_html_link_text = filter_xss(variable_get('print_html_link_text', t('Printer-friendly version')));
680
681 $img = drupal_get_path('module', 'print') . '/icons/print_icon.gif';
682 $title = t('Display a printer-friendly version of this page.');
683 $class = strip_tags($print_html_link_class);
684 $new_window = $print_html_new_window;
685 $format = _print_format_link_aux($print_html_show_link, $print_html_link_text, $img);
686
687 return array('text' => $format['text'],
688 'html' => $format['html'],
689 'attributes' => print_fill_attributes($title, $class, $new_window),
690 );
691 }
692
693 /**
694 * Auxiliary function to display a formatted Printer-friendly link
695 *
696 * Function made available so that developers may call this function from
697 * their defined pages/blocks.
698 *
699 * @param $path
700 * path of the original page (optional). If not specified, the current URL
701 * is used
702 * @param $node
703 * an optional node object, to be used in defining the path, if used, the
704 * path argument is irrelevant
705 * @return
706 * string with the HTML link to the printer-friendly page
707 */
708 function print_insert_link($path = NULL, $node = NULL) {
709 if ($node !== NULL) {
710 $nid = $node->nid;
711 $path = 'node/' . $nid;
712 $allowed_type = print_link_allowed(array('node' => $node));
713 }
714 else {
715 if ($path === NULL) {
716 $nid = preg_replace('!^node/([\d]+)!', '$1', $_GET['q']);
717 $path = $_GET['q'];
718 }
719 else {
720 $nid = NULL;
721 }
722 $allowed_type = print_link_allowed(array('path' => $path));
723 }
724
725 if ($allowed_type) {
726 if ($nid !== NULL) {
727 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
728 $path = 'book/export/html/' . $nid;
729 }
730 else {
731 if (variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT) && ($alias = drupal_lookup_path('alias', $path))) {
732 $path = $alias;
733 }
734 else {
735 $path = $nid;
736 }
737 }
738 $path = PRINT_PATH . '/' . $path;
739 $query = print_query_string_encode($_GET, array('q'));
740 }
741 else {
742 $query = NULL;
743 }
744 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
745 $format = theme('print_format_link');
746 return '<span class="print_html">' . l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) . '</span>';
747 }
748 else {
749 return FALSE;
750 }
751 }
752
753 /**
754 * Check if the provided page is enabled according to the visibility settings
755 *
756 * @param $visibility
757 * current visibility settings:
758 * 0 for show on every page except the listed pages
759 * 1 for show on only the listed pages
760 * @param $pages
761 * list of pages
762 * @return
763 * TRUE if it is enabled, FALSE otherwise
764 */
765 function _print_page_match($visibility, $path, $pages) {
766 if ($pages) {
767 if ($visibility == 2) {
768 if (module_exists('php')) {
769 return php_eval($pages);
770 }
771 else {
772 return FALSE;
773 }
774 }
775 $alias = drupal_get_path_alias($path);
776 $page_match = drupal_match_path($path, $pages);
777 if ($alias != $path) {
778 $page_match = $page_match || drupal_match_path($alias, $pages);
779 }
780
781 return !($visibility xor $page_match);
782 }
783 else {
784 return !$visibility;
785 }
786 }
787
788 /**
789 * Check if the link to the PF version is allowed depending on the settings
790 *
791 * @param $args
792 * array containing the possible parameters:
793 * teaser, node, type, path
794 * @return
795 * FALSE if not allowed
796 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
797 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
798 */
799 function print_link_allowed($args) {
800 $view_mode = isset($args['view_mode']) ? $args['view_mode'] : '';
801 if ((($view_mode == 'teaser') && !variable_get('print_html_link_teaser', PRINT_HTML_LINK_TEASER_DEFAULT))
802 || !in_array($view_mode, array('full', 'teaser', '')) || !user_access('access print')) {
803 // If the teaser link is disabled or the user is not allowed
804 return FALSE;
805 }
806 if (!empty($args['path'])) {
807 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
808 if (ctype_digit($nid)) {
809 $args['node'] = node_load($nid);
810 }
811 }
812 if (!empty($args['node'])) {
813 static $node_type = '';
814
815 $node = $args['node'];
816 if (isset($node->type)) {
817 $node_type = $node->type;
818 }
819 // Node
820 $print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT);
821 $print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT);
822
823 if (!_print_page_match($print_html_node_link_visibility, "node/" . $node->nid, $print_html_node_link_pages)) {
824 // Page not in visibility list
825 return FALSE;
826 }
827 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
828 // Link is for a comment, return the configured setting
829 // Cache this statically to avoid duplicate queries for every comment.
830 static $res = array();
831 if (!isset($res[$node->nid])) {
832 $res[$node->nid] = db_query("SELECT comments FROM {print_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))->fetchField();
833 }
834 $print_display_comment = ($res && ($res[$node->nid] !== FALSE)) ? $res[$node->nid] : variable_get('print_display_comment_' . $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
835 if ($print_display_comment) {
836 return PRINT_ALLOW_NORMAL_LINK;
837 }
838 }
839 else {
840 // Node link
841 if (isset($node->print_display) && !$node->print_display) {
842 // Link for this node is disabled
843 return FALSE;
844 }
845 elseif (isset($node->book)) {
846 // Node is a book;
847 $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
848 switch ($print_html_book_link) {
849 case 1:
850 if (user_access('access printer-friendly version')) {
851 return PRINT_ALLOW_BOOK_LINK;
852 }
853 break;
854 case 2:
855 return PRINT_ALLOW_NORMAL_LINK;
856 }
857 }
858 else {
859 return PRINT_ALLOW_NORMAL_LINK;
860 }
861 }
862 }
863 else {
864 // 'System' page
865 $print_html_sys_link_visibility = variable_get('print_html_sys_link_visibility', PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT);
866 $print_html_sys_link_pages = variable_get('print_html_sys_link_pages', PRINT_HTML_SYS_LINK_PAGES_DEFAULT);
867
868 return _print_page_match($print_html_sys_link_visibility, $_GET['q'], $print_html_sys_link_pages);
869 }
870 return FALSE;
871 }
872
873 /**
874 * Parse an array into a valid urlencoded query string.
875 *
876 * Modified from drupal_query_string_encode to prevent re-encoding of
877 * encoded original. (see #301192)
878 *
879 * @param $query
880 * The array to be processed e.g. $_GET
881 * @param $exclude
882 * The array filled with keys to be excluded.
883 * @return
884 * urlencoded string which can be appended to/as the URL query string
885 */
886 function print_query_string_encode($query, $exclude = array(), $parent = '') {
887 $params = array();
888 foreach ($query as $key => $value) {
889 if (in_array($key, $exclude)) {
890 continue;
891 }
892
893 if (is_array($value)) {
894 $params[$key] = print_query_string_encode($value, $exclude, $key);
895 }
896 else {
897 $params[$key] = $value;
898 }
899 }
900
901 return empty($params) ? NULL : $params;
902 }
903
904 /**
905 * Implements hook_contextual_links_view_alter().
906 */
907 function print_contextual_links_view_alter(&$element, $items) {
908 // Hide all contextual links
909 if (preg_match('!^print!', $_GET['q'])) {
910 unset($element['#links']);
911 }
912 }
913
914 /**
915 * Callback function for the preg_replace_callback replacing spaces with %20
916 *
917 * Replace spaces in URLs with %20
918 *
919 * @param array $matches
920 * array with the matched tag patterns, usually <a...>+text+</a>
921 *
922 * @return string
923 * tag with re-written URL
924 */
925 function _print_replace_spaces($matches) {
926 // first, split the html into the different tag attributes
927 $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
928 $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
929 foreach ($attribs as $key => $value) {
930 $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
931 }
932
933 $size = count($attribs);
934 for ($i=1; $i < $size; $i++) {
935 // If the attribute is href or src, we may need to rewrite the URL in the value
936 if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
937 $url = trim($urls[1], " \t\n\r\0\x0B\"'");
938 $new_url = str_replace(' ', '%20', $url);
939 $matches[1] = str_replace($url, $new_url, $matches[1]);
940 }
941 }
942
943 $ret = '<' . $matches[1] . '>';
944 if (count($matches) == 4) {
945 $ret .= $matches[2] . $matches[3];
946 }
947
948 return $ret;
949 }
950
951 /**
952 * Implements hook_views_api().
953 */
954 function print_views_api() {
955 return array(
956 'api' => 2.0,
957 'path' => drupal_get_path('module', 'print'),
958 );
959 }