Projet

Général

Profil

Paste
Télécharger (25,8 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / print / print_mail / print_mail.module @ dc45a079

1
<?php
2

    
3
/**
4
 * @file
5
 * Displays Printer-friendly versions of Drupal pages.
6
 *
7
 * @ingroup print
8
 */
9

    
10
define('PRINTMAIL_PATH', 'printmail');
11

    
12
// Defined in print.module
13
// define('PRINT_MAIL_FORMAT', 'mail');
14

    
15
define('PRINT_MAIL_LINK_POS_DEFAULT', '{ "link": "link", "block": "block", "help": "help" }');
16
define('PRINT_MAIL_LINK_TEASER_DEFAULT', 0);
17
define('PRINT_MAIL_SHOW_LINK_DEFAULT', 1);
18
define('PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT', 0);
19
define('PRINT_MAIL_NODE_LINK_PAGES_DEFAULT', '');
20
define('PRINT_MAIL_LINK_CLASS_DEFAULT', 'print-mail');
21
define('PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT', 1);
22
define('PRINT_MAIL_SYS_LINK_PAGES_DEFAULT', '');
23
define('PRINT_MAIL_LINK_USE_ALIAS_DEFAULT', 0);
24
define('PRINT_MAIL_BOOK_LINK_DEFAULT', 1);
25
define('PRINT_MAIL_HOURLY_THRESHOLD', 3);
26
define('PRINT_MAIL_USE_REPLY_TO', TRUE);
27
define('PRINT_MAIL_TEASER_DEFAULT_DEFAULT', 1);
28
define('PRINT_MAIL_TEASER_CHOICE_DEFAULT', 0);
29
define('PRINT_MAIL_SEND_OPTION_DEFAULT', 'sendpage');
30
define('PRINT_MAIL_JOB_QUEUE_DEFAULT', 0);
31

    
32
/**
33
 * Implements hook_permission().
34
 */
35
function print_mail_permission() {
36
  return array(
37
    'access send by email' => array(
38
      'title' => t('Access the Send by email functionality'),
39
      'description' => t('Provides the ability to send pages by email and the links to them in the original pages.'),
40
    ),
41
    'send unlimited emails' => array(
42
      'title' => t('Send unlimited emails'),
43
      'description' => t("Overrides the built-in hourly threshold limits when sending emails. This permission should only be granted to trusted users, due to it's potential in enabling the use of your site as a source of email spam."),
44
    ),
45
  );
46
}
47

    
48
/**
49
 * Implements hook_theme().
50
 */
51
function print_mail_theme() {
52
  return array(
53
    'print_mail_format_link' => array(
54
      'variables' => array(),
55
    ),
56
    'print_mail_form' => array(
57
      'render element' => 'form',
58
      'file' => 'print_mail.inc',
59
    ),
60
  );
61
}
62

    
63
/**
64
 * Implements hook_menu().
65
 */
66
function print_mail_menu() {
67
  $items = array();
68

    
69
  $items[PRINTMAIL_PATH] = array(
70
    'title' => variable_get('print_mail_text_title', 'Send page by email'),
71
    'page callback' => 'drupal_get_form',
72
    'page arguments' => array('print_mail_form'),
73
    'access callback' => '_print_mail_access',
74
    'access arguments' => array('access send by email'),
75
    'type' => MENU_CALLBACK,
76
    'file' => 'print_mail.inc',
77
  );
78
  $items[PRINTMAIL_PATH . '/' . PRINTMAIL_PATH] = array(
79
    'access callback' => FALSE,
80
  );
81
  $items['admin/config/user-interface/print/email'] = array(
82
    'title' => 'email',
83
    'description' => 'Configure the settings of the send by email functionality.',
84
    'page callback' => 'drupal_get_form',
85
    'page arguments' => array('print_mail_settings'),
86
    'access arguments'  => array('administer print'),
87
    'weight' => 2,
88
    'type' => MENU_LOCAL_TASK,
89
    'file' => 'print_mail.admin.inc',
90
  );
91
  $items['admin/config/user-interface/print/email/options'] = array(
92
    'title' => 'Options',
93
    'weight' => 1,
94
    'type' => MENU_DEFAULT_LOCAL_TASK,
95
  );
96
  $items['admin/config/user-interface/print/email/strings'] = array(
97
    'title' => 'Text strings',
98
    'description' => 'Override the user-facing strings used in the send by email version.',
99
    'page callback' => 'drupal_get_form',
100
    'page arguments' => array('print_mail_strings_settings'),
101
    'access arguments'  => array('administer print'),
102
    'weight' => 2,
103
    'type' => MENU_LOCAL_TASK,
104
    'file' => 'print_mail.admin.inc',
105
  );
106

    
107
  return $items;
108
}
109

    
110
/**
111
 * Implements hook_requirements().
112
 */
113
function print_mail_requirements($phase) {
114
  $requirements = array();
115
  $t = get_t();
116
  switch ($phase) {
117
    // At runtime, make sure that a PDF generation tool is selected
118
    case 'runtime':
119
      if (module_exists('mailsystem')) {
120
        $mail_system = mailsystem_get();
121
        if (($mail_system['default-system'] != 'DefaultMailSystem') && (!isset($mail_system['print_mail']) || ($mail_system['print_mail'] != 'DefaultMailSystem'))) {
122
          $requirements['print_mail_mailsystem'] = array(
123
            'title' => $t('Printer, email and PDF versions - Send by email'),
124
            'value' => $t('Incompatible Mail System setting detected'),
125
            'description' => $t('The send by email module requires the use of the DefaultMailSystem, please configure it in the !url.', array('!url' => l($t('Mail System Settings page'), 'admin/config/system/mailsystem'))),
126
            'severity' => REQUIREMENT_WARNING,
127
          );
128
        }
129
      }
130
  }
131

    
132
  return $requirements;
133
}
134

    
135
/**
136
 * Implements hook_block_info().
137
 */
138
function print_mail_block_info() {
139
      $block['print_mail-top']['info'] = t('Most emailed');
140
      $block['print_mail-top']['cache'] = DRUPAL_CACHE_GLOBAL;
141
      return $block;
142
}
143

    
144
/**
145
 * Implements hook_block_view().
146
 */
147
function print_mail_block_view($delta = 0) {
148
      switch ($delta) {
149
      case 'print_mail-top':
150
        $block['subject'] = t('Most emailed');
151
        $result = db_query_range("SELECT path FROM {print_mail_page_counter} LEFT JOIN {node} n ON path = CONCAT('node/', n.nid) WHERE status <> 0 OR status IS NULL ORDER BY sentcount DESC", 0, 3)
152
                    ->fetchAll();
153
        if (count($result)) {
154
          $block['content'] = '<div class="item-list"><ul>';
155
          foreach ($result as $obj) {
156
            $block['content'] .= '<li>' . l(_print_get_title($obj->path), $obj->path) . '</li>';
157
          }
158
          $block['content'] .= '</ul></div>';
159
        }
160
        break;
161
      }
162
      return $block;
163
}
164

    
165
/**
166
 * Implements hook_node_view().
167
 */
168
function print_mail_node_view($node, $view_mode) {
169
  $print_mail_link_pos = variable_get('print_mail_link_pos', drupal_json_decode(PRINT_MAIL_LINK_POS_DEFAULT));
170
  $print_mail_link_use_alias = variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT);
171

    
172
  foreach (array('node', 'comment') as $type) {
173
    $allowed_type = print_mail_link_allowed(array('type' => $type, 'node' => $node, 'view_mode' => $view_mode));
174
    if (($allowed_type) && !empty($print_mail_link_pos['link'])) {
175
      drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
176
      $links = array();
177
      $format = theme('print_mail_format_link');
178

    
179
      // Show book link
180
      if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
181
        $links['book_mail'] = array(
182
          'href' => PRINTMAIL_PATH . '/book/export/html/' . $node->nid,
183
          'title' => $format['text'],
184
          'attributes' => $format['attributes'],
185
          'html' => $format['html'],
186
        );
187
      }
188
      elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) {
189
        $path = (($print_mail_link_use_alias) && ($alias = drupal_lookup_path('alias', 'node/' . $node->nid))) ? $alias : $node->nid;
190

    
191
        $links['print_mail'] = array(
192
          'href' => PRINTMAIL_PATH . '/' . $path,
193
          'title' => $format['text'],
194
          'attributes' => $format['attributes'],
195
          'html' => $format['html'],
196
          'query' => print_query_string_encode($_GET, array('q')),
197
        );
198
      }
199

    
200
      $link_content = array(
201
        '#theme' => 'links',
202
        '#links' => $links,
203
        '#attributes' => array('class' => array('links', 'inline')),
204
      );
205

    
206
      if ($type == 'node') {
207
        $node->content['links']['print_mail'] = $link_content;
208
      }
209
      elseif (($type == 'comment') && isset($node->content['comments']['comments'])) {
210
        foreach ($node->content['comments']['comments'] as $cid => $comment) {
211
          if (is_numeric($cid)) {
212
            $link_content['#links']['print_mail']['query']['comment'] = $cid;
213
            $node->content['comments']['comments'][$cid]['links']['print_mail'] = $link_content;
214
          }
215
        }
216
      }
217
    }
218
  }
219

    
220
  // Insert content corner links
221
  if (!empty($print_mail_link_pos['corner']) && ($view_mode == 'full')) {
222
    $node->content['print_links']['#markup'] .= print_mail_insert_link(NULL, $node);
223
  }
224
}
225

    
226
/**
227
 * Implements hook_help().
228
 */
229
function print_mail_help($path, $arg) {
230
  $print_mail_link_pos = variable_get('print_mail_link_pos', drupal_json_decode(PRINT_MAIL_LINK_POS_DEFAULT));
231
  if (($path !== 'node/%') && !(empty($print_mail_link_pos['help']))) {
232
    static $output = FALSE;
233

    
234
    if ($output === FALSE) {
235
      $output = TRUE;
236

    
237
      $link = print_mail_insert_link();
238
      if ($link) {
239
        return "<span class='print-syslink'>$link</span>";
240
      }
241
    }
242
  }
243
}
244

    
245
/**
246
 * Implements hook_node_load().
247
 */
248
function print_mail_node_load($nodes, $types) {
249
  $ids = array();
250
  foreach ($nodes as $node) {
251
    $ids[] = $node->nid;
252
  }
253

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

    
256
  foreach ($nodes as $node) {
257
    $node->print_mail_display = isset($result[$node->nid]) ? intval($result[$node->nid]->link) : variable_get('print_mail_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
258
    $node->print_mail_display_comment = isset($result[$node->nid]) ? intval($result[$node->nid]->comments) : variable_get('print_mail_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
259
    $node->print_mail_display_urllist = isset($result[$node->nid]) ? intval($result[$node->nid]->url_list) : variable_get('print_mail_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
260
  }
261
}
262

    
263
/**
264
 * Implements hook_node_insert().
265
 */
266
function print_mail_node_insert($node) {
267
  if (user_access('administer print') || user_access('node-specific print configuration')) {
268
    if (!isset($node->print_mail_display)) $node->print_mail_display = variable_get('print_mail_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
269
    if (!isset($node->print_mail_display_comment)) $node->print_mail_display_comment = variable_get('print_mail_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
270
    if (!isset($node->print_mail_display_urllist)) $node->print_mail_display_urllist = variable_get('print_mail_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
271

    
272
    _print_mail_node_conf_modify($node->nid, $node->print_mail_display, $node->print_mail_display_comment, $node->print_mail_display_urllist);
273
  }
274
}
275

    
276
/**
277
 * Implements hook_node_update().
278
 */
279
function print_mail_node_update($node) {
280
  if (user_access('administer print') || user_access('node-specific print configuration')) {
281
    if (!isset($node->print_mail_display)) $node->print_mail_display = variable_get('print_mail_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
282
    if (!isset($node->print_mail_display_comment)) $node->print_mail_display_comment = variable_get('print_mail_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
283
    if (!isset($node->print_mail_display_urllist)) $node->print_mail_display_urllist = variable_get('print_mail_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
284

    
285
    _print_mail_node_conf_modify($node->nid, $node->print_mail_display, $node->print_mail_display_comment, $node->print_mail_display_urllist);
286
  }
287
}
288

    
289
/**
290
 * Implements hook_node_delete().
291
 */
292
function print_mail_node_delete($node) {
293
  db_delete('print_mail_node_conf')
294
    ->condition('nid', $node->nid)
295
    ->execute();
296
  db_delete('print_mail_page_counter')
297
    ->condition('path', 'node/' . $node->nid)
298
    ->execute();
299
}
300

    
301
/**
302
 * Implements hook_form_alter().
303
 */
304
function print_mail_form_alter(&$form, &$form_state, $form_id) {
305
  // Add the node-type settings option to activate the mail version link
306
  if ((user_access('administer print') || user_access('node-specific print configuration')) &&
307
      (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
308
    $form['print']['mail_label'] = array(
309
      '#type' => 'markup',
310
      '#markup' => '<p><strong>' . t('Send by email') . '</strong></p>',
311
    );
312

    
313
    $form['print']['print_mail_display'] = array(
314
      '#type' => 'checkbox',
315
      '#title' => t('Show link'),
316
    );
317
    $form['print']['print_mail_display_comment'] = array(
318
      '#type' => 'checkbox',
319
      '#title' => t('Show link in individual comments'),
320
    );
321
    $form['print']['print_mail_display_urllist'] = array(
322
      '#type' => 'checkbox',
323
      '#title' => t('Show Printer-friendly URLs list'),
324
    );
325

    
326
    if ($form_id == 'node_type_form') {
327
      $form['print']['print_mail_display']['#default_value'] = variable_get('print_mail_display_' . $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
328
      $form['print']['print_mail_display_comment']['#default_value'] = variable_get('print_mail_display_comment_' . $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
329
      $form['print']['print_mail_display_urllist']['#default_value'] = variable_get('print_mail_display_urllist_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
330
    }
331
    else {
332
      $node = $form['#node'];
333
      $form['print']['print_mail_display']['#default_value'] = isset($node->print_mail_display) ? $node->print_mail_display : variable_get('print_mail_display_' . $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
334
      $form['print']['print_mail_display_comment']['#default_value'] = isset($node->print_mail_display_comment) ? $node->print_mail_display_comment : variable_get('print_mail_display_comment_' . $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
335
      $form['print']['print_mail_display_urllist']['#default_value'] = isset($node->print_mail_display_urllist) ? $node->print_mail_display_urllist : variable_get('print_mail_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
336
    }
337
  }
338
}
339

    
340
/**
341
 * Implements hook_cron_queue_info().
342
 */
343
function print_mail_cron_queue_info() {
344
  $queues['print_mail_send'] = array(
345
    'worker callback' => 'print_mail_send',
346
    'time' => 60,
347
  );
348
  return $queues;
349
}
350

    
351
function print_mail_send($data) {
352
  drupal_mail($data['module'], $data['key'], $data['to'], $data['language'], $data['params'], $data['from']);
353
}
354

    
355
/**
356
 * Implements hook_mail().
357
 */
358
function print_mail_mail($key, &$message, $params) {
359
  $message['subject'] = $params['subject'];
360

    
361
  if (isset($params['from'])) {
362
    $message['headers']['Reply-To'] = $params['from'];
363
  }
364

    
365
  switch ($key) {
366
    case 'sendpage':
367
      $message['body'][] = check_plain($params['body']);
368
      $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
369
      break;
370
    case 'sendlink':
371
      // Generate plain-text and html versions of message with link
372
      $sendlink_plain = $params['message'] . '\n\n' . $params['link'];
373
      $sendlink_html = $params['message'] . '<br/><br/>' . l($params['title'], $params['link']);
374

    
375
      // Send HTML-only version if MIME library not present
376
      if (!class_exists('Mail_mime')) {
377
        $message['body'][] = check_plain($sendlink_html);
378
        $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
379
        break;
380
      }
381
      // no break on purpose
382
    case 'plain-attachment':
383
    case 'inline-attachment':
384
      // Configure new MIME object
385
      $mime = new Mail_mime("\n");
386
      $mime_params['html_encoding'] = '7bit';
387
      $mime_params['html_charset'] = 'utf-8';
388
      $mime_params['text_charset'] = 'utf-8';
389

    
390
      // Pass message contents into MIME object
391
      switch ($key) {
392
        case 'sendlink':
393
          $mime->setTxtBody($sendlink_plain);
394
          $mime->setHTMLBody($sendlink_html);
395
          break;
396
        case 'inline-attachment':
397
          $mime->setHTMLBody($params['body']);
398
          // no break on purpose
399
        case 'plain-attachment':
400
          $mime->setTxtBody($params['message']);
401
          $mime->addAttachment($params['body'], 'text/html', 'Attachment.html', FALSE);
402
          break;
403
      }
404

    
405
      // Store MIME message output in message array
406
      $message['body'][] = check_plain($mime->get($mime_params));
407
      $message['headers'] = $mime->headers($message['headers']);
408

    
409
      // Strip special characters from Content-Type header
410
      // Required to prevent mime_header_encode() from disrupting Content-Type header
411
      $message['headers']['Content-Type'] = preg_replace('/[^\x20-\x7E]/', '', $message['headers']['Content-Type']);
412
      break;
413
  }
414
}
415

    
416
/**
417
 * Access callback to check a combination of user_acess() and page access
418
 *
419
 * @param $permission
420
 *   permission required to view the page
421
 * @return
422
 *   TRUE if the user has permission to view the page, FALSE otherwise
423
 */
424
function _print_mail_access($permission) {
425
  $page_access = TRUE;
426
  $parts = explode('/', $_GET['q']);
427
  if ($parts[0] == PRINTMAIL_PATH) {
428
    if (count($parts) > 1) {
429
      unset($parts[0]);
430
      $path = implode('/', $parts);
431
      if (ctype_digit($parts[1])) {
432
        if (drupal_lookup_path('source', $path)) {
433
          // This is a numeric alias
434
          $path = drupal_get_normal_path($path);
435
        }
436
        else {
437
          // normal nid
438
          $path = 'node/' . $path;
439
        }
440
      }
441
      else {
442
        $path = drupal_get_normal_path($path);
443
      }
444
      // If the destination page is not accessible, don't show the form
445
      if (!($router_item = menu_get_item($path)) || (!$router_item['access'])) {
446
        $page_access = FALSE;
447
      }
448
    }
449
  }
450

    
451
  return (user_access($permission) && $page_access);
452
}
453

    
454
/**
455
 * Update the print_mail_node_conf table to reflect the given attributes
456
 *
457
 * If updating to the default values, delete the record.
458
 *
459
 * @param $nid
460
 *   value of the nid field (primary key)
461
 * @param $link
462
 *   value of the link field (0 or 1)
463
 * @param $comments
464
 *   value of the comments field (0 or 1)
465
 * @param $url_list
466
 *   value of the url_list field (0 or 1)
467
 */
468
function _print_mail_node_conf_modify($nid, $link, $comments, $url_list) {
469
    db_merge('print_mail_node_conf')
470
      ->key(array('nid' => $nid))
471
      ->fields(array(
472
        'link' => $link,
473
        'comments' => $comments,
474
        'url_list' => $url_list,
475
      ))
476
      ->execute();
477
}
478

    
479
/**
480
 * Format the send by email link
481
 *
482
 * @return
483
 *   array of formatted attributes
484
 * @ingroup themeable
485
 */
486
function theme_print_mail_format_link() {
487
  $print_mail_link_class  = variable_get('print_mail_link_class', PRINT_MAIL_LINK_CLASS_DEFAULT);
488
  $print_mail_show_link = variable_get('print_mail_show_link', PRINT_MAIL_SHOW_LINK_DEFAULT);
489
  $print_mail_link_text = filter_xss(variable_get('print_mail_link_text', t('Send by email')));
490

    
491
  $img = drupal_get_path('module', 'print') . '/icons/mail_icon.gif';
492
  $title = t('Send this page by email.');
493
  $class = strip_tags($print_mail_link_class);
494
  $new_window = FALSE;
495
  $format = _print_format_link_aux($print_mail_show_link, $print_mail_link_text, $img);
496

    
497
  return array('text' => $format['text'],
498
               'html' => $format['html'],
499
               'attributes' => print_fill_attributes($title, $class, $new_window),
500
              );
501
}
502

    
503
/**
504
 * Auxiliary function to display a formatted send by email link
505
 *
506
 * Function made available so that developers may call this function from
507
 * their defined pages/blocks.
508
 *
509
 * @param $path
510
 *   path of the original page (optional). If not specified, the current URL
511
 *   is used
512
 * @param $node
513
 *   an optional node object, to be used in defining the path, if used, the
514
 *   path argument is irrelevant
515
 * @return
516
 *   string with the HTML link to the printer-friendly page
517
 */
518
function print_mail_insert_link($path = NULL, $node = NULL) {
519
  if ($node !== NULL) {
520
    $nid = $node->nid;
521
    $path = 'node/' . $nid;
522
    $allowed_type = print_mail_link_allowed(array('node' => $node));
523
  }
524
  else {
525
    if ($path === NULL) {
526
      $nid = preg_replace('!^node/([\d]+)!', '$1', $_GET['q']);
527
      $path = $_GET['q'];
528
    }
529
    else {
530
      $nid = NULL;
531
    }
532
    $allowed_type = print_mail_link_allowed(array('path' => $path));
533
  }
534

    
535
  if ($allowed_type) {
536
    if ($nid !== NULL) {
537
      if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
538
        $path = 'book/export/html/' . $nid;
539
      }
540
      else {
541
        if (variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT) && ($alias = drupal_lookup_path('alias', $path))) {
542
          $path = $alias;
543
        }
544
        else {
545
          $path = $nid;
546
        }
547
      }
548
      $path = PRINTMAIL_PATH . '/' . $path;
549
      $query = print_query_string_encode($_GET, array('q'));
550
    }
551
    else {
552
      $query = NULL;
553
    }
554
    drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
555
    $format = theme('print_mail_format_link');
556
    return '<span class="print_mail">' . l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) . '</span>';
557
  }
558
  else {
559
    return FALSE;
560
  }
561
}
562

    
563
/**
564
 * Check if the link to send by email is allowed depending on the settings
565
 *
566
 * @param $args
567
 *   array containing the possible parameters:
568
 *    teaser, node, type, path
569
 * @return
570
 *   FALSE if not allowed
571
 *   PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
572
 *   PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
573
 */
574
function print_mail_link_allowed($args) {
575
  $view_mode = isset($args['view_mode']) ? $args['view_mode'] : '';
576
  if ((($view_mode == 'teaser') && !variable_get('print_mail_link_teaser', PRINT_MAIL_LINK_TEASER_DEFAULT))
577
      || !in_array($view_mode, array('full', 'teaser', '')) || !user_access('access send by email')) {
578
    // If the teaser link is disabled or the user is not allowed
579
    return FALSE;
580
  }
581
  if (!empty($args['path'])) {
582
    $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
583
    if (ctype_digit($nid)) {
584
      $args['node'] = node_load($nid);
585
    }
586
  }
587
  if (!empty($args['node'])) {
588
    static $node_type = FALSE;
589

    
590
    $node = $args['node'];
591
    if (isset($node->type)) {
592
      $node_type = $node->type;
593
    }
594
    // Node
595
    $print_mail_node_link_visibility = variable_get('print_mail_node_link_visibility', PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT);
596
    $print_mail_node_link_pages = variable_get('print_mail_node_link_pages', PRINT_MAIL_NODE_LINK_PAGES_DEFAULT);
597

    
598
    if (!_print_page_match($print_mail_node_link_visibility, "node/" . $node->nid, $print_mail_node_link_pages)) {
599
      // Page not in visibility list
600
      return FALSE;
601
    }
602
    elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
603
      // Link is for a comment, return the configured setting
604
      // Cache this statically to avoid duplicate queries for every comment.
605
      static $res = array();
606
      if (!isset($res[$node->nid])) {
607
        $res[$node->nid] = db_query("SELECT comments FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))->fetchField();
608
      }
609
      $print_display_comment = ($res && ($res[$node->nid] !== FALSE)) ? $res[$node->nid] : variable_get('print_mail_display_comment_' . $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
610
      if ($print_display_comment) {
611
        return PRINT_ALLOW_NORMAL_LINK;
612
      }
613
    }
614
    else {
615
      // Node link
616
      if (isset($node->print_mail_display) && !$node->print_mail_display) {
617
        // Link for this node is disabled
618
        return FALSE;
619
      }
620
      elseif (isset($node->book)) {
621
        // Node is a book;
622
        $print_mail_book_link = variable_get('print_mail_book_link', PRINT_MAIL_BOOK_LINK_DEFAULT);
623
        switch ($print_mail_book_link) {
624
          case 1:
625
            if (user_access('access printer-friendly version')) {
626
              return PRINT_ALLOW_BOOK_LINK;
627
            }
628
            break;
629
          case 2:
630
            return PRINT_ALLOW_NORMAL_LINK;
631
        }
632
      }
633
      else {
634
        return PRINT_ALLOW_NORMAL_LINK;
635
      }
636
    }
637
  }
638
  else {
639
    // 'System' page
640
    $print_mail_sys_link_visibility = variable_get('print_mail_sys_link_visibility', PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT);
641
    $print_mail_sys_link_pages = variable_get('print_mail_sys_link_pages', PRINT_MAIL_SYS_LINK_PAGES_DEFAULT);
642

    
643
    return _print_page_match($print_mail_sys_link_visibility, $_GET['q'], $print_mail_sys_link_pages);
644
  }
645
  return FALSE;
646
}
647

    
648
/**
649
 * Implements hook_nollom_form_list().
650
 */
651
function print_mail_mollom_form_list() {
652
  $forms['print_mail_form'] = array(
653
    'title' => t('Send page form'),
654
    'entity' => 'print_mail',
655
  );
656
  return $forms;
657
}
658

    
659
/**
660
 * Implemenents hook_mollom_form_info().
661
 */
662
function print_mail_mollom_form_info($form_id) {
663
  switch ($form_id) {
664
    case 'print_mail_form':
665
      $form_info = array(
666
        'elements' => array(
667
          'fld_from_addr' => t('Sender email'),
668
          'fld_from_name' => t('Sender name'),
669
          'txt_to_addrs' => t('Recipients'),
670
          'fld_subject' => t('Subject'),
671
          'fld_title' => t('Page to be sent'),
672
          'txt_message' => t('Your message'),
673
        ),
674
        'mapping' => array(
675
          'post_title' => 'fld_title',
676
          'author_name' => 'fld_from_name',
677
          'author_mail' => 'fld_from_addr',
678
        ),
679
      );
680
      break;
681
  }
682
  return $form_info;
683
}
684

    
685
/**
686
 * Implements hook_views_api().
687
 */
688
function print_mail_views_api() {
689
  return array(
690
    'api' => 2.0,
691
    'path' => drupal_get_path('module', 'print_mail'),
692
  );
693
}
694

    
695
/**
696
 * Implements hook_rules_action_info().
697
 *
698
 * @ingroup rules
699
 */
700
function print_mail_rules_action_info() {
701
  return array(
702
    'print_mail_action_submit' => array(
703
      'label' => t('Send node as HTML formatted email'),
704
      'group' => t('Send by email'),
705
      'parameter' => array(
706
        'from'      => array('type' => 'text', 'label' => t('From email adress')),
707
        'from_name' => array('type' => 'text', 'label' => t('From name')),
708
        'to'        => array('type' => 'text', 'label' => t('Send email to')),
709
        'subject'   => array('type' => 'text', 'label' => t('Subject')),
710
        'message'   => array(
711
          'type' => 'text',
712
          'label' => t('Message'),
713
          'description' => t('The message that should be displayed (optional).'),
714
          'optional' => TRUE,
715
        ),
716
        'node'      => array('type' => 'node', 'label' => t('Content')),
717
      ),
718
    ),
719
  );
720
}
721

    
722
/**
723
 * Action handler for the print_mail_action_submit
724
 *
725
 * @ingroup rules
726
 */
727
function print_mail_action_submit($from, $from_name, $to, $subject, $message, $node) {
728
  module_load_include('inc', 'print_mail', 'print_mail');
729

    
730
  $form_state['values'] = array(
731
    'path' => 'node/' . $node->nid,
732
    'cid' => NULL,
733
    'query' => NULL,
734
    'title' => $node->title,
735
    'fld_from_addr' => $from,
736
    'fld_from_name' => $from_name,
737
    'txt_to_addrs' => $to,
738
    'fld_subject' => $subject,
739
    'txt_message' => $message,
740
    'chk_teaser' => FALSE,
741
  );
742

    
743
  print_mail_form_submit(NULL, $form_state);
744
}