Project

General

Profile

Download (7.54 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/settings'] = array(
11
		'title' => 'DarwinCore-Archive export',
12
		'page callback' => 'drupal_get_form',
13
		'page arguments' => array('dwca_export_config_form'),
14
		'access arguments' => array('access administration pages'),
15
		'type' => MENU_LOCAL_TASK,
16
		//'file' => 'dwca_export.admin.inc'
17
	);
18

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

    
25

    
26
	return $items;
27
}
28

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

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

    
46
	$form['dwca_export_todo'] = array(
47
		'#markup' => '<p>No settings implemented yet...</p>'
48
	);
49

    
50
	return system_settings_form($form);
51
}
52

    
53

    
54
/**
55
 * menu callback
56
 */
57
function dwca_export_deliver_archive() {
58

    
59
	$tmp_archive_file_name = dwca_export_create_archive( _dwca_export_archive_descriptor_file_map() );
60

    
61
	if($tmp_archive_file_name && file_valid_uri($tmp_archive_file_name)){
62
		file_transfer($tmp_archive_file_name, array('Content-Type' => 'application/zip'));
63
	} else {
64
		throw new Exception(t('Error creating the archive'));
65
	}
66
}
67

    
68

    
69
/**
70
 * Provides the archive_descriptor_file_map which maps dwca file name to a set of view information.
71
 * The view information contains the fields 'view_name', 'display_id', 'out_file_url'.
72
 * The 'out_file_url' is initailly empty and will be set when this function is called
73
 * with both parameters.
74
 *
75
 * @param unknown_type $file_name
76
 * @param unknown_type $out_file_url
77
 *
78
 * @return the archive_descriptor_file_map
79
 */
80
function _dwca_export_archive_descriptor_file_map($file_name = NULL, $out_file_url = null){
81
	static $file_map;
82

    
83
	if(!isset($file_map)){
84
		//TODO load from variables, consider using strongarm
85
		$file_map = array(
86
			'classification.txt' => array(
87
				'view_name'=> 'dwca_export_classification',
88
				'display_id' => 'views_data_export_1',
89
				'out_file_url' => NULL
90
			)
91
		);
92
	}
93

    
94
	if($file_name && $out_file_url){
95
		$file_map[$file_name]['out_file_url'] = $out_file_url;
96
	}
97

    
98
	return $file_map;
99
}
100

    
101
/**
102
 * Walks all view export paths defined in the $views_map.
103
 * Each file is downloaded to the tmp folder and a zip file
104
 * is bundeled of the resulting csv files plus the meta file.
105
 *
106
 * @param $views_map - maps a view paths to dwca filenames
107
 *
108
 * @return the path in the filesystem to the final archive,
109
 * or FALSE in case of an error.
110
 */
111
function dwca_export_create_archive($views_map) {
112

    
113
	global $base_url;
114

    
115
	// execute all views to export the data into
116
	// temporary csv files (temporary://dwca_export_*). the resulting filenames
117
	// will be stored in _dwca_export_archive_descriptor_file_map()
118
	foreach($views_map as $filename=>$view_data){
119

    
120
		$view = views_get_view($view_data['view_name']);
121
		$options = array (
122
		    'output_file' => $filename
123
		);
124
		_dwca_export_views_data_export_override_batch($view, $view_data['display_id'], $options);
125
		$view->execute_display($view_data['display_id']);
126

    
127
	}
128

    
129
	// all data is exported to temporary://dwca_export_*
130
	// now we can start bundeling the actual archive
131
	$tmp_archive_file_name = drupal_tempnam("temporary://", "dwca_export_archive_");
132

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

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

    
145
	// at the moment we are using a static meta.xml file
146
	$metadata = "meta.xml";
147

    
148
	$zip_root_dir = "dwca_export/";
149

    
150
	if ($result !== TRUE) {
151
		throw new Exception(t('Could not create zip_archive %tmp_archive_file_name', array('%tmp_archive_file_name' => $tmp_archive_file_name)));
152
	}else{
153
		// add metadata
154
		$zip->addFile($module_static_dir_absolute.$metadata, $zip_root_dir.$metadata);
155
		// add the csv data files
156
	}
157

    
158
	foreach(_dwca_export_archive_descriptor_file_map() as $dwca_filename=>$view_data){
159

    
160
		$view_temp_file = $view_data['out_file_url'];
161
		if($view_temp_file){
162
			$zip->addFile(drupal_realpath($view_temp_file), $zip_root_dir.$dwca_filename);
163
		}else{
164
			throw new Exception(t('Cannot create %file', array('%file' => $dwca_filename)));
165
		}
166
	}
167

    
168
	$zip->close();
169

    
170
	return $tmp_archive_file_name;
171

    
172

    
173
}
174

    
175
/**
176
 * Helper function that indicates that we want to
177
 * override the batch that the views_data_export view creates
178
 * on it's initial time through.
179
 *
180
 * Also provides a place to stash options that need to stay around
181
 * until the end of the batch
182
 *
183
 * adapted fom views_data_export.drush.inc
184
 */
185
function _dwca_export_views_data_export_override_batch($view = NULL, $display = NULL, $options = TRUE) {
186
	static $_views;
187
	if (isset($view)) {
188
		$_views[$view->name][$display] = $options;
189
	}
190
	return $_views;
191
}
192

    
193

    
194
/**
195
 * Implementation of hook_views_data_export_batch_alter()
196
 *
197
 * adapted fom views_data_export.drush.inc
198
 *
199
 *  @see batch_process() in form.inc
200
 */
201
function dwca_export_views_data_export_batch_alter(&$batch, &$final_destination, &$querystring) {
202

    
203
	$view_name = $batch['view_name'];
204
	$display_id = $batch['display_id'];
205

    
206
	$ok_to_override = _dwca_export_views_data_export_override_batch();
207

    
208
	// Make sure we do nothing if we are called not following the execution of
209
	// our drush command. This could happen if the file with this function in it
210
	// is included during the normal execution of the view
211
	if (!$ok_to_override[$view_name][$display_id]) {
212
		return;
213
	}
214

    
215
	$options = $ok_to_override[$view_name][$display_id];
216

    
217
	// We actually never return from the drupal_alter, but
218
	// use drush's batch system to run the same batch
219

    
220
	// Add a final callback
221
	$batch['operations'][] = array(
222
	    '_dwca_export_views_data_export_batch_finished', array($batch['eid'], $options['output_file']),
223
	);
224

    
225
	$batch['progressive'] = FALSE;
226
}
227

    
228
/**
229
* Implementation of hook_views_data_export_batch_alter()
230
*
231
* @see batch_process() in form.inc
232
*/
233
function dwca_export_batch_alter(&$batch, &$final_destination, &$querystring) {
234
	if($batch['source_url'] == 'dwca_export'){
235
		$batch['progressive'] = FALSE;
236
	}
237
}
238

    
239

    
240

    
241
/**
242
* Get's called at the end of the drush batch process that generated our export
243
*
244
* adapted fom views_data_export.drush.inc
245
*/
246
function _dwca_export_views_data_export_batch_finished($eid, $output_file, &$context) {
247
	// Fetch export info
248
	$export = views_data_export_get($eid);
249

    
250
	// Perform cleanup
251
	$view = views_data_export_view_retrieve($eid);
252
	$view->set_display($export->view_display_id);
253
	$view->display_handler->batched_execution_state = $export;
254
	$view->display_handler->remove_index();
255

    
256
	// Get path to temp file
257
	$temp_file = $view->display_handler->outputfile_path();
258

    
259
	_dwca_export_archive_descriptor_file_map($output_file, $temp_file);
260

    
261
}
(2-2/3)