6 * Display printer-friendly versions of Drupal pages
9 define('PRINTMAIL_PATH', 'printmail');
11 define('PRINT_MAIL_LINK_POS_DEFAULT', 'link');
12 define('PRINT_MAIL_SHOW_LINK_DEFAULT', 1);
13 define('PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT', 0);
14 define('PRINT_MAIL_NODE_LINK_PAGES_DEFAULT', '');
15 define('PRINT_MAIL_LINK_CLASS_DEFAULT', 'print-mail');
16 define('PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT', 1);
17 define('PRINT_MAIL_SYS_LINK_PAGES_DEFAULT', '<front>');
18 define('PRINT_MAIL_BOOK_LINK_DEFAULT', 1);
19 define('PRINT_MAIL_HOURLY_THRESHOLD', 3);
20 define('PRINT_MAIL_TEASER_DEFAULT_DEFAULT', 1);
21 define('PRINT_MAIL_TEASER_CHOICE_DEFAULT', 1);
24 * Implementation of hook_theme().
26 function print_mail_theme() {
28 'print_mail_format_link' => array(
29 'arguments' => array(),
31 'print_mail_form' => array(
32 'arguments' => array('form'),
33 'file' => 'print_mail.inc',
39 * Implementation of hook_menu().
41 function print_mail_menu() {
44 $items[PRINTMAIL_PATH
] = array(
45 'title' => 'Send page by e-mail',
46 'page callback' => 'drupal_get_form',
47 'page arguments' => array('print_mail_form'),
48 'access arguments' => array('access print'),
49 'type' => MENU_CALLBACK
,
50 'file' => 'print_mail.inc',
52 $items['admin/settings/print/mail'] = array(
54 'page callback' => 'drupal_get_form',
55 'page arguments' => array('print_mail_settings'),
56 'access arguments' => array('administer print'),
58 'type' => MENU_LOCAL_TASK
,
59 'file' => 'print_mail.admin.inc',
66 * Implementation of hook_link().
68 function print_mail_link($type, $node = NULL
, $teaser = FALSE
) {
69 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT
=> PRINT_MAIL_LINK_POS_DEFAULT
));
70 $allowed_type = print_mail_link_allowed(array('type' => $type, 'node' => $node, 'teaser' => $teaser));
71 if (($allowed_type) && !empty($print_mail_link_pos['link'])) {
72 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
74 $format = theme('print_mail_format_link');
77 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
78 $links['book_mail'] = array('href' => PRINTMAIL_PATH .
'/book/export/html/'.
$node->nid
,
79 'title' => $format['text'],
80 'attributes' => $format['attributes'],
81 'html' => $format['html'],
86 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK
) {
88 if ($type == 'comment') {
89 $query_arr['comment'] = $node->cid
;
91 $query = print_query_string_encode($query_arr, array('q'));
93 $links['print_mail'] = array('href' => PRINTMAIL_PATH .
'/'.
$node->nid
,
94 'title' => $format['text'],
95 'attributes' => $format['attributes'],
96 'html' => $format['html'],
107 * Implementation of hook_help().
109 function print_mail_help($path, $arg) {
110 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT
=> PRINT_MAIL_LINK_POS_DEFAULT
));
111 if (print_mail_link_allowed(array('path' => $path)) && (preg_match('!^node/!i', $path) == 0) &&
112 !(empty($print_mail_link_pos['link']) && empty($print_mail_link_pos['corner']))) {
113 static
$output = FALSE
;
115 if ($output === FALSE
) {
116 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
119 return '<span class="print-syslink">'.
print_mail_insert_link() .
'</span>';
125 * Implementation of hook_nodeapi().
127 function print_mail_nodeapi(&$node, $op = 'view', $teaser, $page) {
130 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT
=> PRINT_MAIL_LINK_POS_DEFAULT
));
131 $allowed_type = print_mail_link_allowed(array('node' => $node, 'teaser' => $teaser));
132 if (($allowed_type != FALSE
) && !empty($print_mail_link_pos['corner']) &&
133 (preg_match('!^print!i', $_GET['q']) == 0)) {
134 drupal_add_css(drupal_get_path('module', 'print') .
'/css/printlinks.css');
135 if ($allowed_type === PRINT_ALLOW_BOOK_LINK
) {
136 $path = PRINTMAIL_PATH .
'/book/export/html/'.
$node->nid
;
138 $link = print_mail_insert_link($path);
139 $node->content
['print_mail_link'] = array(
140 '#value' => "<span class='print-link'>$link</span>",
148 * Implementation of hook_form_alter().
150 function print_mail_form_alter(&$form, $form_state, $form_id) {
151 // Add the node-type settings option to activate the mail version link
152 if ($form_id == 'node_type_form') {
153 $form['print']['print_mail_display'] = array(
154 '#type' => 'checkbox',
155 '#title' => t('Show send by e-mail link'),
156 '#default_value' => variable_get('print_mail_display_'.
$form['#node_type']->type
, PRINT_TYPE_SHOW_LINK_DEFAULT
),
157 '#description' => t('Displays a link to send the content by e-mail. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
159 $form['print']['print_mail_display_comment'] = array(
160 '#type' => 'checkbox',
161 '#title' => t('Show send by e-mail link in individual comments'),
162 '#default_value' => variable_get('print_mail_display_comment_'.
$form['#node_type']->type
, PRINT_TYPE_COMMENT_LINK_DEFAULT
),
163 '#description' => t('Displays a link to send the comment by e-mail. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
169 * Format the send by e-mail link
172 * array of formatted attributes
175 function theme_print_mail_format_link() {
176 $print_mail_link_class = variable_get('print_mail_link_class', PRINT_MAIL_LINK_CLASS_DEFAULT
);
177 $print_mail_show_link = variable_get('print_mail_show_link', PRINT_MAIL_SHOW_LINK_DEFAULT
);
179 $text = t('Send to friend');
180 $img = drupal_get_path('module', 'print') .
'/icons/mail_icon.gif';
181 $title = t('Send this page by e-mail.');
182 $class = strip_tags($print_mail_link_class);
184 $format = _print_format_link_aux($print_mail_show_link, $text, $img);
186 return array('text' => $format['text'],
187 'html' => $format['html'],
188 'attributes' => print_fill_attributes($title, $class, $new_window),
193 * Auxiliary function to display a formatted send by e-mail link
197 function print_mail_insert_link($path = NULL
) {
198 if (user_access('access print')) {
199 if ($path === NULL
) {
200 $path = PRINTMAIL_PATH .
'/'.
preg_replace('!^node/!', '', $_GET['q']);
201 $query = print_query_string_encode($_GET, array('q'));
206 $format = theme('print_mail_format_link');
207 return '<span class="print_mail">'.
l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE
, 'html' => $format['html'])) .
'</span>';
212 * Determine a the link to send by e-mail is allowed depending on all possible settings
215 * array containing the possible parameters:
216 * teaser, node, type, path
218 * FALSE if not allowed
219 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
220 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
222 function print_mail_link_allowed($args) {
223 if (!empty($args['teaser']) || !user_access('access print')) {
224 // If showing only the teaser or the user is not allowed or link is disabled
227 if (!empty($args['node'])) {
228 static
$node_type = FALSE
;
230 $node = $args['node'];
231 if ($node_type === FALSE
) {
232 if (isset($node->type
)) {
233 $node_type = $node->type
;
240 $print_mail_node_link_visibility = variable_get('print_mail_node_link_visibility', PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT
);
241 $print_mail_node_link_pages = variable_get('print_mail_node_link_pages', PRINT_MAIL_NODE_LINK_PAGES_DEFAULT
);
243 if (!empty($node->printing
) ||
244 !_print_page_match($print_mail_node_link_visibility, $print_mail_node_link_pages)) {
245 // Page not in visibility list or we are working!
248 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
249 // Link is for a comment, return the configured setting
250 return variable_get('print_mail_display_comment_'.
$node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT
);
254 if (isset($node_type) &&
255 !variable_get('print_mail_display_'.
$node_type, PRINT_TYPE_SHOW_LINK_DEFAULT
)) {
256 // Link for this node type is disabled
259 elseif (($node_type == 'book') || isset($node->book
)) {
261 $print_mail_book_link = variable_get('print_mail_book_link', PRINT_MAIL_BOOK_LINK_DEFAULT
);
262 if (!$print_mail_book_link || !user_access('access printer-friendly version')) {
263 // Book link is disabled
267 return PRINT_ALLOW_BOOK_LINK
;
271 return PRINT_ALLOW_NORMAL_LINK
;
277 $print_mail_sys_link_visibility = variable_get('print_mail_sys_link_visibility', PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT
);
278 $print_mail_sys_link_pages = variable_get('print_mail_sys_link_pages', PRINT_MAIL_SYS_LINK_PAGES_DEFAULT
);
280 return _print_page_match($print_mail_sys_link_visibility, $print_mail_sys_link_pages);