Add some help text to content-type settings to explain that the module-wide settings...
[project/print.git] / print_pdf / print_pdf.module
CommitLineData
40cbc88e 1<?php
e9b068d1 2// $Id$
40cbc88e
JV
3
4/**
5 * @file
d8ea68f9 6 * Displays Printer-friendly versions of Drupal pages.
40cbc88e
JV
7 */
8
9include_once('print_pdf.pages.inc');
10include_once('print_pdf.admin.inc');
11
12define('PRINTPDF_PATH', 'printpdf');
13
6ea54f45
JV
14define('PRINT_PDF_FORMAT', 'pdf');
15
40cbc88e
JV
16define('PRINT_PDF_LINK_POS_DEFAULT', 'link');
17define('PRINT_PDF_SHOW_LINK_DEFAULT', 1);
18define('PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT', 0);
19define('PRINT_PDF_NODE_LINK_PAGES_DEFAULT', '');
20define('PRINT_PDF_LINK_CLASS_DEFAULT', 'print-pdf');
21define('PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT', 1);
22define('PRINT_PDF_SYS_LINK_PAGES_DEFAULT', '');
23define('PRINT_PDF_LINK_USE_ALIAS_DEFAULT', 0);
24define('PRINT_PDF_BOOK_LINK_DEFAULT', 1);
25define('PRINT_PDF_PDF_TOOL_DEFAULT', 0);
26define('PRINT_PDF_CONTENT_DISPOSITION_DEFAULT', 2);
27define('PRINT_PDF_PAPER_SIZE_DEFAULT', 'A4');
28define('PRINT_PDF_PAGE_ORIENTATION_DEFAULT', 'portrait');
75237f54 29define('PRINT_PDF_AUTOCONFIG_DEFAULT', 1);
40cbc88e
JV
30define('PRINT_PDF_FONT_FAMILY_DEFAULT', 'dejavusans');
31define('PRINT_PDF_FONT_SIZE_DEFAULT', 10);
6814740a 32define('PRINT_PDF_FILENAME_DEFAULT', '[site-name] - [title] - [mod-yyyy]-[mod-mm]-[mod-dd]');
b08256cd
JV
33define('PRINT_PDF_WKHTMLTOPDF_OPTIONS', "--footer-font-size 7 --footer-right '[page]'");
34define('PRINT_PDF_XVFB_OPTIONS', '');
35// define('PRINT_PDF_XVFB_OPTIONS', ' -fp /usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,/usr/share/fonts/X11/100dpi/');
40cbc88e
JV
36
37/**
38 * Implementation of hook_menu().
39 */
40function print_pdf_menu($may_cache) {
41 $items = array();
42
43 if ($may_cache) {
44 $items[] = array(
45 'path' => PRINTPDF_PATH,
46 'title' => t('Printer-friendly PDF'),
47 'callback' => 'print_pdf_controller',
48 'access' => user_access('access print'),
49 'type' => MENU_CALLBACK,
50 );
51 $items[] = array(
52 'path' => 'admin/settings/print/pdf',
53 'title' => t('PDF'),
54 'callback' => 'drupal_get_form',
55 'callback arguments' => array('print_pdf_settings'),
56 'access' => user_access('administer print'),
57 'weight' => 3,
58 'type' => MENU_LOCAL_TASK,
59 );
2e711b51
JV
60 $items[] = array(
61 'path' => 'admin/settings/print/pdf/options',
62 'title' => t('Options'),
63 'weight' => 1,
64 'type' => MENU_DEFAULT_LOCAL_TASK,
65 );
66 $items[] = array(
67 'path' => 'admin/settings/print/pdf/strings',
68 'title' => t('Text strings'),
69 'callback' => 'drupal_get_form',
70 'callback arguments' => array('print_pdf_strings_settings'),
71 'access' => array('administer print'),
72 'weight' => 2,
73 'type' => MENU_LOCAL_TASK,
74 );
40cbc88e
JV
75 }
76
77 return $items;
78}
79
80/**
6ea54f45
JV
81 * Implementation of hook_block().
82 */
83function print_pdf_block($op = 'list', $delta = 0, $edit = array()) {
84 switch ($op) {
85 case 'list':
86 $block[0]['info'] = t('Most PDFd');
87 return $block;
88 break;
89 case 'configure':
90 return '';
91 case 'save':
92 return;
93 case 'view':
94 switch ($delta) {
95 case 0:
568392c9 96 $block['subject'] = t('Most PDFd');
6ea54f45
JV
97 $result = db_query_range("SELECT path FROM {print_pdf_page_counter} ORDER BY totalcount DESC", 0, 3);
98 if (db_affected_rows()) {
99 $block['content'] = '<div class="item-list"><ul>';
100 while ($obj = db_fetch_object($result)) {
101 $block['content'] .= '<li>'. l(_print_get_title($obj->path), $obj->path) .'</li>';
102 }
103 $block['content'] .= '</ul></div>';
6ea54f45
JV
104 }
105 break;
106 }
107 return $block;
108 break;
109 }
110}
111
112/**
40cbc88e
JV
113 * Implementation of hook_requirements().
114 */
115function print_pdf_requirements($phase) {
116 $requirements = array();
117 $t = get_t();
118 switch ($phase) {
119 // At runtime, make sure that a PDF generation tool is selected
120 case 'runtime':
121 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
122 if (empty($print_pdf_pdf_tool)) {
123 $requirements['print_pdf_tool'] = array(
3ca90137
JV
124 'title' => $t('PDF version'),
125 'value' => $t('No PDF tool selected'),
126 'description' => $t('Please configure it in the <a href="@url">PDF settings page</a>.', array('@url' => url('admin/settings/print/pdf'))),
40cbc88e
JV
127 'severity' => REQUIREMENT_ERROR,
128 );
129 }
130 break;
131 }
132 return $requirements;
133}
134
135/**
136 * Implementation of hook_link().
137 */
138function print_pdf_link($type, $node = NULL, $teaser = FALSE) {
139 $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT));
140 $print_pdf_link_use_alias = variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT);
141 $allowed_type = print_pdf_link_allowed(array('type' => $type, 'node' => $node, 'teaser' => $teaser));
142 if (($allowed_type) && !empty($print_pdf_link_pos['link'])) {
143 drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css');
144 $links = array();
145 $format = theme('print_pdf_format_link');
146
147 // Show book link
148 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
149 $links['book_pdf'] = array('href' => PRINTPDF_PATH .'/book/export/html/'. $node->nid,
150 'title' => $format['text'],
151 'attributes' => $format['attributes'],
152 'html' => $format['html'],
e9b068d1 153 );
40cbc88e
JV
154
155 return $links;
156 }
157 elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) {
158 $query_arr = $_GET;
159 if ($type == 'comment') {
160 $query_arr['comment'] = $node->cid;
161 }
162 $query = print_query_string_encode($query_arr, array('q'));
163 if (empty($query)) $query = NULL;
164
165 if ($print_pdf_link_use_alias) {
166 $path = drupal_get_path_alias('node/'. $node->nid);
167 }
168 else {
169 $path = $node->nid;
170 }
171
172 $links['print_pdf'] = array('href' => PRINTPDF_PATH .'/'. $path,
173 'title' => $format['text'],
174 'attributes' => $format['attributes'],
175 'html' => $format['html'],
176 'query' => $query,
e9b068d1 177 );
40cbc88e
JV
178
179 return $links;
180 }
181 }
d8ea68f9
JV
182 else {
183 return;
184 }
40cbc88e
JV
185}
186
187/**
188 * Implementation of hook_help().
189 */
190function print_pdf_help($path) {
191 $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT));
0e41addb 192 if ((preg_match('!^node/\d+$!', $path) == 0) && !(empty($print_pdf_link_pos['link']) && empty($print_pdf_link_pos['corner']))) {
40cbc88e
JV
193 static $output = FALSE;
194
195 if ($output === FALSE) {
40cbc88e 196 $output = TRUE;
d8ea68f9
JV
197
198 $link = print_pdf_insert_link();
199 if ($link) {
200 return "<span class='print-syslink'>$link</span>";
201 }
40cbc88e
JV
202 }
203 }
204}
205
206/**
207 * Implementation of hook_nodeapi().
208 */
209function print_pdf_nodeapi(&$node, $op = 'view', $teaser, $page) {
210 switch ($op) {
211 case 'view':
6ea54f45 212 // Insert content corner links
40cbc88e 213 $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT));
0e2f03b1 214 if (($teaser === FALSE) && !empty($print_pdf_link_pos['corner']) && !isset($node->printing)) {
d8ea68f9
JV
215 $link = print_pdf_insert_link(NULL, $node);
216 if ($link) {
e09bb0e2 217 $node->content['print_links']['#value'] = preg_replace('!</span>$!', $link .'</span>', $node->content['print_links']['#value']);
40cbc88e 218 }
40cbc88e 219 }
6a20a915 220 break;
149e2ffa 221 case 'load':
6a20a915 222 _print_pdf_set_node_fields($node);
6ea54f45
JV
223 break;
224 case 'update':
6b632dfd 225 if (user_access('administer print') || user_access('node-specific print configuration')) {
6ea54f45 226 _print_pdf_node_conf_modify($node->nid, $node->print_pdf_display, $node->print_pdf_display_comment, $node->print_pdf_display_urllist);
6b632dfd 227 }
6ea54f45
JV
228 break;
229 case 'delete':
230 db_query("DELETE FROM {print_pdf_node_conf} WHERE nid = %d", $node->nid);
abd96759 231 db_query("DELETE FROM {print_pdf_page_counter} WHERE path = 'node/%d'", $node->nid);
6ea54f45 232 break;
40cbc88e
JV
233 }
234}
235
236/**
237 * Implementation of hook_form_alter().
238 */
239function print_pdf_form_alter($form_id, &$form) {
240 // Add the node-type settings option to activate the PDF version link
d39d0535
JV
241 if ((user_access('administer print') || user_access('node-specific print configuration')) && (($form_id == 'node_type_form') ||
242 (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id))) {
6ea54f45
JV
243 $form['print']['pdf_label'] = array(
244 '#type' => 'markup',
245 '#value' => '<p><strong>'. t('PDF version') .'</strong></p>',
246 );
247
40cbc88e
JV
248 $form['print']['print_pdf_display'] = array(
249 '#type' => 'checkbox',
6ea54f45 250 '#title' => t('Show link'),
40cbc88e
JV
251 );
252 $form['print']['print_pdf_display_comment'] = array(
253 '#type' => 'checkbox',
6ea54f45 254 '#title' => t('Show link in individual comments'),
40cbc88e 255 );
6ea54f45
JV
256 $form['print']['print_pdf_display_urllist'] = array(
257 '#type' => 'checkbox',
258 '#title' => t('Show Printer-friendly URLs list'),
259 );
260
261 if ($form_id == 'node_type_form') {
262 $form['print']['print_pdf_display']['#default_value'] = variable_get('print_pdf_display_'. $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
263 $form['print']['print_pdf_display_comment']['#default_value'] = variable_get('print_pdf_display_comment_'. $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
264 $form['print']['print_pdf_display_urllist']['#default_value'] = variable_get('print_pdf_display_urllist_'. $form['#node_type']->type, PRINT_TYPE_URLLIST_DEFAULT);
265 }
266 else {
2e711b51
JV
267 $node = $form['#node'];
268 $form['print']['print_pdf_display']['#default_value'] = isset($node->print_pdf_display) ? $node->print_pdf_display : PRINT_TYPE_SHOW_LINK_DEFAULT;
269 $form['print']['print_pdf_display_comment']['#default_value'] = isset($node->print_pdf_display_comment) ? $node->print_pdf_display_comment : PRINT_TYPE_COMMENT_LINK_DEFAULT;
270 $form['print']['print_pdf_display_urllist']['#default_value'] = isset($node->print_pdf_display_urllist) ? $node->print_pdf_display_urllist : PRINT_TYPE_URLLIST_DEFAULT;
6ea54f45
JV
271 }
272 }
273}
274
275/**
6a20a915
JV
276 * Auxiliary function to assign the per-node settings to the node object fields
277 *
278 * @param $node
279 * node to be modified
280 */
281function _print_pdf_set_node_fields(&$node) {
30502d83
JV
282 if (isset($node->nid)) {
283 $res = db_fetch_object(db_query("SELECT link, comments, url_list FROM {print_pdf_node_conf} WHERE nid = %d", $node->nid));
284 }
285 else {
286 $res = FALSE;
287 }
6a20a915
JV
288 $node->print_pdf_display = $res ? intval($res->link) : PRINT_TYPE_SHOW_LINK_DEFAULT;
289 $node->print_pdf_display_comment = $res ? intval($res->comments) : PRINT_TYPE_COMMENT_LINK_DEFAULT;
290 $node->print_pdf_display_urllist = $res ? intval($res->url_list) : PRINT_TYPE_URLLIST_DEFAULT;
291}
292
293/**
6ea54f45
JV
294 * Update the print_pdf_node_conf table to reflect the given attributes
295 * If updating to the default values, delete the record.
296 *
297 * @param $nid
298 * value of the nid field (primary key)
299 * @param $link
300 * value of the link field (0 or 1)
301 * @param $comments
302 * value of the comments field (0 or 1)
303 * @param $url_list
304 * value of the url_list field (0 or 1)
305 */
306function _print_pdf_node_conf_modify($nid, $link, $comments, $url_list) {
307 if (($link == PRINT_TYPE_SHOW_LINK_DEFAULT) && ($comments == PRINT_TYPE_COMMENT_LINK_DEFAULT) &&
308 ($url_list == PRINT_TYPE_URLLIST_DEFAULT)) {
309 db_query("DELETE FROM {print_pdf_node_conf} WHERE nid = %d", $nid);
310 }
311 else {
312 db_query("UPDATE {print_pdf_node_conf} SET link = %d, comments = %d, url_list = %d WHERE nid = %d", $link, $comments, $url_list, $nid);
313 if (!db_affected_rows()) {
314 db_query("INSERT INTO {print_pdf_node_conf} (nid, link, comments, url_list) VALUES (%d, %d, %d, %d)", $nid, $link, $comments, $url_list);
315 }
40cbc88e
JV
316 }
317}
318
319/**
320 * Format the PDF version link
321 *
322 * @return
323 * array of formatted attributes
324 * @ingroup themeable
325 */
326function theme_print_pdf_format_link() {
327 $print_pdf_link_class = variable_get('print_pdf_link_class', PRINT_PDF_LINK_CLASS_DEFAULT);
328 $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT);
329 $print_pdf_show_link = variable_get('print_pdf_show_link', PRINT_PDF_SHOW_LINK_DEFAULT);
568392c9 330 $print_pdf_link_text = variable_get('print_pdf_link_text', t('PDF version'));
40cbc88e 331
40cbc88e
JV
332 $img = drupal_get_path('module', 'print') .'/icons/pdf_icon.gif';
333 $title = t('Display a PDF version of this page.');
334 $class = strip_tags($print_pdf_link_class);
335 $new_window = ($print_pdf_content_disposition == 1);
568392c9 336 $format = _print_format_link_aux($print_pdf_show_link, $print_pdf_link_text, $img);
40cbc88e
JV
337
338 return array('text' => $format['text'],
339 'html' => $format['html'],
340 'attributes' => print_fill_attributes($title, $class, $new_window),
341 );
342}
343
344/**
d8ea68f9 345 * Auxiliary function to display a formatted PDF version link
40cbc88e 346 *
d8ea68f9
JV
347 * Function made available so that developers may call this function from
348 * their defined pages/blocks.
349 *
350 * @param $path
351 * path of the original page (optional). If not specified, the current URL
352 * is used
353 * @param $node
354 * an optional node object, to be used in defining the path, if used, the
355 * path argument is irrelevant
356 * @return
357 * string with the HTML link to the printer-friendly page
40cbc88e 358 */
d8ea68f9
JV
359function print_pdf_insert_link($path = NULL, $node = NULL) {
360 if ($node !== NULL) {
361 $nid = $node->nid;
362 $path = 'node/'. $nid;
363 $allowed_type = print_pdf_link_allowed(array('node' => $node));
364 }
365 else {
40cbc88e 366 if ($path === NULL) {
d8ea68f9
JV
367 $nid = preg_replace('!^node/!', '', $_GET['q']);
368 $path = $_GET['q'];
369 }
370 else {
371 $nid = NULL;
372 }
373 $allowed_type = print_pdf_link_allowed(array('path' => $path));
374 }
40cbc88e 375
d8ea68f9
JV
376 if ($allowed_type) {
377 if ($nid !== NULL) {
378 if ($allowed_type === PRINT_ALLOW_BOOK_LINK) {
379 $path = 'book/export/html/'. $nid;
40cbc88e
JV
380 }
381 else {
d8ea68f9
JV
382 if (variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT)) {
383 $path = drupal_get_path_alias($path);
384 }
385 else {
386 $path = $nid;
387 }
40cbc88e
JV
388 }
389 $path = PRINTPDF_PATH .'/'. $path;
390 $query = print_query_string_encode($_GET, array('q'));
391 if (empty($query)) {
392 $query = NULL;
393 }
394 }
395 else {
396 $query = NULL;
397 }
d8ea68f9 398 drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css');
40cbc88e 399 $format = theme('print_pdf_format_link');
d8ea68f9
JV
400 return '<span class="print_pdf">'. l($format['text'], $path, $format['attributes'], $query, NULL, TRUE, $format['html']) .'</span>';
401 }
402 else {
403 return FALSE;
40cbc88e
JV
404 }
405}
406
407/**
408 * Determine a the link to the PDF version is allowed depending on all possible settings
409 *
410 * @param $args
411 * array containing the possible parameters:
412 * teaser, node, type, path
413 * @return
414 * FALSE if not allowed
415 * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed
416 * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node
417 */
418function print_pdf_link_allowed($args) {
419 $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
420 if (!empty($args['teaser']) || !user_access('access print') || (empty($print_pdf_pdf_tool))) {
421 // If showing only the teaser or the user is not allowed or link is disabled
422 return FALSE;
423 }
d8ea68f9
JV
424 if (!empty($args['path'])) {
425 $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path']));
426 if (is_numeric($nid)) {
427 $args['node'] = node_load(array('nid' => $nid));
428 }
429 }
40cbc88e
JV
430 if (!empty($args['node'])) {
431 static $node_type = FALSE;
432
433 $node = $args['node'];
434 if ($node_type === FALSE) {
435 if (isset($node->type)) {
436 $node_type = $node->type;
437 }
438 else {
439 $node_type = '';
440 }
441 }
442 // Node
443 $print_pdf_node_link_visibility = variable_get('print_pdf_node_link_visibility', PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT);
444 $print_pdf_node_link_pages = variable_get('print_pdf_node_link_pages', PRINT_PDF_NODE_LINK_PAGES_DEFAULT);
445
abd96759
JV
446 if (!_print_page_match($print_pdf_node_link_visibility, $print_pdf_node_link_pages)) {
447 // Page not in visibility list
40cbc88e
JV
448 return FALSE;
449 }
450 elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) {
451 // Link is for a comment, return the configured setting
6ea54f45
JV
452 $res = db_fetch_object(db_query("SELECT comments FROM {print_pdf_node_conf} WHERE nid = %d", $node->nid));
453 $print_display_comment = $res ? intval($res->comments) : PRINT_TYPE_COMMENT_LINK_DEFAULT;
454 if (($print_display_comment) ||
455 variable_get('print_pdf_display_comment_'. $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT)) {
456 return PRINT_ALLOW_NORMAL_LINK;
457 }
40cbc88e
JV
458 }
459 else {
460 // Node link
6ea54f45
JV
461 if ((!$node->print_pdf_display) || (isset($node_type) &&
462 !variable_get('print_pdf_display_'. $node_type, PRINT_TYPE_SHOW_LINK_DEFAULT))) {
40cbc88e
JV
463 // Link for this node type is disabled
464 return FALSE;
465 }
466 elseif (isset($node->parent)) {
467 // Node is a book;
468 $print_pdf_book_link = variable_get('print_pdf_book_link', PRINT_PDF_BOOK_LINK_DEFAULT);
8b2d4a33
JV
469 switch ($print_pdf_book_link) {
470 case 1:
471 if (user_access('see printer-friendly version')) {
472 return PRINT_ALLOW_BOOK_LINK;
473 }
474 break;
475 case 2:
476 return PRINT_ALLOW_NORMAL_LINK;
40cbc88e
JV
477 }
478 }
479 else {
480 return PRINT_ALLOW_NORMAL_LINK;
481 }
482 }
483 }
484 else {
485 // 'System' page
486 $print_pdf_sys_link_visibility = variable_get('print_pdf_sys_link_visibility', PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT);
487 $print_pdf_sys_link_pages = variable_get('print_pdf_sys_link_pages', PRINT_PDF_SYS_LINK_PAGES_DEFAULT);
488
489 return _print_page_match($print_pdf_sys_link_visibility, $print_pdf_sys_link_pages);
490 }
8b2d4a33 491 return FALSE;
40cbc88e 492}