-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk_pdf.module
245 lines (232 loc) · 7.91 KB
/
bulk_pdf.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
* @file
* Provides the ability of mass-generating PDF files using Tokens and template files.
* Gluing dompdf, token and views_bulk_operations together
*/
/**
* Implementation of hook_menu().
*/
function bulk_pdf_menu() {
$items = array();
$items['bulk-pdf/autocomplete'] = array(
'title' => 'Bulk PDF autocomplete',
'page callback' => 'bulk_pdf_autocomplete',
'access arguments' => array('generating bulk PDFs'),
'type' => MENU_CALLBACK
);
$items['bulk-pdf/generate'] = array(
'title' => 'Generatig PDFs',
'page callback' => 'bulk_pdf_generate',
'access arguments' => array('generating bulk PDFs'),
'type' => MENU_CALLBACK
);
return $items;
}
/**
* Implementation of hook_theme().
*/
function bulk_pdf_theme() {
return array(
'bulk_pdf_page' => array(
'arguments' => array('content' => NULL),
'template' => 'bulk_pdf_page',
),
);
}
/**
* Implementation of hook_perm().
*/
function bulk_pdf_perm() {
return array('generating bulk PDFs');
}
/**
* Implementation of hook_form_alter().
* Choosing model content-types on content-type edit pages.
* Showing all tokens at model node edit pages.
*/
function bulk_pdf_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
$form['bulk_pdf'] = array(
'#type' => 'fieldset',
'#title' => t('Bulk PDF'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['bulk_pdf']['bulk_pdf_model'] = array(
'#type' => 'checkbox',
'#title' => t('Bulk PDF model node'),
'#description' => t('Check if you want to use this content type for as a model for bulk PDFs.'),
'#default_value' => variable_get('bulk_pdf_model_' . $form['#node_type']->type, FALSE),
'#weight' => -15,
);
}
elseif (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id && variable_get('bulk_pdf_model_' . $form['type']['#value'], FALSE)) {
$form['bulk_pdf'] = array(
'#type' => 'fieldset',
'#title' => t('Bulk PDF model tokens'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['bulk_pdf']['tokens'] = array(
'#value' => theme('token_tree', 'all', TRUE, FALSE), // A bit misleading, not all tokens can be used in fact, some miss the context
);
}
}
/**
* Implementation of hook_action_info().
*/
function bulk_pdf_action_info() {
return array(
'bulk_pdf_action' => array(
'type' => 'system',
'description' => t('Generate bulk PDF'),
'configurable' => TRUE,
'permissions' => array('generating bulk PDF, applying tokens from the selected nodes to the model node'),
),
);
}
/**
* Own autocomplete function for fetching titles only from one, selected content-type.
* The content-type is specified in a non-user configurable variable.
*/
function bulk_pdf_autocomplete($string = '') {
$matches = array();
$types = array_keys(node_get_types());
$enabled_types = array();
foreach ($types as $type) {
if (variable_get('bulk_pdf_model_' . $type, FALSE)) {
$enabled_types[] = $type;
}
}
if (!empty($enabled_types)) {
$placeholders = db_placeholders($enabled_types, 'varchar');
$q_args = $enabled_types;
$q_args[] = $string;
$result = db_query("SELECT n.nid, n.title AS node_title FROM {node} n WHERE type IN (" . $placeholders . ") AND title LIKE '%%%s%%'", $q_args);
$references = array();
while ($node = db_fetch_object($result)) {
$references[$node->nid] = array(
'title' => $node->node_title,
'rendered' => check_plain($node->node_title),
);
}
foreach ($references as $id => $row) {
// Add a class wrapper for a few required CSS overrides.
$matches[$row['title'] ." [nid:$id]"] = '<div class="reference-autocomplete">'. $row['rendered'] . '</div>';
}
}
drupal_json($matches);
}
/**
* Action callback
* @see bulk_pdf_action_info()
* Stores the node IDs and hijack the page after the operation is finished.
* VBO is just the selection tool.
*/
function bulk_pdf_action($object, $context) {
static $msg = FALSE;
$_SESSION['bulk_pdf']['nids'][] = $object->nid;
if ($msg == FALSE) {
$msg = TRUE;
// No better way to hijack page after views_bulk_operations finished, no _after hook or something like that to be able to use drupal_goto, also $context does not store information about the last object, object counter
drupal_set_message(t('Please wait.!code', array('!code' => '<script type="text/javascript">window.location="'. url('bulk-pdf/generate') . '";</script>')));
}
}
/**
* Settings form for the action
* @see bulk_pdf_action_info()
*/
function bulk_pdf_action_form($context) {
$form = array();
$form['model'] = array(
'#type' => 'textfield',
'#title' => t('Choose model'),
'#autocomplete_path' => 'bulk-pdf/autocomplete',
'#required' => TRUE,
);
return $form;
}
/**
* Validates and saves the action settings
*/
function bulk_pdf_action_validate($form, &$form_state) {
preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $form_state['values']['model'], $matches);
if (!empty($matches)) {
list(, $title, $nid) = $matches;
if (!empty($title) && ($n = node_load($nid)) && trim($title) != trim($n->title)) {
form_set_error('model', t('Invalid model choice'));
}
else {
$_SESSION['bulk_pdf']['model'] = $nid;
$_SESSION['bulk_pdf']['back'] = $_GET['q'];
}
}
}
/**
* Submit handler for the action settings, unused.
*/
function bulk_pdf_action_submit($form, &$form_state) {
}
/**
* Launches our own Batch API stuff. We need to use a separate one
* to be able to generate multiple pages (entries) into one PDF file
*/
function bulk_pdf_generate() {
$batch_size = variable_get('bulk_pdf_batch_size', 100);
$batch = array(
'title' => t('Generating PDFs'),
'init_message' => t('PDFs are being generated.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('We encountered an error. Please try again.'),
);
$idx = 0;
$nids = array();
if (isset($_SESSION['bulk_pdf']['nids'])) {
foreach ($_SESSION['bulk_pdf']['nids'] as $nid) {
$nids[] = $nid;
$counter++;
if (count($nids) > ($batch_size - 1)) {
$batch['operations'][$idx++] = array('bulk_pdf_generate_process', array($nids));
$nids = array();
}
}
}
if (!empty($nids)) {
$batch['operations'][$idx++] = array('bulk_pdf_generate_process', array($nids));
}
unset($_SESSION['bulk_pdf']['nids']);
if (!empty($batch['operations'])) {
batch_set($batch);
batch_process();
}
elseif (!empty($_SESSION['bulk_pdf']['files'])) {
drupal_set_title(t('Download the PDFs'));
$output = theme('item_list', $_SESSION['bulk_pdf']['files']) . l(t('Back to the first step'), $_SESSION['bulk_pdf']['back']);
unset($_SESSION['bulk_pdf']);
return $output;
}
}
/**
* Generates the PDF file for the set of data nodes using the dompdf module
* It stores the filename in the session variable for showing it later
*/
function bulk_pdf_generate_process($nids, &$context) {
// PDF generation using dompdf is time- and memory-consuming process.
set_time_limit(300); // Using this to try to increase the max execution time. This function never actually lowers the value.
$node = node_load($_SESSION['bulk_pdf']['model']);
$objects = array();
$output = '';
foreach ($nids as $nid) {
$data = node_load($nid);
$output .= _filter_autop(token_replace_multiple($node->body, array('node' => $data)));
}
$filename = file_directory_path() . '/bulk-pdf-' . md5(serialize($objects)) . '-' . $node->nid . '.pdf';
$pdf = dompdf_stream_pdf(theme('bulk_pdf_page', $output));
file_put_contents($filename, $pdf);
$context['finished'] = TRUE;
if (!is_array($_SESSION['bulk_pdf']['files'])) {
$_SESSION['bulk_pdf']['files'] = array();
}
$_SESSION['bulk_pdf']['files'][] = l($filename, $filename, array('absolute' => TRUE));
}