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