Project

General

Profile

« Previous | Next » 

Revision 7156bdbf

Added by Niels Hoffmann over 12 years ago

Added the functionality to create the zip archive. The zip file contains emtpy files at the moment

View differences:

7.x/modules/dwca_export/dwca_export.info
1 1
name = DarwinCore Archive (DwC-A) export
2 2
description = Creates a DarwinCore Archive of this scratchpad
3 3

  
4
dependencies[] = views_data_export
5

  
6 4
package = "Scratchpads"
7

  
8 5
core = 7.x
9 6

  
7
files[] = dwca_export.module
8
dependencies[] = views_data_export
9

  
10
; Theme
11
files[] = theme/dwca_export.theme.inc
12

  
10 13
; manually configured version since this module is not yet hosted on the drupal.org infrastructure
11 14
; see also bhttp://drupal.org/node/542202#version
12 15
version = "7.x-0.1-dev"
7.x/modules/dwca_export/dwca_export.module
33 33
 * drupal will load dwca_export.views_default.inc when this hook is implemented
34 34
 */
35 35
function dwca_export_views_api() {
36
	return array('api' => 3.0);
36
	return array(
37
		'api' => 3.0
38
	);
37 39
}
38 40

  
39 41
/**
......
43 45
function dwca_export_config_form($form, &$form_state) {
44 46

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

  
49 51
	return system_settings_form($form);
......
57 59

  
58 60
	$tmp_archive_file_name = dwca_export_create_archive( _get_views_map() );
59 61

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

  
......
78 80

  
79 81
	$tmp_archive_file_name = drupal_tempnam("temporary://", "dwca_export_");
80 82

  
81
	return $tmp_archive_file_name;
83
	// Unfortunately we cannot use drupals ArchiverZip because there ís
84
	// no way to pass in ZipArchive::CREATE to the constructor to create the archive
85
	// TODO test if zip functionality is available (i.e. if(function_exists('zip_open'))
86
	// but I don't know where the proper location for such a check would be
87
	$zip = new ZipArchive();
88
	// it is safe to use drupal_realpath as the tmp file will be certainly local
89
	// and php's ZipArchive does not handle stream URIs
90
	$result = $zip->open(drupal_realpath($tmp_archive_file_name), ZipArchive::CREATE);
91

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

  
95
	// at the moment we are using a static meta.xml file
96
	$metadata = "meta.xml";
97

  
98
	$zip_root_dir = "dwca_export/";
99

  
100
	if ($result !== TRUE) {
101
		throw new Exception(t('Could not create zip_archive %tmp_archive_file_name', array('%tmp_archive_file_name' => $tmp_archive_file_name)));
102
	}else{
103
		// add metadata
104
		$zip->addFile($module_static_dir_absolute.$metadata, $zip_root_dir.$metadata);
105
		// add the csv data files
106
		foreach($views_map as $view => $file){
107
			$view_temp_file = _create_views_file($view);
108
			if($view_temp_file){
109
				$zip->addFile(drupal_realpath($view_temp_file), $zip_root_dir.$file);
110
			}else{
111
				throw new Exception(t('Cannot create %file', array('%file' => $file)));
112
			}
113
		}
114
		$zip->close();
115

  
116
		return $tmp_archive_file_name;
117
	} 
118
}
119

  
120
/**
121
 * TODO Downloads the view and returns the filepath
122
 *
123
 * @param string $view 
124
 * @param string $file 
125
 * @return a filepath to the downloaded view
126
 * @author Niels Hoffmann
127
 */
128
function _create_views_file($view, $file) {
129
	// FIXME this is a mock
130
	$view_temp_file = drupal_tempnam("temporary://", "dwca_export_view_");
131
	return $view_temp_file;
82 132
}
83 133

  
84 134
/**
......
89 139
function _get_views_map() {
90 140

  
91 141
	return array(
92
		'dwca_export/classification/csv' => 'classification.txt'
142
		'dwca_export/classification/csv' => 'classification.txt',
143
		'dwca_export/extension/test/csv' => 'test.txt',
93 144
	);
94 145
}

Also available in: Unified diff