Project

General

Profile

« Previous | Next » 

Revision 96ddb3af

Added by Lorna Morris about 12 years ago

Added files for multiple description types to archive. Need to combine these all into one file for a single description extension. Need to remove debugging drupal messages.

View differences:

7.x/modules/dwca_export/dwca_export.module
99 99

  
100 100
	foreach(variable_get(FILE_MAP) as $dwca_filename => $view_data){
101 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
		);
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
		}
112 127
	}
113 128

  
114 129
	return $views;
115 130
}
116 131

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

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

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

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

  
127 142
			$dwca_export_archive_descriptor_file_map[$key]['view_name'] = $value;
128
			//$save_variables .= '<p>' . $key . ' ' . $value . '</p>';			
129
		}		
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
		}
130 159
	}
131
	
160

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

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

  
167

  
168

  
169

  
138 170
// Reports an error if view name entered by the user does not exist in the database.
139 171
function dwca_export_config_form_validate($form, &$form_state) {
140 172
	
......
146 178
	
147 179
	foreach ($variables as $key => $value) {
148 180
		
181
		//TODO: Check whether the views for the inner array cotaining all the description views exist
149 182
		if (array_key_exists($key, $dwca_export_archive_descriptor_file_map)) {
150 183
					
151 184
			$view = views_get_view($value);
......
196 229
	if(!isset($file_map)){
197 230
		$file_map = variable_get(FILE_MAP);
198 231
	}
232
	
233
	//lorna - there's no file_name for description
234
	drupal_set_message(t('The file_name... ') . $file_name);
199 235

  
200 236
	if($file_name && $out_file_url){
237
		
238
		//lorna
239
		drupal_set_message(t('The file_name... ') . $file_name . t('The out_file_url... ') . $out_file_url);
240
		
241
		if (($file_name == 'general') || ($file_name == 'morphology')) {
242
			$file_map['description'][$file_name]['out_file_url'] = $out_file_url;
243
			//$file_map[$file_name]['morphology']['out_file_url'] = $out_file_url;
244
		} else {
245
		///
201 246
		$file_map[$file_name]['out_file_url'] = $out_file_url;
247
		}
202 248
	}
203 249

  
204 250
	return $file_map;
......
216 262
 * or FALSE in case of an error.
217 263
 */
218 264
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

  
265
	
252 266
	// all data is exported to temporary://dwca_export_*
253 267
	// now we can start bundling the actual archive
254 268
	$tmp_archive_file_name = drupal_tempnam("temporary://", "dwca_export_archive_");
255

  
269
	
256 270
	// Unfortunately we cannot use drupals ArchiverZip because there ís
257 271
	// no way to pass in ZipArchive::CREATE to the constructor to create the archive
258 272
	// TODO test if zip functionality is available (i.e. if(function_exists('zip_open'))
......
261 275
	// it is safe to use drupal_realpath as the tmp file will be certainly local
262 276
	// and php's ZipArchive does not handle stream URIs
263 277
	$result = $zip->open(drupal_realpath($tmp_archive_file_name), ZipArchive::CREATE);
264

  
278
	
265 279
	// there might be a better way to get at this information
266 280
	$module_static_dir_absolute_path = realpath(drupal_get_path('module', 'dwca_export')) . STATIC_DIR;
267

  
281
	
268 282
	if ($result !== TRUE) {
269 283
		throw new Exception(t('Could not create zip_archive %tmp_archive_file_name', array('%tmp_archive_file_name' => $tmp_archive_file_name)));
270 284
	}
271

  
285
	
272 286
	
273 287
	// add metadata
274 288
	$zip->addFile($module_static_dir_absolute_path.METADATA_FILE_NAME, ARCHIVE_ROOT_DIR.METADATA_FILE_NAME);
289

  
290
	global $base_url;
291

  
292
	// execute all views to export the data into
293
	// temporary csv files (temporary://dwca_export_*). the resulting filenames
294
	// will be stored in _dwca_export_archive_descriptor_file_map()
295
	foreach($views_map as $filename=>$view_data){
296
	
297
		//lorna
298
		if ($filename == 'description') {
299
			
300
			//lorna: this foreach iterates through all the description types in the inner array
301
			foreach($view_data as $filename_inner => $view_data_inner){
302
				$view = views_get_view($view_data_inner['view_name']);
303
					
304
				$options = array (
305
				//'output_file' => $view_data['general']['view_name']
306
			'output_file' => $filename_inner
307
				);//The filename is description, but we have mulitple description views - where do we merge them?
308
					
309
				//lorna
310
				drupal_set_message(t('CREATED A FILE NAME FOR DESCRIPTION: ') . $view_data['general']['view_name']);
311
				drupal_set_message(t('The display name is: ') . $view_data['general']['display_id']);
312
				drupal_set_message(t('The filename_inner is: ') . $filename_inner);
313
					
314
				//_dwca_export_views_data_export_override_batch($view, $view_data['general']['display_id'], $options);
315
				_dwca_export_views_data_export_override_batch($view, $view_data_inner['display_id'], $options);
316
					
317
				//$view->execute_display($view_data['general']['display_id']);
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

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

  
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)));
337
		if ($dwca_filename == 'description') {
338
			
339
			//lorna TODO: loop create a new $view_temp_file for each desciption type
340
			
341
			////////drupal_realpath($tmp_archive_file_name)
342
			$desc_file_name = drupal_tempnam("temporary://", "description");
343
			$desc_file = fopen(drupal_realpath($desc_file_name), "w");
344
			
345

  
346
			
347
			foreach($view_data as $type=>$view_data_inner){
348
				//$view_temp_file = $view_data['general']['out_file_url'];
349
				$view_temp_file = $view_data_inner['out_file_url'];
350
				
351
				drupal_set_message(t('The TYPE is: ') . $type);
352
				drupal_set_message(t('The outfile URL: ') . $view_data_inner['out_file_url']);
353
				//lets start by creating a different filename for each type of desc data???
354
				//////
355
				///////
356
				///
357
				$theFile = fopen($view_temp_file, "r");
358
				//$theText = fgets($theFile);
359
				
360
				while(!feof($theFile)){
361
					$lineOfText = fgets($theFile); 
362
					print "$lineOfText<br />";
363
					fputs($desc_file, $lineOfText);
364
					drupal_set_message($lineOfText);
365
				}
366
				
367
				////
368

  
369
				
370
				
371
				_dwca_export_add_files_to_zip($view_temp_file, $type, $zip);
372
			}
373
			_dwca_export_add_files_to_zip($desc_file_name, $type, $zip);
374
			
375
		} else {
376
		
377
			$view_temp_file = $view_data['out_file_url'];
378
			drupal_set_message(t('The outfile URL!!: ') . $view_data['out_file_url']);
379
			_dwca_export_add_files_to_zip($view_temp_file, $dwca_filename, $zip);
283 380
		}
381
			
382
		//lorna temp file - concatenate desc data files
383
		////
384
		////_dwca_export_add_files_to_zip($view_temp_file, $dwca_filename, $zip);
385
		
284 386
	}
285 387

  
286 388
	$zip->close();
......
288 390
	return $tmp_archive_file_name;
289 391
}
290 392

  
393
function _dwca_export_add_files_to_zip($view_temp_file = NULL, $dwca_filename, $zip){
394
	
395
	if($view_temp_file){
396
		$zip->addFile(drupal_realpath($view_temp_file), ARCHIVE_ROOT_DIR.$dwca_filename);
397
	}else{
398
		throw new Exception(t('Cannot create %file', array('%file' => $dwca_filename)));
399
	}
400
}
401

  
291 402
/**
292 403
 * Helper function that indicates that we want to
293 404
 * override the batch that the views_data_export view creates
......
299 410
 * adapted fom views_data_export.drush.inc
300 411
 */
301 412
function _dwca_export_views_data_export_override_batch($view = NULL, $display = NULL, $options = TRUE) {
413
	
414
	//lorna
415
	if ($view) {
416
		drupal_set_message(t('in override batch WITH VIEW '));
417
	} else {
418
	drupal_set_message(t('in override batch: '));
419
	}
302 420
	static $_views;
303 421
	if (isset($view)) {
422
		
423
		//lorna
424
		drupal_set_message(t('in override batch with view: ') . $view->name);
304 425
		$_views[$view->name][$display] = $options;
305 426
	}
306 427
	return $_views;
......
318 439

  
319 440
	$view_name = $batch['view_name'];
320 441
	$display_id = $batch['display_id'];
442
	
443
	//lorna
444
	drupal_set_message(t('in batch alter 1: ') );
321 445

  
322 446
	$ok_to_override = _dwca_export_views_data_export_override_batch();
323 447

  
......
347 471
* @see batch_process() in form.inc
348 472
*/
349 473
function dwca_export_batch_alter(&$batch, &$final_destination, &$querystring) {
474
	
475
	//lorna
476
	drupal_set_message(t('in batch alter 2: ') );
350 477
	if($batch['source_url'] == 'dwca_export'){
351 478
		$batch['progressive'] = FALSE;
352 479
	}
......
372 499
	// Get path to temp file
373 500
	$temp_file = $view->display_handler->outputfile_path();
374 501

  
502
	//lorna - there's no file_name for description
503
	drupal_set_message(t('CALLING _dwca_export_archive_descriptor_file_map WITH OUTPUT_FILE: ') . $output_file . t(' and temp_FILE: ') . $temp_file);
375 504
	_dwca_export_archive_descriptor_file_map($output_file, $temp_file);
376 505

  
377 506
}

Also available in: Unified diff