-
Notifications
You must be signed in to change notification settings - Fork 0
/
landing_page_creator.module
445 lines (378 loc) · 14.7 KB
/
landing_page_creator.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
function landing_page_creator_help($path, $arg) {
switch ($path) {
case "admin/help#landing_page_creator":
return t("Create landing pages for assignment of DOIs");
break;
}
}
function landing_page_creator_init(){
$mpath = drupal_get_path('module', 'landing_page_creator');
drupal_add_css($mpath . '/css/landing_page_creator.css');
}
function landing_page_creator_permission() {
return array(
'access landing_page_creator content' => array(
'title' => t('Access content for the Landing Page module'),
)
);
}
function landing_page_creator_menu() {
$items = array();
// create landing page
$items['landing-page-creator/form'] = array(
'title' => t('Landing Page creator'),
'page callback' => 'landing_page_creator_form',
'access arguments' => array('access landing_page_creator content'),
'description' => t('Page to submit DOIs request'),
'type' => MENU_CALLBACK,
);
// create landing page administrative page
// it requires to set up datacite account
$items['admin/config/services/doi-request'] = array(
'title' => 'Datacite account configuration',
'description' => 'Configuration for the request of DOI',
'page callback' => 'drupal_get_form',
'page arguments' => array('landing_page_creator_admin'),
'access arguments' => array('access administration pages'),
'file' => 'landing_page_creator.admin.inc',
);
return $items;
}
function landing_page_creator_form() {
//landing_page_creator_login(drupal_get_destination());
return drupal_get_form('creator_form');
}
function creator_form($form, $form_state) {
$form['creation'] = array(
'#type' => 'markup',
'#format' => 'html',
'#markup' => t('Here you create a landing page for your dataset and automatically assign a DOI using the METNO Datacite account. <br>
Upload your xml, with mmd specifications according to <a href="https://github.com/steingod/mmd/blob/master/doc/mmd-specification.pdf">the METNO Metatdata Description</a>. <br><br>
<span style="color:red; font-weight:bold">This is a one-step process: once you submit the form a DOI will be created and cannot be deleted.</span>'),
);
// upload the xml
$form['xml_file'] = array(
'#type' => 'managed_file',
'#title' => t('File upload'),
'#upload_validators' => array('file_validate_extensions' => array('xml'),),
'#upload_location' => 'public://landingpage_xml',
);
$form['submit'] = array(
'#type' => 'submit',
'#submit' => array('landing_page_creator_submit'),
'#value' => t('Submit'),
);
$form['#validate'][] = 'datacite_conf_validate';
return $form;
}
//validate configuration of datacite
function datacite_conf_validate($form, &$form_state) {
//get user and pass from admin configuration
$datacite_user = variable_get('username_datacite');
$datacite_pass = variable_get('pass_datacite');
$datacite_prefix = variable_get('prefix_datacite');
if (!isset($datacite_user) || $datacite_user == ''){
form_set_error('', t('You are connecting to DataCite to obtain a DOI. <br>
Configure your Datacite credentials in the configuration interface'));
}
if (!isset($datacite_pass) || $datacite_pass == ''){
form_set_error('', t('You are connecting to DataCite to obtain a DOI. <br>
Configure your Datacite credentials in the configuration interface'));
}
if (!isset($datacite_prefix) || $datacite_prefix == ''){
form_set_error('', t('You are connecting to DataCite to obtain a DOI. <br>
Configure your Datacite credentials in the configuration interface'));
}
}
// extract mmd to the last child
function depth_mmd($prefix, $iterator) {
$kv_a = array();
$prefix_ = $prefix.' ';
if ($prefix == '') {
$prefix_ = '';
}
foreach ($iterator as $k => $v) {
if ($iterator->hasChildren()) {
$kv_a = array_merge($kv_a, depth_mmd($prefix_ . $k, $v));
if ($k == 'personnel' || $k == 'data_access') {
$kv_a[] = array($k);
}
} else {
//add mmd keys and values to form_state
$kv_a[] = array($prefix_ . $k, (string)$v);
}
}
return $kv_a; //this function returns an array of arrys
}
function landing_page_creator_submit($form, &$form_state) {
global $user;
global $base_url;
global $base_path;
// uploaded file with mmd specifications
$furi = $form['xml_file']["#file"]->uri;
// translate mmd.xml to datacite.xml
// should the repository included in this module?
$furi_rp = drupal_realpath($furi);
exec('xsltproc '.drupal_get_path('module', 'landing_page_creator').'/includes/mmd-to-datacite.xsl '.$furi_rp.' 2>&1', $datacite_metadata, $status); //requires xsl file from repo
$datacite_user = variable_get('username_datacite');
$datacite_pass = variable_get('pass_datacite');
$datacite_prefix = variable_get('prefix_datacite');
$datacite_url = variable_get('url_datacite','');
// send metadata to datacite
//curl -H "Content-Type: application/xml;charset=UTF-8" -X POST -i --user username:password -d @$datacite_metadata.xml https://mds.test.datacite.org/metadata
$xml = implode(" ",$datacite_metadata);
$options_md = array(
'method' => 'POST',
'data' => $xml,
'timeout' => 25,
'headers' => array('Content-Type' => 'application/xml;charset=UTF-8',
'Authorization' => 'Basic ' . base64_encode($datacite_user . (":" . $datacite_pass)),),
);
$result = drupal_http_request('https://mds.'.$datacite_url.'datacite.org/metadata/'.$datacite_prefix, $options_md);
//extract DOI from http response
if ($result->code == 201) {
$doi = explode("metadata/", $result->headers['location'])[1];
if ($datacite_url == 'test.') {
$doi_uri = 'https://handle.test.datacite.org/'.$doi; //test env.
}else{
$doi_uri = 'https://doi.org/'.$doi; // operational env.
}
}else{
drupal_set_message(print_r('Datacite request has failed',TRUE),'warning');
}
//citation becomes:
//Creator (PublicationYear): Title. Version. Publisher. (resourceTypeGeneral). Identifier
$xml_content = file_get_contents($furi); // this is a string from gettype
////get xml object iterator
$xml = new SimpleXmlIterator($xml_content); // problem with boolean
$xml_wns = $xml->children($xml->getNamespaces(true)['mmd']);
//
$form_state['metadata'] = depth_mmd("", $xml_wns);
// personnel
$count_personnel = 0;
$role_list = array();
$name_list = array();
$email_list = array();
$inst_list = array();
// isotopic categories
$count_itc = 0;
$itc_list = array();
// data access type & resource
$count_access = 0;
$dat_list = array();
$dar_list = array();
// isotopic categories
$count_itc = 0;
$itc_list = array();
foreach ($form_state['metadata'] as &$v) {
if ($v[0] == 'metadata_identifier') {
$metadata_id = $v[1];
}
if ($v[0] == 'title') {
$title = $v[1];
}
if ($v[0] == 'abstract') {
$abstract = $v[1];
}
if ($v[0] == 'temporal_extent start_date') {
$start_date = $v[1];
}
if ($v[0] == 'temporal_extent end_date') {
$end_date = $v[1];
}
if ($v[0] == 'geographic_extent rectangle north') {
$north = $v[1];
}
if ($v[0] == 'geographic_extent rectangle south') {
$south = $v[1];
}
if ($v[0] == 'geographic_extent rectangle east') {
$east = $v[1];
}
if ($v[0] == 'geographic_extent rectangle west') {
$west = $v[1];
}
// start personnel
if ($v[0] == 'personnel role'){
$role_list[$count_personnel] = $v[1];
}
if ($v[0] == 'personnel name') {
$name_list[$count_personnel] = $v[1];
}
if ($v[0] == 'personnel email') {
$email_list[$count_personnel] = $v[1];
}
if ($v[0] == 'personnel organisation') {
$inst_list[$count_personnel] = $v[1];
}
if ($v[0] == 'personnel') {
// make sure the arrays are not empty when field is missing
if (!isset($role_list[$count_personnel])) {
$role_list[$count_personnel] = null;
}
if (!isset($name_list[$count_personnel])) {
$name_list[$count_personnel] = null;
}
if (!isset($email_list[$count_personnel])) {
$email_list[$count_personnel] = null;
}
if (!isset($inst_list[$count_personnel])) {
$inst_list[$count_personnel] = null;
}
$count_personnel = $count_personnel + 1;
}
// start data access
if ($v[0] == 'data_access type'){
$dat_list[$count_access] = $v[1];
}
if ($v[0] == 'data_access resource') {
$dar_list[$count_access] = $v[1];
}
if ($v[0] == 'data_access') {
// make sure the arrays are not empty when field is missing
if (!isset($dat_list[$count_access])) {
$dat_list[$count_access] = null;
}
if (!isset($dar_list[$count_access])) {
$dar_list[$count_access] = null;
}
$count_access = $count_access + 1;
}
//isotopic categories
if ($v[0] == 'iso_topic_category'){
$itc_list[$count_itc] = $v[1];
$count_itc = $count_itc + 1;
}
// dataset author title, publication year and publisher
if ($v[0] == 'dataset_citation author') {
$cit_auth = $v[1];
}
if ($v[0] == 'dataset_citation title') {
$cit_tit = $v[1];
}
if ($v[0] == 'dataset_citation publication_date') {
$cit_rdate = $v[1];
}
if ($v[0] == 'dataset_citation publisher') {
$cit_publ = $v[1];
}
//license
if ($v[0] == 'use_constraint') {
$license = $v[1];
}
if (!isset($license)) {
$license = null;
}
}
//define landing page node
$node = new stdClass();
$node->title = $title;
$node->type = "landing_page";
// Sets some defaults. Invokes hook_prepare() and hook_node_prepare().
node_object_prepare($node);
// Or e.g. 'en' if locale is enabled.
$node->language = LANGUAGE_NONE;
$node->uid = $user->uid;
// Status is 1 or 0; published or not.
$node->status = 1;
// Promote is 1 or 0; promoted to front page or not.
$node->promote = 0;
// Comment is 0, 1, 2; 0 = disabled, 1 = read only, or 2 = read/write.
$node->comment = 0;
// Fill in the landing page node with content extracted from mmd and datacite response
// Abstract
$node->field_abstract[$node->language][]['value'] = $abstract;
// Iso topic categories (can be multiple)
for ($cn = 0; $cn < $count_itc; $cn++) {
$node->field_iso_topic_category[$node->language][$cn]['value'] = $itc_list[$cn];
}
// Citation
if(!isset($cit_auth) || $cit_auth == '') {
if (!isset($cit_tit) || $cit_tit == '') {
$cit_string = $title.', ('.date('Y', strtotime($cit_rdate)).') published by '.$cit_publ.'. ';
}else{
$cit_string = $cit_tit.', ('.date('Y', strtotime($cit_rdate)).') published by '.$cit_publ.'. ';
}
}else{
if (!isset($cit_tit) || $cit_tit == '') {
$cit_string = $cit_auth.', '.$title.', ('.date('Y', strtotime($cit_rdate)).') published by '.$cit_publ.'. ';
}else{
$cit_string = $cit_auth.', '.$cit_tit.', ('.date('Y', strtotime($cit_rdate)).') published by '.$cit_publ.'. ';
}
}
$node->field_citation[$node->language][]['value'] = $cit_string;
// DOI
$node->field_doi[$node->language][]['url'] = $doi_uri;
// License
if ($license == 'Public Domain') {
$lic_key = 'CC0';
}elseif ($license == 'Attribution'){
$lic_key = 'CCBY';
}elseif ($license == 'Share-alike'){
$lic_key = 'CCBYSA';
}elseif ($license == 'Noncommercial'){
$lic_key = 'CCBYNC';
}
$node->field_license[$node->language][0]['value'] = $lic_key;
// Contact (can be multiple)
$hr = '';
for ($cn = 0; $cn < $count_personnel; $cn++) {
$node->field_contact[$node->language][$cn] = array('value' => $hr.'<strong>Role: </strong>'.$role_list[$cn].'
<strong>Name: </strong>'.$name_list[$cn].'
<strong>email: </strong>'.$email_list[$cn].'
<strong>Institution: </strong>'.$inst_list[$cn],
'format' => 'full_html',
);
$hr = '<hr>';
}
// date start and end
$node->field_start_date[$node->language][]['value'] = date('Y-m-d\TH:i:s', strtotime($start_date));
if ($end_date != ''){
$node->field_end_date[$node->language][]['value'] = date('Y-m-d\TH:i:s', strtotime($end_date));
}else{
$node->field_end_date[$node->language][]['value'] = null;
}
// bounding box
$node->field_north[$node->language][]['value'] = $north;
$node->field_south[$node->language][]['value'] = $south;
$node->field_east[$node->language][]['value'] = $east;
$node->field_west[$node->language][]['value'] = $west;
$geofield = array(
'geom' => "POLYGON ((".$west." ".$north.",".$east." ".$north.",".$east." ".$south.", ".$west." ".$south.",".$west." ".$north.")) ",
'geo_type' => 'bounds',
'lat' => "$north",
'lon' => "$east",
'left' => $west,
'top' => $north,
'right' => $east,
'bottom' => $south
);
$node->field_bnds[$node->language][0] = $geofield;
// Access (can be multiple)
$hr = '';
for ($cn = 0; $cn < $count_access; $cn++) {
$node->field_access[$node->language][$cn] = array('value' => $hr.'<strong>Type: </strong>'.$dat_list[$cn].'
<strong>Resource: </strong><a href='.$dar_list[$cn].'>'.$dar_list[$cn].'</a>',
'format' => 'full_html',
);
$hr = '<hr>';
}
$node->path['alias'] = "datasets/".$doi;
$node = node_submit($node); // Prepare node for saving
node_save($node);
//register the url to datacite
//curl -H "Content-Type:text/plain;charset=UTF-8" -X PUT --user username:password -d "$(printf 'doi=10.5438/JQX3-61AT\nurl=http://example.org/')" https://mds.test.datacite.org/doi/10.5438/JQX3-61AT
$options_url = array(
'method' => 'PUT',
//'data' => "$(printf 'doi='.$doi.'\nurl='.$base_url.'/'.$node->path['alias'])",
'data' => 'doi='.$doi."\nurl=".$base_url.'/'.$node->path['alias'],
'timeout' => 25,
'headers' => array('Content-Type' => 'text/plain;charset=UTF-8',
'Authorization' => 'Basic ' . base64_encode($datacite_user . (":" . $datacite_pass)),),
);
$result_reg = drupal_http_request('https://mds.'.$datacite_url.'datacite.org/doi/'.$doi, $options_url);
drupal_set_message( "Node with nid " . $node->nid . " saved!\n");
//$form_state['redirect'] = 'node/'.$node->nid;
$form_state['redirect'] = "datasets/".$doi;
}