Fix another part of #435578: decode HTML entities in TCPDF title
[project/print.git] / print_mail / print_mail.module
CommitLineData
c889e2e2 1<?php
f96caeed 2// $Id$
c889e2e2
JV
3
4/**
5 * @file
e60934b7 6 * Displays Printer-friendly versions of Drupal pages.
c889e2e2
JV
7 */
8
7d62e7c4
JV
9define('PRINTMAIL_PATH', 'printmail');
10
855b243a
JV
11define('PRINT_MAIL_FORMAT', 'mail');
12
a8222e12 13define('PRINT_MAIL_LINK_POS_DEFAULT', 'link');
7d62e7c4
JV
14define('PRINT_MAIL_SHOW_LINK_DEFAULT', 1);
15define('PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT', 0);
16define('PRINT_MAIL_NODE_LINK_PAGES_DEFAULT', '');
17define('PRINT_MAIL_LINK_CLASS_DEFAULT', 'print-mail');
18define('PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT', 1);
7a93017e 19define('PRINT_MAIL_SYS_LINK_PAGES_DEFAULT', '');
c0bede24 20define('PRINT_MAIL_LINK_USE_ALIAS_DEFAULT', 0);
7d62e7c4 21define('PRINT_MAIL_BOOK_LINK_DEFAULT', 1);
0d9893f9 22define('PRINT_MAIL_HOURLY_THRESHOLD', 3);
7d62e7c4
JV
23define('PRINT_MAIL_TEASER_DEFAULT_DEFAULT', 1);
24define('PRINT_MAIL_TEASER_CHOICE_DEFAULT', 1);
c889e2e2 25
c889e2e2
JV
26/**
27 * Implementation of hook_theme().
28 */
29function print_mail_theme() {
30 return array(
31 'print_mail_format_link' => array(
32 'arguments' => array(),
33 ),
0706a465
JV
34 'print_mail_form' => array(
35 'arguments' => array('form'),
0706a465 36 ),
c889e2e2
JV
37 );
38}
39
40/**
41 * Implementation of hook_menu().
42 */
43function print_mail_menu() {
44 $items = array();
45
46 $items[PRINTMAIL_PATH] = array(
0706a465 47 'title' => 'Send page by e-mail',
7be5aba1
JV
48 'page callback' => 'drupal_get_form',
49 'page arguments' => array('print_mail_form'),
c889e2e2
JV
50 'access arguments' => array('access print'),
51 'type' => MENU_CALLBACK,
c889e2e2 52 );
5830cfca 53 $items['admin/settings/print/email'] = array(
c889e2e2
JV
54 'title' => 'e-mail',
55 'page callback' => 'drupal_get_form',
56 'page arguments' => array('print_mail_settings'),
57 'access arguments' => array('administer print'),
dcd3fc93 58 'weight' => 2,
c889e2e2 59 'type' => MENU_LOCAL_TASK,
c889e2e2 60 );
98df4e4a
JV
61 $items['admin/settings/print/email/options'] = array(
62 'title' => 'Options',
63 'weight' => 1,
64 'type' => MENU_DEFAULT_LOCAL_TASK,
65 );
66 $items['admin/settings/print/email/strings'] = array(
67 'title' => 'Text strings',
68 'page callback' => 'drupal_get_form',
69 'page arguments' => array('print_mail_strings_settings'),
70 'access arguments' => array('administer print'),
71 'weight' => 2,
72 'type' => MENU_LOCAL_TASK,
73 );
c889e2e2
JV
74
75 return $items;
76}
77
78/**
99e463d7 79 * Implementation of hook_block_list().
855b243a 80 */
99e463d7 81function print_mail_block_list() {
855b243a
JV
82 $block[0]['info'] = t('Most emailed');
83 return $block;
99e463d7
JV
84}
85
86/**
87 * Implementation of hook_block_view().
88 */
89function print_mail_block_view($delta = 0) {
855b243a
JV
90 switch ($delta) {
91 case 0:
92 $block['subject'] = t('Most emailed');
93 $result = db_query_range("SELECT path FROM {print_mail_page_counter} ORDER BY sentcount DESC", 0, 3)
94 ->fetchAll();
95 if (count($result)) {
96 $block['content'] = '<div class="item-list"><ul>';
97 foreach ($result as $obj) {
fe25f54b 98 $block['content'] .= '<li>' . l(_print_get_title($obj->path), $obj->path) . '</li>';
855b243a
JV
99 }
100 $block['content'] .= '</ul></div>';
101 }
102 break;
103 }
104 return $block;
99e463d7
JV
105}
106
107/**
dd0fda21 108 * Implementation of hook_node_view().
c889e2e2 109 */
dd0fda21 110function print_mail_node_view($node, $teaser) {
a8222e12 111 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT => PRINT_MAIL_LINK_POS_DEFAULT));
c0bede24 112 $print_mail_link_use_alias = variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT);
99e463d7 113 $allowed_type = print_mail_link_allowed(array('type' => 'node', 'node' => $node, 'teaser' => $teaser));
a8222e12 114 if (($allowed_type) && !empty($print_mail_link_pos['link'])) {
fe25f54b 115 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
c889e2e2
JV
116 $links = array();
117 $format = theme('print_mail_format_link');
3da84173 118
c889e2e2 119 // Show book link
dd92f6dd 120 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
fe25f54b 121 $links['book_mail'] = array('href' => PRINTMAIL_PATH . '/book/export/html/' . $node->nid,
60739788 122 'title' => $format['text'],
c889e2e2 123 'attributes' => $format['attributes'],
60739788 124 'html' => $format['html'],
f96caeed 125 );
c889e2e2 126 }
dd92f6dd 127 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) {
c889e2e2 128 $query_arr = $_GET;
99e463d7 129 if ('node' == 'comment') {
c889e2e2
JV
130 $query_arr['comment'] = $node->cid;
131 }
973f5b72 132 $query = print_query_string_encode($query_arr, array('q'));
3dc1cef5 133 if (empty($query)) $query = NULL;
c889e2e2 134
c0bede24 135 if ($print_mail_link_use_alias) {
fe25f54b 136 $path = drupal_get_path_alias('node/' . $node->nid);
c0bede24
JV
137 }
138 else {
139 $path = $node->nid;
140 }
141
fe25f54b 142 $links['print_mail'] = array('href' => PRINTMAIL_PATH . '/' . $path,
c889e2e2
JV
143 'title' => $format['text'],
144 'attributes' => $format['attributes'],
145 'html' => $format['html'],
146 'query' => $query,
f96caeed 147 );
c889e2e2 148 }
c889e2e2 149
99e463d7
JV
150 $node->content['links']['print_mail'] = array(
151 '#type' => 'node_links',
152 '#value' => $links,
153 );
c889e2e2 154 }
c889e2e2 155
dd30da23
JV
156 // Insert content corner links
157 if (($teaser === FALSE) && !empty($print_mail_link_pos['corner']) &&
a01ab7bd 158 isset($node->build_mode) && ($node->build_mode == NODE_BUILD_NORMAL)) {
dd30da23
JV
159 $link = print_mail_insert_link(NULL, $node);
160 if ($link) {
f76a10f2 161 $node->content['print_links']['#markup'] = preg_replace('!</span>$!', $link . '</span>', $node->content['print_links']['#markup']);
dd30da23
JV
162 }
163 }
164}
165
166/**
167 * Implementation of hook_help().
168 */
169function print_mail_help($path, $arg) {
170 $print_mail_link_pos = variable_get('print_mail_link_pos', array(PRINT_MAIL_LINK_POS_DEFAULT => PRINT_MAIL_LINK_POS_DEFAULT));
f76a10f2 171 if (($path !== 'node/%') && !(empty($print_mail_link_pos['link']) && empty($print_mail_link_pos['corner']))) {
dd30da23
JV
172 static $output = FALSE;
173
174 if ($output === FALSE) {
175 $output = TRUE;
176
177 $link = print_mail_insert_link();
178 if ($link) {
179 return "<span class='print-syslink'>$link</span>";
8c99bfd6 180 }
dd30da23
JV
181 }
182 }
8c99bfd6
JV
183}
184
185/**
dd0fda21 186 * Implementation of hook_node_load().
855b243a 187 */
dd0fda21 188function print_mail_node_load($nodes, $types) {
129cef51
JV
189 foreach ($nodes as $node) {
190 _print_mail_set_node_fields($node);
191 }
855b243a
JV
192}
193
194/**
dd0fda21 195 * Implementation of hook_node_update().
855b243a 196 */
dd0fda21 197function print_mail_node_update($node) {
047e5681
JV
198 if (user_access('administer print') || user_access('node-specific print configuration')) {
199 _print_mail_node_conf_modify($node->nid, $node->print_mail_display, $node->print_mail_display_comment, $node->print_mail_display_urllist);
200 }
855b243a
JV
201}
202
203/**
dd0fda21 204 * Implementation of hook_node_delete().
855b243a 205 */
dd0fda21 206function print_mail_node_delete($node) {
855b243a
JV
207 db_delete('print_mail_node_conf')
208 ->condition('nid', $node->nid)
209 ->execute();
210 db_delete('print_mail_page_counter')
211 ->condition('path', $node->path)
212 ->execute();
213}
214
215/**
c889e2e2
JV
216 * Implementation of hook_form_alter().
217 */
218function print_mail_form_alter(&$form, $form_state, $form_id) {
219 // Add the node-type settings option to activate the mail version link
fe25f54b 220 if ((user_access('administer print') || user_access('node-specific print configuration')) &&
a43c9735 221 (($form_id == 'node_type_form') || !empty($form['#node_edit_form']))) {
855b243a
JV
222 $form['print']['mail_label'] = array(
223 '#type' => 'markup',
fe25f54b 224 '#markup' => '<p><strong>' . t('Send by e-mail') . '</strong></p>',
855b243a
JV
225 );
226
2597a0ee 227 $form['print']['print_mail_display'] = array(
c889e2e2 228 '#type' => 'checkbox',
855b243a 229 '#title' => t('Show link'),
c889e2e2 230 );
2597a0ee 231 $form['print']['print_mail_display_comment'] = array(
c889e2e2 232 '#type' => 'checkbox',
855b243a 233 '#title' => t('Show link in individual comments'),
c889e2e2 234 );
855b243a
JV
235 $form['print']['print_mail_display_urllist'] = array(
236 '#type' => 'checkbox',
237 '#title' => t('Show Printer-friendly URLs list'),
238 );
239
240 if ($form_id == 'node_type_form') {
fe25f54b
JV
241 $form['print']['print_mail_display']['#default_value'] = variable_get('print_mail_display_' . $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
242 $form['print']['print_mail_display_comment']['#default_value'] = variable_get('print_mail_display_comment_' . $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
243 $form['print']['print_mail_display_urllist']['#default_value'] = variable_get('print_mail_display_urllist_' . $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
855b243a
JV
244 }
245 else {
98df4e4a
JV
246 $node = $form['#node'];
247 $form['print']['print_mail_display']['#default_value'] = isset($node->print_mail_display) ? $node->print_mail_display : PRINT_TYPE_SHOW_LINK_DEFAULT;
248 $form['print']['print_mail_display_comment']['#default_value'] = isset($node->print_mail_display_comment) ? $node->print_mail_display_comment : PRINT_TYPE_COMMENT_LINK_DEFAULT;
249 $form['print']['print_mail_display_urllist']['#default_value'] = isset($node->print_mail_display_urllist) ? $node->print_mail_display_urllist : PRINT_TYPE_URLLIST_DEFAULT;
855b243a
JV
250 }
251 }
252}
253
254/**
255 * Auxiliary function to assign the per-node settings to the node object fields
256 *
257 * @param $node
258 * node to be modified
259 */
260function _print_mail_set_node_fields(&$node) {
48692886
JV
261 if (isset($node->nid)) {
262 $res = db_query("SELECT link, comments, url_list FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
fe25f54b 263 ->fetch();
48692886
JV
264 }
265 else {
266 $res = FALSE;
267 }
855b243a
JV
268 $node->print_mail_display = $res ? intval($res->link) : PRINT_TYPE_SHOW_LINK_DEFAULT;
269 $node->print_mail_display_comment = $res ? intval($res->comments) : PRINT_TYPE_COMMENT_LINK_DEFAULT;
270 $node->print_mail_display_urllist = $res ? intval($res->url_list) : PRINT_TYPE_URLLIST_DEFAULT;
271}
272
273/**
274 * Update the print_mail_node_conf table to reflect the given attributes
275 * If updating to the default values, delete the record.
276 *
277 * @param $nid
278 * value of the nid field (primary key)
279 * @param $link
280 * value of the link field (0 or 1)
281 * @param $comments
282 * value of the comments field (0 or 1)
283 * @param $url_list
284 * value of the url_list field (0 or 1)
285 */
286function _print_mail_node_conf_modify($nid, $link, $comments, $url_list) {
287 if (($link == PRINT_TYPE_SHOW_LINK_DEFAULT) && ($comments == PRINT_TYPE_COMMENT_LINK_DEFAULT) &&
288 ($url_list == PRINT_TYPE_URLLIST_DEFAULT)) {
289 db_delete('print_mail_node_conf')
290 ->condition('nid', $nid)
291 ->execute();
292 }
293 else {
294 db_merge('print_mail_node_conf')
295 ->key(array('nid' => $nid))
296 ->fields(array(
297 'link' => $link,
298 'comments' => $comments,
299 'url_list' => $url_list,
300 ))
301 ->execute();
c889e2e2
JV
302 }
303}
304
305/**
306 * Format the send by e-mail link
307 *
308 * @return
309 * array of formatted attributes
310 * @ingroup themeable
311 */
312function theme_print_mail_format_link() {
7d62e7c4
JV
313 $print_mail_link_class = variable_get('print_mail_link_class', PRINT_MAIL_LINK_CLASS_DEFAULT);
314 $print_mail_show_link = variable_get('print_mail_show_link', PRINT_MAIL_SHOW_LINK_DEFAULT);
855b243a 315 $print_mail_link_text = variable_get('print_mail_link_text', t('Send to friend'));
7d62e7c4 316
fe25f54b 317 $img = drupal_get_path('module', 'print') . '/icons/mail_icon.gif';
c889e2e2 318 $title = t('Send this page by e-mail.');
7d62e7c4
JV
319 $class = strip_tags($print_mail_link_class);
320 $new_window = FALSE;
855b243a 321 $format = _print_format_link_aux($print_mail_show_link, $print_mail_link_text, $img);
c889e2e2
JV
322
323 return array('text' => $format['text'],
324 'html' => $format['html'],
325 'attributes' => print_fill_attributes($title, $class, $new_window),
326 );
327}
328
329/**
330 * Auxiliary function to display a formatted send by e-mail link
331 *
e60934b7
JV
332 * Function made available so that developers may call this function from
333 * their defined pages/blocks.
334 *
335 * @param $path
336 * path of the original page (optional). If not specified, the current URL
337 * is used
338 * @param $node
339 * an optional node object, to be used in defining the path, if used, the
340 * path argument is irrelevant
341 * @return
342 * string with the HTML link to the printer-friendly page
c889e2e2 343 */
e60934b7
JV
344function print_mail_insert_link($path = NULL, $node = NULL) {
345 if ($node !== NULL) {
346 $nid = $node->nid;
fe25f54b 347 $path = 'node/' . $nid;
e60934b7
JV
348 $allowed_type = print_mail_link_allowed(array('node' => $node));
349 }
350 else {
c889e2e2 351 if ($path === NULL) {
e60934b7
JV
352 $nid = preg_replace('!^node/!', '', $_GET['q']);
353 $path = $_GET['q'];
354 }
355 else {
356 $nid = NULL;
357 }
358 $allowed_type = print_mail_link_allowed(array('path' => $path));
359 }
c0bede24 360
e60934b7
JV
361 if ($allowed_type) {
362 if ($nid !== NULL) {
363 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
fe25f54b 364 $path = 'book/export/html/' . $nid;
c0bede24
JV
365 }
366 else {
e60934b7
JV
367 if (variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT)) {
368 $path = drupal_get_path_alias($path);
369 }
370 else {
371 $path = $nid;
372 }
c0bede24 373 }
fe25f54b 374 $path = PRINTMAIL_PATH . '/' . $path;
973f5b72 375 $query = print_query_string_encode($_GET, array('q'));
c889e2e2
JV
376 if (empty($query)) {
377 $query = NULL;
378 }
379 }
730a39bb
JV
380 else {
381 $query = NULL;
382 }
fe25f54b 383 drupal_add_css(drupal_get_path('module', 'print') . '/css/printlinks.css');
c889e2e2 384 $format = theme('print_mail_format_link');
fe25f54b 385 return '<span class="print_mail">' . l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) . '</span>';
c889e2e2 386 }
e60934b7
JV
387 else {
388 return FALSE;
389 }
c889e2e2 390}
dd92f6dd
JV
391
392/**
393 * Determine a the link to send by e-mail is allowed depending on all possible settings
394 *
395 * @param $args
396 * array containing the possible parameters:
397 * teaser, node, type, path
398 * @return
399 * FALSE if not allowed
400 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
401 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
402 */
403function print_mail_link_allowed($args) {
b698536b 404 if (!empty($args['teaser']) || !user_access('access print')) {
dd92f6dd
JV
405 // If showing only the teaser or the user is not allowed or link is disabled
406 return FALSE;
407 }
e60934b7
JV
408 if (!empty($args['path'])) {
409 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
410 if (is_numeric($nid)) {
f0e49fdd 411 $args['node'] = node_load($nid);
e60934b7
JV
412 }
413 }
b698536b 414 if (!empty($args['node'])) {
dd92f6dd 415 static $node_type = FALSE;
742db343
JV
416
417 $node = $args['node'];
adb25444
JV
418 if ($node_type === FALSE) {
419 if (isset($node->type)) {
420 $node_type = $node->type;
421 }
422 else {
423 $node_type = '';
424 }
dd92f6dd
JV
425 }
426 // Node
427 $print_mail_node_link_visibility = variable_get('print_mail_node_link_visibility', PRINT_MAIL_NODE_LINK_VISIBILITY_DEFAULT);
428 $print_mail_node_link_pages = variable_get('print_mail_node_link_pages', PRINT_MAIL_NODE_LINK_PAGES_DEFAULT);
429
a01ab7bd 430 if ((isset($node->build_mode) && ($node->build_mode == NODE_BUILD_PRINT)) ||
dd92f6dd
JV
431 !_print_page_match($print_mail_node_link_visibility, $print_mail_node_link_pages)) {
432 // Page not in visibility list or we are working!
433 return FALSE;
434 }
b698536b 435 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
dd92f6dd 436 // Link is for a comment, return the configured setting
855b243a 437 $res = db_query("SELECT comments FROM {print_mail_node_conf} WHERE nid = :nid", array(':nid' => $node->nid))
fe25f54b 438 ->fetch();
855b243a
JV
439 $print_display_comment = $res ? intval($res->comments) : PRINT_TYPE_COMMENT_LINK_DEFAULT;
440 if (($print_display_comment) ||
fe25f54b 441 variable_get('print_mail_display_comment_' . $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT)) {
855b243a
JV
442 return PRINT_ALLOW_NORMAL_LINK;
443 }
dd92f6dd
JV
444 }
445 else {
446 // Node link
855b243a 447 if ((!$node->print_mail_display) || (isset($node_type) &&
fe25f54b 448 !variable_get('print_mail_display_' . $node_type, PRINT_TYPE_SHOW_LINK_DEFAULT))) {
dd92f6dd
JV
449 // Link for this node type is disabled
450 return FALSE;
451 }
e02f16ac 452 elseif (isset($node->book)) {
dd92f6dd
JV
453 // Node is a book;
454 $print_mail_book_link = variable_get('print_mail_book_link', PRINT_MAIL_BOOK_LINK_DEFAULT);
c64555f4
JV
455 switch ($print_mail_book_link) {
456 case 1:
457 if (user_access('access printer-friendly version')) {
458 return PRINT_ALLOW_BOOK_LINK;
459 }
460 break;
461 case 2:
462 return PRINT_ALLOW_NORMAL_LINK;
dd92f6dd
JV
463 }
464 }
465 else {
466 return PRINT_ALLOW_NORMAL_LINK;
467 }
468 }
469 }
470 else {
471 // 'System' page
472 $print_mail_sys_link_visibility = variable_get('print_mail_sys_link_visibility', PRINT_MAIL_SYS_LINK_VISIBILITY_DEFAULT);
473 $print_mail_sys_link_pages = variable_get('print_mail_sys_link_pages', PRINT_MAIL_SYS_LINK_PAGES_DEFAULT);
474
475 return _print_page_match($print_mail_sys_link_visibility, $print_mail_sys_link_pages);
476 }
c64555f4 477 return FALSE;
dd92f6dd 478}