1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Contains the functions to generate Printer-friendly pages.
|
6
|
*
|
7
|
* This file is included by the core PF module, and includes all the
|
8
|
* functions necessary to generate a PF version of the original page
|
9
|
* in HTML format.
|
10
|
*
|
11
|
* @ingroup print
|
12
|
*/
|
13
|
|
14
|
$_print_urls = PRINT_URLS_DEFAULT;
|
15
|
|
16
|
/**
|
17
|
* Generate an HTML version of the printer-friendly page.
|
18
|
*
|
19
|
* @see print_controller()
|
20
|
*/
|
21
|
function print_controller_html() {
|
22
|
$args = func_get_args();
|
23
|
$path = filter_xss(implode('/', $args));
|
24
|
$cid = isset($_GET['comment']) ? (int) $_GET['comment'] : NULL;
|
25
|
$link = print_print_link();
|
26
|
|
27
|
$node = print_controller($path, $link['format'], $cid);
|
28
|
if ($node) {
|
29
|
// Handle the query.
|
30
|
$query = $_GET;
|
31
|
unset($query['q']);
|
32
|
|
33
|
$html = theme('print', array(
|
34
|
'node' => $node,
|
35
|
'query' => $query,
|
36
|
'format' => $link['format'],
|
37
|
));
|
38
|
drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
|
39
|
drupal_send_headers();
|
40
|
print $html;
|
41
|
|
42
|
$nodepath = (isset($node->nid)) ? 'node/' . $node->nid : drupal_get_normal_path($path);
|
43
|
db_merge('print_page_counter')
|
44
|
->key(array('path' => substr($nodepath, 0, 255)))
|
45
|
->fields(array(
|
46
|
'totalcount' => 1,
|
47
|
'timestamp' => REQUEST_TIME,
|
48
|
))
|
49
|
->expression('totalcount', 'totalcount + 1')
|
50
|
->execute();
|
51
|
}
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* Select the print generator function based on the page type.
|
56
|
*
|
57
|
* Depending on the type of node, this functions chooses the appropriate
|
58
|
* generator function.
|
59
|
*
|
60
|
* @param string $path
|
61
|
* Path of the original page.
|
62
|
* @param string $format
|
63
|
* Format of the page being generated.
|
64
|
* @param int $cid
|
65
|
* Comment ID of the individual comment to be rendered.
|
66
|
* @param string $view_mode
|
67
|
* (Optional) view mode to be used when rendering the content.
|
68
|
*
|
69
|
* @return object
|
70
|
* node-like object to be used in the print template
|
71
|
*
|
72
|
* @see _print_generate_node()
|
73
|
* @see _print_generate_path()
|
74
|
* @see _print_generate_book()
|
75
|
* @see print_preprocess_print()
|
76
|
*/
|
77
|
function print_controller($path, $format, $cid = NULL, $view_mode = PRINT_VIEW_MODE) {
|
78
|
if (empty($path)) {
|
79
|
// If no path was provided, let's try to generate a page for the referer.
|
80
|
global $base_url;
|
81
|
|
82
|
$ref = $_SERVER['HTTP_REFERER'];
|
83
|
$path = preg_replace("!^$base_url/!", '', $ref);
|
84
|
if (($path === $ref) || empty($path)) {
|
85
|
$path = variable_get('site_frontpage', 'node');
|
86
|
}
|
87
|
}
|
88
|
if ($alias = drupal_lookup_path('source', $path)) {
|
89
|
// Indirect call with print/alias
|
90
|
// If there is a path alias with these arguments, generate a
|
91
|
// printer-friendly version for it.
|
92
|
$path = $alias;
|
93
|
}
|
94
|
$parts = explode('/', $path);
|
95
|
$node_router = variable_get('print_node_router', FALSE);
|
96
|
if (($parts[0] == 'node') && (count($parts) > 1) && ctype_digit($parts[1]) && !$node_router) {
|
97
|
array_shift($parts);
|
98
|
$path = implode('/', $parts);
|
99
|
}
|
100
|
$revision_view = preg_match('!^[\d]*/revisions/[\d]*/view$!', $path);
|
101
|
if (ctype_digit($parts[0]) && ((count($parts) == 1) || $revision_view) && !$node_router) {
|
102
|
$vid = $revision_view ? $parts[2] : NULL;
|
103
|
$node = _print_generate_node($path, $format, $vid, $cid, $view_mode);
|
104
|
}
|
105
|
else {
|
106
|
$ret = preg_match('!^book/export/html/(.*)!i', $path, $matches);
|
107
|
if ($ret == 1) {
|
108
|
// This is a book PF page link, handle trough the book handling functions.
|
109
|
$node = _print_generate_book($matches[1], $format);
|
110
|
}
|
111
|
else {
|
112
|
// If no content node was found, handle the page printing with the
|
113
|
// 'printable' engine.
|
114
|
$node = _print_generate_path($path, $format);
|
115
|
}
|
116
|
}
|
117
|
|
118
|
return $node;
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Implements hook_preprocess_HOOK().
|
123
|
*/
|
124
|
function print_preprocess_print(&$variables) {
|
125
|
$node = $variables['node'];
|
126
|
$format = $variables['format'];
|
127
|
$path = drupal_get_path_alias(empty($node->nid) ? $node->path : "node/$node->nid");
|
128
|
|
129
|
static $hooks = NULL;
|
130
|
if (!isset($hooks)) {
|
131
|
drupal_theme_initialize();
|
132
|
$hooks = theme_get_registry();
|
133
|
}
|
134
|
|
135
|
$variables['page']['#show_messages'] = FALSE;
|
136
|
$variables['theme_hook_suggestions'] = array();
|
137
|
|
138
|
// Stolen from theme() so that ALL preprocess functions are called.
|
139
|
$hook = 'page';
|
140
|
$info = $hooks[$hook];
|
141
|
if (isset($info['preprocess functions']) || isset($info['process functions'])) {
|
142
|
foreach (array('preprocess functions', 'process functions') as $phase) {
|
143
|
if (!empty($info[$phase])) {
|
144
|
foreach ($info[$phase] as $processor_function) {
|
145
|
if (function_exists($processor_function)) {
|
146
|
// We don't want a poorly behaved process function changing $hook.
|
147
|
$hook_clone = $hook;
|
148
|
$processor_function($variables, $hook_clone);
|
149
|
}
|
150
|
}
|
151
|
}
|
152
|
}
|
153
|
}
|
154
|
|
155
|
$logo_url = FALSE;
|
156
|
switch (variable_get('print_logo_options', PRINT_LOGO_OPTIONS_DEFAULT)) {
|
157
|
// Theme logo.
|
158
|
case 1:
|
159
|
$logo_url = theme_get_setting('logo');
|
160
|
break;
|
161
|
|
162
|
// User-specifed logo.
|
163
|
case 2:
|
164
|
$logo_url = strip_tags(variable_get('print_logo_url', PRINT_LOGO_URL_DEFAULT));
|
165
|
break;
|
166
|
}
|
167
|
$logo_url = preg_replace('!^' . base_path() . '!', '', $logo_url);
|
168
|
|
169
|
$variables['print_logo'] = $logo_url ? theme('image', array(
|
170
|
'path' => $logo_url,
|
171
|
'alt' => variable_get('site_name', 'Drupal'),
|
172
|
'attributes' => array(
|
173
|
'class' => array('print-logo'),
|
174
|
'id' => 'logo',
|
175
|
),
|
176
|
)) : NULL;
|
177
|
|
178
|
$variables['print_node'] = $node;
|
179
|
$variables['content'] = $node->content;
|
180
|
$variables['scripts'] = drupal_get_js();
|
181
|
$variables['footer_scripts'] = drupal_get_js('footer');
|
182
|
$variables['sourceurl_enabled'] = variable_get('print_sourceurl_enabled', PRINT_SOURCEURL_ENABLED_DEFAULT);
|
183
|
$variables['url'] = url($path, array('absolute' => TRUE, 'query' => $variables['query']));
|
184
|
$variables['source_url'] = url(variable_get('print_sourceurl_forcenode', PRINT_SOURCEURL_FORCENODE_DEFAULT) ? drupal_get_normal_path($path) : $path, array(
|
185
|
'alias' => TRUE,
|
186
|
'absolute' => TRUE,
|
187
|
'query' => $variables['query'],
|
188
|
));
|
189
|
$variables['cid'] = isset($node->cid) ? $node->cid : NULL;
|
190
|
$variables['print_title'] = check_plain($node->title);
|
191
|
$variables['head'] = drupal_get_html_head();
|
192
|
$variables['robots_meta'] = _print_robots_meta_generator();
|
193
|
$variables['css'] = _print_css_generator($variables['expand_css']);
|
194
|
|
195
|
if (variable_get('print_html_sendtoprinter', PRINT_HTML_SENDTOPRINTER_DEFAULT) && ($format == 'html')) {
|
196
|
drupal_add_js('misc/drupal.js', array('group' => JS_LIBRARY));
|
197
|
|
198
|
$window_close = (variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT) && variable_get('print_html_windowclose', PRINT_HTML_WINDOWCLOSE_DEFAULT)) ? 'setTimeout(function(){window.close();}, 1);' : '';
|
199
|
$variables['sendtoprinter'] = '<script type="text/javascript">(function ($) { Drupal.behaviors.print = {attach: function() {$(window).load(function() {window.print();' . $window_close . '})}}})(jQuery);</script>';
|
200
|
}
|
201
|
|
202
|
$type = (isset($node->type)) ? $node->type : '';
|
203
|
$nid = (isset($node->nid)) ? $node->nid : '';
|
204
|
|
205
|
$variables['theme_hook_suggestions'][] = "print__node__{$type}";
|
206
|
$variables['theme_hook_suggestions'][] = "print__node__{$type}__{$nid}";
|
207
|
$variables['theme_hook_suggestions'][] = "print__{$format}";
|
208
|
$variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}";
|
209
|
$variables['theme_hook_suggestions'][] = "print__{$format}__node__{$type}__{$nid}";
|
210
|
}
|
211
|
|
212
|
/**
|
213
|
* Returns HTML for the published line of the print template.
|
214
|
*
|
215
|
* @param array $vars
|
216
|
* An empty associative array.
|
217
|
*
|
218
|
* @return string
|
219
|
* HTML text with the published line
|
220
|
*
|
221
|
* @ingroup themeable
|
222
|
* @ingroup print_themeable
|
223
|
*/
|
224
|
function theme_print_published($vars) {
|
225
|
global $base_url;
|
226
|
|
227
|
$published_site = variable_get('site_name', 0);
|
228
|
return $published_site ? t('Published on %site_name', array('%site_name' => $published_site)) . ' (' . l($base_url, $base_url) . ')' : '';
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* Returns HTML for the breadcrumb line of the print template.
|
233
|
*
|
234
|
* @param array $vars
|
235
|
* An associative array containing:
|
236
|
* - $node: the node object.
|
237
|
*
|
238
|
* @return string
|
239
|
* HTML text with the breadcrumb
|
240
|
*
|
241
|
* @ingroup themeable
|
242
|
* @ingroup print_themeable
|
243
|
*/
|
244
|
function theme_print_breadcrumb($vars) {
|
245
|
$node = $vars['node'];
|
246
|
$old_path = $_GET['q'];
|
247
|
|
248
|
$path = empty($node->nid) ? $node->path : "node/$node->nid";
|
249
|
menu_set_active_item($path);
|
250
|
$breadcrumb = drupal_get_breadcrumb();
|
251
|
if (!empty($breadcrumb)) {
|
252
|
$breadcrumb[] = menu_get_active_title();
|
253
|
menu_set_active_item($old_path);
|
254
|
return filter_xss(implode(' > ', $breadcrumb));
|
255
|
}
|
256
|
else {
|
257
|
menu_set_active_item($old_path);
|
258
|
return '';
|
259
|
}
|
260
|
}
|
261
|
|
262
|
/**
|
263
|
* Returns HTML for the footer of the print template.
|
264
|
*
|
265
|
* @param array $vars
|
266
|
* An empty associative array.
|
267
|
*
|
268
|
* @return string
|
269
|
* HTML text with the footer
|
270
|
*
|
271
|
* @ingroup themeable
|
272
|
* @ingroup print_themeable
|
273
|
*/
|
274
|
function theme_print_footer($vars) {
|
275
|
$footer = '';
|
276
|
|
277
|
switch (variable_get('print_footer_options', PRINT_FOOTER_OPTIONS_DEFAULT)) {
|
278
|
// Theme footer.
|
279
|
case 1:
|
280
|
$footer_blocks = block_get_blocks_by_region('footer');
|
281
|
$footer = variable_get('site_footer', FALSE) . "\n" . drupal_render($footer_blocks);
|
282
|
break;
|
283
|
|
284
|
// User-specified footer.
|
285
|
case 2:
|
286
|
$footer = variable_get('print_footer_user', PRINT_FOOTER_USER_DEFAULT);
|
287
|
break;
|
288
|
}
|
289
|
// Delete the contextual links.
|
290
|
$footer = preg_replace('!\s*<div class="contextual-links-wrapper">.*?</div>!sim', '', $footer);
|
291
|
|
292
|
return filter_xss_admin($footer);
|
293
|
}
|
294
|
|
295
|
/**
|
296
|
* Returns HTML for the source URL line of the print template.
|
297
|
*
|
298
|
* @param array $vars
|
299
|
* An associative array containing:
|
300
|
* - $url: the URL to the full node view.
|
301
|
* - $node: the node object.
|
302
|
* - $cid; comment ID of the comment to display.
|
303
|
*
|
304
|
* @return string
|
305
|
* HTML text with the footer
|
306
|
*
|
307
|
* @ingroup themeable
|
308
|
* @ingroup print_themeable
|
309
|
*/
|
310
|
function theme_print_sourceurl($vars) {
|
311
|
$sourceurl_date = variable_get('print_sourceurl_date', PRINT_SOURCEURL_DATE_DEFAULT);
|
312
|
$url = is_int($vars['cid']) ? $vars['url'] . '#comment-' . $vars['cid'] : $vars['url'];
|
313
|
|
314
|
$output = '<strong>' . t('Source URL');
|
315
|
if ($sourceurl_date && isset($vars['node'])) {
|
316
|
$output .= ' (';
|
317
|
$date = format_date($vars['node']->changed, 'short');
|
318
|
|
319
|
$output .= empty($vars['node']->nid) ? t('retrieved on !date', array('!date' => $date)) : t('modified on !date', array('!date' => $date));
|
320
|
|
321
|
$output .= ')';
|
322
|
}
|
323
|
$output .= ':</strong> ' . $url;
|
324
|
|
325
|
return $output;
|
326
|
}
|
327
|
|
328
|
/**
|
329
|
* Returns HTML for the URL list of the print template.
|
330
|
*
|
331
|
* @param array $vars
|
332
|
* An empty associative array.
|
333
|
*
|
334
|
* @return string
|
335
|
* HTML text with the URL list
|
336
|
*
|
337
|
* @ingroup themeable
|
338
|
* @ingroup print_themeable
|
339
|
*/
|
340
|
function theme_print_url_list($vars) {
|
341
|
global $_print_urls;
|
342
|
|
343
|
// Display the collected links at the bottom of the page. Code once taken from
|
344
|
// Kjartan Mannes' project.module.
|
345
|
if (!empty($_print_urls)) {
|
346
|
$urls = _print_friendly_urls();
|
347
|
$url_list = '';
|
348
|
foreach ($urls as $key => $url) {
|
349
|
drupal_alter('print_url_list', $url);
|
350
|
$url_list .= '[' . ($key + 1) . '] ' . check_plain($url) . "<br />\n";
|
351
|
}
|
352
|
if (!empty($url_list)) {
|
353
|
return "<p><strong>" . t('Links') . "</strong><br />$url_list</p>";
|
354
|
}
|
355
|
}
|
356
|
return '';
|
357
|
}
|
358
|
|
359
|
/**
|
360
|
* Generates a robots meta tag to tell them what they may index.
|
361
|
*
|
362
|
* @return string
|
363
|
* meta robots tag
|
364
|
*/
|
365
|
function _print_robots_meta_generator() {
|
366
|
$robots_meta = array();
|
367
|
|
368
|
if (variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT)) {
|
369
|
$robots_meta[] = 'noindex';
|
370
|
}
|
371
|
if (variable_get('print_robots_nofollow', PRINT_ROBOTS_NOFOLLOW_DEFAULT)) {
|
372
|
$robots_meta[] = 'nofollow';
|
373
|
}
|
374
|
if (variable_get('print_robots_noarchive', PRINT_ROBOTS_NOARCHIVE_DEFAULT)) {
|
375
|
$robots_meta[] = 'noarchive';
|
376
|
}
|
377
|
|
378
|
if (count($robots_meta) > 0) {
|
379
|
return '<meta name="robots" content="' . implode(', ', $robots_meta) . '" />';
|
380
|
}
|
381
|
else {
|
382
|
return '';
|
383
|
}
|
384
|
}
|
385
|
|
386
|
/**
|
387
|
* Generates the CSS directive to include in the printer-friendly version.
|
388
|
*
|
389
|
* @param bool $expand
|
390
|
* If TRUE, the provided CSS will be expanded, instead of given as a list
|
391
|
* of includes.
|
392
|
*
|
393
|
* @return string
|
394
|
* applicable CSS
|
395
|
*/
|
396
|
function _print_css_generator($expand = FALSE) {
|
397
|
$print_css = variable_get('print_css', PRINT_CSS_DEFAULT);
|
398
|
|
399
|
if (!empty($print_css)) {
|
400
|
drupal_add_css(strtr($print_css, array('%t' => drupal_get_path('theme', variable_get('theme_default')))));
|
401
|
}
|
402
|
else {
|
403
|
drupal_add_css(drupal_get_path('module', 'print') . '/css/print.css');
|
404
|
}
|
405
|
$drupal_css = drupal_add_css();
|
406
|
if (!variable_get('print_keep_theme_css', PRINT_KEEP_THEME_CSS_DEFAULT)) {
|
407
|
foreach ($drupal_css as $key => $css_file) {
|
408
|
if ($css_file['group'] == CSS_THEME) {
|
409
|
// Unset the theme's CSS.
|
410
|
unset($drupal_css[$key]);
|
411
|
}
|
412
|
}
|
413
|
}
|
414
|
|
415
|
// Expand the CSS if requested.
|
416
|
if ($expand) {
|
417
|
$style = '';
|
418
|
$css_files = array_keys($drupal_css);
|
419
|
foreach ($css_files as $filename) {
|
420
|
if (file_exists($filename)) {
|
421
|
$style .= file_get_contents($filename, TRUE);
|
422
|
}
|
423
|
}
|
424
|
return "<style type='text/css' media='all'>$style</style>\n";
|
425
|
}
|
426
|
else {
|
427
|
return drupal_get_css($drupal_css);
|
428
|
}
|
429
|
}
|
430
|
|
431
|
/**
|
432
|
* Callback function for the preg_replace_callback for URL-capable patterns.
|
433
|
*
|
434
|
* Manipulate URLs to make them absolute in the URLs list, and add a [n]
|
435
|
* footnote marker.
|
436
|
*
|
437
|
* @param array $matches
|
438
|
* Array with the matched tag patterns, usually <a...>+text+</a>.
|
439
|
*
|
440
|
* @return string
|
441
|
* tag with re-written URL and, if applicable, the [n] index to the URL list
|
442
|
*/
|
443
|
function _print_rewrite_urls($matches) {
|
444
|
global $base_url, $base_root, $_print_urls;
|
445
|
|
446
|
$include_anchors = variable_get('print_urls_anchors', PRINT_URLS_ANCHORS_DEFAULT);
|
447
|
|
448
|
// First, split the html into the different tag attributes.
|
449
|
$pattern = '!\s*(\w+\s*=\s*"(?:\\\"|[^"])*")\s*|\s*(\w+\s*=\s*\'(?:\\\\\'|[^\'])*\')\s*|\s*(\w+\s*=\s*\w+)\s*|\s+!';
|
450
|
$attribs = preg_split($pattern, $matches[1], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
|
451
|
foreach ($attribs as $key => $value) {
|
452
|
$attribs[$key] = preg_replace('!(\w)\s*=\s*(.*)!', '$1=$2', $value);
|
453
|
}
|
454
|
|
455
|
$size = count($attribs);
|
456
|
for ($i = 1; $i < $size; $i++) {
|
457
|
// If the attribute is href or src, rewrite the URL in the value.
|
458
|
if (preg_match('!^(?:href|src)\s*?=(.*)!i', $attribs[$i], $urls) > 0) {
|
459
|
$url = trim($urls[1], " \t\n\r\0\x0B\"'");
|
460
|
|
461
|
if (empty($url)) {
|
462
|
// If URL is empty, use current_url.
|
463
|
$path = explode('/', $_GET['q']);
|
464
|
unset($path[0]);
|
465
|
$path = implode('/', $path);
|
466
|
if (ctype_digit($path)) {
|
467
|
$path = "node/$path";
|
468
|
}
|
469
|
// Printer-friendly URLs is on, so we need to make it absolute.
|
470
|
$newurl = url($path, array(
|
471
|
'fragment' => drupal_substr($url, 1),
|
472
|
'absolute' => TRUE,
|
473
|
));
|
474
|
}
|
475
|
elseif (strpos(html_entity_decode($url), '://') || preg_match('!^mailto:.*?@.*?\..*?$!iu', html_entity_decode($url))) {
|
476
|
// URL is absolute, do nothing.
|
477
|
$newurl = $url;
|
478
|
}
|
479
|
elseif (strpos(html_entity_decode($url), '//') === 0) {
|
480
|
// URL is 'almost absolute', but it does not contain protocol; replace
|
481
|
// with base_path protocol.
|
482
|
$newurl = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . ":" . $url;
|
483
|
$matches[1] = str_replace($url, $newurl, $matches[1]);
|
484
|
}
|
485
|
else {
|
486
|
if ($url[0] == '#') {
|
487
|
// URL is an anchor tag.
|
488
|
if ($include_anchors && (!empty($_print_urls))) {
|
489
|
$path = explode('/', $_GET['q']);
|
490
|
unset($path[0]);
|
491
|
$path = implode('/', $path);
|
492
|
if (ctype_digit($path)) {
|
493
|
$path = "node/$path";
|
494
|
}
|
495
|
// Printer-friendly URLs is on, so we need to make it absolute.
|
496
|
$newurl = url($path, array(
|
497
|
'fragment' => drupal_substr($url, 1),
|
498
|
'absolute' => TRUE,
|
499
|
));
|
500
|
}
|
501
|
// Because base href is the original page, change the link to
|
502
|
// still be usable inside the print page.
|
503
|
$matches[1] = str_replace($url, check_plain(base_path() . $_GET['q'] . $url), $matches[1]);
|
504
|
}
|
505
|
else {
|
506
|
// URL is relative, convert it into absolute URL.
|
507
|
if ($url[0] == '/') {
|
508
|
// If it starts with '/' just append it to the server name.
|
509
|
$newurl = $base_root . '/' . trim($url, '/');
|
510
|
}
|
511
|
elseif (preg_match('!^(?:index.php)?\?q=!i', $url)) {
|
512
|
// If it starts with ?q=, just prepend with the base URL.
|
513
|
$newurl = $base_url . '/' . trim($url, '/');
|
514
|
}
|
515
|
else {
|
516
|
$newurl = url(trim($url, '/'), array('absolute' => TRUE));
|
517
|
}
|
518
|
$matches[1] = str_replace($url, $newurl, $matches[1]);
|
519
|
}
|
520
|
}
|
521
|
}
|
522
|
}
|
523
|
|
524
|
$ret = '<' . $matches[1] . '>';
|
525
|
if (count($matches) == 4) {
|
526
|
$ret .= $matches[2] . $matches[3];
|
527
|
if ((!empty($_print_urls)) && (isset($newurl))) {
|
528
|
$ret .= ' <span class="print-footnote">[' . _print_friendly_urls(trim($newurl)) . ']</span>';
|
529
|
}
|
530
|
}
|
531
|
|
532
|
return filter_xss_admin($ret);
|
533
|
}
|
534
|
|
535
|
/**
|
536
|
* Auxiliary function to store the Printer-friendly URLs list as static.
|
537
|
*
|
538
|
* @param string|int $url
|
539
|
* Absolute URL to be inserted in the list.
|
540
|
*
|
541
|
* @return array|int
|
542
|
* list of URLs previously stored if $url is 0, or the current count
|
543
|
* otherwise.
|
544
|
*/
|
545
|
function _print_friendly_urls($url = 0) {
|
546
|
static $urls = array();
|
547
|
if ($url !== 0) {
|
548
|
$url_idx = array_search($url, $urls);
|
549
|
if ($url_idx !== FALSE) {
|
550
|
return ($url_idx + 1);
|
551
|
}
|
552
|
else {
|
553
|
$urls[] = $url;
|
554
|
return count($urls);
|
555
|
}
|
556
|
}
|
557
|
else {
|
558
|
$ret = $urls;
|
559
|
$urls = array();
|
560
|
return $ret;
|
561
|
}
|
562
|
}
|
563
|
|
564
|
/**
|
565
|
* Check URL list settings for this node.
|
566
|
*
|
567
|
* @param Object $node
|
568
|
* Node object.
|
569
|
* @param string $format
|
570
|
* Format of the page being generated.
|
571
|
*
|
572
|
* @return bool
|
573
|
* TRUE if URL list should be displayed, FALSE otherwise
|
574
|
*/
|
575
|
function _print_url_list_enabled($node, $format) {
|
576
|
if (!isset($node->type)) {
|
577
|
$node_urllist = variable_get('print_' . $format . '_display_sys_urllist', PRINT_TYPE_SYS_URLLIST_DEFAULT);
|
578
|
}
|
579
|
else {
|
580
|
$node_urllist = isset($node->{'print_' . $format . '_display_urllist'}) ? $node->{'print_' . $format . '_display_urllist'} : variable_get('print_' . $format . '_display_urllist_' . $node->type, PRINT_TYPE_URLLIST_DEFAULT);
|
581
|
}
|
582
|
|
583
|
// Get value of Printer-friendly URLs setting.
|
584
|
return (variable_get('print_urls', PRINT_URLS_DEFAULT) && ($node_urllist));
|
585
|
}
|
586
|
|
587
|
/**
|
588
|
* Prepare a Printer-friendly-ready node body for content nodes.
|
589
|
*
|
590
|
* @param int $nid
|
591
|
* Node ID of the node to be rendered into a printer-friendly page.
|
592
|
* @param string $format
|
593
|
* Format of the page being generated.
|
594
|
* @param int $vid
|
595
|
* (Optional) revision ID of the node to use.
|
596
|
* @param int $cid
|
597
|
* (Optional) comment ID of the individual comment to be rendered.
|
598
|
* @param string $view_mode
|
599
|
* (Optional) view mode to be used when rendering the content.
|
600
|
*
|
601
|
* @return object|bool
|
602
|
* filled node-like object to be used in the print template
|
603
|
*/
|
604
|
function _print_generate_node($nid, $format, $vid = NULL, $cid = NULL, $view_mode = PRINT_VIEW_MODE) {
|
605
|
global $_print_urls;
|
606
|
|
607
|
if (!isset($langcode)) {
|
608
|
$langcode = $GLOBALS['language_content']->language;
|
609
|
}
|
610
|
|
611
|
// We can take a node id.
|
612
|
$node = node_load($nid, $vid);
|
613
|
if (!$node) {
|
614
|
// Node not found.
|
615
|
drupal_not_found();
|
616
|
drupal_exit();
|
617
|
}
|
618
|
elseif (!node_access('view', $node)) {
|
619
|
// Access is denied.
|
620
|
drupal_access_denied();
|
621
|
drupal_exit();
|
622
|
}
|
623
|
drupal_set_title($node->title);
|
624
|
|
625
|
$build = array();
|
626
|
if ($cid === NULL) {
|
627
|
// Adapted (simplified) version of node_view
|
628
|
// Render the node content.
|
629
|
node_build_content($node, $view_mode);
|
630
|
|
631
|
// Disable the links area.
|
632
|
unset($node->content['links']);
|
633
|
|
634
|
$build = $node->content;
|
635
|
unset($node->content);
|
636
|
}
|
637
|
|
638
|
if (function_exists('comment_node_page_additions') &&
|
639
|
(($cid != NULL) || (variable_get('print_comments', PRINT_COMMENTS_DEFAULT)))) {
|
640
|
// Print only the requested comment (or if $cid is NULL, all of them).
|
641
|
$comments = comment_node_page_additions($node);
|
642
|
if (!empty($comments)) {
|
643
|
unset($comments['comment_form']);
|
644
|
foreach ($comments['comments'] as $key => &$comment) {
|
645
|
if (is_numeric($key)) {
|
646
|
if (($cid != NULL) && ($key != $cid)) {
|
647
|
unset($comments['comments'][$key]);
|
648
|
}
|
649
|
else {
|
650
|
unset($comment['links']);
|
651
|
}
|
652
|
}
|
653
|
}
|
654
|
|
655
|
$build['comments'] = $comments;
|
656
|
}
|
657
|
}
|
658
|
|
659
|
$build += array(
|
660
|
'#theme' => 'node',
|
661
|
'#node' => $node,
|
662
|
'#view_mode' => $view_mode,
|
663
|
'#language' => $langcode,
|
664
|
'#print_format' => $format,
|
665
|
);
|
666
|
|
667
|
$type = 'node';
|
668
|
drupal_alter(array('node_view', 'entity_view'), $build, $type);
|
669
|
|
670
|
$content = render($build);
|
671
|
|
672
|
// Get rid of any links before the content.
|
673
|
$parts = explode('<div class="content', $content, 2);
|
674
|
if (count($parts) == 2) {
|
675
|
$pattern = '!(.*?)<a [^>]*?>(.*?)</a>(.*?)!mis';
|
676
|
$parts[0] = preg_replace($pattern, '$1$2$3', $parts[0]);
|
677
|
$content = implode('<div class="content', $parts);
|
678
|
}
|
679
|
|
680
|
// Check URL list settings.
|
681
|
$_print_urls = _print_url_list_enabled($node, $format);
|
682
|
|
683
|
// Convert the a href elements.
|
684
|
$pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
|
685
|
$content = preg_replace_callback($pattern, '_print_rewrite_urls', $content);
|
686
|
|
687
|
$node->content = $content;
|
688
|
|
689
|
return $node;
|
690
|
}
|
691
|
|
692
|
/**
|
693
|
* Prepare a Printer-friendly-ready node body for non-content pages.
|
694
|
*
|
695
|
* @param string $path
|
696
|
* Path of the node to be rendered into a printer-friendly page.
|
697
|
* @param string $format
|
698
|
* Format of the page being generated.
|
699
|
*
|
700
|
* @return object|bool
|
701
|
* filled node-like object to be used in the print template
|
702
|
*/
|
703
|
function _print_generate_path($path, $format) {
|
704
|
global $_print_urls;
|
705
|
|
706
|
// Handle node tabs, or cases where the 'node_router' option is enabled.
|
707
|
$parts = explode('/', $path);
|
708
|
if (ctype_digit($parts[0]) && ((count($parts) > 1) || variable_get('print_node_router', FALSE))) {
|
709
|
$path = 'node/' . $path;
|
710
|
}
|
711
|
|
712
|
$path = drupal_get_normal_path($path);
|
713
|
|
714
|
menu_set_active_item($path);
|
715
|
// Adapted from index.php.
|
716
|
$node = new stdClass();
|
717
|
$node->content = menu_execute_active_handler($path, FALSE);
|
718
|
if (is_array($node->content)) {
|
719
|
$node->content = drupal_render($node->content);
|
720
|
}
|
721
|
|
722
|
if (is_int($node->content)) {
|
723
|
switch ($node->content) {
|
724
|
case MENU_NOT_FOUND:
|
725
|
drupal_not_found();
|
726
|
drupal_exit();
|
727
|
break;
|
728
|
|
729
|
case MENU_ACCESS_DENIED:
|
730
|
drupal_access_denied();
|
731
|
drupal_exit();
|
732
|
break;
|
733
|
}
|
734
|
}
|
735
|
|
736
|
$node->title = drupal_get_title();
|
737
|
$node->path = $path;
|
738
|
$node->changed = REQUEST_TIME;
|
739
|
$node->type = '';
|
740
|
|
741
|
// Delete any links area.
|
742
|
$node->content = preg_replace('!\s*<div class="links">.*?</div>!sim', '', $node->content);
|
743
|
|
744
|
// Delete the contextual links also.
|
745
|
$node->content = preg_replace('!\s*<div class="contextual-links-wrapper">.*?</div>!sim', '', $node->content);
|
746
|
|
747
|
// Check URL list settings.
|
748
|
$_print_urls = _print_url_list_enabled($node, $format);
|
749
|
|
750
|
// Convert the a href elements.
|
751
|
$pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
|
752
|
$node->content = preg_replace_callback($pattern, '_print_rewrite_urls', $node->content);
|
753
|
|
754
|
return $node;
|
755
|
}
|
756
|
|
757
|
/**
|
758
|
* Prepare a Printer-friendly-ready node body for book pages.
|
759
|
*
|
760
|
* @param int $nid
|
761
|
* Node ID of the node to be rendered into a printer-friendly page.
|
762
|
* @param string $format
|
763
|
* Format of the page being generated.
|
764
|
*
|
765
|
* @return object|bool
|
766
|
* filled node-like object to be used in the print template
|
767
|
*/
|
768
|
function _print_generate_book($nid, $format) {
|
769
|
global $_print_urls;
|
770
|
|
771
|
$node = node_load($nid);
|
772
|
if (!$node) {
|
773
|
// Node not found.
|
774
|
drupal_not_found();
|
775
|
drupal_exit();
|
776
|
}
|
777
|
elseif (!node_access('view', $node) || (!user_access('access printer-friendly version'))) {
|
778
|
// Access is denied.
|
779
|
drupal_access_denied();
|
780
|
drupal_exit();
|
781
|
}
|
782
|
|
783
|
$tree = book_menu_subtree_data($node->book);
|
784
|
$node->content = book_export_traverse($tree, 'book_node_export');
|
785
|
|
786
|
// Check URL list settings.
|
787
|
$_print_urls = _print_url_list_enabled($node, $format);
|
788
|
|
789
|
// Convert the a href elements.
|
790
|
$pattern = '!<(a\s[^>]*?)>(.*?)(</a>)!is';
|
791
|
$node->content = preg_replace_callback($pattern, '_print_rewrite_urls', $node->content);
|
792
|
|
793
|
return $node;
|
794
|
}
|