5 * Displays Printer-friendly versions of Drupal pages.
10 define('PRINTMAIL_PATH', 'printmail');
12 // Defined in print.module
13 //define('PRINT_MAIL_FORMAT', 'mail');
15 define('PRINT_MAIL_LINK_POS_DEFAULT', 'link');
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_TEASER_DEFAULT_DEFAULT', 1);
27 define('PRINT_MAIL_TEASER_CHOICE_DEFAULT', 1);
28 define('PRINT_MAIL_SEND_OPTION_DEFAULT', 'sendpage');
29 define('PRINT_MAIL_JOB_QUEUE_DEFAULT', 0);
32 * Implements hook_permission().
34 function print_mail_permission() {
36 'access send to friend' => array(
37 'title' => t('Access the Send To Friend functionality'),
38 'description' => t('Provides the ability to send pages to a friend and the links to them in the original pages.'),
44 * Implements hook_theme().
46 function print_mail_theme() {
48 'print_mail_format_link' => array(
49 'variables' => array(),
51 'print_mail_form' => array(
52 'variables' => array('form' => NULL
),
53 'file' => 'print_mail.inc',
59 * Implements hook_menu().
61 function print_mail_menu() {
64 $items[PRINTMAIL_PATH
] = array(
65 'title' => variable_get('print_mail_text_title', 'Send page by e-mail'),
66 'page callback' => 'drupal_get_form',
67 'page arguments' => array('print_mail_form'),
68 'access callback' => '_print_mail_access',
69 'access arguments' => array('access send to friend'),
70 'type' => MENU_CALLBACK
,
71 'file' => 'print_mail.inc',
73 $items[PRINTMAIL_PATH .
'/' . PRINTMAIL_PATH
] = array(
74 'access callback' => FALSE
,
76 $items['admin/config/user-interface/print/email'] = array(
78 'page callback' => 'drupal_get_form',
79 'page arguments' => array('print_mail_settings'),
80 'access arguments' => array('administer print'),
82 'type' => MENU_LOCAL_TASK
,
83 'file' => 'print_mail.admin.inc',
85 $items['admin/config/user-interface/print/email/options'] = array(
88 'type' => MENU_DEFAULT_LOCAL_TASK
,
90 $items['admin/config/user-interface/print/email/strings'] = array(
91 'title' => 'Text strings',
92 'page callback' => 'drupal_get_form',
93 'page arguments' => array('print_mail_strings_settings'),
94 'access arguments' => array('administer print'),
96 'type' => MENU_LOCAL_TASK
,
97 'file' => 'print_mail.admin.inc',
104 * Implements hook_block_info().
106 function print_mail_block_info() {
107 $block['print_mail-top']['info'] = t('Most emailed');
108 $block['print_mail-top']['cache'] = DRUPAL_CACHE_GLOBAL
;
113 * Implements hook_block_view().
115 function print_mail_block_view($delta = 0) {
117 case
'print_mail-top':
118 $block['subject'] = t('Most emailed');
119 $result = db_query_range("SELECT path FROM {print_mail_page_counter} ORDER BY sentcount DESC", 0, 3)
121 if (count($result)) {
122 $block['content'] = '<div class="item-list"><ul>';
123 foreach ($result as
$obj) {
124 $block['content'] .
= '<li>' .
l(_print_get_title($obj->path
), $obj->path
) .
'</li>';
126 $block['content'] .
= '</ul></div>';
134 * Implements hook_node_view().
136 function print_mail_node_view($node, $view_mode) {
137 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT
=> PRINT_MAIL_LINK_POS_DEFAULT
));
138 $print_mail_link_use_alias = variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT
);
139 $allowed_type = print_mail_link_allowed(array('type' => 'node', 'node' => $node, 'view_mode' => $view_mode));
140 if (($allowed_type) && !empty($print_mail_link_pos['link'])) {
141 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
143 $format = theme('print_mail_format_link');
146 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
147 $links['book_mail'] = array('href' => PRINTMAIL_PATH .
'/book/export/html/' .
$node->nid
,
148 'title' => $format['text'],
149 'attributes' => $format['attributes'],
150 'html' => $format['html'],
153 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK
) {
155 if ('node' == 'comment') {
156 $query_arr['comment'] = $node->cid
;
158 $query = print_query_string_encode($query_arr, array('q'));
159 if (empty($query)) $query = NULL
;
161 if ($print_mail_link_use_alias) {
162 $path = drupal_get_path_alias('node/' .
$node->nid
);
168 $links['print_mail'] = array('href' => PRINTMAIL_PATH .
'/' .
$path,
169 'title' => $format['text'],
170 'attributes' => $format['attributes'],
171 'html' => $format['html'],
176 $node->content
['links']['print_mail'] = array(
179 '#attributes' => array('class' => array('links', 'inline')),
183 // Insert content corner links
184 if (!empty($print_mail_link_pos['corner']) && ($view_mode == 'full')) {
185 $node->content
['print_links']['#markup'] .
= print_mail_insert_link(NULL
, $node);
190 * Implements hook_help().
192 function print_mail_help($path, $arg) {
193 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT
=> PRINT_MAIL_LINK_POS_DEFAULT
));
194 if (($path !== 'node/%') && !(empty($print_mail_link_pos['link']) && empty($print_mail_link_pos['corner']))) {
195 static
$output = FALSE
;
197 if ($output === FALSE
) {
200 $link = print_mail_insert_link();
202 return "<span class='print-syslink'>$link</span>";
209 * Implements hook_node_load().
211 function print_mail_node_load($nodes, $types) {
212 foreach ($nodes as
$node) {
213 _print_mail_set_node_fields($node);
218 * Implements hook_node_insert().
220 function print_mail_node_insert($node) {
221 if (user_access('administer print') || user_access('node-specific print configuration')) {
222 if ($node->print_mail_display
=== NULL
) $node->print_mail_display
= variable_get('print_mail_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
223 if ($node->print_mail_display_comment
=== NULL
) $node->print_mail_display_comment
= variable_get('print_mail_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
224 if ($node->print_mail_display_urllist
=== NULL
) $node->print_mail_display_urllist
= variable_get('print_mail_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
226 _print_mail_node_conf_modify($node->nid
, $node->print_mail_display
, $node->print_mail_display_comment
, $node->print_mail_display_urllist
);
231 * Implements hook_node_update().
233 function print_mail_node_update($node) {
234 if (user_access('administer print') || user_access('node-specific print configuration')) {
235 if ($node->print_mail_display
=== NULL
) $node->print_mail_display
= variable_get('print_mail_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
236 if ($node->print_mail_display_comment
=== NULL
) $node->print_mail_display_comment
= variable_get('print_mail_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
237 if ($node->print_mail_display_urllist
=== NULL
) $node->print_mail_display_urllist
= variable_get('print_mail_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
239 _print_mail_node_conf_modify($node->nid
, $node->print_mail_display
, $node->print_mail_display_comment
, $node->print_mail_display_urllist
);
244 * Implements hook_node_delete().
246 function print_mail_node_delete($node) {
247 db_delete('print_mail_node_conf')
248 ->condition('nid', $node->nid
)
250 db_delete('print_mail_page_counter')
251 ->condition('path', 'node/' .
$node->nid
)
256 * Implements hook_form_alter().
258 function print_mail_form_alter(&$form, &$form_state, $form_id) {
259 // Add the node-type settings option to activate the mail version link
260 if ((user_access('administer print') || user_access('node-specific print configuration')) &&
261 (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
262 $form['print']['mail_label'] = array(
264 '#markup' => '<p><strong>' .
t('Send by e-mail') .
'</strong></p>',
267 $form['print']['print_mail_display'] = array(
268 '#type' => 'checkbox',
269 '#title' => t('Show link'),
271 $form['print']['print_mail_display_comment'] = array(
272 '#type' => 'checkbox',
273 '#title' => t('Show link in individual comments'),
275 $form['print']['print_mail_display_urllist'] = array(
276 '#type' => 'checkbox',
277 '#title' => t('Show Printer-friendly URLs list'),
280 if ($form_id == 'node_type_form') {
281 $form['print']['print_mail_display']['#default_value'] = variable_get('print_mail_display_' .
$form['#node_type']->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
282 $form['print']['print_mail_display_comment']['#default_value'] = variable_get('print_mail_display_comment_' .
$form['#node_type']->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
283 $form['print']['print_mail_display_urllist']['#default_value'] = variable_get('print_mail_display_urllist_' .
$form['#node_type']->type
, PRINT_TYPE_URLLIST_DEFAULT
);
286 $node = $form['#node'];
287 $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
);
288 $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
);
289 $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
);
295 * Implements hook_job_queue_functions().
297 function print_mail_job_queue_functions() {
298 $functions['print_mail'] = array(
299 'title' => t('Send to friend'),
305 * Access callback to check a combination of user_acess() and page access
308 * permission required to view the page
310 * TRUE if the user has permission to view the page, FALSE otherwise
312 function _print_mail_access($permission) {
314 $parts = explode('/', $_GET['q']);
315 if ($parts[0] == PRINTMAIL_PATH
) {
316 if (count($parts) > 1) {
318 $path = implode('/', $parts);
319 if (ctype_digit($parts[1])) {
320 if (drupal_lookup_path('source', $path)) {
321 // This is a numeric alias
322 $path = drupal_get_normal_path($path);
326 $path = 'node/' .
$path;
330 $path = drupal_get_normal_path($path);
332 // If the destination page is not accessible, don't show the form
333 if (!($router_item = menu_get_item($path)) || (!$router_item['access'])) {
334 $page_access = FALSE
;
339 return (user_access($permission) && $page_access);
343 * Auxiliary function to assign the per-node settings to the node object fields
346 * node to be modified
348 function _print_mail_set_node_fields(&$node) {
349 if (isset($node->nid
)) {
350 $res = db_query("SELECT link, comments, url_list FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid
))
356 $node->print_mail_display
= $res ?
intval($res->link) : variable_get('print_mail_display_' .
$node->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
357 $node->print_mail_display_comment
= $res ?
intval($res->comments
) : variable_get('print_mail_display_comment_' .
$node->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
358 $node->print_mail_display_urllist
= $res ?
intval($res->url_list
) : variable_get('print_mail_display_urllist_' .
$node->type
, PRINT_TYPE_URLLIST_DEFAULT
);
362 * Update the print_mail_node_conf table to reflect the given attributes
363 * If updating to the default values, delete the record.
366 * value of the nid field (primary key)
368 * value of the link field (0 or 1)
370 * value of the comments field (0 or 1)
372 * value of the url_list field (0 or 1)
374 function _print_mail_node_conf_modify($nid, $link, $comments, $url_list) {
375 db_merge('print_mail_node_conf')
376 ->key(array('nid' => $nid))
379 'comments' => $comments,
380 'url_list' => $url_list,
386 * Format the send by e-mail link
389 * array of formatted attributes
392 function theme_print_mail_format_link() {
393 $print_mail_link_class = variable_get('print_mail_link_class', PRINT_MAIL_LINK_CLASS_DEFAULT
);
394 $print_mail_show_link = variable_get('print_mail_show_link', PRINT_MAIL_SHOW_LINK_DEFAULT
);
395 $print_mail_link_text = filter_xss(variable_get('print_mail_link_text', t('Send to friend')));
397 $img = drupal_get_path('module', 'print') .
'/icons/mail_icon.gif';
398 $title = t('Send this page by e-mail.');
399 $class = strip_tags($print_mail_link_class);
401 $format = _print_format_link_aux($print_mail_show_link, $print_mail_link_text, $img);
403 return array('text' => $format['text'],
404 'html' => $format['html'],
405 'attributes' => print_fill_attributes($title, $class, $new_window),
410 * Auxiliary function to display a formatted send by e-mail link
412 * Function made available so that developers may call this function from
413 * their defined pages/blocks.
416 * path of the original page (optional). If not specified, the current URL
419 * an optional node object, to be used in defining the path, if used, the
420 * path argument is irrelevant
422 * string with the HTML link to the printer-friendly page
424 function print_mail_insert_link($path = NULL
, $node = NULL
) {
425 if ($node !== NULL
) {
427 $path = 'node/' .
$nid;
428 $allowed_type = print_mail_link_allowed(array('node' => $node));
431 if ($path === NULL
) {
432 $nid = preg_replace('!^node/!', '', $_GET['q']);
438 $allowed_type = print_mail_link_allowed(array('path' => $path));
443 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
444 $path = 'book/export/html/' .
$nid;
447 if (variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT
)) {
448 $path = drupal_get_path_alias($path);
454 $path = PRINTMAIL_PATH .
'/' .
$path;
455 $query = print_query_string_encode($_GET, array('q'));
463 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
464 $format = theme('print_mail_format_link');
465 return '<span class="print_mail">' .
l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE
, 'html' => $format['html'])) .
'</span>';
473 * Determine a the link to send by e-mail is allowed depending on all possible settings
476 * array containing the possible parameters:
477 * teaser, node, type, path
479 * FALSE if not allowed
480 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
481 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
483 function print_mail_link_allowed($args) {
484 $view_mode = isset($args['view_mode']) ?
$args['view_mode'] : '';
485 if ((($view_mode == 'teaser') && !variable_get('print_mail_link_teaser', PRINT_MAIL_LINK_TEASER_DEFAULT
))
486 || !in_array($view_mode, array('full', 'teaser', '')) || !user_access('access send to friend')) {
487 // If the teaser link is disabled or the user is not allowed
490 if (!empty($args['path'])) {
491 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
492 if (ctype_digit($nid)) {
493 $args['node'] = node_load($nid);
496 if (!empty($args['node'])) {
497 static
$node_type = FALSE
;
499 $node = $args['node'];
500 if (isset($node->type
)) {
501 $node_type = $node->type
;
504 $print_mail_node_link_visibility = variable_get('print_mail_node_link_visibility', PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT
);
505 $print_mail_node_link_pages = variable_get('print_mail_node_link_pages', PRINT_MAIL_NODE_LINK_PAGES_DEFAULT
);
507 if (!_print_page_match($print_mail_node_link_visibility, "node/" .
$node->nid
, $print_mail_node_link_pages)) {
508 // Page not in visibility list
511 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
512 // Link is for a comment, return the configured setting
513 // Cache this statically to avoid duplicate queries for every comment.
514 static
$res = array();
515 if (!isset($res[$node->nid
])) {
516 $res[$node->nid
] = db_query("SELECT comments FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid
))
519 $print_display_comment = $res ?
intval($res[$node->nid
]->comments
) : variable_get('print_mail_display_comment_' .
$node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
520 if ($print_display_comment) {
521 return PRINT_ALLOW_NORMAL_LINK
;
526 if (!$node->print_mail_display
) {
527 // Link for this node is disabled
530 elseif (isset($node->book
)) {
532 $print_mail_book_link = variable_get('print_mail_book_link', PRINT_MAIL_BOOK_LINK_DEFAULT
);
533 switch ($print_mail_book_link) {
535 if (user_access('access printer-friendly version')) {
536 return PRINT_ALLOW_BOOK_LINK
;
540 return PRINT_ALLOW_NORMAL_LINK
;
544 return PRINT_ALLOW_NORMAL_LINK
;
550 $print_mail_sys_link_visibility = variable_get('print_mail_sys_link_visibility', PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT
);
551 $print_mail_sys_link_pages = variable_get('print_mail_sys_link_pages', PRINT_MAIL_SYS_LINK_PAGES_DEFAULT
);
553 return _print_page_match($print_mail_sys_link_visibility, $_GET['q'], $print_mail_sys_link_pages);