Project

General

Profile

Paste
Download (26.6 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / print / print_ui / print_ui.module @ 76bdcd04

1
<?php
2

    
3
/**
4
 * @file
5
 * Printer-friendly pages User Interface module.
6
 *
7
 * This module handles the display of the printer-friendly sub-module links.
8
 *
9
 * @ingroup print
10
 */
11

    
12
define('PRINT_UI_LINK_POS_DEFAULT', '{ "link": "link", "block": "block", "help": "help" }');
13
define('PRINT_UI_LINK_TEASER_DEFAULT', 0);
14
define('PRINT_UI_SHOW_LINK_DEFAULT', 1);
15
define('PRINT_UI_SYS_LINK_VISIBILITY_DEFAULT', 1);
16
define('PRINT_UI_SYS_LINK_PAGES_DEFAULT', '');
17
define('PRINT_UI_LINK_USE_ALIAS_DEFAULT', 0);
18
define('PRINT_UI_BOOK_LINK_DEFAULT', 1);
19

    
20
define('PRINT_UI_ALLOW_NORMAL_LINK', 1);
21
define('PRINT_UI_ALLOW_BOOK_LINK', 2);
22
define('PRINT_UI_TYPE_FIELDS_WEIGHT', 30);
23

    
24
define('PRINT_UI_TYPE_SHOW_LINK_DEFAULT', 1);
25
define('PRINT_UI_TYPE_COMMENT_LINK_DEFAULT', 0);
26

    
27
/**
28
 * Implements hook_permission().
29
 */
30
function print_ui_permission() {
31
  return array(
32
    'node-specific print configuration' => array(
33
      'title' => t('Node-specific configuration'),
34
      'description' => t('Enable access to the per-node settings.'),
35
    ),
36
  );
37
}
38

    
39
/**
40
 * Implements hook_theme().
41
 */
42
function print_ui_theme() {
43
  return array(
44
    'print_ui_format_link' => array(
45
      'variables' => array('format' => '', 'location' => ''),
46
    ),
47
    'print_ui_settings' => array(
48
      'render element' => 'form',
49
      'file' => 'print_ui.admin.inc',
50
    ),
51
  );
52
}
53

    
54
/**
55
 * Implements hook_menu().
56
 */
57
function print_ui_menu() {
58
  $items = array();
59
  $items['admin/config/user-interface/print/ui'] = array(
60
    'title' => 'Links',
61
    'description' => 'Configure the print module links.',
62
    'page callback' => 'drupal_get_form',
63
    'page arguments' => array('print_ui_settings'),
64
    'access arguments'  => array('administer print'),
65
    'weight' => 9,
66
    'type' => MENU_LOCAL_TASK,
67
    'file' => 'print_ui.admin.inc',
68
  );
69

    
70
  return $items;
71
}
72

    
73
/**
74
 * Implements hook_block_info().
75
 */
76
function print_ui_block_info() {
77
  $block['print-links']['info'] = t('Printer, email and PDF versions');
78
  $block['print-links']['cache'] = DRUPAL_CACHE_PER_PAGE;
79
  return $block;
80
}
81

    
82
/**
83
 * Implements hook_block_view().
84
 */
85
function print_ui_block_view($delta = '') {
86
  $block = array();
87

    
88
  switch ($delta) {
89
    case 'print-links':
90
      $nid = preg_replace('!^node/!', '', $_GET['q']);
91
      if (ctype_digit($nid)) {
92
        $node = node_load($nid);
93
        if (!node_access('view', $node)) {
94
          // If the user doesn't have access to the node, don't show any links.
95
          $block['content'] = '';
96
          return array();
97
        }
98
      }
99
      else {
100
        $node = NULL;
101
      }
102

    
103
      $block['content'] = '';
104
      foreach (module_implements('print_link') as $module) {
105
        $function = $module . '_print_link';
106
        if (function_exists($function)) {
107
          $link = call_user_func_array($function, array());
108

    
109
          $link_pos = variable_get('print_' . $link['format'] . '_link_pos', drupal_json_decode(PRINT_UI_LINK_POS_DEFAULT));
110

    
111
          if (!(empty($link_pos['block']))) {
112
            $links = print_ui_insert_link($link, array('node' => $node, 'location' => 'block'));
113
            if (!empty($links)) {
114
              $block['content'] .= $links;
115
            }
116
          }
117
        }
118
      }
119
      break;
120
  }
121
  return $block;
122
}
123

    
124
/**
125
 * Implements hook_help().
126
 */
127
function print_ui_help($path, $arg) {
128
  $links = '';
129

    
130
  if (($path !== 'node/%') && ($path !== 'node/%/revisions/%/view')) {
131
    static $output = FALSE;
132

    
133
    if ($output === FALSE) {
134
      $output = TRUE;
135

    
136
      foreach (module_implements('print_link') as $module) {
137
        $function = $module . '_print_link';
138
        if (function_exists($function)) {
139
          $link = call_user_func_array($function, array());
140

    
141
          $link_pos = variable_get('print_' . $link['format'] . '_link_pos', drupal_json_decode(PRINT_UI_LINK_POS_DEFAULT));
142

    
143
          if (!empty($link_pos['help'])) {
144
            $links .= print_ui_insert_link($link, array('location' => 'help'));
145
          }
146
        }
147
      }
148
      if ($links) {
149
        return "<span class='print-syslink'>$links</span>";
150
      }
151
    }
152
  }
153
  return '';
154
}
155

    
156
/**
157
 * Implements hook_node_view_alter().
158
 */
159
function print_ui_node_view_alter(&$build) {
160
  if (isset($build['links']['book']['#links']['book_printer'])) {
161
    $book_link = variable_get('print_html_book_link', PRINT_UI_BOOK_LINK_DEFAULT);
162

    
163
    if ($book_link) {
164
      $link = print_print_link();
165
      $link_pos = variable_get('print_html_link_pos', drupal_json_decode(PRINT_UI_LINK_POS_DEFAULT));
166

    
167
      if (!empty($link_pos['link'])) {
168
        $format = theme('print_ui_format_link', array('format' => 'html', 'location' => 'link'));
169

    
170
        $path = '';
171
        switch ($book_link) {
172
          case 1:
173
            $path = $build['links']['book']['#links']['book_printer']['href'];
174
            break;
175

    
176
          case 2:
177
            $link_use_alias = variable_get('print_html_link_use_alias', PRINT_UI_LINK_USE_ALIAS_DEFAULT);
178
            $path = ($link_use_alias && ($alias = drupal_lookup_path('alias', 'node/' . $build['#node']->nid))) ? $alias : $build['#node']->nid;
179
            break;
180
        }
181

    
182
        $build['links']['book']['#links']['book_printer'] = array(
183
          'href' => $link['path'] . '/' . $path,
184
          'title' => $format['text'],
185
          'attributes' => $format['attributes'],
186
          'html' => $format['html'],
187
        );
188
      }
189
      else {
190
        unset($build['links']['book']['#links']['book_printer']);
191
      }
192
    }
193
  }
194
}
195

    
196
/**
197
 * Implements hook_node_view().
198
 */
199
function print_ui_node_view($node, $view_mode) {
200
  $corner_markup = '';
201

    
202
  foreach (module_implements('print_link') as $module) {
203
    $function = $module . '_print_link';
204
    if (function_exists($function)) {
205
      $link = call_user_func_array($function, array());
206

    
207
      $link_pos = variable_get('print_' . $link['format'] . '_link_pos', drupal_json_decode(PRINT_UI_LINK_POS_DEFAULT));
208
      $link_use_alias = variable_get('print_' . $link['format'] . '_link_use_alias', PRINT_UI_LINK_USE_ALIAS_DEFAULT);
209

    
210
      if (!preg_match('!^node/[\d]*/revisions/[\d]*/view$!', $_GET['q'])) {
211
        // Not a revision, use node->nid to build the path.
212
        $path = (($link_use_alias) && ($alias = drupal_lookup_path('alias', 'node/' . $node->nid))) ? $alias : $node->nid;
213
      }
214
      else {
215
        // This is a node revision, replace only the node component.
216
        $path = preg_replace('!^node/!', '', $_GET['q']);
217
      }
218
      $path = $link['path'] . '/' . $path;
219

    
220
      foreach (array('node', 'comment') as $type) {
221
        $allowed_type = print_ui_link_allowed($link, array(
222
          'type' => $type,
223
          'node' => $node,
224
          'view_mode' => $view_mode,
225
        ));
226
        if ($allowed_type) {
227
          drupal_add_css(drupal_get_path('module', 'print_ui') . '/css/print_ui.theme.css');
228
          $links = array();
229
          $format = theme('print_ui_format_link', array('format' => $link['format'], 'location' => 'link'));
230

    
231
          // Show book link.
232
          if ($allowed_type === PRINT_UI_ALLOW_BOOK_LINK) {
233
            $path = $link['path'] . '/book/export/html/' . $node->nid;
234

    
235
            $links['book_' . $link['format']] = array(
236
              'href' => $path,
237
              'title' => $format['text'],
238
              'attributes' => $format['attributes'],
239
              'html' => $format['html'],
240
            );
241
          }
242
          elseif ($allowed_type === PRINT_UI_ALLOW_NORMAL_LINK) {
243
            $links['print_' . $link['format']] = array(
244
              'href' => $path,
245
              'title' => $format['text'],
246
              'attributes' => $format['attributes'],
247
              'html' => $format['html'],
248
              'query' => _print_ui_query_string_encode($_GET, array('q')),
249
            );
250
          }
251

    
252
          $link_content = array(
253
            '#theme' => 'links',
254
            '#links' => $links,
255
            '#attributes' => array('class' => array('links', 'inline')),
256
          );
257

    
258
          // If it's a node, and configured to show the link, but it's not the
259
          // html version of a book then show it.
260
          if (($type == 'node') && !empty($link_pos['link']) && !(isset($node->book) && ($link['format'] == 'html'))) {
261
            $node->content['links']['print_' . $link['format']] = $link_content;
262
          }
263
          elseif (($type == 'comment') && isset($node->content['comments']['comments'])) {
264
            foreach ($node->content['comments']['comments'] as $cid => $comment) {
265
              if (is_numeric($cid)) {
266
                $link_content['#links']['print_' . $link['format']]['query']['comment'] = $cid;
267
                $node->content['comments']['comments'][$cid]['links']['print_' . $link['format']] = $link_content;
268
              }
269
            }
270
          }
271
        }
272
      }
273

    
274
      if (!empty($link_pos['corner'])) {
275
        $corner_markup .= print_ui_insert_link($link, array(
276
          'node' => $node,
277
          'path' => $path,
278
          'location' => 'corner',
279
        ));
280
      }
281
    }
282
  }
283

    
284
  if (($view_mode == 'full') && (!empty($corner_markup))) {
285
    // Insert content corner links.
286
    $node->content['print_links'] = array(
287
      '#prefix' => '<span class="print-link">',
288
      '#markup' => $corner_markup,
289
      '#suffix' => '</span>',
290
      '#weight' => -101,
291
    );
292
  }
293
}
294

    
295
/**
296
 * Implements hook_node_load().
297
 */
298
function print_ui_node_load($nodes, $types) {
299
  $ids = array();
300
  foreach ($nodes as $node) {
301
    $ids[] = $node->nid;
302
  }
303

    
304
  foreach (module_implements('print_link') as $module) {
305
    $function = $module . '_print_link';
306
    if (function_exists($function)) {
307
      $link = call_user_func_array($function, array());
308

    
309
      $display = 'print_' . $link['format'] . '_display';
310
      $display_comment = 'print_' . $link['format'] . '_display_comment';
311
      $display_urllist = 'print_' . $link['format'] . '_display_urllist';
312

    
313
      $result = db_query('SELECT nid, link, comments, url_list FROM {' . $module . '_node_conf} WHERE nid IN (:nids)', array(':nids' => $ids))->fetchAllAssoc('nid');
314

    
315
      foreach ($nodes as $node) {
316
        $node->{$display} = isset($result[$node->nid]) ? intval($result[$node->nid]->link) : variable_get($display . '_' . $node->type, PRINT_UI_TYPE_SHOW_LINK_DEFAULT);
317
        $node->{$display_comment} = isset($result[$node->nid]) ? intval($result[$node->nid]->comments) : variable_get($display_comment . '_' . $node->type, PRINT_UI_TYPE_COMMENT_LINK_DEFAULT);
318
        $node->{$display_urllist} = isset($result[$node->nid]) ? intval($result[$node->nid]->url_list) : variable_get($display_urllist . '_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
319
      }
320
    }
321
  }
322
}
323

    
324
/**
325
 * Implements hook_node_insert().
326
 */
327
function print_ui_node_insert($node) {
328
  print_ui_node_update($node);
329
}
330

    
331
/**
332
 * Implements hook_node_update().
333
 */
334
function print_ui_node_update($node) {
335
  if (user_access('administer print') || user_access('node-specific print configuration')) {
336
    foreach (module_implements('print_link') as $module) {
337
      $function = $module . '_print_link';
338
      if (function_exists($function)) {
339
        $link = call_user_func_array($function, array());
340

    
341
        $display = 'print_' . $link['format'] . '_display';
342
        $display_comment = 'print_' . $link['format'] . '_display_comment';
343
        $display_urllist = 'print_' . $link['format'] . '_display_urllist';
344

    
345
        if (!isset($node->{$display}) || $node->{$display} === NULL) {
346
          $node->{$display} = variable_get($display . '_' . $node->type, PRINT_UI_TYPE_SHOW_LINK_DEFAULT);
347
        }
348
        if (!isset($node->{$display_comment}) || $node->{$display_comment} === NULL) {
349
          $node->{$display_comment} = variable_get($display_comment . '_' . $node->type, PRINT_UI_TYPE_COMMENT_LINK_DEFAULT);
350
        }
351
        if (!isset($node->{$display_urllist}) || $node->{$display_urllist} === NULL) {
352
          $node->{$display_urllist} = variable_get($display_urllist . '_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
353
        }
354

    
355
        db_merge($module . '_node_conf')
356
          ->key(array('nid' => $node->nid))
357
          ->fields(array(
358
            'link' => $node->{$display},
359
            'comments' => $node->{$display_comment},
360
            'url_list' => $node->{$display_urllist},
361
          ))
362
          ->execute();
363
      }
364
    }
365
  }
366
}
367

    
368
/**
369
 * Implements hook_node_delete().
370
 */
371
function print_ui_node_delete($node) {
372
  foreach (module_implements('print_link') as $module) {
373
    $function = $module . '_print_link';
374
    if (function_exists($function)) {
375
      call_user_func_array($function, array());
376

    
377
      db_delete($module . '_node_conf')
378
        ->condition('nid', $node->nid)
379
        ->execute();
380
    }
381
  }
382
}
383

    
384
/**
385
 * Implements hook_form_alter().
386
 */
387
function print_ui_form_alter(&$form, &$form_state, $form_id) {
388
  // Add the node-type settings option to activate the printer-friendly
389
  // version link.
390
  if ((user_access('administer print') || user_access('node-specific print configuration')) &&
391
      (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
392
    $form['print'] = array(
393
      '#type' => 'fieldset',
394
      '#title' => t('Printer, email and PDF versions'),
395
      '#collapsible' => TRUE,
396
      '#collapsed' => TRUE,
397
      '#weight' => PRINT_UI_TYPE_FIELDS_WEIGHT,
398
      '#group' => 'additional_settings',
399
    );
400

    
401
    foreach (module_implements('print_link') as $module) {
402
      $function = $module . '_print_link';
403
      if (function_exists($function)) {
404
        $link = call_user_func_array($function, array());
405

    
406
        $form['print']['print_' . $link['format']] = array(
407
          '#type' => 'fieldset',
408
          '#title' => check_plain($link['text']),
409
          '#collapsible' => TRUE,
410
        );
411

    
412
        $display = 'print_' . $link['format'] . '_display';
413
        $display_comment = 'print_' . $link['format'] . '_display_comment';
414
        $display_urllist = 'print_' . $link['format'] . '_display_urllist';
415

    
416
        $form['print']['print_' . $link['format']][$display] = array(
417
          '#type' => 'checkbox',
418
          '#title' => t('Show link'),
419
        );
420
        $form['print']['print_' . $link['format']][$display_comment] = array(
421
          '#type' => 'checkbox',
422
          '#title' => t('Show link in individual comments'),
423
        );
424
        $form['print']['print_' . $link['format']][$display_urllist] = array(
425
          '#type' => 'checkbox',
426
          '#title' => t('Show Printer-friendly URLs list'),
427
        );
428

    
429
        if ($form_id == 'node_type_form') {
430
          $form['print']['print_' . $link['format']][$display]['#default_value'] = variable_get($display . '_' . $form['#node_type']->type, PRINT_UI_TYPE_SHOW_LINK_DEFAULT);
431
          $form['print']['print_' . $link['format']][$display_comment]['#default_value'] = variable_get($display_comment . '_' . $form['#node_type']->type, PRINT_UI_TYPE_COMMENT_LINK_DEFAULT);
432
          $form['print']['print_' . $link['format']][$display_urllist]['#default_value'] = variable_get($display_urllist . '_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
433
        }
434
        else {
435
          $node = $form['#node'];
436
          $form['print']['print_' . $link['format']][$display]['#default_value'] = isset($node->{$display}) ? $node->{$display} : variable_get($display . '_' . $node->type, PRINT_UI_TYPE_SHOW_LINK_DEFAULT);
437
          $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);
438
          $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);
439
        }
440
      }
441
    }
442
  }
443
}
444

    
445
/**
446
 * Auxiliary function to fill the Printer-friendly link attributes.
447
 *
448
 * @param string $title
449
 *   Text to displayed by the link when hovering over it with the mouse.
450
 * @param string $class
451
 *   Class attribute to be used in the link.
452
 * @param bool $new_window
453
 *   If TRUE opens the target page in a new window.
454
 *
455
 * @return array
456
 *   An associative array containing:
457
 *   - title: text to be used when hovering over the link.
458
 *   - class: CSS class of the link tag.
459
 *   - target: used for opening a new window with the non-javascript method
460
 *   - onclick: open a new window, with the javascript method
461
 *   - rel: SEO-related attribute indicating that the printer-friendly version
462
 *     should not be indexed by search engine robots.
463
 */
464
function _print_ui_fill_attributes($title = '', $class = '', $new_window = FALSE) {
465
  $print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT);
466
  $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
467

    
468
  $attributes = array();
469
  $attributes['title'] = $title;
470
  if (!empty($class)) {
471
    $attributes['class'] = array($class);
472
  }
473

    
474
  if ($new_window) {
475
    switch ($print_newwindow) {
476
      case 0:
477
        $attributes['target'] = '_blank';
478
        break;
479

    
480
      case 1:
481
        $attributes['onclick'] = 'window.open(this.href); return false';
482
        break;
483
    }
484
  }
485
  if (!empty($print_robots_noindex)) {
486
    $attributes['rel'] = 'nofollow';
487
  }
488
  return $attributes;
489
}
490

    
491
/**
492
 * Format the Printer-friendly link.
493
 *
494
 * @return array
495
 *   An associative array containing:
496
 *   - text: The content of the link
497
 *   - html: TRUE if the text contains HTML tags, FALSE if it's plain text
498
 *   - attributes: several attributes of the link tag (title, class, target,
499
 *     onclick, rel)
500
 *
501
 * @see _print_ui_fill_attributes()
502
 * @ingroup themeable
503
 * @ingroup print_themeable
504
 */
505
function theme_print_ui_format_link($vars) {
506
  $format = $vars['format'];
507

    
508
  foreach (module_implements('print_link') as $module) {
509
    $function = $module . '_print_link';
510
    if (function_exists($function)) {
511
      $link = call_user_func_array($function, array());
512

    
513
      if ($link['format'] == $format) {
514
        $link_class = variable_get('print_' . $link['format'] . '_link_class', $link['class']);
515

    
516
        $new_window = FALSE;
517
        $func = $module . '_print_new_window_alter';
518
        if (function_exists($func)) {
519
          $func($new_window, $link['format']);
520
        }
521

    
522
        $show_link = variable_get('print_' . $link['format'] . '_show_link', PRINT_UI_SHOW_LINK_DEFAULT);
523
        $link_text = filter_xss(variable_get('print_' . $link['format'] . '_link_text', $link['text']));
524

    
525
        $text = '';
526
        if ($show_link >= 2) {
527
          $img = drupal_get_path('module', $module) . '/icons/' . $link['icon'];
528
          switch ($show_link) {
529
            case 2:
530
              $text = theme('image', array(
531
                'path' => $img,
532
                'width' => '16px',
533
                'height' => '16px',
534
                'alt' => $link_text,
535
                'title' => $link_text,
536
                'attributes' => array('class' => array('print-icon')),
537
              ));
538
              break;
539

    
540
            case 3:
541
              $text = theme('image', array(
542
                'path' => $img,
543
                'width' => '16px',
544
                'height' => '16px',
545
                'alt' => $link_text,
546
                'title' => $link_text,
547
                'attributes' => array('class' => array('print-icon', 'print-icon-margin')),
548
              )) . $link_text;
549
              break;
550
          }
551
          $html = TRUE;
552
        }
553
        else {
554
          $text = $link_text;
555
          $html = FALSE;
556
        }
557

    
558
        return array(
559
          'text' => $text,
560
          'html' => $html,
561
          'attributes' => _print_ui_fill_attributes($link['description'], strip_tags($link_class), $new_window),
562
        );
563
      }
564
    }
565
  }
566
  return array();
567
}
568

    
569
/**
570
 * Auxiliary function to display a formatted Printer-friendly link.
571
 *
572
 * Function made available so that developers may call this function from
573
 * their defined pages/blocks.
574
 *
575
 * @param array $link
576
 *    Array returned by the hook_print_link() call.
577
 * @param array $args
578
 *   Array of optional arguments:
579
 *   - node: node object, to be used in checking node access. If the path
580
 *     argument is not provided, the path used will be node/nid.
581
 *   - path: path to be used in the link. If not specified, the current URL
582
 *     is used.
583
 *   - location: the location in the page where the link is being inserted
584
 *     ('link', 'corner', 'block', 'help').
585
 *
586
 * @return string
587
 *   string with the HTML link to the printer-friendly page
588
 */
589
function print_ui_insert_link($link, $args = array()) {
590
  $node = isset($args['node']) ? $args['node'] : NULL;
591
  $path = isset($args['path']) ? $args['path'] : NULL;
592
  $location = isset($args['location']) ? $args['location'] : '';
593

    
594
  if ($node !== NULL) {
595
    $nid = $node->nid;
596
    if ($path === NULL) {
597
      $path = 'node/' . $nid;
598
    }
599
    $allowed_type = print_ui_link_allowed($link, array('node' => $node));
600
  }
601
  else {
602
    if ($path === NULL) {
603
      $nid = preg_replace('!^node/([\d]+)!', '$1', $_GET['q']);
604
      $path = $_GET['q'];
605
    }
606
    else {
607
      $nid = NULL;
608
    }
609
    $allowed_type = print_ui_link_allowed($link, array('path' => $path));
610
  }
611

    
612
  if ($allowed_type) {
613
    if ($nid !== NULL) {
614
      if ($allowed_type === PRINT_UI_ALLOW_BOOK_LINK) {
615
        $path = 'book/export/html/' . $nid;
616
      }
617
      else {
618
        if (variable_get('print_' . $link['format'] . '_link_use_alias', PRINT_UI_LINK_USE_ALIAS_DEFAULT) && ($alias = drupal_lookup_path('alias', $path))) {
619
          $path = $alias;
620
        }
621
        else {
622
          $path = $nid;
623
        }
624
      }
625
      $path = $link['path'] . '/' . $path;
626
      $query = _print_ui_query_string_encode($_GET, array('q'));
627
    }
628
    else {
629
      $query = NULL;
630
    }
631
    drupal_add_css(drupal_get_path('module', 'print_ui') . '/css/print_ui.theme.css');
632
    $format = theme('print_ui_format_link', array('format' => $link['format'], 'location' => $location));
633
    return '<span class="print_' . $link['format'] . '">' . l($format['text'], $path, array(
634
      'attributes' => $format['attributes'],
635
      'query' => $query,
636
      'absolute' => TRUE,
637
      'html' => $format['html'],
638
    )) . '</span>';
639
  }
640
  else {
641
    return FALSE;
642
  }
643
}
644

    
645
/**
646
 * Check if the link to the PF version is allowed depending on the settings.
647
 *
648
 * @param array $link
649
 *   Array returned by the hook_print_link() call.
650
 * @param array $args
651
 *   Array containing the possible parameters:
652
 *    view_mode, node, type, path.
653
 *
654
 * @return int
655
 *   FALSE if not allowed
656
 *   PRINT_UI_ALLOW_NORMAL_LINK if a normal link is allowed
657
 *   PRINT_UI_ALLOW_BOOK_LINK if a link is allowed in a book node
658
 */
659
function print_ui_link_allowed($link, $args) {
660
  if (isset($args['view_mode'])) {
661
    $view_mode = $args['view_mode'];
662
    if ((($view_mode == 'teaser') && !variable_get('print_' . $link['format'] . '_link_teaser', PRINT_UI_LINK_TEASER_DEFAULT))
663
      || !in_array($view_mode, array('full', 'teaser'))) {
664
      // If the teaser link is disabled.
665
      return FALSE;
666
    }
667
  }
668
  $link_allowed_func = $link['module'] . '_link_allowed';
669
  if (function_exists($link_allowed_func)) {
670
    if (!$link_allowed_func($args)) {
671
      // If the format-specific function disallows the link.
672
      return FALSE;
673
    }
674
  }
675
  if (!empty($args['path'])) {
676
    $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
677
    if (ctype_digit($nid)) {
678
      $args['node'] = node_load($nid);
679
    }
680
  }
681
  if (!empty($args['node'])) {
682
    static $node_type = '';
683

    
684
    $node = $args['node'];
685
    if (isset($node->type)) {
686
      $node_type = $node->type;
687
    }
688
    // Node.
689
    if (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
690
      // Link is for a comment, return the configured setting
691
      // Cache this statically to avoid duplicate queries for every comment.
692
      static $res = array();
693
      if (!isset($res[$link['format']][$node->nid])) {
694
        $res[$link['format']][$node->nid] = db_query("SELECT comments FROM {" . $link['module'] . "_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))->fetchField();
695
      }
696
      $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);
697
      if ($display_comment) {
698
        return PRINT_UI_ALLOW_NORMAL_LINK;
699
      }
700
    }
701
    else {
702
      // Node link.
703
      $display = 'print_' . $link['format'] . '_display';
704
      if (isset($node->{$display}) && !$node->{$display}) {
705
        // Link for this node is disabled.
706
        return FALSE;
707
      }
708
      elseif (isset($node->book)) {
709
        // Node is a book.
710
        $book_link = variable_get('print_' . $link['format'] . '_book_link', PRINT_UI_BOOK_LINK_DEFAULT);
711
        switch ($book_link) {
712
          case 1:
713
            if (user_access('access printer-friendly version')) {
714
              return PRINT_UI_ALLOW_BOOK_LINK;
715
            }
716
            break;
717

    
718
          case 2:
719
            return PRINT_UI_ALLOW_NORMAL_LINK;
720
        }
721
      }
722
      else {
723
        return PRINT_UI_ALLOW_NORMAL_LINK;
724
      }
725
    }
726
  }
727
  else {
728
    // 'System' page.
729
    $sys_link_visibility = variable_get('print_' . $link['format'] . '_sys_link_visibility', PRINT_UI_SYS_LINK_VISIBILITY_DEFAULT);
730
    $sys_link_pages = variable_get('print_' . $link['format'] . '_sys_link_pages', PRINT_UI_SYS_LINK_PAGES_DEFAULT);
731

    
732
    return _print_ui_page_match($sys_link_visibility, $_GET['q'], $sys_link_pages);
733
  }
734
  return FALSE;
735
}
736

    
737
/**
738
 * Check if the provided page is enabled according to the visibility settings.
739
 *
740
 * @param int $visibility
741
 *   Current visibility settings:
742
 *    0 for show on every page except the listed pages
743
 *    1 for show on only the listed pages.
744
 * @param string $path
745
 *   Current path.
746
 * @param string $pages
747
 *   List of pages.
748
 *
749
 * @return bool
750
 *   TRUE if it is enabled, FALSE otherwise
751
 */
752
function _print_ui_page_match($visibility, $path, $pages) {
753
  if ($pages) {
754
    if ($visibility == 2) {
755
      if (module_exists('php')) {
756
        return php_eval($pages);
757
      }
758
      else {
759
        return FALSE;
760
      }
761
    }
762
    $alias = drupal_get_path_alias($path);
763
    $page_match = drupal_match_path($path, $pages);
764
    if ($alias != $path) {
765
      $page_match = $page_match || drupal_match_path($alias, $pages);
766
    }
767

    
768
    return !($visibility xor $page_match);
769
  }
770
  else {
771
    return !$visibility;
772
  }
773
}
774

    
775
/**
776
 * Parse an array into a valid urlencoded query string.
777
 *
778
 * Modified from drupal_query_string_encode to prevent re-encoding of
779
 * encoded original. (see #301192)
780
 *
781
 * @param array $query
782
 *   The array to be processed e.g. $_GET.
783
 * @param array $exclude
784
 *   The array filled with keys to be excluded.
785
 * @param string $parent
786
 *   The be used in recursive calls.
787
 *
788
 * @return array
789
 *   urlencoded string which can be appended to/as the URL query string
790
 */
791
function _print_ui_query_string_encode($query, $exclude = array(), $parent = '') {
792
  $params = array();
793
  foreach ($query as $key => $value) {
794
    if (in_array($key, $exclude, TRUE)) {
795
      continue;
796
    }
797

    
798
    if (is_array($value)) {
799
      $params[$key] = _print_ui_query_string_encode($value, $exclude, $key);
800
    }
801
    else {
802
      $params[$key] = $value;
803
    }
804
  }
805

    
806
  return empty($params) ? NULL : $params;
807
}