Project

General

Profile

Download (15.7 KB) Statistics
| Branch: | Tag: | Revision:
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
		if ($dwca_filename == 'description') {
103
			
104
			
105
		$views[$dwca_filename] = array (
106
					'#type' => 'fieldset',
107
  					'#title' => t('DESCRIPTIVE DATA TYPES'),
108
				  	'#weight' => 3,
109
				  	'#collapsible' => TRUE,
110
				  	'#collapsed' => TRUE,
111
				  	'#tree' => FALSE,
112
			
113
  		);
114
			foreach($view_data as $dwca_filename_inner => $view_data_inner){
115
				
116
				
117
				$views[$dwca_filename][$dwca_filename_inner] = array(
118
									  	'#type' => 'textfield',
119
									  	'#title' => t($dwca_filename . '_' . $dwca_filename_inner),
120
									  	'#default_value' => $view_data_inner['view_name'],
121
									  	'#size' => 60,
122
									  	'#description' => t('specify view for ' . $dwca_filename_inner),
123
			
124
				);
125
			}
126
			
127
		} else {
128
			$views[$dwca_filename] = array(
129
					  	'#type' => 'textfield',
130
					  	'#title' => t($dwca_filename),
131
					  	'#default_value' => $view_data['view_name'],
132
					  	'#size' => 60,
133
					  	'#description' => t('specify view for ' . $dwca_filename),
134
			
135
			);
136
		}
137
	}
138

    
139
	return $views;
140
}
141

    
142
function dwca_export_config_form_submit($form, &$form_state) {
143

    
144
	$variables = $form_state['input'];
145
	$save_variables = '';
146
	$dwca_export_archive_descriptor_file_map = variable_get(FILE_MAP);
147

    
148
	foreach ($variables as $key => $value) {
149

    
150
		if (array_key_exists($key, $dwca_export_archive_descriptor_file_map)) {
151

    
152
			$dwca_export_archive_descriptor_file_map[$key]['view_name'] = $value;
153
			//$save_variables .= '<p>' . $key . ' ' . $value . '</p>';
154
		} else {
155

    
156
			$description_key = 'description';
157
			
158
			if (array_key_exists($description_key, $dwca_export_archive_descriptor_file_map)) {
159
				
160
				//get the inner array containing the different description data types
161
				$description_map = $dwca_export_archive_descriptor_file_map[$description_key];
162
				//if (array_key_exists($key, $dwca_export_archive_descriptor_file_map['description'])) {
163
				if (array_key_exists($key, $description_map)) {
164
					//drupal_set_message(t('The ooKEY is... ') . $key . t('The ooVALUE is... ') . $value);
165
					$dwca_export_archive_descriptor_file_map[$description_key][$key]['view_name'] = $value;
166
				}
167
			}
168
		}
169
	}
170

    
171
	variable_del(FILE_MAP);
172
	variable_set(FILE_MAP, $dwca_export_archive_descriptor_file_map);
173

    
174
	//drupal_set_message(t('The classification view is ') . $save_variables);// . '<pre>' . print_r($form_state,true) . '</pre>');//$save_variables);
175
}
176

    
177

    
178

    
179

    
180
// Reports an error if view name entered by the user does not exist in the database.
181
function dwca_export_config_form_validate($form, &$form_state) {
182
	
183
	$variables = $form_state['input'];
184
	$dwca_export_archive_descriptor_file_map = variable_get(FILE_MAP);
185
	$view_names = array();
186
	$missing_view_names = '';
187
	$missing_view = false;
188
	
189
	foreach ($variables as $key => $value) {
190
		
191
		//TODO: Check whether the views for the inner array cotaining all the description views exist
192
		if (array_key_exists($key, $dwca_export_archive_descriptor_file_map)) {
193
					
194
			$view = views_get_view($value);
195
			// check whether there is a view named with this value 
196
			if(!$view) {
197
				$view_names[] = $value;
198
				$missing_view_names .= $value . ', ';
199
				$missing_view = true;
200
			}
201
		}
202
	}
203
	
204
	if ($missing_view) {
205
		form_set_error('', t('VIEW(S) ' . $missing_view_names . ' NOT FOUND. Please input another view name.'));
206
	}
207
	
208
}
209

    
210
/**
211
 * menu callback
212
 */
213
function dwca_export_deliver_archive() {
214

    
215
	$tmp_archive_file_name = dwca_export_create_archive( _dwca_export_archive_descriptor_file_map() );
216

    
217
	if($tmp_archive_file_name && file_valid_uri($tmp_archive_file_name)){
218
		file_transfer($tmp_archive_file_name, array('Content-Type' => 'application/zip'));
219
	} else {
220
		throw new Exception(t('Error creating the archive'));
221
	}
222
}
223

    
224

    
225
/**
226
 * Provides the archive_descriptor_file_map which maps dwca file name to a set of view information.
227
 * The view information contains the fields 'view_name', 'display_id', 'out_file_url'.
228
 * The 'out_file_url' is initailly empty and will be set when this function is called
229
 * with both parameters.
230
 *
231
 * @param String $file_name
232
 * @param String $out_file_url
233
 *
234
 * @return the archive_descriptor_file_map
235
 */
236
function _dwca_export_archive_descriptor_file_map($file_name = NULL, $out_file_url = null){
237
	static $file_map;
238

    
239
	if(!isset($file_map)){
240
		$file_map = variable_get(FILE_MAP);
241
	}
242

    
243
	if($file_name && $out_file_url){
244
		
245
		//TODO: Change this - it's not very generic and will not work if we add new types to FILE_MAP
246
		if (($file_name == 'classification') || ($file_name == 'typesandspecimen') ||
247
				 ($file_name == 'reference') || ($file_name == 'image' || ($file_name == 'distribution')) {
248
			
249
			$file_map[$file_name]['out_file_url'] = $out_file_url;
250
			
251
		} else {
252
		
253
			$file_map['description'][$file_name]['out_file_url'] = $out_file_url;
254
		}
255
	}
256

    
257
	return $file_map;
258
}
259

    
260

    
261
/**
262
 * Walks all view export paths defined in the $views_map.
263
 * Each file is downloaded to the tmp folder and a zip file
264
 * is bundeled of the resulting csv files plus the meta file.
265
 *
266
 * @param $views_map - maps a view paths to dwca filenames
267
 *
268
 * @return the path in the filesystem to the final archive,
269
 * or FALSE in case of an error.
270
 */
271
function dwca_export_create_archive($views_map) {
272
	
273
	// all data is exported to temporary://dwca_export_*
274
	// now we can start bundling the actual archive
275
	$tmp_archive_file_name = drupal_tempnam("temporary://", "dwca_export_archive_");
276
	
277
	// Unfortunately we cannot use drupals ArchiverZip because there ís
278
	// no way to pass in ZipArchive::CREATE to the constructor to create the archive
279
	// TODO test if zip functionality is available (i.e. if(function_exists('zip_open'))
280
	// but I don't know where the proper location for such a check would be
281
	$zip = new ZipArchive();
282
	// it is safe to use drupal_realpath as the tmp file will be certainly local
283
	// and php's ZipArchive does not handle stream URIs
284
	$result = $zip->open(drupal_realpath($tmp_archive_file_name), ZipArchive::CREATE);
285
	
286
	// there might be a better way to get at this information
287
	$module_static_dir_absolute_path = realpath(drupal_get_path('module', 'dwca_export')) . STATIC_DIR;
288
	
289
	if ($result !== TRUE) {
290
		throw new Exception(t('Could not create zip_archive %tmp_archive_file_name', array('%tmp_archive_file_name' => $tmp_archive_file_name)));
291
	}
292
	
293
	
294
	// add metadata
295
	$zip->addFile($module_static_dir_absolute_path.METADATA_FILE_NAME, ARCHIVE_ROOT_DIR.METADATA_FILE_NAME);
296

    
297
	global $base_url;
298

    
299
	// execute all views to export the data into
300
	// temporary csv files (temporary://dwca_export_*). the resulting filenames
301
	// will be stored in _dwca_export_archive_descriptor_file_map()
302
	foreach($views_map as $filename=>$view_data){
303
	
304
		//lorna
305
		if ($filename == 'description') {
306
			
307
			//lorna: this foreach iterates through all the description types in the inner array
308
			foreach($view_data as $filename_inner => $view_data_inner){
309
				$view = views_get_view($view_data_inner['view_name']);
310
					
311
				$options = array (
312
					'output_file' => $filename_inner
313
				);
314
					
315
				//_dwca_export_views_data_export_override_batch($view, $view_data['general']['display_id'], $options);
316
				_dwca_export_views_data_export_override_batch($view, $view_data_inner['display_id'], $options);
317
					
318
				$view->execute_display($view_data_inner['display_id']);
319
			}
320
			
321
		}
322
		else {
323
			$view = views_get_view($view_data['view_name']);
324
			$options = array (
325
					    'output_file' => $filename
326
			);
327
			_dwca_export_views_data_export_override_batch($view, $view_data['display_id'], $options);
328
			$view->execute_display($view_data['display_id']);			
329
		}
330
		//end lorna
331
		
332
	}
333

    
334
	// add the csv data files
335
	foreach(_dwca_export_archive_descriptor_file_map() as $dwca_filename=>$view_data){
336

    
337
		if ($dwca_filename == 'description') {
338
			
339
			_dwca_export_concatenate_description_files($view_data, $zip);
340
			
341
		} else {
342
		
343
			$view_temp_file = $view_data['out_file_url'];
344
			_dwca_export_add_files_to_zip($view_temp_file, $dwca_filename, $zip);
345
		}
346
		
347
	}
348

    
349
	$zip->close();
350

    
351
	return $tmp_archive_file_name;
352
}
353

    
354
/**
355
 * 
356
 * Combines multiple Description Views for different description types into a single text file
357
 * @param $view_data The inner array containing the view data for the different description types
358
 * @param $zip The zip file for the files making up the DwC-A.
359
 */
360
function _dwca_export_concatenate_description_files($view_data, $zip){
361
	
362
	$desc_file_name = drupal_tempnam("temporary://", "description.tmp");
363
	$desc_file = fopen(drupal_realpath($desc_file_name), "w");
364

    
365
	$first_desc_file = true;
366
	drupal_set_message('CALLED _dwca_export_concatenate_description_files... ');
367
	
368
	
369
	foreach($view_data as $type=>$view_data_inner){
370

    
371
		$view_temp_file = $view_data_inner['out_file_url'];
372

    
373
		$descTypeFile = fopen($view_temp_file, "r");
374

    
375
		//print column names from first file
376
		if ($first_desc_file === true) {
377
			$lineOfText = fgets($descTypeFile, 4096);
378
			fwrite($desc_file, $lineOfText);
379
			$first_desc_file = false;
380
			//drupal_set_message(t('FIRST desc file is true... '));
381
			//drupal_set_message(t('Setting desc file type to false... '));
382
		} else {
383
			fgets($descTypeFile, 4096);
384
		}
385
				
386

    
387
		while(!feof($descTypeFile)){
388
			$lineOfText = fgets($descTypeFile, 4096);
389

    
390
			$items = explode(',', $lineOfText);
391
		
392
			drupal_set_message(t('items[0]... ') . $items[0] . "!");
393
			drupal_set_message(t('items[1]... ') . $items[1] . "!");
394
			drupal_set_message(t('items[2]... ') . $items[2] . "!");
395
			//don't include empty descriptions or lines at the end of the view which start with the field name id
396
			//if ($items[2]=="\"\""  || (!$items[0]=="\"id\"")) { //doesn't work - produces empty files
397
			//if ($items[2]!="\"\""  || trim($items[2]!="\"\"")) {
398
			if (!$items[0]=="id") {
399

    
400
				// fix conflict with files that have an EOL that differs from the server EOL
401
				$lineOfText = rtrim($lineOfText, "\r\n") . PHP_EOL;
402
				fwrite($desc_file, $lineOfText);
403
				
404
				//TODO: Fix - some descriptions are missing the closing "
405
				//if (!stripos(strrev($lineOfText), '"') === 0) {
406
				//fputs($desc_file, '"' . PHP_EOL);
407
				
408
			} else {
409
				fgets($descTypeFile, 4096);
410
			}
411

    
412
		}
413

    
414
		fclose($descTypeFile);
415

    
416
		//currently also adding each individual desc type to archive
417
		//_dwca_export_add_files_to_zip($view_temp_file, $type, $zip);
418
	}
419
	fclose($desc_file);
420

    
421
	_dwca_export_add_files_to_zip($desc_file_name, 'description', $zip);
422
}
423

    
424
function _dwca_export_add_files_to_zip($view_temp_file = NULL, $dwca_filename, $zip){
425
	
426
	if($view_temp_file){
427
		$zip->addFile(drupal_realpath($view_temp_file), ARCHIVE_ROOT_DIR.$dwca_filename);
428
	}else{
429
		throw new Exception(t('Cannot create %file', array('%file' => $dwca_filename)));
430
	}
431
}
432

    
433
/**
434
 * Helper function that indicates that we want to
435
 * override the batch that the views_data_export view creates
436
 * on it's initial time through.
437
 *
438
 * Also provides a place to stash options that need to stay around
439
 * until the end of the batch
440
 *
441
 * adapted fom views_data_export.drush.inc
442
 */
443
function _dwca_export_views_data_export_override_batch($view = NULL, $display = NULL, $options = TRUE) {
444
	
445
	static $_views;
446
	if (isset($view)) {
447
		
448
		$_views[$view->name][$display] = $options;
449
	}
450
	return $_views;
451
}
452

    
453

    
454
/**
455
 * Implementation of hook_views_data_export_batch_alter()
456
 *
457
 * adapted fom views_data_export.drush.inc
458
 *
459
 *  @see batch_process() in form.inc
460
 */
461
function dwca_export_views_data_export_batch_alter(&$batch, &$final_destination, &$querystring) {
462

    
463
	$view_name = $batch['view_name'];
464
	$display_id = $batch['display_id'];
465

    
466
	$ok_to_override = _dwca_export_views_data_export_override_batch();
467

    
468
	// Make sure we do nothing if we are called not following the execution of
469
	// our drush command. This could happen if the file with this function in it
470
	// is included during the normal execution of the view
471
	if (!$ok_to_override[$view_name][$display_id]) {
472
		return;
473
	}
474

    
475
	$options = $ok_to_override[$view_name][$display_id];
476

    
477
	// We actually never return from the drupal_alter, but
478
	// use drush's batch system to run the same batch
479

    
480
	// Add a final callback
481
	$batch['operations'][] = array(
482
	    '_dwca_export_views_data_export_batch_finished', array($batch['eid'], $options['output_file']),
483
	);
484

    
485
	$batch['progressive'] = FALSE;
486
}
487

    
488
/**
489
* Implementation of hook_views_data_export_batch_alter()
490
*
491
* @see batch_process() in form.inc
492
*/
493
function dwca_export_batch_alter(&$batch, &$final_destination, &$querystring) {
494
	
495
	if($batch['source_url'] == 'dwca_export'){
496
		$batch['progressive'] = FALSE;
497
	}
498
}
499

    
500

    
501

    
502
/**
503
* Get's called at the end of the drush batch process that generated our export
504
*
505
* adapted fom views_data_export.drush.inc
506
*/
507
function _dwca_export_views_data_export_batch_finished($eid, $output_file, &$context) {
508
	// Fetch export info
509
	$export = views_data_export_get($eid);
510

    
511
	// Perform cleanup
512
	$view = views_data_export_view_retrieve($eid);
513
	$view->set_display($export->view_display_id);
514
	$view->display_handler->batched_execution_state = $export;
515
	$view->display_handler->remove_index();
516

    
517
	// Get path to temp file
518
	$temp_file = $view->display_handler->outputfile_path();
519

    
520
	_dwca_export_archive_descriptor_file_map($output_file, $temp_file);
521

    
522
}
(4-4/6)