Current mimemail 6.x-1.x-dev no longer causes problems for the mail function, so...
[project/print.git] / print_mail / print_mail.module
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Displays Printer-friendly versions of Drupal pages.
7 */
8
9 define('PRINTMAIL_PATH', 'printmail');
10
11 define('PRINT_MAIL_FORMAT', 'mail');
12
13 define('PRINT_MAIL_LINK_POS_DEFAULT', 'link');
14 define('PRINT_MAIL_SHOW_LINK_DEFAULT', 1);
15 define('PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT', 0);
16 define('PRINT_MAIL_NODE_LINK_PAGES_DEFAULT', '');
17 define('PRINT_MAIL_LINK_CLASS_DEFAULT', 'print-mail');
18 define('PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT', 1);
19 define('PRINT_MAIL_SYS_LINK_PAGES_DEFAULT', '');
20 define('PRINT_MAIL_LINK_USE_ALIAS_DEFAULT', 0);
21 define('PRINT_MAIL_BOOK_LINK_DEFAULT', 1);
22 define('PRINT_MAIL_HOURLY_THRESHOLD', 3);
23 define('PRINT_MAIL_TEASER_DEFAULT_DEFAULT', 1);
24 define('PRINT_MAIL_TEASER_CHOICE_DEFAULT', 1);
25
26 /**
27 * Implementation of hook_theme().
28 */
29 function print_mail_theme() {
30 return array(
31 'print_mail_format_link' => array(
32 'arguments' => array(),
33 ),
34 'print_mail_form' => array(
35 'arguments' => array('form'),
36 ),
37 );
38 }
39
40 /**
41 * Implementation of hook_menu().
42 */
43 function print_mail_menu() {
44 $items = array();
45
46 $items[PRINTMAIL_PATH] = array(
47 'title' => 'Send page by e-mail',
48 'page callback' => 'drupal_get_form',
49 'page arguments' => array('print_mail_form'),
50 'access arguments' => array('access print'),
51 'type' => MENU_CALLBACK,
52 );
53 $items['admin/settings/print/email'] = array(
54 'title' => 'e-mail',
55 'page callback' => 'drupal_get_form',
56 'page arguments' => array('print_mail_settings'),
57 'access arguments' => array('administer print'),
58 'weight' => 2,
59 'type' => MENU_LOCAL_TASK,
60 );
61 $items['admin/settings/print/email/options'] = array(
62 'title' => 'Options',
63 'weight' => 1,
64 'type' => MENU_DEFAULT_LOCAL_TASK,
65 );
66 $items['admin/settings/print/email/strings'] = array(
67 'title' => 'Text strings',
68 'page callback' => 'drupal_get_form',
69 'page arguments' => array('print_mail_strings_settings'),
70 'access arguments' => array('administer print'),
71 'weight' => 2,
72 'type' => MENU_LOCAL_TASK,
73 );
74
75 return $items;
76 }
77
78 /**
79 * Implementation of hook_block_list().
80 */
81 function print_mail_block_list() {
82 $block[0]['info'] = t('Most emailed');
83 return $block;
84 }
85
86 /**
87 * Implementation of hook_block_view().
88 */
89 function print_mail_block_view($delta = 0) {
90 switch ($delta) {
91 case 0:
92 $block['subject'] = t('Most emailed');
93 $result = db_query_range("SELECT path FROM {print_mail_page_counter} ORDER BY sentcount DESC", 0, 3)
94 ->fetchAll();
95 if (count($result)) {
96 $block['content'] = '<div class="item-list"><ul>';
97 foreach ($result as $obj) {
98 $block['content'] .= '<li>'. l(_print_get_title($obj->path), $obj->path) .'</li>';
99 }
100 $block['content'] .= '</ul></div>';
101 }
102 break;
103 }
104 return $block;
105 }
106
107 /**
108 * Implementation of hook_help().
109 */
110 function print_mail_help($path, $arg) {
111 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT => PRINT_MAIL_LINK_POS_DEFAULT));
112 if ((preg_match('!^node/!i', $path) == 0) &&
113 !(empty($print_mail_link_pos['link']) && empty($print_mail_link_pos['corner']))) {
114 static $output = FALSE;
115
116 if ($output === FALSE) {
117 $output = TRUE;
118
119 $link = print_mail_insert_link();
120 if ($link) {
121 return "<span class='print-syslink'>$link</span>";
122 }
123 }
124 }
125 }
126
127 /**
128 * Implementation of hook_nodeapi_view().
129 */
130 function print_mail_nodeapi_view($node, $teaser) {
131 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT => PRINT_MAIL_LINK_POS_DEFAULT));
132 $print_mail_link_use_alias = variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT);
133 $allowed_type = print_mail_link_allowed(array('type' => 'node', 'node' => $node, 'teaser' => $teaser));
134 if (($allowed_type) && !empty($print_mail_link_pos['link'])) {
135 drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css');
136 $links = array();
137 $format = theme('print_mail_format_link');
138
139 // Show book link
140 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
141 $links['book_mail'] = array('href' => PRINTMAIL_PATH .'/book/export/html/'. $node->nid,
142 'title' => $format['text'],
143 'attributes' => $format['attributes'],
144 'html' => $format['html'],
145 );
146 }
147 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) {
148 $query_arr = $_GET;
149 if ('node' == 'comment') {
150 $query_arr['comment'] = $node->cid;
151 }
152 $query = print_query_string_encode($query_arr, array('q'));
153 if (empty($query)) $query = NULL;
154
155 if ($print_mail_link_use_alias) {
156 $path = drupal_get_path_alias('node/'. $node->nid);
157 }
158 else {
159 $path = $node->nid;
160 }
161
162 $links['print_mail'] = array('href' => PRINTMAIL_PATH .'/'. $path,
163 'title' => $format['text'],
164 'attributes' => $format['attributes'],
165 'html' => $format['html'],
166 'query' => $query,
167 );
168 }
169
170 $node->content['links']['print_mail'] = array(
171 '#type' => 'node_links',
172 '#value' => $links,
173 );
174 }
175
176 // Insert content corner links
177 if (($teaser === FALSE) && !empty($print_mail_link_pos['corner']) &&
178 (preg_match('!^print!i', $_GET['q']) == 0)) {
179 $link = print_mail_insert_link(NULL, $node);
180 if ($link) {
181 $node->content['print_mail_link'] = array(
182 '#markup' => "<span class='print-link'>$link</span>",
183 '#weight' => -102,
184 );
185 }
186 }
187 }
188
189 /**
190 * Implementation of hook_nodeapi_load().
191 */
192 function print_mail_nodeapi_load($nodes, $types) {
193 foreach ($nodes as $node) {
194 _print_mail_set_node_fields($node);
195 }
196 }
197
198 /**
199 * Implementation of hook_nodeapi_update().
200 */
201 function print_mail_nodeapi_update($node) {
202 if (user_access('administer print') || user_access('node-specific print configuration')) {
203 _print_mail_node_conf_modify($node->nid, $node->print_mail_display, $node->print_mail_display_comment, $node->print_mail_display_urllist);
204 }
205 }
206
207 /**
208 * Implementation of hook_nodeapi_delete().
209 */
210 function print_mail_nodeapi_delete($node) {
211 db_delete('print_mail_node_conf')
212 ->condition('nid', $node->nid)
213 ->execute();
214 db_delete('print_mail_page_counter')
215 ->condition('path', $node->path)
216 ->execute();
217 }
218
219 /**
220 * Implementation of hook_form_alter().
221 */
222 function print_mail_form_alter(&$form, $form_state, $form_id) {
223 // Add the node-type settings option to activate the mail version link
224 if ((user_access('administer print') || user_access('node-specific print configuration')) &&
225 (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
226 $form['print']['mail_label'] = array(
227 '#type' => 'markup',
228 '#markup' => '<p><strong>'. t('Send by e-mail') .'</strong></p>',
229 );
230
231 $form['print']['print_mail_display'] = array(
232 '#type' => 'checkbox',
233 '#title' => t('Show link'),
234 );
235 $form['print']['print_mail_display_comment'] = array(
236 '#type' => 'checkbox',
237 '#title' => t('Show link in individual comments'),
238 );
239 $form['print']['print_mail_display_urllist'] = array(
240 '#type' => 'checkbox',
241 '#title' => t('Show Printer-friendly URLs list'),
242 );
243
244 if ($form_id == 'node_type_form') {
245 $form['print']['print_mail_display']['#default_value'] = variable_get('print_mail_display_'. $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
246 $form['print']['print_mail_display_comment']['#default_value'] = variable_get('print_mail_display_comment_'. $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
247 $form['print']['print_mail_display_urllist']['#default_value'] = variable_get('print_mail_display_urllist_'. $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
248 }
249 else {
250 $node = $form['#node'];
251 $form['print']['print_mail_display']['#default_value'] = isset($node->print_mail_display) ? $node->print_mail_display : PRINT_TYPE_SHOW_LINK_DEFAULT;
252 $form['print']['print_mail_display_comment']['#default_value'] = isset($node->print_mail_display_comment) ? $node->print_mail_display_comment : PRINT_TYPE_COMMENT_LINK_DEFAULT;
253 $form['print']['print_mail_display_urllist']['#default_value'] = isset($node->print_mail_display_urllist) ? $node->print_mail_display_urllist : PRINT_TYPE_URLLIST_DEFAULT;
254 }
255 }
256 }
257
258 /**
259 * Auxiliary function to assign the per-node settings to the node object fields
260 *
261 * @param $node
262 * node to be modified
263 */
264 function _print_mail_set_node_fields(&$node) {
265 if (isset($node->nid)) {
266 $res = db_query("SELECT link, comments, url_list FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
267 ->fetch();
268 }
269 else {
270 $res = FALSE;
271 }
272 $node->print_mail_display = $res ? intval($res->link) : PRINT_TYPE_SHOW_LINK_DEFAULT;
273 $node->print_mail_display_comment = $res ? intval($res->comments) : PRINT_TYPE_COMMENT_LINK_DEFAULT;
274 $node->print_mail_display_urllist = $res ? intval($res->url_list) : PRINT_TYPE_URLLIST_DEFAULT;
275 }
276
277 /**
278 * Update the print_mail_node_conf table to reflect the given attributes
279 * If updating to the default values, delete the record.
280 *
281 * @param $nid
282 * value of the nid field (primary key)
283 * @param $link
284 * value of the link field (0 or 1)
285 * @param $comments
286 * value of the comments field (0 or 1)
287 * @param $url_list
288 * value of the url_list field (0 or 1)
289 */
290 function _print_mail_node_conf_modify($nid, $link, $comments, $url_list) {
291 if (($link == PRINT_TYPE_SHOW_LINK_DEFAULT) && ($comments == PRINT_TYPE_COMMENT_LINK_DEFAULT) &&
292 ($url_list == PRINT_TYPE_URLLIST_DEFAULT)) {
293 db_delete('print_mail_node_conf')
294 ->condition('nid', $nid)
295 ->execute();
296 }
297 else {
298 db_merge('print_mail_node_conf')
299 ->key(array('nid' => $nid))
300 ->fields(array(
301 'link' => $link,
302 'comments' => $comments,
303 'url_list' => $url_list,
304 ))
305 ->execute();
306 }
307 }
308
309 /**
310 * Format the send by e-mail link
311 *
312 * @return
313 * array of formatted attributes
314 * @ingroup themeable
315 */
316 function theme_print_mail_format_link() {
317 $print_mail_link_class = variable_get('print_mail_link_class', PRINT_MAIL_LINK_CLASS_DEFAULT);
318 $print_mail_show_link = variable_get('print_mail_show_link', PRINT_MAIL_SHOW_LINK_DEFAULT);
319 $print_mail_link_text = variable_get('print_mail_link_text', t('Send to friend'));
320
321 $img = drupal_get_path('module', 'print') .'/icons/mail_icon.gif';
322 $title = t('Send this page by e-mail.');
323 $class = strip_tags($print_mail_link_class);
324 $new_window = FALSE;
325 $format = _print_format_link_aux($print_mail_show_link, $print_mail_link_text, $img);
326
327 return array('text' => $format['text'],
328 'html' => $format['html'],
329 'attributes' => print_fill_attributes($title, $class, $new_window),
330 );
331 }
332
333 /**
334 * Auxiliary function to display a formatted send by e-mail link
335 *
336 * Function made available so that developers may call this function from
337 * their defined pages/blocks.
338 *
339 * @param $path
340 * path of the original page (optional). If not specified, the current URL
341 * is used
342 * @param $node
343 * an optional node object, to be used in defining the path, if used, the
344 * path argument is irrelevant
345 * @return
346 * string with the HTML link to the printer-friendly page
347 */
348 function print_mail_insert_link($path = NULL, $node = NULL) {
349 if ($node !== NULL) {
350 $nid = $node->nid;
351 $path = 'node/'. $nid;
352 $allowed_type = print_mail_link_allowed(array('node' => $node));
353 }
354 else {
355 if ($path === NULL) {
356 $nid = preg_replace('!^node/!', '', $_GET['q']);
357 $path = $_GET['q'];
358 }
359 else {
360 $nid = NULL;
361 }
362 $allowed_type = print_mail_link_allowed(array('path' => $path));
363 }
364
365 if ($allowed_type) {
366 if ($nid !== NULL) {
367 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
368 $path = 'book/export/html/'. $nid;
369 }
370 else {
371 if (variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT)) {
372 $path = drupal_get_path_alias($path);
373 }
374 else {
375 $path = $nid;
376 }
377 }
378 $path = PRINTMAIL_PATH .'/'. $path;
379 $query = print_query_string_encode($_GET, array('q'));
380 if (empty($query)) {
381 $query = NULL;
382 }
383 }
384 else {
385 $query = NULL;
386 }
387 drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css');
388 $format = theme('print_mail_format_link');
389 return '<span class="print_mail">'. l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) .'</span>';
390 }
391 else {
392 return FALSE;
393 }
394 }
395
396 /**
397 * Determine a the link to send by e-mail is allowed depending on all possible settings
398 *
399 * @param $args
400 * array containing the possible parameters:
401 * teaser, node, type, path
402 * @return
403 * FALSE if not allowed
404 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
405 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
406 */
407 function print_mail_link_allowed($args) {
408 if (!empty($args['teaser']) || !user_access('access print')) {
409 // If showing only the teaser or the user is not allowed or link is disabled
410 return FALSE;
411 }
412 if (!empty($args['path'])) {
413 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
414 if (is_numeric($nid)) {
415 $args['node'] = node_load($nid);
416 }
417 }
418 if (!empty($args['node'])) {
419 static $node_type = FALSE;
420
421 $node = $args['node'];
422 if ($node_type === FALSE) {
423 if (isset($node->type)) {
424 $node_type = $node->type;
425 }
426 else {
427 $node_type = '';
428 }
429 }
430 // Node
431 $print_mail_node_link_visibility = variable_get('print_mail_node_link_visibility', PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT);
432 $print_mail_node_link_pages = variable_get('print_mail_node_link_pages', PRINT_MAIL_NODE_LINK_PAGES_DEFAULT);
433
434 if (!empty($node->printing) ||
435 !_print_page_match($print_mail_node_link_visibility, $print_mail_node_link_pages)) {
436 // Page not in visibility list or we are working!
437 return FALSE;
438 }
439 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
440 // Link is for a comment, return the configured setting
441 $res = db_query("SELECT comments FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
442 ->fetch();
443 $print_display_comment = $res ? intval($res->comments) : PRINT_TYPE_COMMENT_LINK_DEFAULT;
444 if (($print_display_comment) ||
445 variable_get('print_mail_display_comment_'. $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT)) {
446 return PRINT_ALLOW_NORMAL_LINK;
447 }
448 }
449 else {
450 // Node link
451 if ((!$node->print_mail_display) || (isset($node_type) &&
452 !variable_get('print_mail_display_'. $node_type, PRINT_TYPE_SHOW_LINK_DEFAULT))) {
453 // Link for this node type is disabled
454 return FALSE;
455 }
456 elseif (isset($node->book)) {
457 // Node is a book;
458 $print_mail_book_link = variable_get('print_mail_book_link', PRINT_MAIL_BOOK_LINK_DEFAULT);
459 switch ($print_mail_book_link) {
460 case 1:
461 if (user_access('access printer-friendly version')) {
462 return PRINT_ALLOW_BOOK_LINK;
463 }
464 break;
465 case 2:
466 return PRINT_ALLOW_NORMAL_LINK;
467 }
468 }
469 else {
470 return PRINT_ALLOW_NORMAL_LINK;
471 }
472 }
473 }
474 else {
475 // 'System' page
476 $print_mail_sys_link_visibility = variable_get('print_mail_sys_link_visibility', PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT);
477 $print_mail_sys_link_pages = variable_get('print_mail_sys_link_pages', PRINT_MAIL_SYS_LINK_PAGES_DEFAULT);
478
479 return _print_page_match($print_mail_sys_link_visibility, $print_mail_sys_link_pages);
480 }
481 return FALSE;
482 }