km - introduced printer friendly icon
[project/print.git] / print.module
1 <?php
2 /* $Id$ */
3
4 /********************************************************************
5 * Drupal Hooks :: Overview
6 ********************************************************************/
7
8 /**
9 * Implementation of hook_help().
10 */
11 function print_help($section) {
12 switch ($section) {
13 case 'admin/modules#description':
14 $output = t('Allows users to create printer-friendly pages for nodes.');
15 break;
16 }
17
18 return $output;
19 }
20
21 /**
22 * Implementation of hook_menu().
23 */
24 function print_menu($may_cache) {
25 $items = array();
26
27 if (!$may_cache) {
28 $items[] = array('path' => 'node/'. arg(1) .'/print', 'title' => t('printer friendly page'),
29 'callback' => 'print_page', 'access' => user_access('access content'),
30 'type' => MENU_CALLBACK);
31 }
32
33 return $items;
34 }
35
36 /********************************************************************
37 * Drupal Hooks :: Core
38 ********************************************************************/
39
40 /**
41 * Implementation of hook_link().
42 */
43 function print_link($type, $node = 0, $main) {
44 $links = array();
45
46 if ($node->type == 'book' && function_exists('book_link')) {
47 return;
48 }
49
50 if ($type == 'node' && variable_get('print_show_link', 1) && $main == 0) {
51 $links[] = theme('print_link', $node);
52 }
53
54 return $links;
55 }
56
57 function print_settings() {
58 $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.'));
59
60 $field .= form_checkbox(t('A list of the node\'s links at the bottom'), 'print_urls', 1, variable_get('print_urls', 0));
61 $output .= form_group(t('Print page elements'), $field);
62 $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."));
63 $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.'));
64
65 return $output;
66 }
67
68 /********************************************************************
69 * Module Functions :: Controllers
70 ********************************************************************/
71
72 function print_page() {
73 $nid = arg(1);
74 if (is_numeric($nid)) {
75 print_generate($nid);
76 }
77 }
78
79 /********************************************************************
80 * Module Functions
81 ********************************************************************/
82
83 /**
84 * Outputs a printer friendly page.
85 */
86 function print_generate($title) {
87 global $base_url;
88
89 /* We can take a node id or a node title */
90 $node = (is_numeric($title)) ? node_load(array('nid' => $title)) : node_load(array('title' => $title));
91 if (!$node->title) return false;
92
93 $teaser = false;
94 $page = true;
95 /* This section is ripped from node_view.
96 This does everything node_view does except theme the node! */
97
98 // Remove the delimiter (if any) that separates the teaser from the body.
99 // TODO: this strips legitimate uses of '<!--break-->' also.
100 $node->body = str_replace('<!--break-->', '', $node->body);
101
102 // The 'view' hook can be implemented to overwrite the default function
103 // to display nodes.
104 if (node_hook($node, 'view'))
105 node_invoke($node, 'view', $teaser, $page);
106 else
107 $node = node_prepare($node, $teaser);
108 // Allow modules to change $node->body before viewing.
109 node_invoke_nodeapi($node, 'view', $teaser, $page);
110
111 /* End of code stealing from node_view() */
112
113 if (variable_get('print_urls', 1)) {
114
115 /* Collect links and display them at the bottom of the page. Code once taken from Kjartan Mannes' project.module */
116 $pattern = "@href=([\']?[\"]?)([^\"|^\'|^|^>]*)([^>]*)>(.+?)</a>@ise";
117 $node->body = preg_replace($pattern, "'<u>'.stripslashes('\\4').'</u> ['. print_friendly_urls(stripslashes('\\2')) .']'", $node->body);
118 $urls = print_friendly_urls();
119 if (count($urls)) {
120 $node->pfp_links = '';
121 $max = count($urls);
122 for ($i = 0; $i < $max; $i++) {
123 $node->pfp_links .= '['. ($i + 1) .'] '. $urls[$i] ."<br />\n";
124 }
125 }
126 }
127
128 /* Grab and format the src URL */
129 $node->source_url = $base_url. '/'. url("node/$node->nid");
130 $node->language = $GLOBALS['locale'];
131 $node->printcss = variable_get('print_css', 'misc/print.css');
132
133 include_once('print.tpl.php');
134 }
135
136 function print_friendly_urls($url = 0) {
137 global $base_url;
138 static $urls = array();
139 if ($url) {
140 $urls[] = strpos($url, '://') ? $url : $base_url. '/'. url($url);
141 return count($urls);
142 }
143 return $urls;
144 }
145
146 /********************************************************************
147 * Module Functions :: Themeable Functions
148 ********************************************************************/
149
150 function theme_print_link($node) {
151 $links = '';
152 if ($icon = variable_get('print_icon', 1)) {
153 $links .= l('<img src="'.$icon.'" alt="">', "node/$node->nid/print");
154 }
155 $links .= l(t('printer friendly page'), "node/$node->nid/print", array('title' => t('Display a printer friendly page.')));
156
157 return $links;
158 }
159 ?>