-
Notifications
You must be signed in to change notification settings - Fork 3
/
islandora_data.module
executable file
·295 lines (269 loc) · 11.9 KB
/
islandora_data.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* @file
* Handles the creation and display of tabular data objects.
*/
/**
* Implements hook_menu().
*/
function islandora_data_menu() {
$items = array();
$items['admin/islandora/tabulardata'] = array(
'title' => 'Tabular Data Solution Pack',
'description' => 'Configure the Islandora Tabular Data Solution Pack.',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('islandora_data_admin_settings'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_theme().
*/
function islandora_data_theme($existing, $type, $theme, $path) {
return array(
'islandora_data' => array(
'template' => 'theme/islandora-data',
'pattern' => 'islandora_data__',
'variables' => array('islandora_object' => NULL),
),
);
}
/**
* Implements hook_islandora_required_objects().
*/
function islandora_data_islandora_required_objects(IslandoraTuque $connection) {
$module_path = drupal_get_path('module', 'islandora_data');
// Tabular data Content Model.
$data_content_model = $connection->repository->constructObject('islandora:sp_data');
$data_content_model->owner = 'fedoraAdmin';
$data_content_model->label = 'Islandora Tabular Data Content Model';
$data_content_model->models = 'fedora-system:ContentModel-3.0';
// ISLANDORACM Datastream.
$datastream = $data_content_model->constructDatastream('ISLANDORACM', 'X');
$datastream->label = 'Islandora content model';
$datastream->mimetype = 'text/xml';
$datastream->setContentFromFile("$module_path/xml/islandora_data_content_model.xml", FALSE);
$data_content_model->ingestDatastream($datastream);
// DS-COMPOSITE-MODEL Datastream.
$datastream = $data_content_model->constructDatastream('DS-COMPOSITE-MODEL', 'X');
$datastream->label = 'DS-COMPOSITE-MODEL';
$datastream->mimetype = 'text/xml';
$datastream->setContentFromFile("$module_path/xml/islandora_data_ds_composite_model.xml", FALSE);
$data_content_model->ingestDatastream($datastream);
// Tabular Data Collection.
$data_collection = $connection->repository->constructObject('islandora:sp_data_collection');
$data_collection->owner = 'fedoraAdmin';
$data_collection->label = 'Tabular Data Collection';
$data_collection->models = 'islandora:collectionCModel';
$data_collection->relationships->add(FEDORA_RELS_EXT_URI, 'isMemberOfCollection', 'islandora:root');
// Collection Policy Datastream.
$datastream = $data_collection->constructDatastream('COLLECTION_POLICY', 'X');
$datastream->label = 'Collection policy';
$datastream->mimetype = 'text/xml';
$datastream->setContentFromFile("$module_path/xml/islandora_data_collection_policy.xml", FALSE);
$data_collection->ingestDatastream($datastream);
// TN Datastream.
$datastream = $data_collection->constructDatastream('TN', 'M');
$datastream->label = 'Thumbnail';
$datastream->mimetype = 'image/png';
$datastream->setContentFromFile("$module_path/images/folder.png", FALSE);
$data_collection->ingestDatastream($datastream);
return array(
'islandora_data' => array(
'title' => 'Islandora tabular data',
'objects' => array(
$data_content_model,
$data_collection,
),
),
);
}
/**
* Implements hook_CMODEL_PID_islandora_view_object().
*/
function islandora_data_islandora_sp_data_islandora_view_object($object, $page_number, $page_size) {
$output = theme('islandora_data', array('islandora_object' => $object));
return array('' => $output);
}
/**
* Implements hook_CMODEL_PID_islandora_object_ingested().
*/
function islandora_data_islandora_sp_data_islandora_object_ingested($object) {
module_load_include('inc', 'islandora_data', 'includes/derivatives');
islandora_data_create_all_derivatives($object);
}
/**
* Implements hook_preprocess_theme().
*/
function islandora_data_preprocess_islandora_data(array &$variables) {
drupal_add_js('misc/form.js');
drupal_add_js('misc/collapse.js');
$islandora_object = $variables['islandora_object'];
$repository = $islandora_object->repository;
module_load_include('inc', 'islandora', 'includes/datastream');
module_load_include('inc', 'islandora', 'includes/utilities');
module_load_include('inc', 'islandora', 'includes/metadata');
if (islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $islandora_object['DC'])) {
try {
$dc = $islandora_object['DC']->content;
$dc_object = DublinCore::importFromXMLString($dc);
}
catch (Exception $e) {
drupal_set_message(t('Error retrieving object %s %t', array('%s' => $islandora_object->id, '
%t' => $e->getMessage())), 'error', FALSE);
}
}
$variables['islandora_dublin_core'] = isset($dc_object) ? $dc_object : NULL;
$variables['dc_array'] = isset($dc_object) ? $dc_object->asArray() : array();
$variables['islandora_object_label'] = $islandora_object->label;
$variables['theme_hook_suggestions'][] = 'islandora_data__' . str_replace(':', '_', $islandora_object->id);
$variables['parent_collections'] = islandora_get_parents_from_rels_ext($islandora_object);
$variables['metadata'] = islandora_retrieve_metadata_markup($islandora_object);
// Thumbnail.
if (isset($islandora_object['TN']) && islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $islandora_object['TN'])) {
$thumbnail_size_url = url("islandora/object/{$islandora_object->id}/datastream/TN/view");
$params = array(
'title' => $islandora_object->label,
'path' => $thumbnail_size_url,
);
$variables['islandora_thumbnail_img'] = theme('image', $params);
}
// CSV datastream(s).
// First, define a default for the return code from curl.
$ret = 1;
if (isset($islandora_object['OBJ']) && islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $islandora_object['OBJ'])) {
$ds_links = array();
foreach ($islandora_object as $ds) {
// We only want to present links to the Ethercalc URLs for datastreams that begin with 'CSV_DATA'.
// There will be one per worksheet in the Excel and OpenOffice files.
if (preg_match('/^CSV_DATA/', $ds->id)) {
$ethercalc_host = variable_get('islandora_data_solution_pack_ethercalc_host', 'http://example.com:8000');
// Download the datastream and save it to a temp file so we can POST it to ethercalc.
$current_process_id = getmypid();
$ds_content_file_path = '/tmp/' . $ds->id . '.' . $current_process_id;
$ds->getContent($ds_content_file_path);
$ds_content_file_size = filesize($ds_content_file_path);
$ds_content_file_handle = fopen($ds_content_file_path, "rb");
// PHP Curl POST:
$curl_instance = curl_init();
$curl_ran = 0;
// Don't try posting if the file is extremely large; Ethercalc will hang
if ($ds_content_file_size <= 5000000) {
curl_setopt($curl_instance, CURLOPT_URL, $ethercalc_host.'/_');
curl_setopt($curl_instance, CURLOPT_POST, 1);
curl_setopt($curl_instance, CURLOPT_POSTFIELDS, file_get_contents($ds_content_file_path));
curl_setopt($curl_instance, CURLOPT_HEADER, 1);
curl_setopt($curl_instance, CURLOPT_HTTPHEADER, array("Content-Type: text/csv"));
curl_setopt($curl_instance, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl_instance);
$curl_ran = 1;
$ethercalc_path = islandora_data_get_response_header($output, 'Location');
}
// 2) Ethercalc renders a Javascript viewer at curl http://example.com:8000/zmy8gm660u, which we
// want to appear in a new browser tab. If curl executed successfully, render links to the
// Ethercalc URLs.
if(curl_errno($curl_instance)==0 && $curl_ran==1) {
$link = l(t("View @ds_label in Ethercalc", array('@ds_label' => $ds->label) ),
url($ethercalc_host . $ethercalc_path,
array('absolute' => TRUE, 'external' => TRUE)),
array('attributes' => array('target' => '_blank', 'title' => t('Opens Ethercalc in a new tab')))
);
$ds_links[] = $link;
}
// Add links to download the raw CSV files regardless of whether curl worked.
$link = l(t("Download @dsid", array('@dsid' => $ds->id)),
islandora_datastream_get_url($ds, $type = 'download')
);
$ds_links[] = $link;
// Clean up the temp file containing the datastream content.
file_unmanaged_delete($ds_content_file_path);
// If curl executed successfully, just render the links.
if(curl_errno($curl_instance)==0 && $curl_ran==1) {
$variables['islandora_content'] = theme('item_list', array('items' => $ds_links,
'title' => t('CSV files, one per worksheet'), 'type' => 'ul', 'attributes' => array()));
}
// If curl didn't execute successfully, render a note to the user and the links to download the raw CSV files.
else {
if (count($ds_links) == 1) {
$variables['islandora_content'] = '<div class="messages warning">' .
t("Sorry, we can't display your CSV file in the browser -- either it's too large, or we're encountering some technical difficulties. You can download it using the link below.") . '</div>';
}
else {
$variables['islandora_content'] = '<div class="messages warning">' .
t("Sorry, we can't display at least one of your CSV files in the browser -- either they're too large, or we're encountering some technical difficulties. You can download them using the links below.") . '</div>';
}
$variables['islandora_content'] .= theme('item_list', array('items' => $ds_links,
'title' => t('CSV files, one per worksheet'), 'type' => 'ul', 'attributes' => array()));
}
curl_close($curl_instance);
}
}
}
}
/**
* Implements hook_islandora_content_model_forms_form_associations().
*/
function islandora_data_islandora_content_model_forms_form_associations() {
return array(
'islandora_data_mods_form' => array(
'content_model' => 'islandora:sp_data',
'form_name' => 'Basic tabular data MODS form',
'dsid' => 'MODS',
'title_field' => array('titleInfo', 'title'),
'transform' => 'mods_to_dc.xsl',
'template' => FALSE,
),
);
}
/**
* Implements hook_islandora_xml_form_builder_forms().
*/
function islandora_data_islandora_xml_form_builder_forms() {
$module_path = drupal_get_path('module', 'islandora_data');
return array(
'Basic tabular data MODS form' => array(
'form_file' => "$module_path/xml/islandora_data_form_mods.xml",
),
);
}
/**
* Implements hook_islandora_ingest_steps().
*/
function islandora_data_islandora_sp_data_islandora_ingest_steps() {
return array(
'islandora_data' => array(
'weight' => 10,
'type' => 'form',
'form_id' => 'islandora_data_upload_form',
'module' => 'islandora_data',
'file' => 'includes/data_upload.form.inc',
),
);
}
/**
* Parses out a specific header value from the HTTP response.
*
* @param string $curl_result
* The entire cURL result, as provided by curl_exec() with
* curl_setopt($curl_instance, CURLOPT_RETURNTRANSFER, 1).
* @param string $wanted_header
* The name of the header whose value you want, without the
* trailing colon (:).
* @return bool|string
* The value of the header, FALSE if header not found.
*/
/**
* Parses header response from Ethercalc.
*/
function islandora_data_get_response_header($curl_result, $wanted_header) {
foreach (explode("\r\n", $curl_result) as $header) {
if (preg_match('/'. preg_quote($wanted_header) . '/', $header)) {
$header_value = str_replace($wanted_header . ':', '', $header);
$cleaned_underscore = str_replace('_/','', $header_value);
return trim($cleaned_underscore);
}
}
}