1
|
<?php
|
2
|
|
3
|
// at the moment we are using a static meta.xml file
|
4
|
define ("METADATA_FILE_NAME", "meta.xml");
|
5
|
define ("STATIC_DIR", "/static/");
|
6
|
define ("ARCHIVE_ROOT_DIR", "dwca_export/");
|
7
|
define ('FILE_MAP', 'dwca_export_archive_descriptor_file_map');
|
8
|
|
9
|
|
10
|
/**
|
11
|
* Implements hook_menu().
|
12
|
*/
|
13
|
function dwca_export_menu() {
|
14
|
|
15
|
$items = array();
|
16
|
|
17
|
$items['admin/config/system/dwca_export'] = array(
|
18
|
'title' => 'Darwin Core Archive export',
|
19
|
'description' => t('Create a DarwinCore Archive of this scratchpad.'),
|
20
|
'page callback' => 'drupal_get_form',
|
21
|
'page arguments' => array('dwca_export_config_form'),
|
22
|
'access arguments' => array('access DwC-A export settings'),
|
23
|
'type' => MENU_NORMAL_ITEM,
|
24
|
//'file' => 'dwca_export.admin.inc'
|
25
|
);
|
26
|
|
27
|
|
28
|
$items['dwca_export'] = array(
|
29
|
'page callback' => 'dwca_export_deliver_archive',
|
30
|
'access arguments' => array('access content'),
|
31
|
'type' => MENU_CALLBACK
|
32
|
);
|
33
|
|
34
|
|
35
|
return $items;
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* Implementation of hook_views_api()
|
40
|
*
|
41
|
* drupal will load dwca_export.views_default.inc when this hook is implemented
|
42
|
*/
|
43
|
function dwca_export_views_api() {
|
44
|
return array(
|
45
|
'api' => 3.0
|
46
|
);
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Implementation of hook_ctools_plugin_api().
|
51
|
*/
|
52
|
function dwca_export_ctools_plugin_api(){
|
53
|
list($module, $api) = func_get_args();
|
54
|
if($module == "strongarm" && $api == "strongarm"){
|
55
|
return array(
|
56
|
"version" => 1
|
57
|
);
|
58
|
}
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Form function, called by drupal_get_form()
|
63
|
* in dwca_export_menu().
|
64
|
*/
|
65
|
function dwca_export_config_form($form, &$form_state) {
|
66
|
|
67
|
global $base_url;
|
68
|
|
69
|
$form['dwca_export_info'] = array(
|
70
|
'#markup' => '<p>For general information on the DarwinCore Archive format please refer to '
|
71
|
. l('GBIF - Standards and Tools - Darwin Core Archives', 'http://www.gbif.org/informatics/standards-and-tools/publishing-data/data-standards/darwin-core-archives/')
|
72
|
.'</p>'
|
73
|
);
|
74
|
|
75
|
$form['dwca_export_execute'] = array(
|
76
|
'#markup' => '<p>The DarwinCore Archive export is available at '. l('dwca_export', 'dwca_export').'</p>'
|
77
|
//'#type' => 'button',
|
78
|
//'#value' => t('Export Scratchpad to DarwinCore Archive'),
|
79
|
//'#weight' => 19,
|
80
|
);
|
81
|
|
82
|
$form['dwca_export_view_mapping'] = dwca_export_select_view_form();
|
83
|
|
84
|
|
85
|
|
86
|
$form['#submit'][] = 'dwca_export_config_form_submit';
|
87
|
return system_settings_form($form);
|
88
|
|
89
|
}
|
90
|
|
91
|
|
92
|
function dwca_export_select_view_form() {
|
93
|
|
94
|
$views = array(
|
95
|
'#type' => 'fieldset',
|
96
|
'#title' => t('View to file mapping'),
|
97
|
//'#tree' => TRUE,
|
98
|
);
|
99
|
|
100
|
foreach(variable_get(FILE_MAP) as $dwca_filename => $view_data){
|
101
|
|
102
|
|
103
|
$views[$dwca_filename] = array(
|
104
|
'#type' => 'textfield',
|
105
|
'#title' => t($dwca_filename),
|
106
|
'#default_value' => $view_data['view_name'],
|
107
|
'#size' => 60,
|
108
|
'#maxlength' => 64,
|
109
|
'#description' => t('specify view for ' . $dwca_filename),
|
110
|
|
111
|
);
|
112
|
}
|
113
|
|
114
|
return $views;
|
115
|
}
|
116
|
|
117
|
function dwca_export_config_form_submit($form, &$form_state) {
|
118
|
|
119
|
$variables = $form_state['input'];
|
120
|
$save_variables = '';
|
121
|
$dwca_export_archive_descriptor_file_map = variable_get(FILE_MAP);
|
122
|
|
123
|
foreach ($variables as $key => $value) {
|
124
|
|
125
|
if (array_key_exists($key, $dwca_export_archive_descriptor_file_map)) {
|
126
|
|
127
|
$dwca_export_archive_descriptor_file_map[$key]['view_name'] = $value;
|
128
|
//$save_variables .= '<p>' . $key . ' ' . $value . '</p>';
|
129
|
}
|
130
|
}
|
131
|
|
132
|
variable_del(FILE_MAP);
|
133
|
variable_set(FILE_MAP, $dwca_export_archive_descriptor_file_map);
|
134
|
|
135
|
//drupal_set_message(t('The classification view is ') . $save_variables);// . '<pre>' . print_r($form_state,true) . '</pre>');//$save_variables);
|
136
|
}
|
137
|
|
138
|
// Reports an error if view name entered by the user does not exist in the database.
|
139
|
function dwca_export_config_form_validate($form, &$form_state) {
|
140
|
|
141
|
$variables = $form_state['input'];
|
142
|
$dwca_export_archive_descriptor_file_map = variable_get(FILE_MAP);
|
143
|
$view_names = array();
|
144
|
$missing_view_names = '';
|
145
|
$missing_view = false;
|
146
|
|
147
|
foreach ($variables as $key => $value) {
|
148
|
|
149
|
if (array_key_exists($key, $dwca_export_archive_descriptor_file_map)) {
|
150
|
|
151
|
$view = views_get_view($value);
|
152
|
// check whether there is a view named with this value
|
153
|
if(!$view) {
|
154
|
$view_names[] = $value;
|
155
|
$missing_view_names .= $value . ', ';
|
156
|
$missing_view = true;
|
157
|
}
|
158
|
}
|
159
|
}
|
160
|
|
161
|
if ($missing_view) {
|
162
|
form_set_error('', t('VIEW(S) ' . $missing_view_names . ' NOT FOUND. Please input another view name.'));
|
163
|
}
|
164
|
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* menu callback
|
169
|
*/
|
170
|
function dwca_export_deliver_archive() {
|
171
|
|
172
|
$tmp_archive_file_name = dwca_export_create_archive( _dwca_export_archive_descriptor_file_map() );
|
173
|
|
174
|
if($tmp_archive_file_name && file_valid_uri($tmp_archive_file_name)){
|
175
|
file_transfer($tmp_archive_file_name, array('Content-Type' => 'application/zip'));
|
176
|
} else {
|
177
|
throw new Exception(t('Error creating the archive'));
|
178
|
}
|
179
|
}
|
180
|
|
181
|
|
182
|
/**
|
183
|
* Provides the archive_descriptor_file_map which maps dwca file name to a set of view information.
|
184
|
* The view information contains the fields 'view_name', 'display_id', 'out_file_url'.
|
185
|
* The 'out_file_url' is initailly empty and will be set when this function is called
|
186
|
* with both parameters.
|
187
|
*
|
188
|
* @param String $file_name
|
189
|
* @param String $out_file_url
|
190
|
*
|
191
|
* @return the archive_descriptor_file_map
|
192
|
*/
|
193
|
function _dwca_export_archive_descriptor_file_map($file_name = NULL, $out_file_url = null){
|
194
|
static $file_map;
|
195
|
|
196
|
if(!isset($file_map)){
|
197
|
$file_map = variable_get(FILE_MAP);
|
198
|
}
|
199
|
|
200
|
if($file_name && $out_file_url){
|
201
|
$file_map[$file_name]['out_file_url'] = $out_file_url;
|
202
|
}
|
203
|
|
204
|
return $file_map;
|
205
|
}
|
206
|
|
207
|
|
208
|
/**
|
209
|
* Walks all view export paths defined in the $views_map.
|
210
|
* Each file is downloaded to the tmp folder and a zip file
|
211
|
* is bundeled of the resulting csv files plus the meta file.
|
212
|
*
|
213
|
* @param $views_map - maps a view paths to dwca filenames
|
214
|
*
|
215
|
* @return the path in the filesystem to the final archive,
|
216
|
* or FALSE in case of an error.
|
217
|
*/
|
218
|
function dwca_export_create_archive($views_map) {
|
219
|
|
220
|
global $base_url;
|
221
|
|
222
|
// execute all views to export the data into
|
223
|
// temporary csv files (temporary://dwca_export_*). the resulting filenames
|
224
|
// will be stored in _dwca_export_archive_descriptor_file_map()
|
225
|
foreach($views_map as $filename=>$view_data){
|
226
|
|
227
|
$view = views_get_view($view_data['view_name']);
|
228
|
|
229
|
//lorna
|
230
|
// Get the view and its fields.
|
231
|
$view->set_display();
|
232
|
$fields = $view->display_handler->get_field_labels();
|
233
|
foreach ($fields as $field_key => $field_label) {
|
234
|
//$field_properties = $field_copy;
|
235
|
//$field_properties['title'] = $field_label;
|
236
|
//$fields[$field_key] = $field_properties;
|
237
|
if ($field_label == 'uninomial') {
|
238
|
drupal_set_message(t('The field label is... ') . $field_label);
|
239
|
}
|
240
|
}
|
241
|
//end lorna
|
242
|
|
243
|
|
244
|
$options = array (
|
245
|
'output_file' => $filename
|
246
|
);
|
247
|
_dwca_export_views_data_export_override_batch($view, $view_data['display_id'], $options);
|
248
|
$view->execute_display($view_data['display_id']);
|
249
|
|
250
|
}
|
251
|
|
252
|
// all data is exported to temporary://dwca_export_*
|
253
|
// now we can start bundling the actual archive
|
254
|
$tmp_archive_file_name = drupal_tempnam("temporary://", "dwca_export_archive_");
|
255
|
|
256
|
// Unfortunately we cannot use drupals ArchiverZip because there ís
|
257
|
// no way to pass in ZipArchive::CREATE to the constructor to create the archive
|
258
|
// TODO test if zip functionality is available (i.e. if(function_exists('zip_open'))
|
259
|
// but I don't know where the proper location for such a check would be
|
260
|
$zip = new ZipArchive();
|
261
|
// it is safe to use drupal_realpath as the tmp file will be certainly local
|
262
|
// and php's ZipArchive does not handle stream URIs
|
263
|
$result = $zip->open(drupal_realpath($tmp_archive_file_name), ZipArchive::CREATE);
|
264
|
|
265
|
// there might be a better way to get at this information
|
266
|
$module_static_dir_absolute_path = realpath(drupal_get_path('module', 'dwca_export')) . STATIC_DIR;
|
267
|
|
268
|
if ($result !== TRUE) {
|
269
|
throw new Exception(t('Could not create zip_archive %tmp_archive_file_name', array('%tmp_archive_file_name' => $tmp_archive_file_name)));
|
270
|
}
|
271
|
|
272
|
|
273
|
// add metadata
|
274
|
$zip->addFile($module_static_dir_absolute_path.METADATA_FILE_NAME, ARCHIVE_ROOT_DIR.METADATA_FILE_NAME);
|
275
|
// add the csv data files
|
276
|
foreach(_dwca_export_archive_descriptor_file_map() as $dwca_filename=>$view_data){
|
277
|
|
278
|
$view_temp_file = $view_data['out_file_url'];
|
279
|
if($view_temp_file){
|
280
|
$zip->addFile(drupal_realpath($view_temp_file), ARCHIVE_ROOT_DIR.$dwca_filename);
|
281
|
}else{
|
282
|
throw new Exception(t('Cannot create %file', array('%file' => $dwca_filename)));
|
283
|
}
|
284
|
}
|
285
|
|
286
|
$zip->close();
|
287
|
|
288
|
return $tmp_archive_file_name;
|
289
|
}
|
290
|
|
291
|
/**
|
292
|
* Helper function that indicates that we want to
|
293
|
* override the batch that the views_data_export view creates
|
294
|
* on it's initial time through.
|
295
|
*
|
296
|
* Also provides a place to stash options that need to stay around
|
297
|
* until the end of the batch
|
298
|
*
|
299
|
* adapted fom views_data_export.drush.inc
|
300
|
*/
|
301
|
function _dwca_export_views_data_export_override_batch($view = NULL, $display = NULL, $options = TRUE) {
|
302
|
static $_views;
|
303
|
if (isset($view)) {
|
304
|
$_views[$view->name][$display] = $options;
|
305
|
}
|
306
|
return $_views;
|
307
|
}
|
308
|
|
309
|
|
310
|
/**
|
311
|
* Implementation of hook_views_data_export_batch_alter()
|
312
|
*
|
313
|
* adapted fom views_data_export.drush.inc
|
314
|
*
|
315
|
* @see batch_process() in form.inc
|
316
|
*/
|
317
|
function dwca_export_views_data_export_batch_alter(&$batch, &$final_destination, &$querystring) {
|
318
|
|
319
|
$view_name = $batch['view_name'];
|
320
|
$display_id = $batch['display_id'];
|
321
|
|
322
|
$ok_to_override = _dwca_export_views_data_export_override_batch();
|
323
|
|
324
|
// Make sure we do nothing if we are called not following the execution of
|
325
|
// our drush command. This could happen if the file with this function in it
|
326
|
// is included during the normal execution of the view
|
327
|
if (!$ok_to_override[$view_name][$display_id]) {
|
328
|
return;
|
329
|
}
|
330
|
|
331
|
$options = $ok_to_override[$view_name][$display_id];
|
332
|
|
333
|
// We actually never return from the drupal_alter, but
|
334
|
// use drush's batch system to run the same batch
|
335
|
|
336
|
// Add a final callback
|
337
|
$batch['operations'][] = array(
|
338
|
'_dwca_export_views_data_export_batch_finished', array($batch['eid'], $options['output_file']),
|
339
|
);
|
340
|
|
341
|
$batch['progressive'] = FALSE;
|
342
|
}
|
343
|
|
344
|
/**
|
345
|
* Implementation of hook_views_data_export_batch_alter()
|
346
|
*
|
347
|
* @see batch_process() in form.inc
|
348
|
*/
|
349
|
function dwca_export_batch_alter(&$batch, &$final_destination, &$querystring) {
|
350
|
if($batch['source_url'] == 'dwca_export'){
|
351
|
$batch['progressive'] = FALSE;
|
352
|
}
|
353
|
}
|
354
|
|
355
|
|
356
|
|
357
|
/**
|
358
|
* Get's called at the end of the drush batch process that generated our export
|
359
|
*
|
360
|
* adapted fom views_data_export.drush.inc
|
361
|
*/
|
362
|
function _dwca_export_views_data_export_batch_finished($eid, $output_file, &$context) {
|
363
|
// Fetch export info
|
364
|
$export = views_data_export_get($eid);
|
365
|
|
366
|
// Perform cleanup
|
367
|
$view = views_data_export_view_retrieve($eid);
|
368
|
$view->set_display($export->view_display_id);
|
369
|
$view->display_handler->batched_execution_state = $export;
|
370
|
$view->display_handler->remove_index();
|
371
|
|
372
|
// Get path to temp file
|
373
|
$temp_file = $view->display_handler->outputfile_path();
|
374
|
|
375
|
_dwca_export_archive_descriptor_file_map($output_file, $temp_file);
|
376
|
|
377
|
}
|