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