1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Displays Printer-friendly versions of Drupal pages.
|
6
|
*
|
7
|
* This is the core module of the PF package, with the Drupal hooks
|
8
|
* and other administrative functions.
|
9
|
*
|
10
|
* @ingroup print
|
11
|
*/
|
12
|
|
13
|
define('PRINT_VIEW_MODE', 'print');
|
14
|
|
15
|
define('PRINT_LOGO_OPTIONS_DEFAULT', 1);
|
16
|
define('PRINT_LOGO_URL_DEFAULT', '');
|
17
|
define('PRINT_FOOTER_OPTIONS_DEFAULT', 1);
|
18
|
define('PRINT_FOOTER_USER_DEFAULT', '');
|
19
|
define('PRINT_CSS_DEFAULT', '');
|
20
|
define('PRINT_KEEP_THEME_CSS_DEFAULT', 0);
|
21
|
define('PRINT_URLS_DEFAULT', 1);
|
22
|
define('PRINT_URLS_ANCHORS_DEFAULT', 0);
|
23
|
define('PRINT_COMMENTS_DEFAULT', 0);
|
24
|
define('PRINT_NODE_ROUTER_DEFAULT', 0);
|
25
|
define('PRINT_NEWWINDOW_DEFAULT', 1);
|
26
|
|
27
|
define('PRINT_TYPE_URLLIST_DEFAULT', 1);
|
28
|
define('PRINT_TYPE_SYS_URLLIST_DEFAULT', 0);
|
29
|
define('PRINT_TYPE_LINK_TEXT_ENABLED_DEFAULT', 0);
|
30
|
|
31
|
define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
|
32
|
define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);
|
33
|
define('PRINT_HTML_WINDOWCLOSE_DEFAULT', 1);
|
34
|
|
35
|
define('PRINT_SOURCEURL_ENABLED_DEFAULT', 1);
|
36
|
define('PRINT_SOURCEURL_DATE_DEFAULT', 0);
|
37
|
define('PRINT_SOURCEURL_FORCENODE_DEFAULT', 0);
|
38
|
|
39
|
define('PRINT_ROBOTS_NOINDEX_DEFAULT', 1);
|
40
|
define('PRINT_ROBOTS_NOFOLLOW_DEFAULT', 1);
|
41
|
define('PRINT_ROBOTS_NOARCHIVE_DEFAULT', 0);
|
42
|
|
43
|
define('PRINT_LIB_PATH', 'sites/all/libraries');
|
44
|
|
45
|
/**
|
46
|
* Implements hook_print_link().
|
47
|
*/
|
48
|
function print_print_link() {
|
49
|
return array(
|
50
|
'format' => 'html',
|
51
|
'text' => t('Printer-friendly version'),
|
52
|
'description' => t('Display a printer-friendly version of this page.'),
|
53
|
'path' => 'print',
|
54
|
'class' => 'print-page',
|
55
|
'icon' => 'print_icon.png',
|
56
|
'module' => 'print',
|
57
|
);
|
58
|
}
|
59
|
|
60
|
/**
|
61
|
* Implements hook_print_new_window_alter().
|
62
|
*/
|
63
|
function print_print_new_window_alter(&$new_window, $format) {
|
64
|
$new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Implements hook_permission().
|
69
|
*/
|
70
|
function print_permission() {
|
71
|
return array(
|
72
|
'administer print' => array(
|
73
|
'title' => t('Administer the module'),
|
74
|
'description' => t('Perform maintenance tasks for the print module.'),
|
75
|
),
|
76
|
'access print' => array(
|
77
|
'title' => t('Access the printer-friendly page'),
|
78
|
'description' => t('View the printer-friendly pages and the links to them in the original pages.'),
|
79
|
),
|
80
|
);
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Implements hook_theme().
|
85
|
*/
|
86
|
function print_theme() {
|
87
|
return array(
|
88
|
'print' => array(
|
89
|
'variables' => array(
|
90
|
'node' => NULL,
|
91
|
'query' => NULL,
|
92
|
'format' => '',
|
93
|
'expand_css' => FALSE,
|
94
|
'message' => '',
|
95
|
),
|
96
|
'template' => 'print',
|
97
|
'file' => 'print.pages.inc',
|
98
|
),
|
99
|
'print_published' => array(
|
100
|
'variables' => array(),
|
101
|
'file' => 'print.pages.inc',
|
102
|
),
|
103
|
'print_breadcrumb' => array(
|
104
|
'variables' => array('node' => NULL),
|
105
|
'file' => 'print.pages.inc',
|
106
|
),
|
107
|
'print_footer' => array(
|
108
|
'variables' => array(),
|
109
|
'file' => 'print.pages.inc',
|
110
|
),
|
111
|
'print_sourceurl' => array(
|
112
|
'variables' => array('url' => '', 'node' => NULL, 'cid' => NULL),
|
113
|
'file' => 'print.pages.inc',
|
114
|
),
|
115
|
'print_url_list' => array(
|
116
|
'variables' => array(),
|
117
|
'file' => 'print.pages.inc',
|
118
|
),
|
119
|
);
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Implements hook_preprocess_HOOK().
|
124
|
*/
|
125
|
function print_preprocess_node(&$variables) {
|
126
|
if (($variables['elements']['#view_mode'] == PRINT_VIEW_MODE) && isset($variables['elements']['#print_format'])) {
|
127
|
$type = $variables['elements']['#node']->type;
|
128
|
$format = $variables['elements']['#print_format'];
|
129
|
$nid = $variables['elements']['#node']->nid;
|
130
|
|
131
|
$variables['theme_hook_suggestions'][] = "node__print";
|
132
|
$variables['theme_hook_suggestions'][] = "node__print__{$format}";
|
133
|
$variables['theme_hook_suggestions'][] = "node__print__{$format}__node__{$type}";
|
134
|
$variables['theme_hook_suggestions'][] = "node__print__{$format}__node__{$nid}";
|
135
|
}
|
136
|
}
|
137
|
|
138
|
/**
|
139
|
* Implements hook_menu().
|
140
|
*/
|
141
|
function print_menu() {
|
142
|
$link = print_print_link();
|
143
|
$items = array();
|
144
|
|
145
|
$items[$link['path']] = array(
|
146
|
'title' => 'Printer-friendly',
|
147
|
'page callback' => 'print_controller_html',
|
148
|
'access arguments' => array('access print'),
|
149
|
'type' => MENU_CALLBACK,
|
150
|
'file' => 'print.pages.inc',
|
151
|
);
|
152
|
$items[$link['path'] . '/' . $link['path']] = array(
|
153
|
'access callback' => FALSE,
|
154
|
);
|
155
|
$items['admin/config/user-interface/print'] = array(
|
156
|
'title' => 'Printer, email and PDF versions',
|
157
|
'description' => 'Adds a printer-friendly version link to content and administrative pages.',
|
158
|
'page callback' => 'drupal_get_form',
|
159
|
'page arguments' => array('print_html_settings'),
|
160
|
'access arguments' => array('administer print'),
|
161
|
'file' => 'print.admin.inc',
|
162
|
);
|
163
|
$items['admin/config/user-interface/print/html'] = array(
|
164
|
'title' => 'Web page',
|
165
|
'weight' => 1,
|
166
|
'type' => MENU_DEFAULT_LOCAL_TASK,
|
167
|
);
|
168
|
$items['admin/config/user-interface/print/common'] = array(
|
169
|
'title' => 'Settings',
|
170
|
'description' => 'Settings for the common functionalities for all the print sub-modules.',
|
171
|
'page callback' => 'drupal_get_form',
|
172
|
'page arguments' => array('print_main_settings'),
|
173
|
'access arguments' => array('administer print'),
|
174
|
'weight' => 10,
|
175
|
'type' => MENU_LOCAL_TASK,
|
176
|
'file' => 'print.admin.inc',
|
177
|
);
|
178
|
|
179
|
return $items;
|
180
|
}
|
181
|
|
182
|
/**
|
183
|
* Implements hook_variable_info().
|
184
|
*/
|
185
|
function print_variable_info($options) {
|
186
|
$link = print_print_link();
|
187
|
|
188
|
$variable['print_html_link_text'] = array(
|
189
|
'title' => t('Printer-friendly version'),
|
190
|
'description' => t('Text used in the link to the printer-friendly version.'),
|
191
|
'type' => 'string',
|
192
|
'default' => t($link['text']),
|
193
|
);
|
194
|
|
195
|
return $variable;
|
196
|
}
|
197
|
|
198
|
/**
|
199
|
* Implements hook_block_info().
|
200
|
*/
|
201
|
function print_block_info() {
|
202
|
$block['print-top']['info'] = t('Most printed');
|
203
|
$block['print-top']['cache'] = DRUPAL_CACHE_GLOBAL;
|
204
|
return $block;
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* Implements hook_block_view().
|
209
|
*/
|
210
|
function print_block_view($delta = '') {
|
211
|
$block = array();
|
212
|
|
213
|
switch ($delta) {
|
214
|
case 'print-top':
|
215
|
$block['subject'] = t('Most printed');
|
216
|
$result = db_query_range("SELECT path FROM {print_page_counter} LEFT JOIN {node} n ON path = CONCAT('node/', n.nid) WHERE status <> 0 OR status IS NULL ORDER BY totalcount DESC", 0, 3)
|
217
|
->fetchAll();
|
218
|
if (count($result)) {
|
219
|
$items = array();
|
220
|
foreach ($result as $obj) {
|
221
|
$items[] = l(_print_get_title($obj->path), $obj->path);
|
222
|
}
|
223
|
$block['content'] = theme('item_list', array('items' => $items, 'type' => 'ul'));
|
224
|
}
|
225
|
break;
|
226
|
}
|
227
|
return $block;
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Implements hook_help().
|
232
|
*/
|
233
|
function print_help($path, $arg) {
|
234
|
$ret = '';
|
235
|
switch ($path) {
|
236
|
case 'admin/help#print':
|
237
|
// Return a line-break version of the module README.
|
238
|
$ret = _filter_autop(file_get_contents(drupal_get_path('module', 'print') . '/README.txt'));
|
239
|
}
|
240
|
return $ret;
|
241
|
}
|
242
|
|
243
|
/**
|
244
|
* Implements hook_node_delete().
|
245
|
*/
|
246
|
function print_node_delete($node) {
|
247
|
db_delete('print_page_counter')
|
248
|
->condition('path', 'node/' . $node->nid)
|
249
|
->execute();
|
250
|
}
|
251
|
|
252
|
/**
|
253
|
* Implements hook_entity_info_alter().
|
254
|
*/
|
255
|
function print_entity_info_alter(&$info) {
|
256
|
// Add the 'Print' view mode for nodes.
|
257
|
$info['node']['view modes'] += array(
|
258
|
PRINT_VIEW_MODE => array(
|
259
|
'label' => t('Print'),
|
260
|
'custom settings' => FALSE,
|
261
|
),
|
262
|
);
|
263
|
// Add the 'Print' view mode for field_collections.
|
264
|
if (module_exists('field_collection')) {
|
265
|
$info['field_collection_item']['view modes'] += array(
|
266
|
PRINT_VIEW_MODE => array(
|
267
|
'label' => t('Print'),
|
268
|
'custom settings' => FALSE,
|
269
|
),
|
270
|
);
|
271
|
}
|
272
|
}
|
273
|
|
274
|
/**
|
275
|
* Auxiliary function to discover a given page's title.
|
276
|
*
|
277
|
* @param string $path
|
278
|
* Path of the page being identified.
|
279
|
*
|
280
|
* @return string
|
281
|
* string with the page's title
|
282
|
*/
|
283
|
function _print_get_title($path) {
|
284
|
$path = drupal_get_normal_path($path);
|
285
|
$nid = preg_replace('!^node/!', '', $path);
|
286
|
if (ctype_digit($nid)) {
|
287
|
return db_query("SELECT title FROM {node} WHERE nid = :nid", array(':nid' => $nid))
|
288
|
->fetchField();
|
289
|
}
|
290
|
else {
|
291
|
// Not a node, try to get title from the menu system.
|
292
|
$menu_item = menu_get_item($path);
|
293
|
if (!empty($menu_item['title'])) {
|
294
|
return $menu_item['title'];
|
295
|
}
|
296
|
elseif (drupal_substr($menu_item['page_callback'], 0, 6) == 'views_') {
|
297
|
// It's a view, load the view to have access to the title.
|
298
|
$view = views_get_view($menu_item['page_arguments']['0']);
|
299
|
return $view->get_title();
|
300
|
}
|
301
|
else {
|
302
|
return NULL;
|
303
|
}
|
304
|
}
|
305
|
}
|
306
|
|
307
|
/**
|
308
|
* Auxiliary function to display a formatted Printer-friendly link.
|
309
|
*
|
310
|
* Function made available so that developers may call this function from
|
311
|
* their defined pages/blocks.
|
312
|
*
|
313
|
* @param string $path
|
314
|
* path to be used in the link. If not specified, the current URL is used.
|
315
|
* @param object $node
|
316
|
* node object, to be used in checking node access. If the path argument is
|
317
|
* not provided, the path used will be node/nid.
|
318
|
* @param string $location
|
319
|
* Where in the page where the link is being inserted ('link', 'corner',
|
320
|
* 'block', 'help').
|
321
|
*
|
322
|
* @return string
|
323
|
* HTML link to the printer-friendly page
|
324
|
*
|
325
|
* @ingroup print_api
|
326
|
*/
|
327
|
function print_insert_link($path = NULL, $node = NULL, $location = '') {
|
328
|
if (function_exists('print_ui_insert_link')) {
|
329
|
return print_ui_insert_link(print_print_link(), array(
|
330
|
'path' => $path,
|
331
|
'node' => $node,
|
332
|
'location' => $location,
|
333
|
));
|
334
|
}
|
335
|
else {
|
336
|
return FALSE;
|
337
|
}
|
338
|
}
|
339
|
|
340
|
/**
|
341
|
* Check if the link to the PF version is allowed depending on the settings.
|
342
|
*
|
343
|
* @param array $args
|
344
|
* Array containing the possible parameters:
|
345
|
* view_mode, node, type, path.
|
346
|
*
|
347
|
* @return bool
|
348
|
* FALSE if not allowed, TRUE otherwise
|
349
|
*/
|
350
|
function print_link_allowed($args) {
|
351
|
return (user_access('access print'));
|
352
|
}
|
353
|
|
354
|
/**
|
355
|
* Implements hook_contextual_links_view_alter().
|
356
|
*/
|
357
|
function print_contextual_links_view_alter(&$element, $items) {
|
358
|
// Hide all contextual links.
|
359
|
if (preg_match('!^print!', $_GET['q'])) {
|
360
|
unset($element['#links']);
|
361
|
}
|
362
|
}
|
363
|
|
364
|
/**
|
365
|
* Implements hook_views_api().
|
366
|
*/
|
367
|
function print_views_api() {
|
368
|
return array(
|
369
|
'api' => 2.0,
|
370
|
'path' => drupal_get_path('module', 'print'),
|
371
|
);
|
372
|
}
|