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
|
|
12
|
'title' => t('DarwinCore-Archive export'),
|
13
|
'page callback' => 'drupal_get_form',
|
14
|
'page arguments' => array('dwca_export_config_form'),
|
15
|
'access arguments' => array('access administration pages'),
|
16
|
'type' => MENU_LOCAL_TASK,
|
17
|
//'file' => 'dwca_export.admin.inc'
|
18
|
);
|
19
|
|
20
|
$items['dwca_export'] = array(
|
21
|
'page callback' => 'dwca_export_deliver_archive',
|
22
|
'access arguments' => TRUE,
|
23
|
'type' => MENU_CALLBACK
|
24
|
);
|
25
|
|
26
|
|
27
|
return $items;
|
28
|
}
|
29
|
|
30
|
/**
|
31
|
* Implementation of hook_views_api()
|
32
|
*
|
33
|
* drupal will load dwca_export.views_default.inc when this hook is implemented
|
34
|
*/
|
35
|
function dwca_export_views_api() {
|
36
|
return array(
|
37
|
'api' => 3.0
|
38
|
);
|
39
|
}
|
40
|
|
41
|
/**
|
42
|
* Form function, called by drupal_get_form()
|
43
|
* in dwca_export_menu().
|
44
|
*/
|
45
|
function dwca_export_config_form($form, &$form_state) {
|
46
|
|
47
|
$form['dwca_export_todo'] = array(
|
48
|
'#markup' => '<p>No settings implemented yet...</p>'
|
49
|
);
|
50
|
|
51
|
return system_settings_form($form);
|
52
|
}
|
53
|
|
54
|
|
55
|
/**
|
56
|
* menu callback
|
57
|
*/
|
58
|
function dwca_export_deliver_archive() {
|
59
|
|
60
|
$tmp_archive_file_name = dwca_export_create_archive( _get_views_map() );
|
61
|
|
62
|
if($tmp_archive_file_name && file_valid_uri($tmp_archive_file_name)){
|
63
|
file_transfer($tmp_archive_file_name, array('Content-Type' => 'application/zip'));
|
64
|
} else {
|
65
|
throw new Exception(t('Error creating the archive'));
|
66
|
}
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Walks all view export paths defined in the $views_map.
|
71
|
* Each file is downloaded to the tmp folder and a zip file
|
72
|
* is bundeled of the resulting csv files plus the meta file.
|
73
|
*
|
74
|
* @param - $views_map maps a view paths to dwca filenames
|
75
|
*
|
76
|
* @return the path in the filesystem to the final archive,
|
77
|
* or FALSE in case of an error.
|
78
|
*/
|
79
|
function dwca_export_create_archive($views_map) {
|
80
|
|
81
|
$tmp_archive_file_name = drupal_tempnam("temporary://", "dwca_export_");
|
82
|
|
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;
|
132
|
}
|
133
|
|
134
|
/**
|
135
|
* provides the settings which map a view path to
|
136
|
* a dwca filename.
|
137
|
*
|
138
|
*/
|
139
|
function _get_views_map() {
|
140
|
|
141
|
return array(
|
142
|
'dwca_export/classification/csv' => 'classification.txt',
|
143
|
'dwca_export/extension/test/csv' => 'test.txt',
|
144
|
);
|
145
|
}
|