Project

General

Profile

Download (14.5 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
			foreach($view_data as $dwca_filename_inner => $view_data_inner){
105
				$views[$dwca_filename][$dwca_filename_inner] = array(
106
									  	'#type' => 'textfield',
107
									  	'#title' => t($dwca_filename . '_' . $dwca_filename_inner),
108
									  	'#default_value' => $view_data_inner['view_name'],
109
									  	'#size' => 60,
110
									  	'#maxlength' => 64,
111
									  	'#description' => t('specify view for ' . $dwca_filename_inner),
112
			
113
				);
114
			}
115
			
116
		} else {
117
			$views[$dwca_filename] = array(
118
					  	'#type' => 'textfield',
119
					  	'#title' => t($dwca_filename),
120
					  	'#default_value' => $view_data['view_name'],
121
					  	'#size' => 60,
122
					  	'#maxlength' => 64,
123
					  	'#description' => t('specify view for ' . $dwca_filename),
124
			
125
			);
126
		}
127
	}
128

    
129
	return $views;
130
}
131

    
132
function dwca_export_config_form_submit($form, &$form_state) {
133

    
134
	$variables = $form_state['input'];
135
	$save_variables = '';
136
	$dwca_export_archive_descriptor_file_map = variable_get(FILE_MAP);
137

    
138
	foreach ($variables as $key => $value) {
139

    
140
		if (array_key_exists($key, $dwca_export_archive_descriptor_file_map)) {
141

    
142
			$dwca_export_archive_descriptor_file_map[$key]['view_name'] = $value;
143
			//$save_variables .= '<p>' . $key . ' ' . $value . '</p>';
144
		} else {
145

    
146
			$description_key = 'description';
147
			
148
			if (array_key_exists($description_key, $dwca_export_archive_descriptor_file_map)) {
149
				
150
				//get the inner array containing the different description data types
151
				$description_map = $dwca_export_archive_descriptor_file_map[$description_key];
152
				//if (array_key_exists($key, $dwca_export_archive_descriptor_file_map['description'])) {
153
				if (array_key_exists($key, $description_map)) {
154
					drupal_set_message(t('The ooKEY is... ') . $key . t('The ooVALUE is... ') . $value);
155
					$dwca_export_archive_descriptor_file_map[$description_key][$key]['view_name'] = $value;
156
				}
157
			}
158
		}
159
	}
160

    
161
	variable_del(FILE_MAP);
162
	variable_set(FILE_MAP, $dwca_export_archive_descriptor_file_map);
163

    
164
	//drupal_set_message(t('The classification view is ') . $save_variables);// . '<pre>' . print_r($form_state,true) . '</pre>');//$save_variables);
165
}
166

    
167

    
168

    
169

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

    
200
/**
201
 * menu callback
202
 */
203
function dwca_export_deliver_archive() {
204

    
205
	$tmp_archive_file_name = dwca_export_create_archive( _dwca_export_archive_descriptor_file_map() );
206

    
207
	if($tmp_archive_file_name && file_valid_uri($tmp_archive_file_name)){
208
		file_transfer($tmp_archive_file_name, array('Content-Type' => 'application/zip'));
209
	} else {
210
		throw new Exception(t('Error creating the archive'));
211
	}
212
}
213

    
214

    
215
/**
216
 * Provides the archive_descriptor_file_map which maps dwca file name to a set of view information.
217
 * The view information contains the fields 'view_name', 'display_id', 'out_file_url'.
218
 * The 'out_file_url' is initailly empty and will be set when this function is called
219
 * with both parameters.
220
 *
221
 * @param String $file_name
222
 * @param String $out_file_url
223
 *
224
 * @return the archive_descriptor_file_map
225
 */
226
function _dwca_export_archive_descriptor_file_map($file_name = NULL, $out_file_url = null){
227
	static $file_map;
228

    
229
	if(!isset($file_map)){
230
		$file_map = variable_get(FILE_MAP);
231
	}
232

    
233
	if($file_name && $out_file_url){
234
		
235
		if (($file_name == 'general') || ($file_name == 'morphology')) {
236
			$file_map['description'][$file_name]['out_file_url'] = $out_file_url;
237
			//$file_map[$file_name]['morphology']['out_file_url'] = $out_file_url;
238
		} else {
239
		
240
		$file_map[$file_name]['out_file_url'] = $out_file_url;
241
		}
242
	}
243

    
244
	return $file_map;
245
}
246

    
247

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

    
284
	global $base_url;
285

    
286
	// execute all views to export the data into
287
	// temporary csv files (temporary://dwca_export_*). the resulting filenames
288
	// will be stored in _dwca_export_archive_descriptor_file_map()
289
	foreach($views_map as $filename=>$view_data){
290
	
291
		//lorna
292
		if ($filename == 'description') {
293
			
294
			//lorna: this foreach iterates through all the description types in the inner array
295
			foreach($view_data as $filename_inner => $view_data_inner){
296
				$view = views_get_view($view_data_inner['view_name']);
297
					
298
				$options = array (
299
					'output_file' => $filename_inner
300
				);
301
					
302
				//_dwca_export_views_data_export_override_batch($view, $view_data['general']['display_id'], $options);
303
				_dwca_export_views_data_export_override_batch($view, $view_data_inner['display_id'], $options);
304
					
305
				$view->execute_display($view_data_inner['display_id']);
306
			}
307
			
308
		}
309
		else {
310
			$view = views_get_view($view_data['view_name']);
311
			$options = array (
312
					    'output_file' => $filename
313
			);
314
			_dwca_export_views_data_export_override_batch($view, $view_data['display_id'], $options);
315
			$view->execute_display($view_data['display_id']);			
316
		}
317
		//end lorna
318
		
319
	}
320

    
321
	// add the csv data files
322
	foreach(_dwca_export_archive_descriptor_file_map() as $dwca_filename=>$view_data){
323

    
324
		if ($dwca_filename == 'description') {
325
			
326
			_dwca_export_concatenate_description_files($view_data, $zip);
327
			
328
		} else {
329
		
330
			$view_temp_file = $view_data['out_file_url'];
331
			_dwca_export_add_files_to_zip($view_temp_file, $dwca_filename, $zip);
332
		}
333
		
334
	}
335

    
336
	$zip->close();
337

    
338
	return $tmp_archive_file_name;
339
}
340

    
341
/**
342
 * 
343
 * Combines multiple Description Views for different description types into a single text file
344
 * @param $view_data The inner array containing the view data for the different description types
345
 * @param $zip The zip file for the files making up the DwC-A.
346
 */
347
function _dwca_export_concatenate_description_files($view_data, $zip){
348
	
349
	$desc_file_name = drupal_tempnam("temporary://", "description.tmp");
350
	$desc_file = fopen(drupal_realpath($desc_file_name), "w");
351

    
352
	$first_desc_file = true;
353
	foreach($view_data as $type=>$view_data_inner){
354

    
355
		$view_temp_file = $view_data_inner['out_file_url'];
356

    
357
		$theFile = fopen($view_temp_file, "r");
358

    
359
		//print column names from first file
360
		if ($first_desc_file) {
361
			$lineOfText = fgets($theFile);
362
			fputs($desc_file, $lineOfText);
363
			$first_desc_file = false;
364
		} else {
365
			fgets($theFile);
366
		}
367

    
368
		while(!feof($theFile)){
369
			$lineOfText = fgets($theFile);
370
			print "$lineOfText<br />";
371

    
372
			$items = explode(',', $lineOfText);
373

    
374
			if (!empty($items[2])) {
375

    
376
				fputs($desc_file, $lineOfText);
377
				//add an end of line if there isn't one
378
				//if (!stripos(strrev($lineOfText), '"') === 0) {
379
				//fputs($desc_file, '"' . PHP_EOL);
380
				fputs($desc_file, PHP_EOL);
381
				//}
382
			} else {
383
				fgets($theFile);
384
			}
385

    
386
		}
387

    
388
		fclose($theFile);
389

    
390
		//currently also adding each individual desc type to archive
391
		_dwca_export_add_files_to_zip($view_temp_file, $type, $zip);
392
	}
393
	fclose($desc_file);
394

    
395
	_dwca_export_add_files_to_zip($desc_file_name, 'description', $zip);
396
}
397

    
398
function _dwca_export_add_files_to_zip($view_temp_file = NULL, $dwca_filename, $zip){
399
	
400
	if($view_temp_file){
401
		$zip->addFile(drupal_realpath($view_temp_file), ARCHIVE_ROOT_DIR.$dwca_filename);
402
	}else{
403
		throw new Exception(t('Cannot create %file', array('%file' => $dwca_filename)));
404
	}
405
}
406

    
407
/**
408
 * Helper function that indicates that we want to
409
 * override the batch that the views_data_export view creates
410
 * on it's initial time through.
411
 *
412
 * Also provides a place to stash options that need to stay around
413
 * until the end of the batch
414
 *
415
 * adapted fom views_data_export.drush.inc
416
 */
417
function _dwca_export_views_data_export_override_batch($view = NULL, $display = NULL, $options = TRUE) {
418
	
419
	static $_views;
420
	if (isset($view)) {
421
		
422
		$_views[$view->name][$display] = $options;
423
	}
424
	return $_views;
425
}
426

    
427

    
428
/**
429
 * Implementation of hook_views_data_export_batch_alter()
430
 *
431
 * adapted fom views_data_export.drush.inc
432
 *
433
 *  @see batch_process() in form.inc
434
 */
435
function dwca_export_views_data_export_batch_alter(&$batch, &$final_destination, &$querystring) {
436

    
437
	$view_name = $batch['view_name'];
438
	$display_id = $batch['display_id'];
439

    
440
	$ok_to_override = _dwca_export_views_data_export_override_batch();
441

    
442
	// Make sure we do nothing if we are called not following the execution of
443
	// our drush command. This could happen if the file with this function in it
444
	// is included during the normal execution of the view
445
	if (!$ok_to_override[$view_name][$display_id]) {
446
		return;
447
	}
448

    
449
	$options = $ok_to_override[$view_name][$display_id];
450

    
451
	// We actually never return from the drupal_alter, but
452
	// use drush's batch system to run the same batch
453

    
454
	// Add a final callback
455
	$batch['operations'][] = array(
456
	    '_dwca_export_views_data_export_batch_finished', array($batch['eid'], $options['output_file']),
457
	);
458

    
459
	$batch['progressive'] = FALSE;
460
}
461

    
462
/**
463
* Implementation of hook_views_data_export_batch_alter()
464
*
465
* @see batch_process() in form.inc
466
*/
467
function dwca_export_batch_alter(&$batch, &$final_destination, &$querystring) {
468
	
469
	if($batch['source_url'] == 'dwca_export'){
470
		$batch['progressive'] = FALSE;
471
	}
472
}
473

    
474

    
475

    
476
/**
477
* Get's called at the end of the drush batch process that generated our export
478
*
479
* adapted fom views_data_export.drush.inc
480
*/
481
function _dwca_export_views_data_export_batch_finished($eid, $output_file, &$context) {
482
	// Fetch export info
483
	$export = views_data_export_get($eid);
484

    
485
	// Perform cleanup
486
	$view = views_data_export_view_retrieve($eid);
487
	$view->set_display($export->view_display_id);
488
	$view->display_handler->batched_execution_state = $export;
489
	$view->display_handler->remove_index();
490

    
491
	// Get path to temp file
492
	$temp_file = $view->display_handler->outputfile_path();
493

    
494
	_dwca_export_archive_descriptor_file_map($output_file, $temp_file);
495

    
496
}
(3-3/5)