1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Displays Printer-friendly versions of Drupal pages.
|
6
|
*
|
7
|
* @ingroup print
|
8
|
*/
|
9
|
|
10
|
define('PRINT_EPUB_EPUB_TOOL_DEFAULT', 0);
|
11
|
define('PRINT_EPUB_IMAGES_VIA_FILE_DEFAULT', 0);
|
12
|
define('PRINT_EPUB_FILENAME_DEFAULT', '[site:name] - [node:title] - [node:changed:custom:Y-m-d]');
|
13
|
|
14
|
/**
|
15
|
* Implements hook_print_link().
|
16
|
*/
|
17
|
function print_epub_print_link() {
|
18
|
return array(
|
19
|
'format' => 'epub',
|
20
|
'text' => t('EPUB version'),
|
21
|
'description' => t('Display a EPUB version of this page.'),
|
22
|
'path' => 'printepub',
|
23
|
'class' => 'print-epub',
|
24
|
'icon' => 'epub_icon.png',
|
25
|
'module' => 'print_epub',
|
26
|
);
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* Implements hook_permission().
|
31
|
*/
|
32
|
function print_epub_permission() {
|
33
|
return array(
|
34
|
'access EPUB version' => array(
|
35
|
'title' => t('Access the EPUB version'),
|
36
|
'description' => t('View the EPUB versions and the links to them in the original pages.'),
|
37
|
),
|
38
|
);
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* Implements hook_menu().
|
43
|
*/
|
44
|
function print_epub_menu() {
|
45
|
$link = print_epub_print_link();
|
46
|
$items = array();
|
47
|
|
48
|
$items[$link['path']] = array(
|
49
|
'title' => 'Printer-friendly EPUB',
|
50
|
'page callback' => 'print_epub_controller',
|
51
|
'access arguments' => array('access EPUB version'),
|
52
|
'type' => MENU_CALLBACK,
|
53
|
'file' => 'print_epub.pages.inc',
|
54
|
);
|
55
|
$items[$link['path'] . '/' . $link['path']] = array(
|
56
|
'access callback' => FALSE,
|
57
|
);
|
58
|
$items['admin/config/user-interface/print/epub'] = array(
|
59
|
'title' => 'EPUB',
|
60
|
'description' => 'Configure the settings of the EPUB generation functionality.',
|
61
|
'page callback' => 'drupal_get_form',
|
62
|
'page arguments' => array('print_epub_settings'),
|
63
|
'access arguments' => array('administer print'),
|
64
|
'weight' => 3,
|
65
|
'type' => MENU_LOCAL_TASK,
|
66
|
'file' => 'print_epub.admin.inc',
|
67
|
);
|
68
|
$items['admin/config/user-interface/print/epub/options'] = array(
|
69
|
'title' => 'Options',
|
70
|
'weight' => -1,
|
71
|
'type' => MENU_DEFAULT_LOCAL_TASK,
|
72
|
);
|
73
|
|
74
|
return $items;
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Implements hook_variable_info().
|
79
|
*/
|
80
|
function print_epub_variable_info($options) {
|
81
|
$link = print_epub_print_link();
|
82
|
|
83
|
$variable['print_epub_link_text'] = array(
|
84
|
'title' => t('EPUB version'),
|
85
|
'description' => t('Text used in the link to the EPUB version.'),
|
86
|
'type' => 'string',
|
87
|
'default' => t($link['text']),
|
88
|
);
|
89
|
|
90
|
return $variable;
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Implements hook_block_info().
|
95
|
*/
|
96
|
function print_epub_block_info() {
|
97
|
$block['print_epub-top']['info'] = t('Most EPUBed');
|
98
|
$block['print_epub-top']['cache'] = DRUPAL_CACHE_GLOBAL;
|
99
|
return $block;
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* Implements hook_block_view().
|
104
|
*/
|
105
|
function print_epub_block_view($delta = 0) {
|
106
|
$block = array();
|
107
|
switch ($delta) {
|
108
|
case 'print_epub-top':
|
109
|
$block['subject'] = t('Most EPUBd');
|
110
|
$result = db_query_range("SELECT path FROM {print_epub_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)
|
111
|
->fetchAll();
|
112
|
if (count($result)) {
|
113
|
$items = array();
|
114
|
foreach ($result as $obj) {
|
115
|
$items[] = l(_print_get_title($obj->path), $obj->path);
|
116
|
}
|
117
|
$block['content'] = theme('item_list', array('items' => $items, 'type' => 'ul'));
|
118
|
}
|
119
|
break;
|
120
|
}
|
121
|
return $block;
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* Implements hook_requirements().
|
126
|
*/
|
127
|
function print_epub_requirements($phase) {
|
128
|
$requirements = array();
|
129
|
$t = get_t();
|
130
|
switch ($phase) {
|
131
|
// At runtime, make sure that a EPUB generation tool is selected.
|
132
|
case 'runtime':
|
133
|
$print_epub_epub_tool = variable_get('print_epub_epub_tool', PRINT_EPUB_EPUB_TOOL_DEFAULT);
|
134
|
if (empty($print_epub_epub_tool)) {
|
135
|
$requirements['print_epub_tool'] = array(
|
136
|
'title' => $t('Printer, email and EPUB versions - EPUB generation library'),
|
137
|
'value' => $t('No EPUB tool selected'),
|
138
|
'description' => $t('Please configure it in the !url.', array('!url' => l($t('EPUB settings page'), 'admin/config/user-interface/print/epub'))),
|
139
|
'severity' => REQUIREMENT_ERROR,
|
140
|
);
|
141
|
}
|
142
|
else {
|
143
|
$tool = explode('|', $print_epub_epub_tool);
|
144
|
|
145
|
if (!is_file($tool[1]) || !is_readable($tool[1])) {
|
146
|
$requirements['print_epub_tool'] = array(
|
147
|
'title' => $t('Printer, email and EPUB versions - EPUB generation library'),
|
148
|
'value' => $t('File not found'),
|
149
|
'description' => $t('The currently selected EPUB generation library (%file) is no longer accessible.', array('%file' => $tool[1])),
|
150
|
'severity' => REQUIREMENT_ERROR,
|
151
|
);
|
152
|
}
|
153
|
}
|
154
|
break;
|
155
|
}
|
156
|
return $requirements;
|
157
|
}
|
158
|
|
159
|
/**
|
160
|
* Implements hook_node_delete().
|
161
|
*/
|
162
|
function print_epub_node_delete($node) {
|
163
|
db_delete('print_epub_page_counter')
|
164
|
->condition('path', 'node/' . $node->nid)
|
165
|
->execute();
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Auxiliary function to display a formatted EPUB version link.
|
170
|
*
|
171
|
* Function made available so that developers may call this function from
|
172
|
* their defined pages/blocks.
|
173
|
*
|
174
|
* @param string $path
|
175
|
* path to be used in the link. If not specified, the current URL is used.
|
176
|
* @param object $node
|
177
|
* node object, to be used in checking node access. If the path argument is
|
178
|
* not provided, the path used will be node/nid.
|
179
|
* @param string $location
|
180
|
* Where in the page where the link is being inserted ('link', 'corner',
|
181
|
* 'block', 'help').
|
182
|
*
|
183
|
* @return bool
|
184
|
* string with the HTML link to the printer-friendly page
|
185
|
*
|
186
|
* @ingroup print_api
|
187
|
*/
|
188
|
function print_epub_insert_link($path = NULL, $node = NULL, $location = '') {
|
189
|
if (function_exists('print_ui_insert_link')) {
|
190
|
return print_ui_insert_link(print_epub_print_link(), array(
|
191
|
'path' => $path,
|
192
|
'node' => $node,
|
193
|
'location' => $location,
|
194
|
));
|
195
|
}
|
196
|
else {
|
197
|
return FALSE;
|
198
|
}
|
199
|
}
|
200
|
|
201
|
/**
|
202
|
* Check if the link to the EPUB version is allowed depending on the settings.
|
203
|
*
|
204
|
* @param array $args
|
205
|
* Array containing the possible parameters:
|
206
|
* view_mode, node, type, path.
|
207
|
*
|
208
|
* @return bool
|
209
|
* FALSE if not allowed, TRUE otherwise
|
210
|
*/
|
211
|
function print_epub_link_allowed($args) {
|
212
|
$print_epub_epub_tool = variable_get('print_epub_epub_tool', PRINT_EPUB_EPUB_TOOL_DEFAULT);
|
213
|
|
214
|
return (user_access('access EPUB version') && (!empty($print_epub_epub_tool)));
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* Generate a EPUB version of the provided HTML.
|
219
|
*
|
220
|
* @param string $html
|
221
|
* HTML content of the EPUB.
|
222
|
* @param array $meta
|
223
|
* Meta information to be used in the EPUB
|
224
|
* - url: original URL
|
225
|
* - name: author's name
|
226
|
* - title: Page title
|
227
|
* - node: node object.
|
228
|
* @param string $filename
|
229
|
* (optional) Filename of the generated EPUB.
|
230
|
*
|
231
|
* @return string|null
|
232
|
* generated EPUB page, or NULL in case of error
|
233
|
*
|
234
|
* @see print_epub_controller()
|
235
|
*
|
236
|
* @ingroup print_api
|
237
|
*/
|
238
|
function print_epub_generate_html($html, $meta, $filename = NULL) {
|
239
|
$epub_tool = explode('|', variable_get('print_epub_epub_tool', PRINT_EPUB_EPUB_TOOL_DEFAULT));
|
240
|
|
241
|
module_load_include('inc', $epub_tool[0], $epub_tool[0] . '.pages');
|
242
|
|
243
|
$function = $epub_tool[0] . '_print_epub_generate';
|
244
|
if (function_exists($function)) {
|
245
|
return $function($html, $meta, $filename);
|
246
|
}
|
247
|
|
248
|
return NULL;
|
249
|
}
|
250
|
|
251
|
/**
|
252
|
* Implements hook_views_api().
|
253
|
*/
|
254
|
function print_epub_views_api() {
|
255
|
return array(
|
256
|
'api' => 2.0,
|
257
|
'path' => drupal_get_path('module', 'print_epub'),
|
258
|
);
|
259
|
}
|