6 * Displays Printer-friendly versions of Drupal pages.
9 define('PRINTMAIL_PATH', 'printmail');
11 define('PRINT_MAIL_FORMAT', 'mail');
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);
27 * Implementation of hook_theme().
29 function print_mail_theme() {
31 'print_mail_format_link' => array(
32 'arguments' => array(),
34 'print_mail_form' => array(
35 'arguments' => array('form'),
41 * Implementation of hook_menu().
43 function print_mail_menu() {
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
,
53 $items['admin/settings/print/email'] = array(
55 'page callback' => 'drupal_get_form',
56 'page arguments' => array('print_mail_settings'),
57 'access arguments' => array('administer print'),
59 'type' => MENU_LOCAL_TASK
,
66 * Implementation of hook_block().
68 function print_mail_block($op = 'list', $delta = 0, $edit = array()) {
71 $block[0]['info'] = t('Most emailed');
81 $block['subject'] = t('Most emailed');
82 $result = db_query_range("SELECT path FROM {print_mail_page_counter} ORDER BY sentcount DESC", 0, 3)
85 $block['content'] = '<div class="item-list"><ul>';
86 foreach ($result as
$obj) {
87 $block['content'] .
= '<li>'.
l(_print_get_title($obj->path
), $obj->path
) .
'</li>';
89 $block['content'] .
= '</ul></div>';
99 * Implementation of hook_link().
101 function print_mail_link($type, $node = NULL
, $teaser = FALSE
) {
102 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT
=> PRINT_MAIL_LINK_POS_DEFAULT
));
103 $print_mail_link_use_alias = variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT
);
104 $allowed_type = print_mail_link_allowed(array('type' => $type, 'node' => $node, 'teaser' => $teaser));
105 if (($allowed_type) && !empty($print_mail_link_pos['link'])) {
106 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
108 $format = theme('print_mail_format_link');
111 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
112 $links['book_mail'] = array('href' => PRINTMAIL_PATH .
'/book/export/html/'.
$node->nid
,
113 'title' => $format['text'],
114 'attributes' => $format['attributes'],
115 'html' => $format['html'],
120 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK
) {
122 if ($type == 'comment') {
123 $query_arr['comment'] = $node->cid
;
125 $query = print_query_string_encode($query_arr, array('q'));
126 if (empty($query)) $query = NULL
;
128 if ($print_mail_link_use_alias) {
129 $path = drupal_get_path_alias('node/'.
$node->nid
);
135 $links['print_mail'] = array('href' => PRINTMAIL_PATH .
'/'.
$path,
136 'title' => $format['text'],
137 'attributes' => $format['attributes'],
138 'html' => $format['html'],
151 * Implementation of hook_help().
153 function print_mail_help($path, $arg) {
154 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT
=> PRINT_MAIL_LINK_POS_DEFAULT
));
155 if ((preg_match('!^node/!i', $path) == 0) &&
156 !(empty($print_mail_link_pos['link']) && empty($print_mail_link_pos['corner']))) {
157 static
$output = FALSE
;
159 if ($output === FALSE
) {
162 $link = print_mail_insert_link();
164 return "<span class='print-syslink'>$link</span>";
171 * Implementation of hook_nodeapi_view().
173 function print_mail_nodeapi_view(&$node, $teaser, $page) {
174 // Insert content corner links
175 if ($teaser === FALSE
) {
176 _print_mail_set_node_fields($node);
178 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT
=> PRINT_MAIL_LINK_POS_DEFAULT
));
179 if (($teaser === FALSE
) && !empty($print_mail_link_pos['corner']) &&
180 (preg_match('!^print!i', $_GET['q']) == 0)) {
181 $link = print_mail_insert_link(NULL
, $node);
183 $node->content
['print_mail_link'] = array(
184 '#markup' => "<span class='print-link'>$link</span>",
192 * Implementation of hook_nodeapi_prepare().
194 function print_mail_nodeapi_prepare(&$node, $teaser, $page) {
195 _print_mail_set_node_fields($node);
199 * Implementation of hook_nodeapi_update().
201 function print_mail_nodeapi_update(&$node, $teaser, $page) {
202 _print_mail_node_conf_modify($node->nid
, $node->print_mail_display
, $node->print_mail_display_comment
, $node->print_mail_display_urllist
);
206 * Implementation of hook_nodeapi_delete().
208 function print_mail_nodeapi_delete(&$node, $teaser, $page) {
209 db_delete('print_mail_node_conf')
210 ->condition('nid', $node->nid
)
212 db_delete('print_mail_page_counter')
213 ->condition('path', $node->path
)
218 * Implementation of hook_form_alter().
220 function print_mail_form_alter(&$form, $form_state, $form_id) {
221 // Add the node-type settings option to activate the mail version link
222 if (($form_id == 'node_type_form') || !empty($form['#node_edit_form'])) {
223 $form['print']['mail_label'] = array(
225 '#markup' => '<p><strong>'.
t('Send by e-mail') .
'</strong></p>',
228 $form['print']['print_mail_display'] = array(
229 '#type' => 'checkbox',
230 '#title' => t('Show link'),
232 $form['print']['print_mail_display_comment'] = array(
233 '#type' => 'checkbox',
234 '#title' => t('Show link in individual comments'),
236 $form['print']['print_mail_display_urllist'] = array(
237 '#type' => 'checkbox',
238 '#title' => t('Show Printer-friendly URLs list'),
241 if ($form_id == 'node_type_form') {
242 $form['print']['print_mail_display']['#default_value'] = variable_get('print_mail_display_'.
$form['#node_type']->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
);
243 $form['print']['print_mail_display_comment']['#default_value'] = variable_get('print_mail_display_comment_'.
$form['#node_type']->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
244 $form['print']['print_mail_display_urllist']['#default_value'] = variable_get('print_mail_display_urllist_'.
$form['#node_type']->type
, PRINT_TYPE_URLLIST_DEFAULT
);
247 $form['print']['print_mail_display']['#default_value'] = $form['#node']->print_mail_display
;
248 $form['print']['print_mail_display_comment']['#default_value'] = $form['#node']->print_mail_display_comment
;
249 $form['print']['print_mail_display_urllist']['#default_value'] = $form['#node']->print_mail_display_urllist
;
255 * Auxiliary function to assign the per-node settings to the node object fields
258 * node to be modified
260 function _print_mail_set_node_fields(&$node) {
261 $res = db_query("SELECT link, comments, url_list FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid
))
263 $node->print_mail_display
= $res ?
intval($res->link) : PRINT_TYPE_SHOW_LINK_DEFAULT
;
264 $node->print_mail_display_comment
= $res ?
intval($res->comments
) : PRINT_TYPE_COMMENT_LINK_DEFAULT
;
265 $node->print_mail_display_urllist
= $res ?
intval($res->url_list
) : PRINT_TYPE_URLLIST_DEFAULT
;
269 * Update the print_mail_node_conf table to reflect the given attributes
270 * If updating to the default values, delete the record.
273 * value of the nid field (primary key)
275 * value of the link field (0 or 1)
277 * value of the comments field (0 or 1)
279 * value of the url_list field (0 or 1)
281 function _print_mail_node_conf_modify($nid, $link, $comments, $url_list) {
282 if (($link == PRINT_TYPE_SHOW_LINK_DEFAULT
) && ($comments == PRINT_TYPE_COMMENT_LINK_DEFAULT
) &&
283 ($url_list == PRINT_TYPE_URLLIST_DEFAULT
)) {
284 db_delete('print_mail_node_conf')
285 ->condition('nid', $nid)
289 db_merge('print_mail_node_conf')
290 ->key(array('nid' => $nid))
293 'comments' => $comments,
294 'url_list' => $url_list,
301 * Format the send by e-mail link
304 * array of formatted attributes
307 function theme_print_mail_format_link() {
308 $print_mail_link_class = variable_get('print_mail_link_class', PRINT_MAIL_LINK_CLASS_DEFAULT
);
309 $print_mail_show_link = variable_get('print_mail_show_link', PRINT_MAIL_SHOW_LINK_DEFAULT
);
310 $print_mail_link_text = variable_get('print_mail_link_text', t('Send to friend'));
312 $img = drupal_get_path('module', 'print') .
'/icons/mail_icon.gif';
313 $title = t('Send this page by e-mail.');
314 $class = strip_tags($print_mail_link_class);
316 $format = _print_format_link_aux($print_mail_show_link, $print_mail_link_text, $img);
318 return array('text' => $format['text'],
319 'html' => $format['html'],
320 'attributes' => print_fill_attributes($title, $class, $new_window),
325 * Auxiliary function to display a formatted send by e-mail link
327 * Function made available so that developers may call this function from
328 * their defined pages/blocks.
331 * path of the original page (optional). If not specified, the current URL
334 * an optional node object, to be used in defining the path, if used, the
335 * path argument is irrelevant
337 * string with the HTML link to the printer-friendly page
339 function print_mail_insert_link($path = NULL
, $node = NULL
) {
340 if ($node !== NULL
) {
342 $path = 'node/'.
$nid;
343 $allowed_type = print_mail_link_allowed(array('node' => $node));
346 if ($path === NULL
) {
347 $nid = preg_replace('!^node/!', '', $_GET['q']);
353 $allowed_type = print_mail_link_allowed(array('path' => $path));
358 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
359 $path = 'book/export/html/'.
$nid;
362 if (variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT
)) {
363 $path = drupal_get_path_alias($path);
369 $path = PRINTMAIL_PATH .
'/'.
$path;
370 $query = print_query_string_encode($_GET, array('q'));
378 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
379 $format = theme('print_mail_format_link');
380 return '<span class="print_mail">'.
l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE
, 'html' => $format['html'])) .
'</span>';
388 * Determine a the link to send by e-mail is allowed depending on all possible settings
391 * array containing the possible parameters:
392 * teaser, node, type, path
394 * FALSE if not allowed
395 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
396 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
398 function print_mail_link_allowed($args) {
399 if (!empty($args['teaser']) || !user_access('access print')) {
400 // If showing only the teaser or the user is not allowed or link is disabled
403 if (!empty($args['path'])) {
404 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
405 if (is_numeric($nid)) {
406 $args['node'] = node_load($nid);
409 if (!empty($args['node'])) {
410 static
$node_type = FALSE
;
412 $node = $args['node'];
413 if ($node_type === FALSE
) {
414 if (isset($node->type
)) {
415 $node_type = $node->type
;
422 $print_mail_node_link_visibility = variable_get('print_mail_node_link_visibility', PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT
);
423 $print_mail_node_link_pages = variable_get('print_mail_node_link_pages', PRINT_MAIL_NODE_LINK_PAGES_DEFAULT
);
425 if (!empty($node->printing
) ||
426 !_print_page_match($print_mail_node_link_visibility, $print_mail_node_link_pages)) {
427 // Page not in visibility list or we are working!
430 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
431 // Link is for a comment, return the configured setting
432 $res = db_query("SELECT comments FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid
))
434 $print_display_comment = $res ?
intval($res->comments
) : PRINT_TYPE_COMMENT_LINK_DEFAULT
;
435 if (($print_display_comment) ||
436 variable_get('print_mail_display_comment_'.
$node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT
)) {
437 return PRINT_ALLOW_NORMAL_LINK
;
442 if ((!$node->print_mail_display
) || (isset($node_type) &&
443 !variable_get('print_mail_display_'.
$node_type, PRINT_TYPE_SHOW_LINK_DEFAULT
))) {
444 // Link for this node type is disabled
447 elseif (isset($node->book
)) {
449 $print_mail_book_link = variable_get('print_mail_book_link', PRINT_MAIL_BOOK_LINK_DEFAULT
);
450 switch ($print_mail_book_link) {
452 if (user_access('access printer-friendly version')) {
453 return PRINT_ALLOW_BOOK_LINK
;
457 return PRINT_ALLOW_NORMAL_LINK
;
461 return PRINT_ALLOW_NORMAL_LINK
;
467 $print_mail_sys_link_visibility = variable_get('print_mail_sys_link_visibility', PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT
);
468 $print_mail_sys_link_pages = variable_get('print_mail_sys_link_pages', PRINT_MAIL_SYS_LINK_PAGES_DEFAULT
);
470 return _print_page_match($print_mail_sys_link_visibility, $print_mail_sys_link_pages);