| Commit | Line | Data |
|---|---|---|
| 9c35190f | 1 | <?php |
| f163b6a0 | 2 | // $Id$ |
| 9c35190f JV |
3 | |
| 4 | /** | |
| 5 | * @file | |
| 7934fadc | 6 | * Displays Printer-friendly versions of Drupal pages. |
| 9c35190f JV |
7 | */ |
| 8 | ||
| cc224831 | 9 | define('PRINTPDF_PATH', 'printpdf'); |
| 9c35190f | 10 | |
| cc224831 JV |
11 | define('PRINT_PDF_LINK_POS_DEFAULT', 'link'); |
| 12 | define('PRINT_PDF_SHOW_LINK_DEFAULT', 1); | |
| 13 | define('PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT', 0); | |
| 14 | define('PRINT_PDF_NODE_LINK_PAGES_DEFAULT', ''); | |
| 15 | define('PRINT_PDF_LINK_CLASS_DEFAULT', 'print-pdf'); | |
| 16 | define('PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT', 1); | |
| a1057bea | 17 | define('PRINT_PDF_SYS_LINK_PAGES_DEFAULT', ''); |
| 16082a66 | 18 | define('PRINT_PDF_LINK_USE_ALIAS_DEFAULT', 0); |
| cc224831 JV |
19 | define('PRINT_PDF_BOOK_LINK_DEFAULT', 1); |
| 20 | define('PRINT_PDF_PDF_TOOL_DEFAULT', 0); | |
| 21 | define('PRINT_PDF_CONTENT_DISPOSITION_DEFAULT', 2); | |
| 22 | define('PRINT_PDF_PAPER_SIZE_DEFAULT', 'A4'); | |
| 23 | define('PRINT_PDF_PAGE_ORIENTATION_DEFAULT', 'portrait'); | |
| e7fb3470 JV |
24 | define('PRINT_PDF_FONT_FAMILY_DEFAULT', 'dejavusans'); |
| 25 | define('PRINT_PDF_FONT_SIZE_DEFAULT', 10); | |
| cef86536 | 26 | |
| 9c35190f | 27 | /** |
| 77efbeae JV |
28 | * Implementation of hook_init(). |
| 29 | */ | |
| 30 | function print_pdf_init() { | |
| 31 | $GLOBALS['conf']['cache'] = FALSE; | |
| 32 | } | |
| 33 | ||
| 34 | /** | |
| e8517dd5 | 35 | * Implementation of hook_theme(). |
| 9c35190f JV |
36 | */ |
| 37 | function print_pdf_theme() { | |
| 38 | return array( | |
| 39 | 'print_pdf_format_link' => array( | |
| 40 | 'arguments' => array(), | |
| 41 | ), | |
| 2891fe2b JV |
42 | 'print_pdf_dompdf_footer' => array( |
| 43 | 'arguments' => array(), | |
| 9350c4b5 | 44 | 'file' => 'print_pdf.pages.inc', |
| 2891fe2b | 45 | ), |
| e7fb3470 JV |
46 | 'print_pdf_tcpdf_header' => array( |
| 47 | 'arguments' => array(), | |
| 9350c4b5 | 48 | 'file' => 'print_pdf.pages.inc', |
| e7fb3470 JV |
49 | ), |
| 50 | 'print_pdf_tcpdf_page' => array( | |
| 51 | 'arguments' => array(), | |
| 9350c4b5 | 52 | 'file' => 'print_pdf.pages.inc', |
| e7fb3470 JV |
53 | ), |
| 54 | 'print_pdf_tcpdf_content' => array( | |
| 55 | 'arguments' => array(), | |
| 9350c4b5 | 56 | 'file' => 'print_pdf.pages.inc', |
| e7fb3470 JV |
57 | ), |
| 58 | 'print_pdf_tcpdf_footer' => array( | |
| 59 | 'arguments' => array(), | |
| 9350c4b5 | 60 | 'file' => 'print_pdf.pages.inc', |
| e7fb3470 JV |
61 | ), |
| 62 | 'print_pdf_tcpdf_footer2' => array( | |
| 63 | 'arguments' => array(), | |
| 9350c4b5 | 64 | 'file' => 'print_pdf.pages.inc', |
| e7fb3470 | 65 | ), |
| 9c35190f JV |
66 | ); |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| e8517dd5 | 70 | * Implementation of hook_menu(). |
| 9c35190f JV |
71 | */ |
| 72 | function print_pdf_menu() { | |
| 73 | $items = array(); | |
| 74 | ||
| 75 | $items[PRINTPDF_PATH] = array( | |
| 76 | 'title' => 'Printer-friendly PDF', | |
| 77 | 'page callback' => 'print_pdf_controller', | |
| 78 | 'access arguments' => array('access print'), | |
| 79 | 'type' => MENU_CALLBACK, | |
| 76faa6b6 | 80 | 'file' => 'print_pdf.pages.inc', |
| 9c35190f | 81 | ); |
| cef86536 JV |
82 | $items['admin/settings/print/pdf'] = array( |
| 83 | 'title' => 'PDF', | |
| 84 | 'page callback' => 'drupal_get_form', | |
| 85 | 'page arguments' => array('print_pdf_settings'), | |
| 86 | 'access arguments' => array('administer print'), | |
| cc224831 | 87 | 'weight' => 3, |
| cef86536 JV |
88 | 'type' => MENU_LOCAL_TASK, |
| 89 | 'file' => 'print_pdf.admin.inc', | |
| 90 | ); | |
| 9c35190f JV |
91 | |
| 92 | return $items; | |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 8d3c5d9d JV |
96 | * Implementation of hook_requirements(). |
| 97 | */ | |
| 98 | function print_pdf_requirements($phase) { | |
| 99 | $requirements = array(); | |
| 100 | $t = get_t(); | |
| 101 | switch ($phase) { | |
| 102 | // At runtime, make sure that a PDF generation tool is selected | |
| 103 | case 'runtime': | |
| cc224831 JV |
104 | $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT); |
| 105 | if (empty($print_pdf_pdf_tool)) { | |
| 8d3c5d9d | 106 | $requirements['print_pdf_tool'] = array( |
| ea946e81 JV |
107 | 'title' => $t('PDF version'), |
| 108 | 'value' => $t('No PDF tool selected'), | |
| 109 | 'description' => $t('Please configure it in the <a href="@url">PDF settings page</a>.', array('@url' => url('admin/settings/print/pdf'))), | |
| 8d3c5d9d JV |
110 | 'severity' => REQUIREMENT_ERROR, |
| 111 | ); | |
| 112 | } | |
| 113 | break; | |
| 114 | } | |
| 115 | return $requirements; | |
| 116 | } | |
| 117 | ||
| 118 | /** | |
| 9c35190f JV |
119 | * Implementation of hook_link(). |
| 120 | */ | |
| 121 | function print_pdf_link($type, $node = NULL, $teaser = FALSE) { | |
| cc224831 | 122 | $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT)); |
| 16082a66 | 123 | $print_pdf_link_use_alias = variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT); |
| cc224831 JV |
124 | $allowed_type = print_pdf_link_allowed(array('type' => $type, 'node' => $node, 'teaser' => $teaser)); |
| 125 | if (($allowed_type) && !empty($print_pdf_link_pos['link'])) { | |
| 76faa6b6 | 126 | drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css'); |
| 9c35190f | 127 | $links = array(); |
| 9c35190f | 128 | $format = theme('print_pdf_format_link'); |
| 3704b344 | 129 | |
| 77d2b585 | 130 | // Show book link |
| cc224831 JV |
131 | if ($allowed_type === PRINT_ALLOW_BOOK_LINK) { |
| 132 | $links['book_pdf'] = array('href' => PRINTPDF_PATH .'/book/export/html/'. $node->nid, | |
| 1c493781 | 133 | 'title' => $format['text'], |
| 77d2b585 | 134 | 'attributes' => $format['attributes'], |
| 1c493781 | 135 | 'html' => $format['html'], |
| f163b6a0 | 136 | ); |
| 77d2b585 JV |
137 | |
| 138 | return $links; | |
| 9c35190f | 139 | } |
| cc224831 | 140 | elseif ($allowed_type === PRINT_ALLOW_NORMAL_LINK) { |
| 77d2b585 JV |
141 | $query_arr = $_GET; |
| 142 | if ($type == 'comment') { | |
| 143 | $query_arr['comment'] = $node->cid; | |
| 144 | } | |
| cc224831 | 145 | $query = print_query_string_encode($query_arr, array('q')); |
| 0e51bae4 | 146 | if (empty($query)) $query = NULL; |
| 9c35190f | 147 | |
| 16082a66 JV |
148 | if ($print_pdf_link_use_alias) { |
| 149 | $path = drupal_get_path_alias('node/'. $node->nid); | |
| 150 | } | |
| 151 | else { | |
| 152 | $path = $node->nid; | |
| 153 | } | |
| 154 | ||
| 155 | $links['print_pdf'] = array('href' => PRINTPDF_PATH .'/'. $path, | |
| 77d2b585 JV |
156 | 'title' => $format['text'], |
| 157 | 'attributes' => $format['attributes'], | |
| 158 | 'html' => $format['html'], | |
| 159 | 'query' => $query, | |
| f163b6a0 | 160 | ); |
| 77d2b585 JV |
161 | |
| 162 | return $links; | |
| 163 | } | |
| 9c35190f | 164 | } |
| 7934fadc JV |
165 | else { |
| 166 | return; | |
| 167 | } | |
| 9c35190f JV |
168 | } |
| 169 | ||
| 170 | /** | |
| e8517dd5 | 171 | * Implementation of hook_help(). |
| 9c35190f JV |
172 | */ |
| 173 | function print_pdf_help($path, $arg) { | |
| cc224831 | 174 | $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT)); |
| 7934fadc | 175 | if ((preg_match('!^node/!i', $path) == 0) && |
| cc224831 | 176 | !(empty($print_pdf_link_pos['link']) && empty($print_pdf_link_pos['corner']))) { |
| 9c35190f JV |
177 | static $output = FALSE; |
| 178 | ||
| 179 | if ($output === FALSE) { | |
| 180 | $output = TRUE; | |
| 7934fadc JV |
181 | |
| 182 | $link = print_pdf_insert_link(); | |
| 183 | if ($link) { | |
| 184 | return "<span class='print-syslink'>$link</span>"; | |
| 185 | } | |
| 9c35190f JV |
186 | } |
| 187 | } | |
| 188 | } | |
| 189 | ||
| 190 | /** | |
| cc224831 JV |
191 | * Implementation of hook_nodeapi(). |
| 192 | */ | |
| 193 | function print_pdf_nodeapi(&$node, $op = 'view', $teaser, $page) { | |
| 194 | switch ($op) { | |
| 195 | case 'view': | |
| 196 | $print_pdf_link_pos = variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT)); | |
| 7934fadc | 197 | if (($teaser === FALSE) && !empty($print_pdf_link_pos['corner']) && |
| cc224831 | 198 | (preg_match('!^print!i', $_GET['q']) == 0)) { |
| 7934fadc JV |
199 | $link = print_pdf_insert_link(NULL, $node); |
| 200 | if ($link) { | |
| 201 | $node->content['print_pdf_link'] = array( | |
| 202 | '#value' => "<span class='print-link'>$link</span>", | |
| 890aa37d | 203 | '#weight' => -103, |
| 7934fadc | 204 | ); |
| cc224831 | 205 | } |
| cc224831 JV |
206 | } |
| 207 | } | |
| 208 | } | |
| 209 | ||
| 210 | /** | |
| e8517dd5 | 211 | * Implementation of hook_form_alter(). |
| 9c35190f JV |
212 | */ |
| 213 | function print_pdf_form_alter(&$form, $form_state, $form_id) { | |
| 4ed2260c | 214 | // Add the node-type settings option to activate the PDF version link |
| e8517dd5 | 215 | if ($form_id == 'node_type_form') { |
| 8d3c5d9d | 216 | $form['print']['print_pdf_display'] = array( |
| 9c35190f | 217 | '#type' => 'checkbox', |
| 4ed2260c | 218 | '#title' => t('Show PDF version link'), |
| cc224831 | 219 | '#default_value' => variable_get('print_pdf_display_'. $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT), |
| 4ed2260c | 220 | '#description' => t('Displays the link to a PDF version of the content. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))), |
| 9c35190f | 221 | ); |
| 8d3c5d9d | 222 | $form['print']['print_pdf_display_comment'] = array( |
| 9c35190f | 223 | '#type' => 'checkbox', |
| 4ed2260c | 224 | '#title' => t('Show PDF version link in individual comments'), |
| cc224831 | 225 | '#default_value' => variable_get('print_pdf_display_comment_'. $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT), |
| 4ed2260c | 226 | '#description' => t('Displays the link to a PDF version of the comment. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))), |
| 9c35190f JV |
227 | ); |
| 228 | } | |
| 229 | } | |
| 230 | ||
| 77d2b585 JV |
231 | /** |
| 232 | * Format the PDF version link | |
| 233 | * | |
| 234 | * @return | |
| 235 | * array of formatted attributes | |
| 236 | * @ingroup themeable | |
| 237 | */ | |
| 9c35190f | 238 | function theme_print_pdf_format_link() { |
| cc224831 JV |
239 | $print_pdf_link_class = variable_get('print_pdf_link_class', PRINT_PDF_LINK_CLASS_DEFAULT); |
| 240 | $print_pdf_content_disposition = variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT); | |
| 241 | $print_pdf_show_link = variable_get('print_pdf_show_link', PRINT_PDF_SHOW_LINK_DEFAULT); | |
| 242 | ||
| 1d37b654 | 243 | $text = t('PDF version'); |
| ded70136 | 244 | $img = drupal_get_path('module', 'print') .'/icons/pdf_icon.gif'; |
| 67026c64 | 245 | $title = t('Display a PDF version of this page.'); |
| cc224831 JV |
246 | $class = strip_tags($print_pdf_link_class); |
| 247 | $new_window = ($print_pdf_content_disposition == 1); | |
| 248 | $format = _print_format_link_aux($print_pdf_show_link, $text, $img); | |
| 67026c64 JV |
249 | |
| 250 | return array('text' => $format['text'], | |
| 251 | 'html' => $format['html'], | |
| 77d2b585 | 252 | 'attributes' => print_fill_attributes($title, $class, $new_window), |
| e8517dd5 | 253 | ); |
| 9c35190f JV |
254 | } |
| 255 | ||
| 256 | /** | |
| 7934fadc | 257 | * Auxiliary function to display a formatted PDF version link |
| 9c35190f | 258 | * |
| 7934fadc JV |
259 | * Function made available so that developers may call this function from |
| 260 | * their defined pages/blocks. | |
| 261 | * | |
| 262 | * @param $path | |
| 263 | * path of the original page (optional). If not specified, the current URL | |
| 264 | * is used | |
| 265 | * @param $node | |
| 266 | * an optional node object, to be used in defining the path, if used, the | |
| 267 | * path argument is irrelevant | |
| 268 | * @return | |
| 269 | * string with the HTML link to the printer-friendly page | |
| 9c35190f | 270 | */ |
| 7934fadc JV |
271 | function print_pdf_insert_link($path = NULL, $node = NULL) { |
| 272 | if ($node !== NULL) { | |
| 273 | $nid = $node->nid; | |
| 274 | $path = 'node/'. $nid; | |
| 275 | $allowed_type = print_pdf_link_allowed(array('node' => $node)); | |
| 276 | } | |
| 277 | else { | |
| 9c35190f | 278 | if ($path === NULL) { |
| 7934fadc JV |
279 | $nid = preg_replace('!^node/!', '', $_GET['q']); |
| 280 | $path = $_GET['q']; | |
| 281 | } | |
| 282 | else { | |
| 283 | $nid = NULL; | |
| 284 | } | |
| 285 | $allowed_type = print_pdf_link_allowed(array('path' => $path)); | |
| 286 | } | |
| 16082a66 | 287 | |
| 7934fadc JV |
288 | if ($allowed_type) { |
| 289 | if ($nid !== NULL) { | |
| 290 | if ($allowed_type === PRINT_ALLOW_BOOK_LINK) { | |
| 291 | $path = 'book/export/html/'. $nid; | |
| 16082a66 JV |
292 | } |
| 293 | else { | |
| 7934fadc JV |
294 | if (variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT)) { |
| 295 | $path = drupal_get_path_alias($path); | |
| 296 | } | |
| 297 | else { | |
| 298 | $path = $nid; | |
| 299 | } | |
| 16082a66 JV |
300 | } |
| 301 | $path = PRINTPDF_PATH .'/'. $path; | |
| cc224831 | 302 | $query = print_query_string_encode($_GET, array('q')); |
| 9c35190f JV |
303 | if (empty($query)) { |
| 304 | $query = NULL; | |
| 305 | } | |
| 306 | } | |
| 998eb98b JV |
307 | else { |
| 308 | $query = NULL; | |
| 309 | } | |
| 7934fadc | 310 | drupal_add_css(drupal_get_path('module', 'print') .'/css/printlinks.css'); |
| 9c35190f | 311 | $format = theme('print_pdf_format_link'); |
| cc224831 JV |
312 | return '<span class="print_pdf">'. l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) .'</span>'; |
| 313 | } | |
| 7934fadc JV |
314 | else { |
| 315 | return FALSE; | |
| 316 | } | |
| cc224831 JV |
317 | } |
| 318 | ||
| 319 | /** | |
| 320 | * Determine a the link to the PDF version is allowed depending on all possible settings | |
| 321 | * | |
| 322 | * @param $args | |
| 323 | * array containing the possible parameters: | |
| 324 | * teaser, node, type, path | |
| 325 | * @return | |
| 326 | * FALSE if not allowed | |
| 327 | * PRINT_ALLOW_NORMAL_LINK if a normal link is allowed | |
| 328 | * PRINT_ALLOW_BOOK_LINK if a link is allowed in a book node | |
| 329 | */ | |
| 330 | function print_pdf_link_allowed($args) { | |
| e7fb3470 | 331 | $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT); |
| 5fc86047 | 332 | if (!empty($args['teaser']) || !user_access('access print') || (empty($print_pdf_pdf_tool))) { |
| cc224831 JV |
333 | // If showing only the teaser or the user is not allowed or link is disabled |
| 334 | return FALSE; | |
| 335 | } | |
| 7934fadc JV |
336 | if (!empty($args['path'])) { |
| 337 | $nid = preg_replace('!^node/!', '', drupal_get_normal_path($args['path'])); | |
| 338 | if (is_numeric($nid)) { | |
| 339 | $args['node'] = node_load(array('nid' => $nid)); | |
| 340 | } | |
| 341 | } | |
| 5fc86047 | 342 | if (!empty($args['node'])) { |
| cc224831 | 343 | static $node_type = FALSE; |
| 9eb5cf31 JV |
344 | |
| 345 | $node = $args['node']; | |
| 2b83ebd6 JV |
346 | if ($node_type === FALSE) { |
| 347 | if (isset($node->type)) { | |
| 348 | $node_type = $node->type; | |
| 349 | } | |
| 350 | else { | |
| 351 | $node_type = ''; | |
| 352 | } | |
| cc224831 JV |
353 | } |
| 354 | // Node | |
| 355 | $print_pdf_node_link_visibility = variable_get('print_pdf_node_link_visibility', PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT); | |
| 356 | $print_pdf_node_link_pages = variable_get('print_pdf_node_link_pages', PRINT_PDF_NODE_LINK_PAGES_DEFAULT); | |
| 357 | ||
| 7934fadc | 358 | if (!empty($node->printing) || |
| cc224831 JV |
359 | !_print_page_match($print_pdf_node_link_visibility, $print_pdf_node_link_pages)) { |
| 360 | // Page not in visibility list or we are working! | |
| 361 | return FALSE; | |
| 362 | } | |
| 5fc86047 | 363 | elseif (isset($args['type']) && ($args['type'] == 'comment') && isset($node_type)) { |
| cc224831 JV |
364 | // Link is for a comment, return the configured setting |
| 365 | return variable_get('print_pdf_display_comment_'. $node_type, PRINT_TYPE_COMMENT_LINK_DEFAULT); | |
| 366 | } | |
| 367 | else { | |
| 368 | // Node link | |
| 7934fadc | 369 | if (isset($node_type) && |
| cc224831 JV |
370 | !variable_get('print_pdf_display_'. $node_type, PRINT_TYPE_SHOW_LINK_DEFAULT)) { |
| 371 | // Link for this node type is disabled | |
| 372 | return FALSE; | |
| 373 | } | |
| 2891fe2b | 374 | elseif (isset($node->book)) { |
| cc224831 JV |
375 | // Node is a book; |
| 376 | $print_pdf_book_link = variable_get('print_pdf_book_link', PRINT_PDF_BOOK_LINK_DEFAULT); | |
| ea77807a JV |
377 | switch ($print_pdf_book_link) { |
| 378 | case 1: | |
| 379 | if (user_access('access printer-friendly version')) { | |
| 380 | return PRINT_ALLOW_BOOK_LINK; | |
| 381 | } | |
| 382 | break; | |
| 383 | case 2: | |
| 384 | return PRINT_ALLOW_NORMAL_LINK; | |
| cc224831 JV |
385 | } |
| 386 | } | |
| 387 | else { | |
| 388 | return PRINT_ALLOW_NORMAL_LINK; | |
| 389 | } | |
| 390 | } | |
| 391 | } | |
| 392 | else { | |
| 393 | // 'System' page | |
| 394 | $print_pdf_sys_link_visibility = variable_get('print_pdf_sys_link_visibility', PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT); | |
| 395 | $print_pdf_sys_link_pages = variable_get('print_pdf_sys_link_pages', PRINT_PDF_SYS_LINK_PAGES_DEFAULT); | |
| 396 | ||
| 397 | return _print_page_match($print_pdf_sys_link_visibility, $print_pdf_sys_link_pages); | |
| 9c35190f | 398 | } |
| ea77807a | 399 | return FALSE; |
| 9c35190f | 400 | } |