summaryrefslogtreecommitdiffstats
path: root/print.module
blob: 8552134a7e9c4b3dda8bf6a54720aa1452db2d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/* $Id$ */

/********************************************************************
 * Drupal Hooks :: Overview
 ********************************************************************/

/**
 * Implementation of hook_help().
 */
function print_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      $output = t('Allows users to create printer-friendly pages for nodes.');
      break;
  }

  return $output;
}

/**
 * Implementation of hook_menu().
 */
function print_menu($may_cache) {
  $items = array();

  if (!$may_cache) {
  $items[] = array('path' => 'node/'. arg(1) .'/print', 'title' => t('printer friendly page'),
    'callback' => 'print_page', 'access' => user_access('access content'),
    'type' => MENU_CALLBACK);
  }

  return $items;
}

/********************************************************************
 * Drupal Hooks :: Core
 ********************************************************************/

/**
 * Implementation of hook_link().
 */
function print_link($type, $node = 0, $main) {
  $links = array();

  if ($node->type == 'book' && function_exists('book_link'))  {
    return;
  }

  if ($type == 'node' && variable_get('print_show_link', 1) && $main == 0) {
    $links[] = theme('print_link', $node);
  }

  return $links;
}

function print_settings() {
  $output = form_textfield(t('Stylesheet URL'), 'print_css', variable_get('print_css', 'misc/print.css'), 60, 64, t('The URL to your print cascading stylesheet.'));

  $field .= form_checkbox(t('A list of the node\'s links at the bottom'), 'print_urls', 1, variable_get('print_urls', 0));
  $output .= form_group(t('Print page elements'), $field);
  $output .= form_radios(t('Printer friendly page link'), 'print_show_link', variable_get('print_show_link', 1), array(t("Disabled"), t("Enabled")), t("Enable or disable the 'printer friendly page' link for each node. Even if the link is disabled, you can still view the print version of a node by going to 'node/nid/print' where nid is the numeric id of the node."));
  $output .= form_textfield(t('Printer friendly page icon'), 'print_icon', variable_get('print_icon', 'misc/print.gif'), 60, 64, t('An optional icon to put in front of the printer friendly page link.'));

  return $output;
}

/********************************************************************
 * Module Functions :: Controllers
 ********************************************************************/

function print_page() {
  $nid = arg(1);
  if (is_numeric($nid)) {
    print_generate($nid);
  }
}

/********************************************************************
 * Module Functions
 ********************************************************************/

/**
 * Outputs a printer friendly page.
 */
function print_generate($title) {
  global $base_url;

  /* We can take a node id or a node title */
  $node = (is_numeric($title)) ? node_load(array('nid' => $title)) : node_load(array('title' => $title));
  if (!$node->title) return false;

  $teaser = false;
  $page = true;
  /* This section is ripped from node_view.
     This does everything node_view does except theme the node! */

  // Remove the delimiter (if any) that separates the teaser from the body.
  // TODO: this strips legitimate uses of '<!--break-->' also.
  $node->body = str_replace('<!--break-->', '', $node->body);

  // The 'view' hook can be implemented to overwrite the default function
  // to display nodes.
  if (node_hook($node, 'view'))
    node_invoke($node, 'view', $teaser, $page);
  else
    $node = node_prepare($node, $teaser);
  // Allow modules to change $node->body before viewing.
  node_invoke_nodeapi($node, 'view', $teaser, $page);

  /* End of code stealing from node_view() */

  if (variable_get('print_urls', 1)) {

    /* Collect links and display them at the bottom of the page. Code once taken from Kjartan Mannes' project.module  */
    $pattern = "@href=([\']?[\"]?)([^\"|^\'|^|^>]*)([^>]*)>(.+?)</a>@ise";
    $node->body = preg_replace($pattern, "'<u>'.stripslashes('\\4').'</u> ['. print_friendly_urls(stripslashes('\\2')) .']'", $node->body);
    $urls = print_friendly_urls();
    if (count($urls)) {
      $node->pfp_links = '';
      $max = count($urls);
      for ($i = 0; $i < $max; $i++) {
        $node->pfp_links .= '['. ($i + 1) .'] '. $urls[$i] ."<br />\n";
      }
    }
  }

  /* Grab and format the src URL */
  $node->source_url = $base_url. '/'. url("node/$node->nid");
  $node->language = $GLOBALS['locale'];
  $node->printcss = variable_get('print_css', 'misc/print.css');

  include_once('print.tpl.php');
}

function print_friendly_urls($url = 0) {
  global $base_url;
  static $urls = array();
  if ($url) {
    $urls[] = strpos($url, '://') ? $url : $base_url. '/'. url($url);
    return count($urls);
  }
  return $urls;
}

/********************************************************************
 * Module Functions :: Themeable Functions
 ********************************************************************/

function theme_print_link($node) {
  $links = '';
  if ($icon = variable_get('print_icon', 1)) {
    $links .= l('<img src="'.$icon.'" alt="">', "node/$node->nid/print");
  }
  $links .= l(t('printer friendly page'), "node/$node->nid/print", array('title' => t('Display a printer friendly page.')));

  return $links;
}
?>