| Commit | Line | Data |
|---|---|---|
| b09f08f7 | 1 | <?php |
| 1e156b5a | 2 | // $Id$ |
| b09f08f7 | 3 | |
| 1e156b5a JV |
4 | /** |
| 5 | * @file | |
| 6 | * Contains the functions to generate Printer-friendly pages. | |
| 7 | * | |
| 8 | * This file is included by the core PF module, and includes all the | |
| 9 | * functions necessary to generate a PF version of the original page | |
| 10 | * in HTML format. | |
| 11 | */ | |
| b09f08f7 | 12 | |
| 1e156b5a JV |
13 | /** |
| 14 | * Generate an HTML version of the printer-friendly page | |
| 15 | * | |
| 16 | * @see print_controller() | |
| 17 | * @see _print_get_template() | |
| 18 | */ | |
| b09f08f7 | 19 | function print_controller_html() { |
| 5b1b9562 | 20 | $args = func_get_args(); |
| 0706a465 | 21 | // Remove the print/ prefix |
| 5b1b9562 | 22 | $path = implode('/', $args); |
| 20ab24e6 | 23 | $cid = isset($_GET['comment']) ? (int)$_GET['comment'] : NULL; |
| 0706a465 JV |
24 | |
| 25 | $print = print_controller($path, $cid); | |
| 7d62e7c4 JV |
26 | $node = $print['node']; |
| 27 | include_once(_print_get_template('html', $print['type'])); | |
| b09f08f7 JV |
28 | } |
| 29 | ||
| 30 | /** | |
| 0706a465 | 31 | * Select the print generator function based on the page type |
| 1e156b5a JV |
32 | * |
| 33 | * Depending on the type of node, this functions chooses the appropriate | |
| 34 | * generator function. | |
| 35 | * | |
| 0706a465 JV |
36 | * @param $path |
| 37 | * path of the original page | |
| 38 | * @param $cid | |
| 39 | * comment ID of the individual comment to be rendered | |
| 40 | * @param $teaser | |
| 41 | * if set to TRUE, outputs only the node's teaser | |
| fb70cd39 JV |
42 | * @param $message |
| 43 | * optional sender's message (used by the send e-mail module) | |
| 1e156b5a JV |
44 | * @return |
| 45 | * array with the fields to be used in the template | |
| 46 | * @see _print_generate_node() | |
| 47 | * @see _print_generate_path() | |
| 48 | * @see _print_generate_book() | |
| b09f08f7 | 49 | */ |
| 27dc0ea1 | 50 | function print_controller($path, $cid, $teaser = FALSE, $message = NULL) { |
| 0706a465 | 51 | if (!is_numeric($path)) { |
| b09f08f7 JV |
52 | // Indirect call with print/alias |
| 53 | // If there is a path alias with these arguments, generate a printer-friendly version for it | |
| 0706a465 | 54 | $path = drupal_get_normal_path($path); |
| 7d62e7c4 | 55 | $ret = preg_match('!^node/(.*)!i', $path, $matches); |
| b09f08f7 | 56 | if ($ret == 1) { |
| 0706a465 | 57 | $path = $matches[1]; |
| b09f08f7 JV |
58 | } |
| 59 | } | |
| 7d62e7c4 | 60 | $parts = explode('/', $path); |
| 9490b60e | 61 | if (is_numeric($parts[0])) { |
| 27dc0ea1 | 62 | $print = _print_generate_node($path, $cid, $teaser, $message); |
| b09f08f7 JV |
63 | } |
| 64 | else { | |
| 7d62e7c4 | 65 | $ret = preg_match('!^book/export/html/(.*)!i', $path, $matches); |
| b09f08f7 JV |
66 | if ($ret == 1) { |
| 67 | // This is a book PF page link, handle trough the book handling functions | |
| 27dc0ea1 | 68 | $print = _print_generate_book($matches[1], $teaser, $message); |
| b09f08f7 JV |
69 | } |
| 70 | else { | |
| 71 | // If no content node was found, handle the page printing with the 'printable' engine | |
| 27dc0ea1 | 72 | $print = _print_generate_path($path, $teaser, $message); |
| b09f08f7 JV |
73 | } |
| 74 | } | |
| 75 | ||
| 76 | return $print; | |
| 77 | } | |
| 78 | ||
| b09f08f7 | 79 | /** |
| 1e156b5a | 80 | * Generates a robots meta tag to tell them what they may index |
| b09f08f7 | 81 | * |
| 1e156b5a | 82 | * @return |
| 3483245d | 83 | * string with the meta robots tag |
| b09f08f7 JV |
84 | */ |
| 85 | function _print_robots_meta_generator() { | |
| 7d62e7c4 JV |
86 | $print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT); |
| 87 | $print_robots_nofollow = variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT); | |
| 88 | $print_robots_noarchive = variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT); | |
| b09f08f7 JV |
89 | $robots_meta = array(); |
| 90 | ||
| 7d62e7c4 | 91 | if (!empty($print_robots_noindex)) { |
| b09f08f7 JV |
92 | $robots_meta[] = 'noindex'; |
| 93 | } | |
| 7d62e7c4 | 94 | if (!empty($print_robots_nofollow)) { |
| b09f08f7 JV |
95 | $robots_meta[] = 'nofollow'; |
| 96 | } | |
| 7d62e7c4 | 97 | if (!empty($print_robots_noarchive)) { |
| b09f08f7 JV |
98 | $robots_meta[] = 'noarchive'; |
| 99 | } | |
| b09f08f7 | 100 | |
| 7e748d74 JV |
101 | if (count($robots_meta) > 0) { |
| 102 | $robots_meta = implode(', ', $robots_meta); | |
| 7d62e7c4 | 103 | $robots_meta = "<meta name='robots' content='$robots_meta' />\n"; |
| b09f08f7 JV |
104 | } |
| 105 | else { | |
| 106 | $robots_meta = ''; | |
| 107 | } | |
| 108 | ||
| 109 | return $robots_meta; | |
| 110 | } | |
| 111 | ||
| 112 | /** | |
| 1e156b5a | 113 | * Post-processor that fills the array for the template with common details |
| b09f08f7 | 114 | * |
| 1e156b5a JV |
115 | * @param $node |
| 116 | * generated node with a printer-friendly node body | |
| fb70cd39 JV |
117 | * @param $message |
| 118 | * optional sender's message (used by the send e-mail module) | |
| 1e156b5a | 119 | * @param $cid |
| 3483245d | 120 | * id of current comment being generated (NULL when not generating |
| 1e156b5a JV |
121 | * an individual comment) |
| 122 | * @return | |
| 123 | * array with the fields to be used in the template | |
| b09f08f7 | 124 | */ |
| 27dc0ea1 | 125 | function _print_var_generator($node, $message = NULL, $cid = NULL) { |
| b09f08f7 JV |
126 | global $base_url, $language; |
| 127 | ||
| 128 | $path = empty($node->nid) ? $node->path : "node/$node->nid"; | |
| 129 | ||
| 130 | $themed = theme('print_text'); | |
| 131 | ||
| 132 | // print module settings | |
| 7d62e7c4 JV |
133 | $print_css = variable_get('print_css', PRINT_CSS_DEFAULT); |
| 134 | $print_urls = variable_get('print_urls', PRINT_URLS_DEFAULT); | |
| ee8a10b8 | 135 | $print_logo_options = variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT); |
| 7d62e7c4 JV |
136 | $print_logo_url = variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT); |
| 137 | $print_html_sendtoprinter = variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT); | |
| 138 | $print_sourceurl_enabled = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT); | |
| 139 | $print_sourceurl_forcenode = variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT); | |
| 140 | $print_sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT); | |
| b97f7b85 JV |
141 | $print_footer_options = variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT); |
| 142 | $print_footer_user = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT); | |
| 7d62e7c4 JV |
143 | |
| 144 | $print['language'] = $language->language; | |
| 145 | $print['title'] = $node->title; | |
| 146 | $print['head'] = drupal_get_html_head(); | |
| 147 | $print['scripts'] = drupal_get_js(); | |
| 148 | $print['robots_meta'] = _print_robots_meta_generator(); | |
| 149 | $print['url'] = url($path, array('absolute' => TRUE)); | |
| 150 | $print['base_href'] = "<base href='". $print['url'] ."' />\n"; | |
| 151 | $print['favicon'] = theme_get_setting('toggle_favicon') ? "<link rel='shortcut icon' href='". theme_get_setting('favicon') ."' type='image/x-icon' />\n" : ''; | |
| b09f08f7 | 152 | |
| 7d62e7c4 | 153 | if (!empty($print_css)) { |
| 6f6bfb3e | 154 | $replace_pairs = array('%b' => base_path(), '%t' => path_to_theme()); |
| 24332e75 | 155 | $user_css = strip_tags(strtr($print_css, $replace_pairs)); |
| b09f08f7 JV |
156 | } |
| 157 | else { | |
| 24332e75 | 158 | drupal_add_css(drupal_get_path('module', 'print') .'/css/print.css'); |
| b09f08f7 | 159 | } |
| 24332e75 JV |
160 | $drupal_css = drupal_add_css(); |
| 161 | foreach ($drupal_css as $key => $types) { | |
| 162 | // Unset the theme's CSS | |
| 163 | $drupal_css[$key]['theme'] = array(); | |
| 27dc0ea1 JV |
164 | } |
| 165 | ||
| 166 | // If we are sending a message via e-mail, the CSS must be embedded | |
| 167 | if (!empty($message)) { | |
| 7d62e7c4 | 168 | $style = ''; |
| 24332e75 JV |
169 | $css_files = array(); |
| 170 | foreach ($drupal_css as $types) { | |
| 171 | foreach ($types as $values) { | |
| 172 | $css_files = array_merge($css_files, array_keys($values)); | |
| 173 | } | |
| 174 | } | |
| 175 | if (!empty($print_css)) { | |
| 0f1f957b | 176 | // Convert to a local path, by removing the base_path |
| 24332e75 JV |
177 | $pattern = '!^'. base_path() .'!'; |
| 178 | $css_files[] = preg_replace($pattern, '', $user_css); | |
| 179 | } | |
| 180 | foreach ($css_files as $filename) { | |
| 0f1f957b JV |
181 | $res = file_get_contents($filename, TRUE); |
| 182 | if ($res != FALSE) { | |
| 183 | $style .= $res; | |
| 184 | } | |
| 27dc0ea1 | 185 | } |
| 7d62e7c4 | 186 | $print['css'] = "<style type='text/css' media='all'>$style</style>\n"; |
| 27dc0ea1 JV |
187 | } |
| 188 | else { | |
| 24332e75 JV |
189 | $print['css'] = drupal_get_css($drupal_css); |
| 190 | if (!empty($print_css)) { | |
| 191 | $print['css'] .= "<link type='text/css' rel='stylesheet' media='all' href='$user_css' />\n"; | |
| 27dc0ea1 | 192 | } |
| 5d9fa914 | 193 | } |
| b09f08f7 | 194 | |
| 7d62e7c4 | 195 | $print['sendtoprinter'] = $print_html_sendtoprinter ? ' onload="window.print();"' : ''; |
| 3483245d | 196 | |
| ee8a10b8 JV |
197 | switch ($print_logo_options) { |
| 198 | case 0: // none | |
| 199 | $logo_url = 0; | |
| 200 | break; | |
| 201 | case 1: // theme's | |
| 202 | $logo_url = theme_get_setting('logo'); | |
| 203 | break; | |
| 204 | case 2: // user-specifed | |
| 205 | $logo_url = strip_tags($print_logo_url); | |
| 206 | break; | |
| 207 | } | |
| 7d62e7c4 | 208 | $print['logo'] = $logo_url ? "<img class='print-logo' src='$logo_url' alt='' />\n" : ''; |
| 1424b51c | 209 | |
| b97f7b85 JV |
210 | switch ($print_footer_options) { |
| 211 | case 0: // none | |
| 212 | $footer = ''; | |
| 213 | break; | |
| 214 | case 1: // theme's | |
| 215 | $footer = filter_xss_admin(variable_get('site_footer', FALSE)) ."\n". theme('blocks', 'footer'); | |
| 216 | $logo_url = theme_get_setting('logo'); | |
| 217 | break; | |
| 218 | case 2: // user-specifed | |
| 219 | $footer = $print_footer_user; | |
| 220 | break; | |
| 221 | } | |
| 222 | $print['footer_message'] = $footer; | |
| 223 | ||
| 1424b51c | 224 | $published_site = variable_get('site_name', 0); |
| eaac64a9 | 225 | if ($published_site) { |
| 7d62e7c4 JV |
226 | $published = (empty($themed['published'])) ? t('Published on %site_name', array('%site_name' => $published_site)) : ($themed['published'] .' '. $published_site); |
| 227 | $print['site_name'] = $published .' ('. l($base_url, $base_url) .')'; | |
| 1424b51c JV |
228 | } |
| 229 | else { | |
| 7d62e7c4 | 230 | $print['site_name'] = ''; |
| 1424b51c | 231 | } |
| b09f08f7 | 232 | |
| 7d62e7c4 | 233 | if ($print_sourceurl_enabled == 1) { |
| dad7232c | 234 | /* Grab and format the src URL */ |
| 7d62e7c4 JV |
235 | if (empty($print_sourceurl_forcenode)) { |
| 236 | $url = $print['url']; | |
| dad7232c JV |
237 | } |
| 238 | else { | |
| eaac64a9 | 239 | $url = $base_url .'/'. (((bool)variable_get('clean_url', '0')) ? '' : '?q=') . $path; |
| dad7232c | 240 | } |
| 20ab24e6 | 241 | if (is_int($cid)) { |
| 7d62e7c4 | 242 | $url .= '#comment-$cid'; |
| dad7232c | 243 | } |
| 1424b51c | 244 | $retrieved_date = format_date(time(), 'small'); |
| 7d62e7c4 JV |
245 | $retrieved = (empty($themed['retrieved'])) ? t('retrieved on %date', array('%date' => $retrieved_date)) : ($themed['retrieved'] .' '. $retrieved_date); |
| 246 | $print['printdate'] = $print_sourceurl_date ? " ($retrieved)" : ''; | |
| 1424b51c | 247 | |
| 7d62e7c4 JV |
248 | $source_url = (empty($themed['sourceURL'])) ? t('Source URL') : $themed['sourceURL']; |
| 249 | $print['source_url'] = '<strong>'. $source_url . $print['printdate'] .':</strong> '. l($url, $url); | |
| b09f08f7 JV |
250 | } |
| 251 | else { | |
| 7d62e7c4 | 252 | $print['source_url'] = ''; |
| b09f08f7 JV |
253 | } |
| 254 | ||
| 7e748d74 JV |
255 | if (isset($node->type)) { |
| 256 | $node_type = $node->type; | |
| 1424b51c | 257 | |
| 3da84173 | 258 | if (theme_get_setting("toggle_node_info_$node_type")) { |
| eaac64a9 | 259 | $by_author = ($node->name ? $node->name : variable_get('anonymous', t('Anonymous'))); |
| 7d62e7c4 JV |
260 | $by = (empty($themed['by'])) ? t('By %author', array('%author' => $by_author)) : ($themed['by'] .' '. $by_author); |
| 261 | $print['submitted'] = $by; | |
| 1424b51c | 262 | |
| eaac64a9 | 263 | $created_datetime = format_date($node->created, 'small'); |
| 7d62e7c4 JV |
264 | $created = (empty($themed['created'])) ? t('Created %date', array('%date' => $created_datetime)) : ($themed['created'] .' '. $created_datetime); |
| 265 | $print['created'] = $created; | |
| eaac64a9 JV |
266 | } |
| 267 | else { | |
| 7d62e7c4 JV |
268 | $print['submitted'] = ''; |
| 269 | $print['created'] = ''; | |
| eaac64a9 | 270 | } |
| 1424b51c | 271 | |
| 7d62e7c4 | 272 | $print['type'] = $node->type; |
| 7e748d74 JV |
273 | } |
| 274 | else { | |
| 7d62e7c4 JV |
275 | $print['submitted'] = ''; |
| 276 | $print['created'] = ''; | |
| 277 | $print['type'] = ''; | |
| 7e748d74 | 278 | } |
| ca01bdd2 | 279 | |
| b09f08f7 JV |
280 | menu_set_active_item($path); |
| 281 | $breadcrumb = drupal_get_breadcrumb(); | |
| 282 | if (!empty($breadcrumb)) { | |
| 283 | $breadcrumb[] = menu_get_active_title(); | |
| 7d62e7c4 | 284 | $print['breadcrumb'] = implode(' > ', $breadcrumb); |
| b09f08f7 JV |
285 | } |
| 286 | else { | |
| 7d62e7c4 | 287 | $print['breadcrumb'] = ''; |
| b09f08f7 JV |
288 | } |
| 289 | ||
| 290 | // Display the collected links at the bottom of the page. Code once taken from Kjartan Mannes' project.module | |
| 1a92e513 | 291 | $print['pfp_links'] = ''; |
| 7d62e7c4 | 292 | if (!empty($print_urls)) { |
| b09f08f7 JV |
293 | $urls = _print_friendly_urls(); |
| 294 | $max = count($urls); | |
| 1424b51c | 295 | $pfp_links = ''; |
| b09f08f7 | 296 | if ($max) { |
| b09f08f7 | 297 | for ($i = 0; $i < $max; $i++) { |
| 1424b51c | 298 | $pfp_links .= '['. ($i + 1) .'] '. $urls[$i] ."<br />\n"; |
| b09f08f7 | 299 | } |
| 7d62e7c4 JV |
300 | $links = (empty($themed['links'])) ? t('Links') : $themed['links']; |
| 301 | $print['pfp_links'] = "<p><strong>$links:</strong><br />$pfp_links</p>"; | |
| b09f08f7 JV |
302 | } |
| 303 | } | |
| 304 | ||
| b09f08f7 JV |
305 | if (module_exists('taxonomy')) { |
| 306 | $terms = taxonomy_link('taxonomy terms', $node); | |
| 7d62e7c4 | 307 | $print['taxonomy'] = theme('links', $terms); |
| b09f08f7 JV |
308 | } |
| 309 | ||
| 7d62e7c4 JV |
310 | $print['content'] = $node->body; |
| 311 | $print['node'] = $node; | |
| 312 | $print['message'] = $message; | |
| b09f08f7 JV |
313 | |
| 314 | return $print; | |
| 315 | } | |
| 316 | ||
| 317 | /** | |
| 1e156b5a | 318 | * Callback function for the preg_replace_callback for URL-capable patterns |
| b09f08f7 | 319 | * |
| 3483245d | 320 | * Manipulate URLs to make them absolute in the URLs list, and to add a |
| 1e156b5a | 321 | * [n] footnote marker. |
| 3483245d | 322 | * |
| 1e156b5a JV |
323 | * @param $matches |
| 324 | * array with the matched tag patterns, usually <a...>+text+</a> | |
| 325 | * @return | |
| 3483245d | 326 | * tag with re-written URL and when appropriate the [n] index to the |
| 1e156b5a | 327 | * URL list |
| b09f08f7 JV |
328 | */ |
| 329 | function _print_rewrite_urls($matches) { | |
| 330 | global $base_url, $base_root; | |
| 331 | ||
| 332 | // Get value of Printer-friendly URLs setting | |
| 7d62e7c4 | 333 | $print_urls = variable_get('print_urls', PRINT_URLS_DEFAULT); |
| b09f08f7 JV |
334 | |
| 335 | // first, split the html into the different tag attributes | |
| 3da84173 | 336 | $pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!'; |
| 6ca5f5a4 JV |
337 | $attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); |
| 338 | foreach ($attribs as $key => $value) { | |
| 1c9c68be | 339 | $attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value); |
| 6ca5f5a4 | 340 | } |
| b09f08f7 | 341 | |
| 7d62e7c4 JV |
342 | $size = count($attribs); |
| 343 | for ($i=1; $i < $size; $i++) { | |
| b09f08f7 | 344 | // If the attribute is href or src, we may need to rewrite the URL in the value |
| 1c9c68be | 345 | if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) { |
| 400943b8 | 346 | $url = trim($urls[1], " \t\n\r\0\x0B\"'"); |
| b09f08f7 | 347 | |
| 1c9c68be | 348 | if (strpos($url, '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', $url)) { |
| b09f08f7 JV |
349 | // URL is absolute, do nothing |
| 350 | $newurl = urldecode($url); | |
| 351 | } | |
| 352 | else { | |
| bb0645f8 | 353 | if ($url[0] == '#') { |
| b09f08f7 | 354 | // URL is an anchor tag |
| 6ca5f5a4 | 355 | if (!empty($print_urls)) { |
| bb0645f8 JV |
356 | $path = explode('/', $_GET['q']); |
| 357 | unset($path[0]); | |
| 358 | $path = implode('/', $path); | |
| b09f08f7 JV |
359 | if (is_numeric($path)) { |
| 360 | $path = "node/$path"; | |
| 361 | } | |
| 362 | // Printer-friendly URLs is on, so we need to make it absolute | |
| 363 | $newurl = url($path, array('fragment' => substr(urldecode($url), 1), 'absolute' => TRUE)); | |
| 364 | } | |
| 365 | // Because base href is the original page, change the link to | |
| 366 | // still be usable inside the print page | |
| 367 | $matches[1] = str_replace($url, $_GET['q'] . $url, $matches[1]); | |
| 368 | } | |
| 369 | else { | |
| 370 | // URL is relative, convert it into absolute URL | |
| bb0645f8 | 371 | if ($url[0] == '/') { |
| b09f08f7 | 372 | // If it starts with '/' just append it to the server name |
| 7d62e7c4 | 373 | $newurl = $base_root .'/'. trim(urldecode($url), '/'); |
| b09f08f7 | 374 | } |
| 1c9c68be JV |
375 | elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) { |
| 376 | // If it starts with ?q=, just prepend with the base URL | |
| 7d62e7c4 | 377 | $newurl = $base_url .'/'. trim(urldecode($url), '/'); |
| b09f08f7 JV |
378 | } |
| 379 | else { | |
| 7d62e7c4 | 380 | $newurl = url(trim(urldecode($url), '/'), array('absolute' => TRUE)); |
| b09f08f7 | 381 | } |
| 3da84173 | 382 | $matches[1] = str_replace($url, $newurl, $matches[1]); |
| b09f08f7 JV |
383 | } |
| 384 | } | |
| 385 | } | |
| 386 | } | |
| 387 | ||
| b09f08f7 JV |
388 | $ret = '<'. $matches[1] .'>'; |
| 389 | if (count($matches) == 4) { | |
| 390 | $ret .= $matches[2] . $matches[3]; | |
| 6ca5f5a4 | 391 | if ((!empty($print_urls)) && (isset($newurl))) { |
| b09f08f7 JV |
392 | $ret .= ' <span class="print-footnote">['. _print_friendly_urls(trim(stripslashes($newurl))) .']</span>'; |
| 393 | } | |
| 394 | } | |
| 395 | ||
| 396 | return $ret; | |
| 397 | } | |
| 398 | ||
| 399 | /** | |
| 400 | * Auxiliary function to store the Printer-friendly URLs list as static. | |
| 401 | * | |
| 1e156b5a JV |
402 | * @param $url |
| 403 | * absolute URL to be inserted in the list | |
| 3483245d JV |
404 | * @return |
| 405 | * list of URLs previously stored if $url is 0, or the current count | |
| 1e156b5a | 406 | * otherwise. |
| b09f08f7 JV |
407 | */ |
| 408 | function _print_friendly_urls($url = 0) { | |
| 409 | static $urls = array(); | |
| 410 | if ($url) { | |
| 411 | $url_idx = array_search($url, $urls); | |
| 412 | if ($url_idx !== FALSE) { | |
| 413 | return ($url_idx + 1); | |
| 414 | } | |
| 415 | else { | |
| 416 | $urls[] = $url; | |
| 417 | return count($urls); | |
| 418 | } | |
| 419 | } | |
| 21d8ff1e JV |
420 | $ret = $urls; |
| 421 | $urls = array(); | |
| 422 | return $ret; | |
| b09f08f7 JV |
423 | } |
| 424 | ||
| 425 | /** | |
| 1e156b5a | 426 | * Choose most appropriate template |
| 3483245d | 427 | * |
| b09f08f7 JV |
428 | * Auxiliary function to resolve the most appropriate template trying to find |
| 429 | * a content specific template in the theme or module dir before falling back | |
| 430 | * on a generic template also in those dirs. | |
| 431 | * | |
| 0d7894a0 JV |
432 | * @param format |
| 433 | * format of the PF page being rendered (html, pdf, etc.) | |
| 1e156b5a JV |
434 | * @param $type |
| 435 | * name of the node type being rendered in a PF page | |
| 436 | * @return | |
| 437 | * filename of the most suitable template | |
| b09f08f7 | 438 | */ |
| 0d7894a0 JV |
439 | function _print_get_template($format = NULL, $type = NULL) { |
| 440 | $filenames = array(); | |
| 441 | // First try to find a template defined both for the format and then the type | |
| 442 | if (!empty($format) && !empty($type)) { | |
| 443 | $filenames[] = "print_$format.node-$type.tpl.php"; | |
| 444 | } | |
| 445 | // Then only for the format | |
| 446 | if (!empty($format)) { | |
| 447 | $filenames[] = "print_$format.tpl.php"; | |
| 448 | } | |
| 449 | // If the node type is known, then try to find that type's template file | |
| 450 | if (!empty($type)) { | |
| 451 | $filenames[] = "print.node-$type.tpl.php"; | |
| 452 | } | |
| 453 | // Finally search for a generic template file | |
| 7d62e7c4 | 454 | $filenames[] = 'print.tpl.php'; |
| 0d7894a0 JV |
455 | |
| 456 | foreach ($filenames as $value) { | |
| b09f08f7 | 457 | // First in the theme directory |
| 7d62e7c4 | 458 | $file = drupal_get_path('theme', $GLOBALS['theme_key']) .'/'. $value; |
| 0d7894a0 JV |
459 | if (file_exists($file)) { |
| 460 | return $file; | |
| b09f08f7 JV |
461 | } |
| 462 | // Then in the module directory | |
| 7d62e7c4 | 463 | $file = drupal_get_path('module', 'print') .'/'. $value; |
| 0d7894a0 JV |
464 | if (file_exists($file)) { |
| 465 | return $file; | |
| b09f08f7 JV |
466 | } |
| 467 | } | |
| b09f08f7 JV |
468 | } |
| 469 | ||
| b09f08f7 | 470 | /** |
| 1e156b5a | 471 | * Prepare a Printer-friendly-ready node body for content nodes |
| 3483245d | 472 | * |
| 1e156b5a JV |
473 | * @param $nid |
| 474 | * node ID of the node to be rendered into a printer-friendly page | |
| 475 | * @param $cid | |
| 476 | * comment ID of the individual comment to be rendered | |
| 0706a465 JV |
477 | * @param $teaser |
| 478 | * if set to TRUE, outputs only the node's teaser | |
| fb70cd39 JV |
479 | * @param $message |
| 480 | * optional sender's message (used by the send e-mail module) | |
| 1e156b5a JV |
481 | * @return |
| 482 | * filled array ready to be used in the template | |
| b09f08f7 | 483 | */ |
| 27dc0ea1 | 484 | function _print_generate_node($nid, $cid = NULL, $teaser = FALSE, $message = NULL) { |
| b09f08f7 JV |
485 | // We can take a node id |
| 486 | $node = node_load(array('nid' => $nid)); | |
| 487 | if (!node_access('view', $node)) { | |
| 488 | // Access is denied | |
| 489 | return drupal_access_denied(); | |
| 490 | } | |
| b0a02151 | 491 | drupal_set_title($node->title); |
| b09f08f7 JV |
492 | |
| 493 | //alert other modules that we are generating a printer-friendly page, so they can choose to show/hide info | |
| 7e748d74 | 494 | $node->printing = TRUE; |
| b09f08f7 JV |
495 | // Turn off Pagination by the Paging module |
| 496 | unset($node->pages); | |
| 497 | unset($node->page_count); | |
| 498 | ||
| b09f08f7 JV |
499 | if ($cid === NULL) { |
| 500 | // Adapted (simplified) version of node_view for Drupal 5.x | |
| 501 | //Render the node content | |
| 0706a465 | 502 | $node = node_build_content($node, $teaser, TRUE); |
| b09f08f7 | 503 | // Disable fivestar widget output |
| 7d62e7c4 | 504 | unset($node->content['fivestar_widget']); |
| b09f08f7 | 505 | // Disable service links module output |
| 7d62e7c4 | 506 | unset($node->content['service_links']); |
| 3483245d | 507 | |
| b09f08f7 | 508 | $node->body = drupal_render($node->content); |
| 9490b60e JV |
509 | //TODO the following was part of the fix for http://drupal.org/node/254863 |
| 510 | //check if it is reproducible and find the exact condition which | |
| 511 | //triggered it | |
| 512 | //$node->body = html_entity_decode($node->body); | |
| b09f08f7 JV |
513 | } |
| 514 | ||
| 7d62e7c4 | 515 | $print_comments = variable_get('print_comments', PRINT_COMMENTS_DEFAULT); |
| b09f08f7 | 516 | |
| 7d62e7c4 | 517 | if (function_exists('comment_render') && (($cid != NULL) || ($print_comments))) { |
| b09f08f7 JV |
518 | //Print only the requested comment (or if $cid is NULL, all of them) |
| 519 | $comments = comment_render($node, $cid); | |
| 3483245d | 520 | |
| b09f08f7 | 521 | //Remove the comment forms |
| 7d62e7c4 | 522 | $comments = preg_replace('!<form.*?id="comment-.*?">.*?</form>!sim', '', $comments); |
| b09f08f7 | 523 | //Remove the 'Post new comment' title |
| 7d62e7c4 | 524 | $comments = preg_replace('!<h2.*?>Post new comment</h2>!', '', $comments); |
| b09f08f7 | 525 | //Remove the comment title hyperlink |
| 7d62e7c4 | 526 | $comments = preg_replace('!(<h3.*?>)(<a.*?>)(.*?)</a>(</h3>)!', '$1$3$4', $comments); |
| e458482f | 527 | //Remove the comment author link |
| 7d62e7c4 | 528 | $pattern = '!(<span class="submitted">)(.*?)<a.*?>(.*?)</a>(</span>)!sim'; |
| c42f715c | 529 | if (preg_match($pattern, $comments)) { |
| 7d62e7c4 | 530 | $comments = preg_replace($pattern , '$1$2$3$4', $comments); |
| c42f715c | 531 | } |
| b09f08f7 | 532 | //Remove the comment links |
| 7d62e7c4 | 533 | $comments = preg_replace('!\s*<ul class="links">.*?</ul>!sim', '', $comments); |
| b09f08f7 JV |
534 | if ($cid != NULL) { |
| 535 | // Single comment requested, output only the comment | |
| 536 | unset($node->body); | |
| 537 | } | |
| 538 | $node->body .= $comments; | |
| 539 | } | |
| 540 | ||
| 7e748d74 | 541 | node_invoke_nodeapi($node, 'alter', FALSE, TRUE); |
| b09f08f7 JV |
542 | |
| 543 | // Convert the a href elements | |
| 7d62e7c4 JV |
544 | $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is'; |
| 545 | $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body); | |
| b09f08f7 JV |
546 | |
| 547 | init_theme(); | |
| 548 | ||
| 27dc0ea1 | 549 | $print = _print_var_generator($node, $message, $cid); |
| b09f08f7 JV |
550 | |
| 551 | return $print; | |
| 552 | } | |
| 553 | ||
| 554 | /** | |
| 1e156b5a | 555 | * Prepare a Printer-friendly-ready node body for non-content pages |
| 3483245d | 556 | * |
| 1e156b5a JV |
557 | * @param $path |
| 558 | * path of the node to be rendered into a printer-friendly page | |
| fb70cd39 JV |
559 | * @param $teaser |
| 560 | * if set to TRUE, outputs only the node's teaser | |
| 561 | * @param $message | |
| 562 | * optional sender's message (used by the send e-mail module) | |
| 1e156b5a JV |
563 | * @return |
| 564 | * filled array ready to be used in the template | |
| b09f08f7 | 565 | */ |
| 27dc0ea1 | 566 | function _print_generate_path($path, $teaser = FALSE, $message = NULL) { |
| b09f08f7 JV |
567 | $path = drupal_get_normal_path($path); |
| 568 | ||
| 569 | menu_set_active_item($path); | |
| 570 | // Adapted from index.php. | |
| 571 | $node = new stdClass(); | |
| 572 | $node->body = menu_execute_active_handler($path); | |
| 00590736 | 573 | $node->title = drupal_get_title(); |
| b09f08f7 JV |
574 | $node->path = $path; |
| 575 | ||
| 576 | // It may happen that a drupal_not_found is called in the above call | |
| 577 | if (preg_match('/404 Not Found/', drupal_get_headers()) == 1) { | |
| 578 | return; | |
| 579 | } | |
| 580 | ||
| 9eaec26d JV |
581 | if (is_int($node->body)) { |
| 582 | switch ($node->body) { | |
| 583 | case MENU_NOT_FOUND: | |
| 584 | return drupal_not_found(); | |
| 585 | break; | |
| 586 | case MENU_ACCESS_DENIED: | |
| 587 | return drupal_access_denied(); | |
| 588 | break; | |
| 589 | } | |
| b09f08f7 JV |
590 | } |
| 591 | ||
| 592 | // Delete any links area | |
| 7d62e7c4 | 593 | $node->body = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->body); |
| b09f08f7 JV |
594 | |
| 595 | // Convert the a href elements | |
| 7d62e7c4 JV |
596 | $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is'; |
| 597 | $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body); | |
| b09f08f7 JV |
598 | |
| 599 | init_theme(); | |
| 600 | ||
| 27dc0ea1 | 601 | $print = _print_var_generator($node, $message); |
| b09f08f7 JV |
602 | |
| 603 | return $print; | |
| 604 | } | |
| 605 | ||
| 606 | ||
| 607 | /** | |
| 1e156b5a | 608 | * Prepare a Printer-friendly-ready node body for book pages |
| 3483245d | 609 | * |
| 1e156b5a JV |
610 | * @param $nid |
| 611 | * node ID of the node to be rendered into a printer-friendly page | |
| fb70cd39 JV |
612 | * @param $teaser |
| 613 | * if set to TRUE, outputs only the node's teaser | |
| 614 | * @param $message | |
| 615 | * optional sender's message (used by the send e-mail module) | |
| 1e156b5a JV |
616 | * @return |
| 617 | * filled array ready to be used in the template | |
| b09f08f7 | 618 | */ |
| 27dc0ea1 | 619 | function _print_generate_book($nid, $teaser = FALSE, $message = NULL) { |
| b09f08f7 JV |
620 | $node = node_load(array('nid' => $nid)); |
| 621 | if (!node_access('view', $node) || (!user_access('access printer-friendly version'))) { | |
| 622 | // Access is denied | |
| 623 | return drupal_access_denied(); | |
| 624 | } | |
| 625 | ||
| 626 | $tree = book_menu_subtree_data($node->book); | |
| 627 | $node->body = book_export_traverse($tree, 'book_node_export'); | |
| 628 | ||
| 629 | // Convert the a href elements | |
| 7d62e7c4 JV |
630 | $pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is'; |
| 631 | $node->body = preg_replace_callback($pattern, '_print_rewrite_urls', $node->body); | |
| b09f08f7 JV |
632 | |
| 633 | init_theme(); | |
| 634 | ||
| 27dc0ea1 | 635 | $print = _print_var_generator($node, $message); |
| b09f08f7 | 636 | // The title is already displayed by the book_recurse, so avoid duplication |
| 7d62e7c4 | 637 | $print['title'] = ''; |
| b09f08f7 JV |
638 | |
| 639 | return $print; | |
| 640 | } |