Project

General

Profile

Download (10.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2

    
3
/**
4
 * Implements hook_menu().
5
 */
6
function dwca_export_menu() {
7

    
8
	$items = array();
9

    
10
	$items['admin/config/system/dwca_export'] = array(
11
		'title' => 'Darwin Core Archive export',
12
		'description' => t('Create a DarwinCore Archive of this scratchpad.'),
13
		'page callback' => 'drupal_get_form',
14
		'page arguments' => array('dwca_export_config_form'),
15
		'access arguments' => array('access DwC-A export settings'),
16
		'type' => MENU_NORMAL_ITEM,
17
		//'file' => 'dwca_export.admin.inc'
18
	);
19

    
20

    
21
	$items['dwca_export'] = array(
22
		'page callback' => 'dwca_export_deliver_archive',
23
		'access arguments' => array('access content'),
24
		'type' => MENU_CALLBACK
25
	);
26

    
27

    
28
	return $items;
29
}
30

    
31
/**
32
 * Implementation of hook_views_api()
33
 *
34
 * drupal will load dwca_export.views_default.inc when this hook is implemented
35
 */
36
function dwca_export_views_api() {
37
	return array(
38
		'api' => 3.0
39
	);
40
}
41

    
42
/**
43
 * Form function, called by drupal_get_form()
44
 * in dwca_export_menu().
45
 */
46
function dwca_export_config_form($form, &$form_state) {
47

    
48
	global $base_url;
49

    
50
	$form['dwca_export_todo'] = array(
51
		'#markup' => '<p><em>No settings implemented yet...</em></p>'
52
		.'<p>The DarwinCore Archive export is available at '. l('dwca_export', 'dwca_export').'</p>'
53
		.'<p>For general information on the DarwinCore Archive format please refer to  '
54
		. l('GBIF - Standards and Tools - Darwin Core Archives', 'http://www.gbif.org/informatics/standards-and-tools/publishing-data/data-standards/darwin-core-archives/')
55
		.'</p>'
56
	);
57

    
58
	$form['views'] = dwca_export_select_view_form();
59

    
60
	$form['#submit'][] = 'dwca_export_config_form_submit';
61
	return system_settings_form($form);
62

    
63
}
64

    
65

    
66
function dwca_export_select_view_form() {
67

    
68
	$views = array(
69
    		'#type' => 'fieldset',
70
    		'#title' => t('Select views'),
71
    		//'#tree' => TRUE,
72
  	);
73

    
74
	$default_value = _dwca_export_archive_descriptor_file_map();
75

    
76
	$views['classification_view'] = array(
77
  	'#type' => 'textfield',
78
  	'#title' => t('classification:'),
79
  	'#default_value' => $default_value['classification.txt']['view_name'],
80
  	'#size' => 60,
81
  	'#maxlength' => 64,
82
  	'#description' => t('specify view for classification.txt'),
83
	);
84

    
85
	$views['specimen_view'] = array(
86
  	'#type' => 'textfield',
87
  	'#title' => t('specimen:'),
88
	'#default_value' => variable_get('specimen.txt', 'dwca_export_specimen'),
89
  	'#size' => 60,
90
  	'#maxlength' => 64,
91
  	'#description' => t('specify view for specimen.txt'),
92
	);
93

    
94
	$views['reference_view'] = array(
95
  	'#type' => 'textfield',
96
  	'#title' => t('reference:'),
97
  	'#size' => 60,
98
  	'#maxlength' => 64,
99
  	'#description' => t('specify view for reference.txt'),
100
	);
101

    
102
	$views['description_view'] = array(
103
  	'#type' => 'textfield',
104
  	'#title' => t('description:'),
105
  	'#size' => 60,
106
  	'#maxlength' => 64,
107
  	'#description' => t('specify view for description.txt'),
108
	);
109

    
110
	$views['distribution_view'] = array(
111
  	'#type' => 'textfield',
112
  	'#title' => t('distribution:'),
113
  	'#size' => 60,
114
  	'#maxlength' => 64,
115
  	'#description' => t('specify view for distribution.txt'),
116
	);
117

    
118
	$views['image_view'] = array(
119
  	'#type' => 'textfield',
120
  	'#title' => t('image:'),
121
  	'#size' => 60,
122
  	'#maxlength' => 64,
123
  	'#description' => t('specify view for image.txt'),
124
	);
125

    
126
//variable_get('classification.txt', 'test'),
127
	//$views['submit'] = array('#type' => 'submit', '#value' => t('Save selected'));
128

    
129
	return $views;
130
}
131

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

    
134
// 	db_query("INSERT INTO {table} (classification_view) VALUES ('%s')", $form_state['values']['classification_view']);  
135
//variable_set($name, $value)
136
	drupal_set_message(t('Your config form has been saved.'));
137

    
138
//throw new Exception(t('Could not create zip_archive %tmp_archive_file_name', array('%tmp_archive_file_name' => $tmp_archive_file_name)));
139
}
140

    
141

    
142
/**
143
 * menu callback
144
 */
145
function dwca_export_deliver_archive() {
146

    
147
	$tmp_archive_file_name = dwca_export_create_archive( _dwca_export_archive_descriptor_file_map() );
148

    
149
	if($tmp_archive_file_name && file_valid_uri($tmp_archive_file_name)){
150
		file_transfer($tmp_archive_file_name, array('Content-Type' => 'application/zip'));
151
	} else {
152
		throw new Exception(t('Error creating the archive'));
153
	}
154
}
155

    
156

    
157
/**
158
 * Provides the archive_descriptor_file_map which maps dwca file name to a set of view information.
159
 * The view information contains the fields 'view_name', 'display_id', 'out_file_url'.
160
 * The 'out_file_url' is initailly empty and will be set when this function is called
161
 * with both parameters.
162
 *
163
 * @param unknown_type $file_name
164
 * @param unknown_type $out_file_url
165
 *
166
 * @return the archive_descriptor_file_map
167
 */
168
function _dwca_export_archive_descriptor_file_map($file_name = NULL, $out_file_url = null){
169
	static $file_map;
170

    
171
	if(!isset($file_map)){
172
		//TODO load from variables, consider using strongarm
173
		$file_map = array(
174
			'classification.txt' => array(
175
				'view_name'=> 'dwca_export_classification',
176
				'display_id' => 'views_data_export_1',
177
				'out_file_url' => NULL
178
			),
179
			'typesandspecimen.txt' => array(
180
				'view_name'=> 'view_test_specimen_export',
181
				'display_id' => 'views_data_export_1',
182
				'out_file_url' => NULL
183
			)
184
		);
185
	}
186

    
187
	if($file_name && $out_file_url){
188
		$file_map[$file_name]['out_file_url'] = $out_file_url;
189
	}
190

    
191
	return $file_map;
192
}
193

    
194

    
195
/**
196
 * Walks all view export paths defined in the $views_map.
197
 * Each file is downloaded to the tmp folder and a zip file
198
 * is bundeled of the resulting csv files plus the meta file.
199
 *
200
 * @param $views_map - maps a view paths to dwca filenames
201
 *
202
 * @return the path in the filesystem to the final archive,
203
 * or FALSE in case of an error.
204
 */
205
function dwca_export_create_archive($views_map) {
206

    
207
	global $base_url;
208

    
209
	// execute all views to export the data into
210
	// temporary csv files (temporary://dwca_export_*). the resulting filenames
211
	// will be stored in _dwca_export_archive_descriptor_file_map()
212
	foreach($views_map as $filename=>$view_data){
213

    
214
		$view = views_get_view($view_data['view_name']);
215
		$options = array (
216
		    'output_file' => $filename
217
		);
218
		_dwca_export_views_data_export_override_batch($view, $view_data['display_id'], $options);
219
		$view->execute_display($view_data['display_id']);
220

    
221
	}
222

    
223
	// all data is exported to temporary://dwca_export_*
224
	// now we can start bundeling the actual archive
225
	$tmp_archive_file_name = drupal_tempnam("temporary://", "dwca_export_archive_");
226

    
227
	// Unfortunately we cannot use drupals ArchiverZip because there ís
228
	// no way to pass in ZipArchive::CREATE to the constructor to create the archive
229
	// TODO test if zip functionality is available (i.e. if(function_exists('zip_open'))
230
	// but I don't know where the proper location for such a check would be
231
	$zip = new ZipArchive();
232
	// it is safe to use drupal_realpath as the tmp file will be certainly local
233
	// and php's ZipArchive does not handle stream URIs
234
	$result = $zip->open(drupal_realpath($tmp_archive_file_name), ZipArchive::CREATE);
235

    
236
	// there might be a better way to get at this information
237
	$module_static_dir_absolute = realpath(drupal_get_path('module', 'dwca_export')) . "/static/";
238

    
239
	// at the moment we are using a static meta.xml file
240
	$metadata = "meta.xml";
241

    
242
	$zip_root_dir = "dwca_export/";
243

    
244
	if ($result !== TRUE) {
245
		throw new Exception(t('Could not create zip_archive %tmp_archive_file_name', array('%tmp_archive_file_name' => $tmp_archive_file_name)));
246
	}
247
	
248
	
249
	// add metadata
250
	$zip->addFile($module_static_dir_absolute.$metadata, $zip_root_dir.$metadata);
251
	// add the csv data files
252
	foreach(_dwca_export_archive_descriptor_file_map() as $dwca_filename=>$view_data){
253

    
254
		$view_temp_file = $view_data['out_file_url'];
255
		if($view_temp_file){
256
			$zip->addFile(drupal_realpath($view_temp_file), $zip_root_dir.$dwca_filename);
257
		}else{
258
			throw new Exception(t('Cannot create %file', array('%file' => $dwca_filename)));
259
		}
260
	}
261

    
262
	$zip->close();
263

    
264
	return $tmp_archive_file_name;
265

    
266

    
267
}
268

    
269
/**
270
 * Helper function that indicates that we want to
271
 * override the batch that the views_data_export view creates
272
 * on it's initial time through.
273
 *
274
 * Also provides a place to stash options that need to stay around
275
 * until the end of the batch
276
 *
277
 * adapted fom views_data_export.drush.inc
278
 */
279
function _dwca_export_views_data_export_override_batch($view = NULL, $display = NULL, $options = TRUE) {
280
	static $_views;
281
	if (isset($view)) {
282
		$_views[$view->name][$display] = $options;
283
	}
284
	return $_views;
285
}
286

    
287

    
288
/**
289
 * Implementation of hook_views_data_export_batch_alter()
290
 *
291
 * adapted fom views_data_export.drush.inc
292
 *
293
 *  @see batch_process() in form.inc
294
 */
295
function dwca_export_views_data_export_batch_alter(&$batch, &$final_destination, &$querystring) {
296

    
297
	$view_name = $batch['view_name'];
298
	$display_id = $batch['display_id'];
299

    
300
	$ok_to_override = _dwca_export_views_data_export_override_batch();
301

    
302
	// Make sure we do nothing if we are called not following the execution of
303
	// our drush command. This could happen if the file with this function in it
304
	// is included during the normal execution of the view
305
	if (!$ok_to_override[$view_name][$display_id]) {
306
		return;
307
	}
308

    
309
	$options = $ok_to_override[$view_name][$display_id];
310

    
311
	// We actually never return from the drupal_alter, but
312
	// use drush's batch system to run the same batch
313

    
314
	// Add a final callback
315
	$batch['operations'][] = array(
316
	    '_dwca_export_views_data_export_batch_finished', array($batch['eid'], $options['output_file']),
317
	);
318

    
319
	$batch['progressive'] = FALSE;
320
}
321

    
322
/**
323
* Implementation of hook_views_data_export_batch_alter()
324
*
325
* @see batch_process() in form.inc
326
*/
327
function dwca_export_batch_alter(&$batch, &$final_destination, &$querystring) {
328
	if($batch['source_url'] == 'dwca_export'){
329
		$batch['progressive'] = FALSE;
330
	}
331
}
332

    
333

    
334

    
335
/**
336
* Get's called at the end of the drush batch process that generated our export
337
*
338
* adapted fom views_data_export.drush.inc
339
*/
340
function _dwca_export_views_data_export_batch_finished($eid, $output_file, &$context) {
341
	// Fetch export info
342
	$export = views_data_export_get($eid);
343

    
344
	// Perform cleanup
345
	$view = views_data_export_view_retrieve($eid);
346
	$view->set_display($export->view_display_id);
347
	$view->display_handler->batched_execution_state = $export;
348
	$view->display_handler->remove_index();
349

    
350
	// Get path to temp file
351
	$temp_file = $view->display_handler->outputfile_path();
352

    
353
	_dwca_export_archive_descriptor_file_map($output_file, $temp_file);
354

    
355
}
(3-3/4)