Repair type-specific link settings
[project/print.git] / print_mail / print_mail.module
1 <?php
2 /* $Id$ */
3
4 /**
5 * @file
6 * Display printer-friendly versions of Drupal pages
7 */
8
9 define('PRINTMAIL_PATH', 'printmail');
10
11 define('PRINT_MAIL_LINK_POS_DEFAULT', 'link');
12 define('PRINT_MAIL_SHOW_LINK_DEFAULT', 1);
13 define('PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT', 0);
14 define('PRINT_MAIL_NODE_LINK_PAGES_DEFAULT', '');
15 define('PRINT_MAIL_LINK_CLASS_DEFAULT', 'print-mail');
16 define('PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT', 1);
17 define('PRINT_MAIL_SYS_LINK_PAGES_DEFAULT', '<front>');
18 define('PRINT_MAIL_BOOK_LINK_DEFAULT', 1);
19 define('PRINT_MAIL_HOURLY_THRESHOLD', 3);
20 define('PRINT_MAIL_TEASER_DEFAULT_DEFAULT', 1);
21 define('PRINT_MAIL_TEASER_CHOICE_DEFAULT', 1);
22
23 /**
24 * Implementation of hook_theme().
25 */
26 function print_mail_theme() {
27 return array(
28 'print_mail_format_link' => array(
29 'arguments' => array(),
30 ),
31 'print_mail_form' => array(
32 'arguments' => array('form'),
33 'file' => 'print_mail.inc',
34 ),
35 );
36 }
37
38 /**
39 * Implementation of hook_menu().
40 */
41 function print_mail_menu() {
42 $items = array();
43
44 $items[PRINTMAIL_PATH] = array(
45 'title' => 'Send page by e-mail',
46 'page callback' => 'drupal_get_form',
47 'page arguments' => array('print_mail_form'),
48 'access arguments' => array('access print'),
49 'type' => MENU_CALLBACK,
50 'file' => 'print_mail.inc',
51 );
52 $items['admin/settings/print/mail'] = array(
53 'title' => 'e-mail',
54 'page callback' => 'drupal_get_form',
55 'page arguments' => array('print_mail_settings'),
56 'access arguments' => array('administer print'),
57 'weight' => 2,
58 'type' => MENU_LOCAL_TASK,
59 'file' => 'print_mail.admin.inc',
60 );
61
62 return $items;
63 }
64
65 /**
66 * Implementation of hook_link().
67 */
68 function print_mail_link($type, $node = NULL, $teaser = FALSE) {
69 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT => PRINT_MAIL_LINK_POS_DEFAULT));
70 $allowed_type = print_mail_link_allowed(array('type' => $type, 'node' => $node, 'teaser' => $teaser));
71 if (($allowed_type) && !empty($print_mail_link_pos['link'])) {
72 drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css');
73 $links = array();
74 $format = theme('print_mail_format_link');
75
76 // Show book link
77 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
78 $links['book_mail'] = array('href' => PRINTMAIL_PATH .'/book/export/html/'. $node->nid,
79 'title' => $format['text'],
80 'attributes' => $format['attributes'],
81 'html' => $format['html'],
82 );
83
84 return $links;
85 }
86 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) {
87 $query_arr = $_GET;
88 if ($type == 'comment') {
89 $query_arr['comment'] = $node->cid;
90 }
91 $query = print_query_string_encode($query_arr, array('q'));
92
93 $links['print_mail'] = array('href' => PRINTMAIL_PATH .'/'. $node->nid,
94 'title' => $format['text'],
95 'attributes' => $format['attributes'],
96 'html' => $format['html'],
97 'query' => $query,
98 );
99
100 return $links;
101 }
102 }
103 return;
104 }
105
106 /**
107 * Implementation of hook_help().
108 */
109 function print_mail_help($path, $arg) {
110 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT => PRINT_MAIL_LINK_POS_DEFAULT));
111 if (print_mail_link_allowed(array('path' => $path)) && (preg_match('!^node/!i', $path) == 0) &&
112 !(empty($print_mail_link_pos['link']) && empty($print_mail_link_pos['corner']))) {
113 static $output = FALSE;
114
115 if ($output === FALSE) {
116 drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css');
117 $output = TRUE;
118
119 return '<span class="print-syslink">'. print_mail_insert_link() .'</span>';
120 }
121 }
122 }
123
124 /**
125 * Implementation of hook_nodeapi().
126 */
127 function print_mail_nodeapi(&$node, $op = 'view', $teaser, $page) {
128 switch ($op) {
129 case 'view':
130 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT => PRINT_MAIL_LINK_POS_DEFAULT));
131 $allowed_type = print_mail_link_allowed(array('node' => $node, 'teaser' => $teaser));
132 if (($allowed_type != FALSE) && !empty($print_mail_link_pos['corner']) &&
133 (preg_match('!^print!i', $_GET['q']) == 0)) {
134 drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css');
135 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
136 $path = PRINTMAIL_PATH .'/book/export/html/'. $node->nid;
137 }
138 $link = print_mail_insert_link($path);
139 $node->content['print_mail_link'] = array(
140 '#value' => "<span class='print-link'>$link</span>",
141 '#weight' => -2,
142 );
143 }
144 }
145 }
146
147 /**
148 * Implementation of hook_form_alter().
149 */
150 function print_mail_form_alter(&$form, $form_state, $form_id) {
151 // Add the node-type settings option to activate the mail version link
152 if ($form_id == 'node_type_form') {
153 $form['print']['print_mail_display'] = array(
154 '#type' => 'checkbox',
155 '#title' => t('Show send by e-mail link'),
156 '#default_value' => variable_get('print_mail_display_'. $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT),
157 '#description' => t('Displays a link to send the content by e-mail. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
158 );
159 $form['print']['print_mail_display_comment'] = array(
160 '#type' => 'checkbox',
161 '#title' => t('Show send by e-mail link in individual comments'),
162 '#default_value' => variable_get('print_mail_display_comment_'. $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT),
163 '#description' => t('Displays a link to send the comment by e-mail. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
164 );
165 }
166 }
167
168 /**
169 * Format the send by e-mail link
170 *
171 * @return
172 * array of formatted attributes
173 * @ingroup themeable
174 */
175 function theme_print_mail_format_link() {
176 $print_mail_link_class = variable_get('print_mail_link_class', PRINT_MAIL_LINK_CLASS_DEFAULT);
177 $print_mail_show_link = variable_get('print_mail_show_link', PRINT_MAIL_SHOW_LINK_DEFAULT);
178
179 $text = t('Send to friend');
180 $img = drupal_get_path('module', 'print') .'/icons/mail_icon.gif';
181 $title = t('Send this page by e-mail.');
182 $class = strip_tags($print_mail_link_class);
183 $new_window = FALSE;
184 $format = _print_format_link_aux($print_mail_show_link, $text, $img);
185
186 return array('text' => $format['text'],
187 'html' => $format['html'],
188 'attributes' => print_fill_attributes($title, $class, $new_window),
189 );
190 }
191
192 /**
193 * Auxiliary function to display a formatted send by e-mail link
194 *
195 * @return string
196 */
197 function print_mail_insert_link($path = NULL) {
198 if (user_access('access print')) {
199 if ($path === NULL) {
200 $path = PRINTMAIL_PATH .'/'. preg_replace('!^node/!', '', $_GET['q']);
201 $query = print_query_string_encode($_GET, array('q'));
202 if (empty($query)) {
203 $query = NULL;
204 }
205 }
206 $format = theme('print_mail_format_link');
207 return '<span class="print_mail">'. l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) .'</span>';
208 }
209 }
210
211 /**
212 * Determine a the link to send by e-mail is allowed depending on all possible settings
213 *
214 * @param $args
215 * array containing the possible parameters:
216 * teaser, node, type, path
217 * @return
218 * FALSE if not allowed
219 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
220 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
221 */
222 function print_mail_link_allowed($args) {
223 if (!empty($args['teaser']) || !user_access('access print')) {
224 // If showing only the teaser or the user is not allowed or link is disabled
225 return FALSE;
226 }
227 if (!empty($args['node'])) {
228 static $node_type = FALSE;
229
230 $node = $args['node'];
231 if ($node_type === FALSE) {
232 if (isset($node->type)) {
233 $node_type = $node->type;
234 }
235 else {
236 $node_type = '';
237 }
238 }
239 // Node
240 $print_mail_node_link_visibility = variable_get('print_mail_node_link_visibility', PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT);
241 $print_mail_node_link_pages = variable_get('print_mail_node_link_pages', PRINT_MAIL_NODE_LINK_PAGES_DEFAULT);
242
243 if (!empty($node->printing) ||
244 !_print_page_match($print_mail_node_link_visibility, $print_mail_node_link_pages)) {
245 // Page not in visibility list or we are working!
246 return FALSE;
247 }
248 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
249 // Link is for a comment, return the configured setting
250 return variable_get('print_mail_display_comment_'. $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
251 }
252 else {
253 // Node link
254 if (isset($node_type) &&
255 !variable_get('print_mail_display_'. $node_type, PRINT_TYPE_SHOW_LINK_DEFAULT)) {
256 // Link for this node type is disabled
257 return FALSE;
258 }
259 elseif (($node_type == 'book') || isset($node->book)) {
260 // Node is a book;
261 $print_mail_book_link = variable_get('print_mail_book_link', PRINT_MAIL_BOOK_LINK_DEFAULT);
262 if (!$print_mail_book_link || !user_access('access printer-friendly version')) {
263 // Book link is disabled
264 return FALSE;
265 }
266 else {
267 return PRINT_ALLOW_BOOK_LINK;
268 }
269 }
270 else {
271 return PRINT_ALLOW_NORMAL_LINK;
272 }
273 }
274 }
275 else {
276 // 'System' page
277 $print_mail_sys_link_visibility = variable_get('print_mail_sys_link_visibility', PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT);
278 $print_mail_sys_link_pages = variable_get('print_mail_sys_link_pages', PRINT_MAIL_SYS_LINK_PAGES_DEFAULT);
279
280 return _print_page_match($print_mail_sys_link_visibility, $print_mail_sys_link_pages);
281 }
282 }