Use new node_load argument interface
[project/print.git] / print_mail / print_mail.module
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Displays Printer-friendly versions of Drupal pages.
7 */
8
9 define('PRINTMAIL_PATH', 'printmail');
10
11 define('PRINT_MAIL_FORMAT', 'mail');
12
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);
25
26 /**
27 * Implementation of hook_theme().
28 */
29 function print_mail_theme() {
30 return array(
31 'print_mail_format_link' => array(
32 'arguments' => array(),
33 ),
34 'print_mail_form' => array(
35 'arguments' => array('form'),
36 ),
37 );
38 }
39
40 /**
41 * Implementation of hook_menu().
42 */
43 function print_mail_menu() {
44 $items = array();
45
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,
52 );
53 $items['admin/settings/print/email'] = array(
54 'title' => 'e-mail',
55 'page callback' => 'drupal_get_form',
56 'page arguments' => array('print_mail_settings'),
57 'access arguments' => array('administer print'),
58 'weight' => 2,
59 'type' => MENU_LOCAL_TASK,
60 );
61
62 return $items;
63 }
64
65 /**
66 * Implementation of hook_block().
67 */
68 function print_mail_block($op = 'list', $delta = 0, $edit = array()) {
69 switch ($op) {
70 case 'list':
71 $block[0]['info'] = t('Most emailed');
72 return $block;
73 break;
74 case 'configure':
75 return '';
76 case 'save':
77 return;
78 case 'view':
79 switch ($delta) {
80 case 0:
81 $block['subject'] = t('Most emailed');
82 $result = db_query_range("SELECT path FROM {print_mail_page_counter} ORDER BY sentcount DESC", 0, 3)
83 ->fetchAll();
84 if (count($result)) {
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>';
88 }
89 $block['content'] .= '</ul></div>';
90 }
91 break;
92 }
93 return $block;
94 break;
95 }
96 }
97
98 /**
99 * Implementation of hook_link().
100 */
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');
107 $links = array();
108 $format = theme('print_mail_format_link');
109
110 // Show book 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'],
116 );
117
118 return $links;
119 }
120 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) {
121 $query_arr = $_GET;
122 if ($type == 'comment') {
123 $query_arr['comment'] = $node->cid;
124 }
125 $query = print_query_string_encode($query_arr, array('q'));
126 if (empty($query)) $query = NULL;
127
128 if ($print_mail_link_use_alias) {
129 $path = drupal_get_path_alias('node/'. $node->nid);
130 }
131 else {
132 $path = $node->nid;
133 }
134
135 $links['print_mail'] = array('href' => PRINTMAIL_PATH .'/'. $path,
136 'title' => $format['text'],
137 'attributes' => $format['attributes'],
138 'html' => $format['html'],
139 'query' => $query,
140 );
141
142 return $links;
143 }
144 }
145 else {
146 return;
147 }
148 }
149
150 /**
151 * Implementation of hook_help().
152 */
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;
158
159 if ($output === FALSE) {
160 $output = TRUE;
161
162 $link = print_mail_insert_link();
163 if ($link) {
164 return "<span class='print-syslink'>$link</span>";
165 }
166 }
167 }
168 }
169
170 /**
171 * Implementation of hook_nodeapi_view().
172 */
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);
177 }
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);
182 if ($link) {
183 $node->content['print_mail_link'] = array(
184 '#markup' => "<span class='print-link'>$link</span>",
185 '#weight' => -102,
186 );
187 }
188 }
189 }
190
191 /**
192 * Implementation of hook_nodeapi_prepare().
193 */
194 function print_mail_nodeapi_prepare(&$node, $teaser, $page) {
195 _print_mail_set_node_fields($node);
196 }
197
198 /**
199 * Implementation of hook_nodeapi_update().
200 */
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);
203 }
204
205 /**
206 * Implementation of hook_nodeapi_delete().
207 */
208 function print_mail_nodeapi_delete(&$node, $teaser, $page) {
209 db_delete('print_mail_node_conf')
210 ->condition('nid', $node->nid)
211 ->execute();
212 db_delete('print_mail_page_counter')
213 ->condition('path', $node->path)
214 ->execute();
215 }
216
217 /**
218 * Implementation of hook_form_alter().
219 */
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(
224 '#type' => 'markup',
225 '#markup' => '<p><strong>'. t('Send by e-mail') .'</strong></p>',
226 );
227
228 $form['print']['print_mail_display'] = array(
229 '#type' => 'checkbox',
230 '#title' => t('Show link'),
231 );
232 $form['print']['print_mail_display_comment'] = array(
233 '#type' => 'checkbox',
234 '#title' => t('Show link in individual comments'),
235 );
236 $form['print']['print_mail_display_urllist'] = array(
237 '#type' => 'checkbox',
238 '#title' => t('Show Printer-friendly URLs list'),
239 );
240
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);
245 }
246 else {
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;
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 */
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))
262 ->fetch();
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;
266 }
267
268 /**
269 * Update the print_mail_node_conf table to reflect the given attributes
270 * If updating to the default values, delete the record.
271 *
272 * @param $nid
273 * value of the nid field (primary key)
274 * @param $link
275 * value of the link field (0 or 1)
276 * @param $comments
277 * value of the comments field (0 or 1)
278 * @param $url_list
279 * value of the url_list field (0 or 1)
280 */
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)
286 ->execute();
287 }
288 else {
289 db_merge('print_mail_node_conf')
290 ->key(array('nid' => $nid))
291 ->fields(array(
292 'link' => $link,
293 'comments' => $comments,
294 'url_list' => $url_list,
295 ))
296 ->execute();
297 }
298 }
299
300 /**
301 * Format the send by e-mail link
302 *
303 * @return
304 * array of formatted attributes
305 * @ingroup themeable
306 */
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'));
311
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);
315 $new_window = FALSE;
316 $format = _print_format_link_aux($print_mail_show_link, $print_mail_link_text, $img);
317
318 return array('text' => $format['text'],
319 'html' => $format['html'],
320 'attributes' => print_fill_attributes($title, $class, $new_window),
321 );
322 }
323
324 /**
325 * Auxiliary function to display a formatted send by e-mail link
326 *
327 * Function made available so that developers may call this function from
328 * their defined pages/blocks.
329 *
330 * @param $path
331 * path of the original page (optional). If not specified, the current URL
332 * is used
333 * @param $node
334 * an optional node object, to be used in defining the path, if used, the
335 * path argument is irrelevant
336 * @return
337 * string with the HTML link to the printer-friendly page
338 */
339 function print_mail_insert_link($path = NULL, $node = NULL) {
340 if ($node !== NULL) {
341 $nid = $node->nid;
342 $path = 'node/'. $nid;
343 $allowed_type = print_mail_link_allowed(array('node' => $node));
344 }
345 else {
346 if ($path === NULL) {
347 $nid = preg_replace('!^node/!', '', $_GET['q']);
348 $path = $_GET['q'];
349 }
350 else {
351 $nid = NULL;
352 }
353 $allowed_type = print_mail_link_allowed(array('path' => $path));
354 }
355
356 if ($allowed_type) {
357 if ($nid !== NULL) {
358 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
359 $path = 'book/export/html/'. $nid;
360 }
361 else {
362 if (variable_get('print_mail_link_use_alias', PRINT_MAIL_LINK_USE_ALIAS_DEFAULT)) {
363 $path = drupal_get_path_alias($path);
364 }
365 else {
366 $path = $nid;
367 }
368 }
369 $path = PRINTMAIL_PATH .'/'. $path;
370 $query = print_query_string_encode($_GET, array('q'));
371 if (empty($query)) {
372 $query = NULL;
373 }
374 }
375 else {
376 $query = NULL;
377 }
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>';
381 }
382 else {
383 return FALSE;
384 }
385 }
386
387 /**
388 * Determine a the link to send by e-mail is allowed depending on all possible settings
389 *
390 * @param $args
391 * array containing the possible parameters:
392 * teaser, node, type, path
393 * @return
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
397 */
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
401 return FALSE;
402 }
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);
407 }
408 }
409 if (!empty($args['node'])) {
410 static $node_type = FALSE;
411
412 $node = $args['node'];
413 if ($node_type === FALSE) {
414 if (isset($node->type)) {
415 $node_type = $node->type;
416 }
417 else {
418 $node_type = '';
419 }
420 }
421 // Node
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);
424
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!
428 return FALSE;
429 }
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))
433 ->fetch();
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;
438 }
439 }
440 else {
441 // Node 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
445 return FALSE;
446 }
447 elseif (isset($node->book)) {
448 // Node is a book;
449 $print_mail_book_link = variable_get('print_mail_book_link', PRINT_MAIL_BOOK_LINK_DEFAULT);
450 switch ($print_mail_book_link) {
451 case 1:
452 if (user_access('access printer-friendly version')) {
453 return PRINT_ALLOW_BOOK_LINK;
454 }
455 break;
456 case 2:
457 return PRINT_ALLOW_NORMAL_LINK;
458 }
459 }
460 else {
461 return PRINT_ALLOW_NORMAL_LINK;
462 }
463 }
464 }
465 else {
466 // 'System' page
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);
469
470 return _print_page_match($print_mail_sys_link_visibility, $print_mail_sys_link_pages);
471 }
472 return FALSE;
473 }