6 * Displays Printer-friendly versions of Drupal pages.
11 define('PRINTMAIL_PATH', 'printmail');
13 define('PRINT_MAIL_FORMAT', 'mail');
15 define('PRINT_MAIL_LINK_POS_DEFAULT', 'link');
16 define('PRINT_MAIL_SHOW_LINK_DEFAULT', 1);
17 define('PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT', 0);
18 define('PRINT_MAIL_NODE_LINK_PAGES_DEFAULT', '');
19 define('PRINT_MAIL_LINK_CLASS_DEFAULT', 'print-mail');
20 define('PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT', 1);
21 define('PRINT_MAIL_SYS_LINK_PAGES_DEFAULT', '');
22 define('PRINT_MAIL_LINK_USE_ALIAS_DEFAULT', 0);
23 define('PRINT_MAIL_BOOK_LINK_DEFAULT', 1);
24 define('PRINT_MAIL_HOURLY_THRESHOLD', 3);
25 define('PRINT_MAIL_TEASER_DEFAULT_DEFAULT', 1);
26 define('PRINT_MAIL_TEASER_CHOICE_DEFAULT', 1);
27 define('PRINT_MAIL_JOB_QUEUE_DEFAULT', 0);
30 * Implements hook_permission().
32 function print_mail_permission() {
34 'access send to friend' => array(
35 'title' => t('Access the Send To Friend functionality'),
36 'description' => t('Provides the ability to send pages to a friend and the links to them in the original pages.'),
42 * Implements hook_theme().
44 function print_mail_theme() {
46 'print_mail_format_link' => array(
47 'arguments' => array(),
49 'print_mail_form' => array(
50 'arguments' => array('form'),
51 'file' => 'print_mail.inc',
57 * Implements hook_menu().
59 function print_mail_menu() {
62 $items[PRINTMAIL_PATH
] = array(
63 'title' => 'Send page by e-mail',
64 'page callback' => 'drupal_get_form',
65 'page arguments' => array('print_mail_form'),
66 'access arguments' => array('access send to friend'),
67 'type' => MENU_CALLBACK
,
68 'file' => 'print_mail.inc',
70 $items[PRINTMAIL_PATH .
'/' . PRINTMAIL_PATH
] = array(
71 'access callback' => FALSE
,
73 $items['admin/settings/print/email'] = array(
75 'page callback' => 'drupal_get_form',
76 'page arguments' => array('print_mail_settings'),
77 'access arguments' => array('administer print'),
79 'type' => MENU_LOCAL_TASK
,
80 'file' => 'print_mail.admin.inc',
82 $items['admin/settings/print/email/options'] = array(
85 'type' => MENU_DEFAULT_LOCAL_TASK
,
87 $items['admin/settings/print/email/strings'] = array(
88 'title' => 'Text strings',
89 'page callback' => 'drupal_get_form',
90 'page arguments' => array('print_mail_strings_settings'),
91 'access arguments' => array('administer print'),
93 'type' => MENU_LOCAL_TASK
,
94 'file' => 'print_mail.admin.inc',
101 * Implements hook_block_list().
103 function print_mail_block_list() {
104 $block[0]['info'] = t('Most emailed');
109 * Implements hook_block_view().
111 function print_mail_block_view($delta = 0) {
114 $block['subject'] = t('Most emailed');
115 $result = db_query_range("SELECT path FROM {print_mail_page_counter} ORDER BY sentcount DESC", 0, 3)
117 if (count($result)) {
118 $block['content'] = '<div class="item-list"><ul>';
119 foreach ($result as
$obj) {
120 $block['content'] .
= '<li>' .
l(_print_get_title($obj->path
), $obj->path
) .
'</li>';
122 $block['content'] .
= '</ul></div>';
130 * Implements hook_node_view().
132 function print_mail_node_view($node, $build_mode) {
133 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT
=> PRINT_MAIL_LINK_POS_DEFAULT
));
134 $print_mail_link_use_alias = variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT
);
135 $allowed_type = print_mail_link_allowed(array('type' => 'node', 'node' => $node, 'teaser' => ($build_mode == 'teaser')));
136 if (($allowed_type) && !empty($print_mail_link_pos['link'])) {
137 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
139 $format = theme('print_mail_format_link');
142 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
143 $links['book_mail'] = array('href' => PRINTMAIL_PATH .
'/book/export/html/' .
$node->nid
,
144 'title' => $format['text'],
145 'attributes' => $format['attributes'],
146 'html' => $format['html'],
149 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK
) {
151 if ('node' == 'comment') {
152 $query_arr['comment'] = $node->cid
;
154 $query = print_query_string_encode($query_arr, array('q'));
155 if (empty($query)) $query = NULL
;
157 if ($print_mail_link_use_alias) {
158 $path = drupal_get_path_alias('node/' .
$node->nid
);
164 $links['print_mail'] = array('href' => PRINTMAIL_PATH .
'/' .
$path,
165 'title' => $format['text'],
166 'attributes' => $format['attributes'],
167 'html' => $format['html'],
172 $node->content
['links']['print_mail'] = array(
175 '#attributes' => array('class' => 'links inline'),
179 // Insert content corner links
180 if (!empty($print_mail_link_pos['corner']) && ($build_mode == 'full')) {
181 $link = print_mail_insert_link(NULL
, $node);
183 $node->content
['print_links']['#markup'] = preg_replace('!</span>$!', $link .
'</span>', $node->content
['print_links']['#markup']);
189 * Implements hook_help().
191 function print_mail_help($path, $arg) {
192 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT
=> PRINT_MAIL_LINK_POS_DEFAULT
));
193 if (($path !== 'node/%') && !(empty($print_mail_link_pos['link']) && empty($print_mail_link_pos['corner']))) {
194 static
$output = FALSE
;
196 if ($output === FALSE
) {
199 $link = print_mail_insert_link();
201 return "<span class='print-syslink'>$link</span>";
208 * Implements hook_node_load().
210 function print_mail_node_load($nodes, $types) {
211 foreach ($nodes as
$node) {
212 _print_mail_set_node_fields($node);
217 * Implements hook_node_update().
219 function print_mail_node_update($node) {
220 if (user_access('administer print') || user_access('node-specific print configuration')) {
221 _print_mail_node_conf_modify($node->nid
, $node->print_mail_display
, $node->print_mail_display_comment
, $node->print_mail_display_urllist
);
226 * Implements hook_node_delete().
228 function print_mail_node_delete($node) {
229 db_delete('print_mail_node_conf')
230 ->condition('nid', $node->nid
)
232 db_delete('print_mail_page_counter')
233 ->condition('path', 'node/' .
$node->nid
)
238 * Implements hook_form_alter().
240 function print_mail_form_alter(&$form, &$form_state, $form_id) {
241 // Add the node-type settings option to activate the mail version link
242 if ((user_access('administer print') || user_access('node-specific print configuration')) &&
243 (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
244 $form['print']['mail_label'] = array(
246 '#markup' => '<p><strong>' .
t('Send by e-mail') .
'</strong></p>',
249 $form['print']['print_mail_display'] = array(
250 '#type' => 'checkbox',
251 '#title' => t('Show link'),
253 $form['print']['print_mail_display_comment'] = array(
254 '#type' => 'checkbox',
255 '#title' => t('Show link in individual comments'),
257 $form['print']['print_mail_display_urllist'] = array(
258 '#type' => 'checkbox',
259 '#title' => t('Show Printer-friendly URLs list'),
262 if ($form_id == 'node_type_form') {
263 $form['print']['print_mail_display']['#default_value'] = variable_get('print_mail_display_' .
$form['#node_type']->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
264 $form['print']['print_mail_display_comment']['#default_value'] = variable_get('print_mail_display_comment_' .
$form['#node_type']->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
265 $form['print']['print_mail_display_urllist']['#default_value'] = variable_get('print_mail_display_urllist_' .
$form['#node_type']->type
, PRINT_TYPE_URLLIST_DEFAULT
);
268 $node = $form['#node'];
269 $form['print']['print_mail_display']['#default_value'] = isset($node->print_mail_display
) ?
$node->print_mail_display
: PRINT_TYPE_SHOW_LINK_DEFAULT
;
270 $form['print']['print_mail_display_comment']['#default_value'] = isset($node->print_mail_display_comment
) ?
$node->print_mail_display_comment
: PRINT_TYPE_COMMENT_LINK_DEFAULT
;
271 $form['print']['print_mail_display_urllist']['#default_value'] = isset($node->print_mail_display_urllist
) ?
$node->print_mail_display_urllist
: PRINT_TYPE_URLLIST_DEFAULT
;
277 * Implements hook_job_queue_functions().
279 function print_mail_job_queue_functions() {
280 $functions['print_mail'] = array(
281 'title' => t('Send to friend'),
287 * Auxiliary function to assign the per-node settings to the node object fields
290 * node to be modified
292 function _print_mail_set_node_fields(&$node) {
293 if (isset($node->nid
)) {
294 $res = db_query("SELECT link, comments, url_list FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid
))
300 $node->print_mail_display
= $res ?
intval($res->link) : PRINT_TYPE_SHOW_LINK_DEFAULT
;
301 $node->print_mail_display_comment
= $res ?
intval($res->comments
) : PRINT_TYPE_COMMENT_LINK_DEFAULT
;
302 $node->print_mail_display_urllist
= $res ?
intval($res->url_list
) : PRINT_TYPE_URLLIST_DEFAULT
;
306 * Update the print_mail_node_conf table to reflect the given attributes
307 * If updating to the default values, delete the record.
310 * value of the nid field (primary key)
312 * value of the link field (0 or 1)
314 * value of the comments field (0 or 1)
316 * value of the url_list field (0 or 1)
318 function _print_mail_node_conf_modify($nid, $link, $comments, $url_list) {
319 if (($link == PRINT_TYPE_SHOW_LINK_DEFAULT
) && ($comments == PRINT_TYPE_COMMENT_LINK_DEFAULT
) &&
320 ($url_list == PRINT_TYPE_URLLIST_DEFAULT
)) {
321 db_delete('print_mail_node_conf')
322 ->condition('nid', $nid)
326 db_merge('print_mail_node_conf')
327 ->key(array('nid' => $nid))
330 'comments' => $comments,
331 'url_list' => $url_list,
338 * Format the send by e-mail link
341 * array of formatted attributes
344 function theme_print_mail_format_link() {
345 $print_mail_link_class = variable_get('print_mail_link_class', PRINT_MAIL_LINK_CLASS_DEFAULT
);
346 $print_mail_show_link = variable_get('print_mail_show_link', PRINT_MAIL_SHOW_LINK_DEFAULT
);
347 $print_mail_link_text = filter_xss(variable_get('print_mail_link_text', t('Send to friend')));
349 $img = drupal_get_path('module', 'print') .
'/icons/mail_icon.gif';
350 $title = t('Send this page by e-mail.');
351 $class = strip_tags($print_mail_link_class);
353 $format = _print_format_link_aux($print_mail_show_link, $print_mail_link_text, $img);
355 return array('text' => $format['text'],
356 'html' => $format['html'],
357 'attributes' => print_fill_attributes($title, $class, $new_window),
362 * Auxiliary function to display a formatted send by e-mail link
364 * Function made available so that developers may call this function from
365 * their defined pages/blocks.
368 * path of the original page (optional). If not specified, the current URL
371 * an optional node object, to be used in defining the path, if used, the
372 * path argument is irrelevant
374 * string with the HTML link to the printer-friendly page
376 function print_mail_insert_link($path = NULL
, $node = NULL
) {
377 if ($node !== NULL
) {
379 $path = 'node/' .
$nid;
380 $allowed_type = print_mail_link_allowed(array('node' => $node));
383 if ($path === NULL
) {
384 $nid = preg_replace('!^node/!', '', $_GET['q']);
390 $allowed_type = print_mail_link_allowed(array('path' => $path));
395 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
396 $path = 'book/export/html/' .
$nid;
399 if (variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT
)) {
400 $path = drupal_get_path_alias($path);
406 $path = PRINTMAIL_PATH .
'/' .
$path;
407 $query = print_query_string_encode($_GET, array('q'));
415 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
416 $format = theme('print_mail_format_link');
417 return '<span class="print_mail">' .
l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE
, 'html' => $format['html'])) .
'</span>';
425 * Determine a the link to send by e-mail is allowed depending on all possible settings
428 * array containing the possible parameters:
429 * teaser, node, type, path
431 * FALSE if not allowed
432 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
433 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
435 function print_mail_link_allowed($args) {
436 if (!empty($args['teaser']) || !user_access('access send to friend')) {
437 // If showing only the teaser or the user is not allowed or link is disabled
440 if (!empty($args['path'])) {
441 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
442 if (is_numeric($nid)) {
443 $args['node'] = node_load($nid);
446 if (!empty($args['node'])) {
447 static
$node_type = FALSE
;
449 $node = $args['node'];
450 if ($node_type === FALSE
) {
451 if (isset($node->type
)) {
452 $node_type = $node->type
;
459 $print_mail_node_link_visibility = variable_get('print_mail_node_link_visibility', PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT
);
460 $print_mail_node_link_pages = variable_get('print_mail_node_link_pages', PRINT_MAIL_NODE_LINK_PAGES_DEFAULT
);
462 if (!_print_page_match($print_mail_node_link_visibility, "node/".
$node->nid
, $print_mail_node_link_pages)) {
463 // Page not in visibility list
466 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
467 // Link is for a comment, return the configured setting
468 $res = db_query("SELECT comments FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid
))
470 $print_display_comment = $res ?
intval($res->comments
) : PRINT_TYPE_COMMENT_LINK_DEFAULT
;
471 if (($print_display_comment) ||
472 variable_get('print_mail_display_comment_' .
$node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT
)) {
473 return PRINT_ALLOW_NORMAL_LINK
;
478 if ((!$node->print_mail_display
) || (isset($node_type) &&
479 !variable_get('print_mail_display_' .
$node_type, PRINT_TYPE_SHOW_LINK_DEFAULT
))) {
480 // Link for this node type is disabled
483 elseif (isset($node->book
)) {
485 $print_mail_book_link = variable_get('print_mail_book_link', PRINT_MAIL_BOOK_LINK_DEFAULT
);
486 switch ($print_mail_book_link) {
488 if (user_access('access printer-friendly version')) {
489 return PRINT_ALLOW_BOOK_LINK
;
493 return PRINT_ALLOW_NORMAL_LINK
;
497 return PRINT_ALLOW_NORMAL_LINK
;
503 $print_mail_sys_link_visibility = variable_get('print_mail_sys_link_visibility', PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT
);
504 $print_mail_sys_link_pages = variable_get('print_mail_sys_link_pages', PRINT_MAIL_SYS_LINK_PAGES_DEFAULT
);
506 return _print_page_match($print_mail_sys_link_visibility, $_GET['q'], $print_mail_sys_link_pages);