print.module 30.5 KB
Newer Older
1 2
<?php

3
/**
4
 * @defgroup print Printer, email and PDF versions
5 6 7 8 9 10 11 12 13 14
 *
 * Welcome to the print module developer's documentation. The interesting
 * functions for themers are those that start with 'theme_'.
 *
 * - Printer-friendly pages
 *   - @link print.module Module main file @endlink
 *   - @link print.admin.inc Settings form @endlink
 *   - @link print.pages.inc HTML generation @endlink
 *   - @link print.install (Un)Install routines @endlink
 *   - @link print.tpl.php Page generation template @endlink
15
 * - Send by email
16 17 18 19 20 21 22 23 24 25 26 27 28
 *   - @link print_mail.module Module main file @endlink
 *   - @link print_mail.admin.inc Settings form @endlink
 *   - @link print_mail.inc Mail form and send mail routine @endlink
 *   - @link print_mail.install (Un)Install routines @endlink
 * - PDF version
 *   - @link print_pdf.module Module main file @endlink
 *   - @link print_pdf.admin.inc Settings form @endlink
 *   - @link print_pdf.pages.inc PDF generation @endlink
 *   - @link print_pdf.class.inc Auxiliary PHP5 class @endlink
 *   - @link print_pdf.class_php4.inc Auxiliary PHP4 class @endlink
 *   - @link print_pdf.install (Un)Install routines @endlink
 */

29 30
/**
 * @file
jcnventura's avatar
jcnventura committed
31 32 33 34
 * Displays Printer-friendly versions of Drupal pages.
 *
 * This is the core module of the PF package, with the Drupal hooks
 * and other administrative functions.
35 36
 *
 * @ingroup print
37 38
 */

39 40
define('PRINT_PATH', 'print');

41
define('PRINT_HTML_FORMAT', 'html');
42 43
define('PRINT_MAIL_FORMAT', 'mail');
define('PRINT_PDF_FORMAT', 'pdf');
44
define('PRINT_LOGO_OPTIONS_DEFAULT', 1);
45
define('PRINT_LOGO_URL_DEFAULT', '');
46 47
define('PRINT_FOOTER_OPTIONS_DEFAULT', 1);
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 53 54
define('PRINT_COMMENTS_DEFAULT', 0);
define('PRINT_NEWWINDOW_DEFAULT', 1);

55
define('PRINT_HTML_LINK_POS_DEFAULT', 'link');
56
define('PRINT_HTML_LINK_TEASER_DEFAULT', 0);
57 58 59 60 61
define('PRINT_HTML_SHOW_LINK_DEFAULT', 1);
define('PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT', 0);
define('PRINT_HTML_NODE_LINK_PAGES_DEFAULT', '');
define('PRINT_HTML_LINK_CLASS_DEFAULT', 'print-page');
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 65 66
define('PRINT_HTML_BOOK_LINK_DEFAULT', 1);
define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);
67
define('PRINT_HTML_WINDOWCLOSE_DEFAULT', 1);
68 69 70 71 72 73 74 75 76 77 78

define('PRINT_SOURCEURL_ENABLED_DEFAULT', 1);
define('PRINT_SOURCEURL_DATE_DEFAULT', 0);
define('PRINT_SOURCEURL_FORCENODE_DEFAULT', 0);

define('PRINT_ROBOTS_NOINDEX_DEFAULT', 1);
define('PRINT_ROBOTS_NOFOLLOW_DEFAULT', 1);
define('PRINT_ROBOTS_NOARCHIVE_DEFAULT', 0);

define('PRINT_TYPE_SHOW_LINK_DEFAULT', 1);
define('PRINT_TYPE_COMMENT_LINK_DEFAULT', 0);
79
define('PRINT_TYPE_URLLIST_DEFAULT', 1);
80
define('PRINT_TYPE_SYS_URLLIST_DEFAULT', 0);
jcnventura's avatar
jcnventura committed
81

82 83
define('PRINT_ALLOW_NORMAL_LINK', 1);
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 92 93 94
    'administer print' => array(
      'title' => t('Administer the module'),
      'description' => t('Perform maintenance tasks for the print module.'),
    ),
jcnventura's avatar
jcnventura committed
95 96 97 98
    'node-specific print configuration' => array(
      'title' => t('Node-specific configuration'),
      'description' => t('Enable access to the per-node settings.'),
    ),
jcnventura's avatar
jcnventura committed
99 100 101 102
    'access print' => array(
      'title' => t('Access the printer-friendly page'),
      'description' => t('View the printer-friendly pages and the links to them in the original pages.'),
    ),
103
  );
104
}
105

jcnventura's avatar
jcnventura committed
106
/**
107
 * Implements hook_theme().
jcnventura's avatar
jcnventura committed
108 109 110 111
 */
function print_theme() {
  return array(
    'print_format_link' => array(
112
      'variables' => array(),
jcnventura's avatar
jcnventura committed
113
    ),
114
    'print' => array(
115
      'variables' => array('print' => array(), 'type' => PRINT_HTML_FORMAT, 'node' => NULL),
116 117
      'template' => 'print',
    ),
jcnventura's avatar
jcnventura committed
118 119 120
  );
}

121
/**
122
 * Implements hook_preprocess_HOOK().
123
 */
124 125 126 127 128 129 130 131 132 133
function print_preprocess_node(&$variables) {
  if ($variables['elements']['#view_mode'] == 'print') {
    $type = $variables['elements']['#node']->type;
    $format = $variables['elements']['#print_format'];
    $nid = $variables['elements']['#node']->nid;

    $variables['theme_hook_suggestions'][] = "node__print";
    $variables['theme_hook_suggestions'][] = "node__print__{$format}";
    $variables['theme_hook_suggestions'][] = "node__print__{$format}__node__{$type}";
    $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 142 143 144
  static $hooks = NULL;
  if (!isset($hooks)) {
    drupal_theme_initialize();
    $hooks = theme_get_registry();
145
  }
146 147 148

  $variables['page']['#show_messages'] = FALSE;

jcnventura's avatar
jcnventura committed
149
  // Stolen from theme() so that ALL preprocess functions are called
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
  $hook = 'page';
  $info = $hooks[$hook];
  if (isset($info['preprocess functions']) || isset($info['process functions'])) {
    $variables['theme_hook_suggestions'] = array();
    foreach (array('preprocess functions', 'process functions') as $phase) {
      if (!empty($info[$phase])) {
        foreach ($info[$phase] as $processor_function) {
          if (function_exists($processor_function)) {
            // We don't want a poorly behaved process function changing $hook.
            $hook_clone = $hook;
            $processor_function($variables, $hook_clone);
          }
        }
      }
    }
165
  }
166 167 168

  $format = $variables['type'];
  $type = (isset($variables['node']->type)) ? $variables['node']->type : '';
169
  $nid = (isset($variables['node']->nid)) ? $variables['node']->nid : '';
170

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 183
  $items = array();

184 185
  $items[PRINT_PATH] = array(
    'title' => 'Printer-friendly',
jcnventura's avatar
jcnventura committed
186
    'page callback' => 'print_controller_html',
187
    'access arguments' => array('access print'),
jcnventura's avatar
jcnventura committed
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',
jcnventura's avatar
jcnventura committed
198 199
    'page arguments' => array('print_html_settings'),
    'access arguments'  => array('administer print'),
200
    'file' => 'print.admin.inc',
jcnventura's avatar
jcnventura committed
201
  );
202
  $items['admin/config/user-interface/print/html'] = array(
jcnventura's avatar
jcnventura committed
203
    'title' => 'Web page',
204
    'weight' => 1,
jcnventura's avatar
jcnventura committed
205 206
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
207
  $items['admin/config/user-interface/print/html/options'] = array(
208 209 210 211
    'title' => 'Options',
    'weight' => 1,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
212
  $items['admin/config/user-interface/print/html/strings'] = array(
213
    'title' => 'Text strings',
jcnventura's avatar
jcnventura committed
214
    'description' => 'Override the user-facing strings used in the printer-friendly version.',
215 216 217 218 219
    'page callback' => 'drupal_get_form',
    'page arguments' => array('print_html_strings_settings'),
    'access arguments'  => array('administer print'),
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
220
    'file' => 'print.admin.inc',
221
  );
222
  $items['admin/config/user-interface/print/common'] = array(
jcnventura's avatar
jcnventura committed
223
    'title' => 'Settings',
jcnventura's avatar
jcnventura committed
224
    'description' => 'Settings for the common functionalities for all the print sub-modules.',
jcnventura's avatar
jcnventura committed
225
    'page callback' => 'drupal_get_form',
226 227
    'page arguments' => array('print_main_settings'),
    'access arguments'  => array('administer print'),
jcnventura's avatar
jcnventura committed
228 229
    'weight' => 10,
    'type' => MENU_LOCAL_TASK,
230
    'file' => 'print.admin.inc',
231
  );
232
  $items['admin/config/user-interface/print/common/options'] = array(
233 234 235 236
    'title' => 'Options',
    'weight' => 1,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
237
  $items['admin/config/user-interface/print/common/strings'] = array(
238
    'title' => 'Text strings',
jcnventura's avatar
jcnventura committed
239
    'description' => 'Override the user-facing strings used by the print module.',
240 241 242 243 244
    'page callback' => 'drupal_get_form',
    'page arguments' => array('print_main_strings_settings'),
    'access arguments'  => array('administer print'),
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
245
    'file' => 'print.admin.inc',
246
  );
247 248 249 250

  return $items;
}

251
/**
252
 * Implements hook_block_info().
253
 */
254
function print_block_info() {
255
      $block['print-links']['info'] = t('Printer, email and PDF versions');
jcnventura's avatar
jcnventura committed
256 257 258
      $block['print-links']['cache'] = DRUPAL_CACHE_PER_PAGE;
      $block['print-top']['info'] = t('Most printed');
      $block['print-top']['cache'] = DRUPAL_CACHE_GLOBAL;
259
      return $block;
260
}
jcnventura's avatar
jcnventura committed
261 262

/**
263
 * Implements hook_block_view().
jcnventura's avatar
jcnventura committed
264
 */
265
function print_block_view($delta = '') {
266 267
  switch ($delta) {
    case 'print-links':
268
      $nid = preg_replace('!^node/!', '', $_GET['q']);
269
      if (ctype_digit($nid)) {
270
        $node = node_load($nid);
271 272 273 274 275
        if (!node_access('view', $node)) {
          // If the user doesn't have access to the node, don't show any links
          $block['content'] == '';
          return;
        }
276 277 278 279 280 281 282 283 284 285
      }
      else {
        $node = NULL;
      }
      $funcs = get_defined_functions();
      $block['content'] = '';
      foreach ($funcs['user'] as $func) {
        if (preg_match('!^print.*?_insert_link$!', $func)) {
          $link = $func(NULL, $node);
          if (!empty($link)) {
286
            $block['content'] .= $link;
287 288
          }
        }
289
      }
290 291 292
      break;
    case 'print-top':
      $block['subject'] = t('Most printed');
jcnventura's avatar
jcnventura committed
293
      $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)
294 295 296 297 298
                  ->fetchAll();
      if (count($result)) {
        $block['content'] = '<div class="item-list"><ul>';
        foreach ($result as $obj) {
          $block['content'] .= '<li>' . l(_print_get_title($obj->path), $obj->path) . '</li>';
299
        }
300
        $block['content'] .= '</ul></div>';
301
      }
302 303 304
      break;
  }
  return $block;
305
}
306

jcnventura's avatar
jcnventura committed
307
/**
308
 * Implements hook_node_view_alter().
jcnventura's avatar
jcnventura committed
309
 */
310 311 312
function print_node_view_alter(&$build) {
  if (isset($build['links']['book']['#links']['book_printer'])) {
    $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
jcnventura's avatar
jcnventura committed
313

314 315
    if ($print_html_book_link) {
      $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
316

317
      if (!empty($print_html_link_pos['link'])) {
jcnventura's avatar
jcnventura committed
318 319
        $format = theme('print_format_link');

320 321
        switch ($print_html_book_link) {
          case 1:
322
            $path = $build['links']['book']['#links']['book_printer']['href'];
323 324 325
            break;
          case 2:
            $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT);
326
            $path = ($print_html_link_use_alias && ($alias = drupal_lookup_path('alias', 'node/' . $build['#node']->nid))) ? $alias : $build['#node']->nid;
327 328 329
            break;
        }

330
        $build['links']['book']['#links']['book_printer'] = array(
jcnventura's avatar
jcnventura committed
331
          'href' => PRINT_PATH . '/' . $path,
332 333 334 335
          'title' => $format['text'],
          'attributes' => $format['attributes'],
          'html' => $format['html'],
        );
336 337 338
      }
      else {
        unset($build['links']['book']['#links']['book_printer']);
jcnventura's avatar
jcnventura committed
339 340 341 342 343
      }
    }
  }
}

344
/**
345
 * Implements hook_help().
346 347
 */
function print_help($path, $arg) {
jcnventura's avatar
jcnventura committed
348 349 350
  switch ($path) {
    case 'admin/help#print':
      // Return a line-break version of the module README
351
      return _filter_autop(file_get_contents(drupal_get_path('module', 'print') . '/README.txt'));
jcnventura's avatar
jcnventura committed
352 353
  }

354
  $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
355
  if (($path !== 'node/%') && !(empty($print_html_link_pos['link']) && empty($print_html_link_pos['corner']))) {
356 357 358 359
    static $output = FALSE;

    if ($output === FALSE) {
      $output = TRUE;
jcnventura's avatar
jcnventura committed
360

361 362 363 364
      $link = print_insert_link();
      if ($link) {
        return "<span class='print-syslink'>$link</span>";
      }
365
    }
366
  }
367
}
368

369
/**
370
 * Implements hook_node_view().
371
 */
372
function print_node_view($node, $view_mode) {
373
  $type = 'node';
jcnventura's avatar
jcnventura committed
374 375
  $print_html_link_pos = variable_get('print_html_link_pos', array(PRINT_HTML_LINK_POS_DEFAULT => PRINT_HTML_LINK_POS_DEFAULT));
  $print_html_link_use_alias = variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT);
376
  $allowed_type = print_link_allowed(array('type' => $type, 'node' => $node, 'view_mode' => $view_mode));
jcnventura's avatar
jcnventura committed
377
  if (($allowed_type === PRINT_ALLOW_NORMAL_LINK) && !isset($node->book) && !empty($print_html_link_pos['link'])) {
jcnventura's avatar
jcnventura committed
378
    drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
jcnventura's avatar
jcnventura committed
379 380 381 382
    $links = array();
    $format = theme('print_format_link');

    $query_arr = $_GET;
383
    if ($type == 'comment') {
jcnventura's avatar
jcnventura committed
384 385 386 387 388
      $query_arr['comment'] = $node->cid;
    }
    $query = print_query_string_encode($query_arr, array('q'));
    if (empty($query)) $query = NULL;

389 390
    if (($print_html_link_use_alias) && ($alias = drupal_lookup_path('alias', 'node/' . $node->nid))) {
      $path = $alias;
jcnventura's avatar
jcnventura committed
391 392 393 394 395 396
    }
    else {
      $path = $node->nid;
    }

    $links['print_html'] = array(
jcnventura's avatar
jcnventura committed
397
      'href' => PRINT_PATH . '/' . $path,
jcnventura's avatar
jcnventura committed
398 399 400 401 402 403 404
      'title' => $format['text'],
      'attributes' => $format['attributes'],
      'html' => $format['html'],
      'query' => $query,
    );

    $node->content['links']['print_html'] = array(
405 406
      '#theme' => 'links',
      '#links' => $links,
407
      '#attributes' => array('class' => array('links', 'inline')),
jcnventura's avatar
jcnventura committed
408 409 410
    );
  }

411
  if ($view_mode == 'full') {
jcnventura's avatar
jcnventura committed
412
      // Insert content corner links
413
      $node->content['print_links'] = array(
414 415 416
        '#prefix' => '<span class="print-link">',
        '#markup' => '',
        '#suffix' => '</span>',
417 418
        '#weight' => -101,
      );
419
      if (!empty($print_html_link_pos['corner'])) {
420
        $node->content['print_links']['#markup'] .= print_insert_link(NULL, $node);
421
      }
422
  }
423
}
424

425
/**
426
 * Implements hook_node_load().
427
 */
428
function print_node_load($nodes, $types) {
429
  $ids = array();
430
  foreach ($nodes as $node) {
431 432 433 434 435 436 437 438 439
    $ids[] = $node->nid;
  }

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

  foreach ($nodes as $node) {
    $node->print_display = isset($result[$node->nid]) ? intval($result[$node->nid]->link) : variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
    $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);
    $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);
440
  }
441
}
442

443 444 445
/**
 * Implements hook_node_insert().
 */
446
function print_node_insert($node) {
447
  if (user_access('administer print') || user_access('node-specific print configuration')) {
448 449 450
    if (!isset($node->print_display)) $node->print_display = variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
    if (!isset($node->print_display_comment)) $node->print_display_comment = variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
    if (!isset($node->print_display_urllist)) $node->print_display_urllist = variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
451

452 453
    _print_node_conf_modify($node->nid, $node->print_display, $node->print_display_comment, $node->print_display_urllist);
  }
454
}
455

456
/**
457
 * Implements hook_node_update().
458
 */
459
function print_node_update($node) {
460
  if (user_access('administer print') || user_access('node-specific print configuration')) {
461 462 463
    if (!isset($node->print_display)) $node->print_display = variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
    if (!isset($node->print_display_comment)) $node->print_display_comment = variable_get('print_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
    if (!isset($node->print_display_urllist)) $node->print_display_urllist = variable_get('print_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
464

465 466
    _print_node_conf_modify($node->nid, $node->print_display, $node->print_display_comment, $node->print_display_urllist);
  }
467
}
468 469

/**
470
 * Implements hook_node_delete().
471
 */
472
function print_node_delete($node) {
473 474 475 476
  db_delete('print_node_conf')
    ->condition('nid', $node->nid)
    ->execute();
  db_delete('print_page_counter')
477
    ->condition('path', 'node/' . $node->nid)
478
    ->execute();
479
}
480

481
/**
482
 * Implements hook_form_alter().
483
 */
484
function print_form_alter(&$form, &$form_state, $form_id) {
485
  // Add the node-type settings option to activate the printer-friendly version link
jcnventura's avatar
jcnventura committed
486
  if ((user_access('administer print') || user_access('node-specific print configuration')) &&
jcnventura's avatar
jcnventura committed
487
      (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
488 489
    $form['print'] = array(
      '#type' => 'fieldset',
490
      '#title' => t('Printer, email and PDF versions'),
491 492
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
493
      '#weight' => PRINT_TYPE_FIELDS_WEIGHT,
494
      '#group' => 'additional_settings',
495 496
    );

497 498
    $form['print']['label'] = array(
      '#type' => 'markup',
jcnventura's avatar
jcnventura committed
499
      '#markup' => '<p><strong>' . t('Printer-friendly version') . '</strong></p>',
500 501
    );

502
    $form['print']['print_display'] = array(
503
      '#type' => 'checkbox',
504
      '#title' => t('Show link'),
505
    );
506
    $form['print']['print_display_comment'] = array(
507
      '#type' => 'checkbox',
508 509 510 511 512
      '#title' => t('Show link in individual comments'),
    );
    $form['print']['print_display_urllist'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show Printer-friendly URLs list'),
513
    );
514 515

    if ($form_id == 'node_type_form') {
jcnventura's avatar
jcnventura committed
516 517 518
      $form['print']['print_display']['#default_value'] = variable_get('print_display_' . $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
      $form['print']['print_display_comment']['#default_value'] = variable_get('print_display_comment_' . $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
      $form['print']['print_display_urllist']['#default_value'] = variable_get('print_display_urllist_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
519 520
    }
    else {
521
      $node = $form['#node'];
522 523 524
      $form['print']['print_display']['#default_value'] = isset($node->print_display) ? $node->print_display : variable_get('print_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
      $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);
      $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);
525 526
    }
  }
527
}
528

529
/**
530
 * Implements hook_entity_info_alter().
531
 */
532 533 534
function print_entity_info_alter(&$info) {
  // Add the 'Print' view mode for nodes.
  $info['node']['view modes'] += array(
535
    'print' => array(
536 537
      'label' => t('Print'),
      'custom settings' => FALSE,
538 539
    ),
  );
540
}
541

542 543 544 545 546 547 548 549 550 551 552
/**
 * Auxiliary function to discover a given page's title
 *
 * @param $path
 *   path of the page being identified
 * @return
 *   string with the page's title
 */
function _print_get_title($path) {
  $path = drupal_get_normal_path($path);
  $nid = preg_replace('!^node/!', '', $path);
553
  if (ctype_digit($nid)) {
554
    $res = db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $nid))
jcnventura's avatar
jcnventura committed
555
              ->fetchField();
556 557 558
  }
  else {
    $res = db_query("SELECT link_title FROM {menu_links} WHERE link_path = :link_path", array(':link_path' => $path))
jcnventura's avatar
jcnventura committed
559
              ->fetchField();
560 561 562 563 564
  }
  return $res;
}

/**
jcnventura's avatar
jcnventura committed
565 566 567
 * Modify the print_node_conf_table
 *
 * Update the print_node_conf table to reflect the given attributes.
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
 * If updating to the default values, delete the record.
 *
 * @param $nid
 *   value of the nid field (primary key)
 * @param $link
 *   value of the link field (0 or 1)
 * @param $comments
 *   value of the comments field (0 or 1)
 * @param $url_list
 *   value of the url_list field (0 or 1)
 */
function _print_node_conf_modify($nid, $link, $comments, $url_list) {
    db_merge('print_node_conf')
      ->key(array('nid' => $nid))
      ->fields(array(
        'link' => $link,
        'comments' => $comments,
        'url_list' => $url_list,
      ))
      ->execute();
588 589
}

590
/**
jcnventura's avatar
jcnventura committed
591
 * Auxiliary function to fill the Printer-friendly link attributes
592
 *
593 594 595 596
 * @param $title
 *   text to displayed by the link when hovering over it with the mouse
 * @param $class
 *   class attribute to be used in the link
597 598
 * @param $new_window
 *   if TRUE opens the target page in a new window
jcnventura's avatar
jcnventura committed
599 600
 * @return
 *   array of formatted attributes
601
 */
602
function print_fill_attributes($title = '', $class = '', $new_window = FALSE) {
603 604
  $print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT);
  $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
605

606 607
  $attributes = array();
  $attributes['title'] = $title;
608
  if (!empty($class)) {
609
    $attributes['class'] = array($class);
610
  }
611

612
  if ($new_window) {
613
    switch ($print_newwindow) {
614 615 616 617 618 619 620
    case 0:
      $attributes['target'] = '_blank';
      break;
    case 1:
      $attributes['onclick'] = 'window.open(this.href); return false';
      break;
    }
621
  }
622
  if (!empty($print_robots_noindex)) {
jcnventura's avatar
jcnventura committed
623
    $attributes['rel'] = 'nofollow';
624
  }
625 626
  return $attributes;
}
627

628 629 630 631 632 633 634 635 636 637 638 639 640 641
/**
 * Auxiliary function to set the link text and html flag
 *
 * @param $type
 *   type of link: 0 or 1 for a text-only link, 2 for icon-only and 3 for
 *   both text and icon
 * @param $text
 *   text to be displayed on the link to the printer-friendly page
 * @param $img
 *   path to the icon file
 * @return
 *   array with the link text and html flag
 */
function _print_format_link_aux($type = 0, $text = '', $img = '') {
642 643 644 645
  if ($type >= 2) {
    $html = TRUE;
    switch ($type) {
    case 2:
646
      $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon'))));
647 648
      break;
    case 3:
649
      $text = theme('image', array('path' => $img, 'alt' => $text, 'title' => $text, 'attributes' => array('class' => array('print-icon', 'print-icon-margin')))) . $text;
650 651 652 653 654 655
      break;
    }
  }
  else {
    $html = FALSE;
  }
jcnventura's avatar
jcnventura committed
656

657 658
  return array('text' => $text,
               'html' => $html,
659
              );
660 661
}

jcnventura's avatar
jcnventura committed
662 663 664 665 666 667 668
/**
 * Format the Printer-friendly link
 *
 * @return
 *   array of formatted attributes
 * @ingroup themeable
 */
jcnventura's avatar
jcnventura committed
669
function theme_print_format_link() {
670 671 672
  $print_html_link_class = variable_get('print_html_link_class', PRINT_HTML_LINK_CLASS_DEFAULT);
  $print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
  $print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
673
  $print_html_link_text = filter_xss(variable_get('print_html_link_text', t('Printer-friendly version')));
674

jcnventura's avatar
jcnventura committed
675
  $img = drupal_get_path('module', 'print') . '/icons/print_icon.gif';
676
  $title = t('Display a printer-friendly version of this page.');
677 678
  $class = strip_tags($print_html_link_class);
  $new_window = $print_html_new_window;
679
  $format = _print_format_link_aux($print_html_show_link, $print_html_link_text, $img);
680 681 682

  return array('text' => $format['text'],
               'html' => $format['html'],
683
               'attributes' => print_fill_attributes($title, $class, $new_window),
684
              );
685 686 687
}

/**
jcnventura's avatar
jcnventura committed
688 689
 * Auxiliary function to display a formatted Printer-friendly link
 *
jcnventura's avatar
jcnventura committed
690 691 692 693
 * Function made available so that developers may call this function from
 * their defined pages/blocks.
 *
 * @param $path
jcnventura's avatar
jcnventura committed
694
 *   path of the original page (optional). If not specified, the current URL
jcnventura's avatar
jcnventura committed
695
 *   is used
696 697 698
 * @param $node
 *   an optional node object, to be used in defining the path, if used, the
 *   path argument is irrelevant
jcnventura's avatar
jcnventura committed
699 700
 * @return
 *   string with the HTML link to the printer-friendly page
701
 */
702 703 704
function print_insert_link($path = NULL, $node = NULL) {
  if ($node !== NULL) {
    $nid = $node->nid;
jcnventura's avatar
jcnventura committed
705
    $path = 'node/' . $nid;
706 707 708
    $allowed_type = print_link_allowed(array('node' => $node));
  }
  else {
jcnventura's avatar
jcnventura committed
709
    if ($path === NULL) {
710 711 712 713 714 715 716 717
      $nid = preg_replace('!^node/!', '', $_GET['q']);
      $path = $_GET['q'];
    }
    else {
      $nid = NULL;
    }
    $allowed_type = print_link_allowed(array('path' => $path));
  }
718

719 720 721
  if ($allowed_type) {
    if ($nid !== NULL) {
      if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
jcnventura's avatar
jcnventura committed
722
        $path = 'book/export/html/' . $nid;
723 724
      }
      else {
725 726
        if (variable_get('print_html_link_use_alias', PRINT_HTML_LINK_USE_ALIAS_DEFAULT) && ($alias = drupal_lookup_path('alias', $path))) {
          $path = $alias;
727 728 729 730
        }
        else {
          $path = $nid;
        }
731
      }
jcnventura's avatar
jcnventura committed
732
      $path = PRINT_PATH . '/' . $path;
733
      $query = print_query_string_encode($_GET, array('q'));
jcnventura's avatar
jcnventura committed
734 735 736
      if (empty($query)) {
        $query = NULL;
      }
737
    }
jcnventura's avatar
jcnventura committed
738 739 740
    else {
      $query = NULL;
    }
jcnventura's avatar
jcnventura committed
741
    drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
jcnventura's avatar
jcnventura committed
742
    $format = theme('print_format_link');
jcnventura's avatar
jcnventura committed
743
    return '<span class="print_html">' . l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) . '</span>';
744
  }
745 746 747
  else {
    return FALSE;
  }
748
}
749 750

/**
jcnventura's avatar
jcnventura committed
751
 * Check if the provided page is enabled according to the visibility settings
752 753
 *
 * @param $visibility
754
 *   current visibility settings:
755 756 757 758 759 760 761
 *    0 for show on every page except the listed pages
 *    1 for show on only the listed pages
 * @param $pages
 *   list of pages
 * @return
 *   TRUE if it is enabled, FALSE otherwise
 */
762
function _print_page_match($visibility, $path, $pages) {
763
  if ($pages) {
jcnventura's avatar
jcnventura committed
764
    if ($visibility == 2) {
765 766 767 768 769 770
      if (module_exists('php')) {
        return php_eval($pages);
      }
      else {
        return FALSE;
      }
jcnventura's avatar
jcnventura committed
771
    }
772
    $alias = drupal_get_path_alias($path);
773
    $page_match = drupal_match_path($path, $pages);
774 775
    if ($alias != $path) {
      $page_match = $page_match || drupal_match_path($alias, $pages);
776 777 778 779 780
    }

    return !($visibility xor $page_match);
  }
  else {
781
    return !$visibility;
782 783
  }
}
784 785

/**
jcnventura's avatar
jcnventura committed
786
 * Check if the link to the PF version is allowed depending on the settings
787 788 789 790 791 792 793 794 795 796
 *
 * @param $args
 *   array containing the possible parameters:
 *    teaser, node, type, path
 * @return
 *   FALSE if not allowed
 *   PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
 *   PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
 */
function print_link_allowed($args) {
797 798
  $view_mode = isset($args['view_mode']) ? $args['view_mode'] : '';
  if ((($view_mode == 'teaser') && !variable_get('print_html_link_teaser', PRINT_HTML_LINK_TEASER_DEFAULT))
799
      || !in_array($view_mode, array('full', 'teaser', '')) || !user_access('access print')) {
800
    // If the teaser link is disabled or the user is not allowed
801 802
    return FALSE;
  }
803 804
  if (!empty($args['path'])) {
    $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
805
    if (ctype_digit($nid)) {
806
      $args['node'] = node_load($nid);
807 808
    }
  }
809
  if (!empty($args['node'])) {
810
    static $node_type = '';
jcnventura's avatar
jcnventura committed
811 812

    $node = $args['node'];
813 814
    if (isset($node->type)) {
      $node_type = $node->type;
815 816 817 818 819
    }
    // Node
    $print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT);
    $print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT);

820
    if (!_print_page_match($print_html_node_link_visibility, "node/" . $node->nid, $print_html_node_link_pages)) {
821
      // Page not in visibility list
822 823
      return FALSE;
    }
824
    elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
825
      // Link is for a comment, return the configured setting
826 827 828 829 830 831 832
      // Cache this statically to avoid duplicate queries for every comment.
      static $res = array();
      if (!isset($res[$node->nid])) {
        $res[$node->nid] = db_query("SELECT comments FROM {print_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
                                    ->fetch();
      }
      $print_display_comment = $res ? intval($res[$node->nid]->comments) : variable_get('print_display_comment_' . $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
833
      if ($print_display_comment) {
834 835
        return PRINT_ALLOW_NORMAL_LINK;
      }
836 837 838
    }
    else {
      // Node link
839
      if (isset($node->print_display) && !$node->print_display) {
840
        // Link for this node is disabled
841 842
        return FALSE;
      }
843
      elseif (isset($node->book)) {
844 845
        // Node is a book;
        $print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
846 847 848 849 850 851 852 853
        switch ($print_html_book_link) {
          case 1:
            if (user_access('access printer-friendly version')) {
              return PRINT_ALLOW_BOOK_LINK;
            }
            break;
          case 2:
            return PRINT_ALLOW_NORMAL_LINK;
854 855 856 857 858 859 860 861 862 863 864 865
        }
      }
      else {
        return PRINT_ALLOW_NORMAL_LINK;
      }
    }
  }
  else {
    // 'System' page
    $print_html_sys_link_visibility = variable_get('print_html_sys_link_visibility', PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT);
    $print_html_sys_link_pages = variable_get('print_html_sys_link_pages', PRINT_HTML_SYS_LINK_PAGES_DEFAULT);

866
    return _print_page_match($print_html_sys_link_visibility, $_GET['q'], $print_html_sys_link_pages);
867
  }
868
  return FALSE;
869
}
870 871 872

/**
 * Parse an array into a valid urlencoded query string.
jcnventura's avatar
jcnventura committed
873
 *
874
 * Modified from drupal_query_string_encode to prevent re-encoding of
875
 * encoded original. (see #301192)
876 877 878 879 880 881 882 883
 *
 * @param $query
 *   The array to be processed e.g. $_GET
 * @param $exclude
 *   The array filled with keys to be excluded.
 * @return
 *   urlencoded string which can be appended to/as the URL query string
 */
884
function print_query_string_encode($query, $exclude = array(), $parent = '') {
885 886
  $params = array();
  foreach ($query as $key => $value) {
887
    if ($parent) {
jcnventura's avatar
jcnventura committed
888
      $key = $parent . '[' . $key . ']';
889 890
    }

891 892 893
    if (in_array($key, $exclude)) {
      continue;
    }
894 895

    if (is_array($value)) {
896
      $params[] = print_query_string_encode($value, $exclude, $key);
897 898
    }
    else {
899
      $params[$key] = $value;
900
    }
901
  }
902

903
  return $params;
904
}
905 906 907 908 909 910 911 912

/**
 * Implements hook_contextual_links_view_alter().
 */
function print_contextual_links_view_alter(&$element, $items) {
  // Hide contextual links, always
  $element['#attributes']['class'][] = 'element-hidden';
}