-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb2share.module
More file actions
907 lines (748 loc) · 28.6 KB
/
b2share.module
File metadata and controls
907 lines (748 loc) · 28.6 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
<?php
/**
*@file
*b2share.module
* Allows a direct upload to B2Share using the REST API
*/
/**
* Implements hook_menu().
*
* Provides a configuration menu for the B2Share Module
*/
function b2share_menu() {
$items = array();
$items['admin/config/system/b2share'] = array(
'title' => 'B2Share Configuration',
'description' => 'Settings for B2Share implementation module',
'page callback' => 'drupal_get_form',
'page arguments' => array('b2share_form'),
'access arguments' => array('access administration pages'),
);
$items['admin/config/system/b2share/settings'] = array(
'title' => 'General Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10
);
$items['admin/config/system/b2share/metadata'] = array(
'title' => 'Metadata',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array('b2share_form_metadata'),
'access arguments' => array('access administration pages')
);
return $items;
}
/**
* Provides a configuration page for the menu.
*/
function b2share_form($form, &$form_state) {
$form['b2share_url'] = array(
'#type' => 'textfield',
'#title' => 'B2Share Base URL',
'#description' => 'Please enter the URL the rest calls should address.',
'#size' => 70,
'#maxlength' => 70,
'#required' => TRUE,
'#default_value' => variable_get('b2share_url', 'https://trng-b2share.eudat.eu'),
);
$form['b2share_token'] = array(
'#type' => 'textfield',
'#title' => 'B2Share Access Token',
'#description' => 'Please enter the access token generated by B2Share.',
'#size' => 70,
'#maxlength' => 70,
'#required' => TRUE,
'#default_value' => variable_get('b2share_token', 'Example LKR35GP7TG'),
);
$form['http_code_visible'] = array(
'#type' => 'checkbox',
'#title' => 'Display HTTP status codes',
'#description' => 'Should the status codes be displayed?',
'#required' => FALSE,
'#default_value' => variable_get('http_code_visible', 0),
);
$form['status_message_visible'] = array(
'#type' => 'checkbox',
'#title' => 'Display curl status messages',
'#description' => 'Should the curl status codes be displayed? ATTENTION: This can/will expose the token to users',
'#required' => FALSE,
'#default_value' => variable_get('status_message_visible', 0),
);
$form['ignore_outdate_certificates'] = array(
'#type' => 'checkbox',
'#title' => 'Ignore outdated security certificates',
'#description' => 'Should curl make its calls ignoring any security certificates? WARNING: SECURITY ISSUES',
'#required' => FALSE,
'#default_value' => variable_get('ignore_outdate_certificates', 0),
);
$form['save_dois'] = array(
'#type' => 'checkbox',
'#title' => 'Save DOIs?',
'#description' => 'Should DOIs be saved? (This is only possible if the b2share instance issues DOIs)',
'#required' => FALSE,
'#default_value' => variable_get('save_dois', 1),
);
$curl_version = function_exists('curl_version');
if ($curl_version == false ) {
drupal_set_message(t('Curl is NOT installed.'), 'error');
}
else {
drupal_set_message(t('Curl is installed.'));
}
return system_settings_form($form);
}
/**
* Provides a configuration page for the menu
*/
function b2share_form_metadata($form, &$form_state) {
$form['b2share_domain'] = array(
'#type' => 'textfield',
'#title' => 'Community (ID)',
'#description' => 'Please enter the Domain the files should be stored under (the ID of the community)',
'#size' => 70,
'#maxlength' => 80,
'#required' => TRUE,
'#default_value' => variable_get('b2share_domain', 'e9b9792e-79fb-4b07-b6b4-b9c2bd06d095'),
);
// print all communities
$base_url = variable_get('b2share_url');
$base_url = rtrim($base_url, '/') . '/';
$url = '<a href="' . $base_url.'api/communities' . '" target="_blank">' . $base_url.'api/communities </a>';
drupal_set_message(t('The communities on B2Share are listed here: ' . $url));
return system_settings_form($form);
}
/**
* Implements hook_field_info().
*/
function b2share_field_info() {
return array(
'b2share' => array(
'label' => t('B2Share Field'),
'description' => t('Provides an interface to use the B2Share API'),
'default_widget' => 'b2share_widget',
'default_formatter' => 'b2share_formatter',
'property_type' => 'text',
'property_callbacks' => array('b2share_field_property_callback'),
)
);
}
/**
* Implements hook_field_widget_info().
*/
function b2share_field_widget_info() {
return array(
'b2share' => array(
'label' => t('B2Share Widget'),
'field types' => array('b2share'),
)
);
}
/**
* Implements hook_field_settings_form().
*/
function b2share_field_settings_form($field, $instance, $has_data) {
$settings = $field['settings'];
$form = array();
return $form;
}
/**
* Implements hook_field_instance_settings_form().
*/
function b2share_field_instance_settings_form($field, $instance) {
$settings = $instance['settings'];
$form['collapsible'] = array(
'#type' => 'radios',
'#title' => t('Is this field collapsible?'),
'#default_value' => isset($settings['collapsible']) ? $settings['collapsible'] : 0,
'#options' => array(
t('Collapsible'),
t('Not collapsible'),
),
);
$form['collapsed'] = array(
'#type' => 'radios',
'#title' => t('Is this field collapsed?'),
'#default_value' => isset($settings['collapsed']) ? $settings['collapsed'] : 1,
'#options' => array(
t('Collapsed'),
t('Not collapsed'),
),
);
return $form;
}
/**
* Implements hook_field_formatter_info().
*/
function b2share_field_formatter_info() {
return array(
'b2share_formatter' => array(
'label' => t('B2Share Field Formatter'),
'field types' => array('b2share'),
),
'b2share_pid_formatter' => array(
'label' => t('B2Share PID Field Formatter'),
'field types' => array('b2share'),
),
'b2share_doi_formatter' => array(
'label' => t('B2Share DOI Field Formatter'),
'field types' => array('b2share'),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function b2share_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'b2share_formatter' :
foreach ($items as $delta => $item) {
if (isset($item['pid'])) {
$pid_content = 'PID: <a href="' . $item['pid'].'"target="_blank">'. $item['pid'] . '</a>';
$element[$delta]['#markup'] = $pid_content;
}
if (isset($item['doi'])) {
$pid_content = $pid_content . '<br>';
$element[$delta]['#markup'] = $pid_content. 'DOI: <a href="' . $item['doi'].'"target="_blank">'. $item['doi'] . '</a>';
}
}
break;
case 'b2share_pid_formatter' :
foreach ($items as $delta => $item) {
if (isset($item['pid'])) {
$element[$delta]['#markup'] = '<a href="' . $item['pid'].'"target="_blank">'. $item['pid']. '</a>';
}
}
break;
case 'b2share_doi_formatter' :
foreach ($items as $delta => $item) {
if (isset($item['doi'])) {
$element[$delta]['#markup'] = $item['doi'];
}
}
break;
}
return $element;
}
/**
* Implements hook_field_widget_form().
*/
function b2share_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
switch ($instance['widget']['type']) {
case 'b2share':
// check when field is called
$cur_path = current_path();
$b2share_fieldname = $instance['field_name'];
// hidden field for saving the fieldname
$form['database_field_name'] = array(
'#type' => 'hidden',
'#default_value' => $b2share_fieldname,
);
// add javascript and css
$element['#attached']['js'] = array(
drupal_get_path('module', 'b2share') . '/js/b2share.js',
);
$element['#attached']['css'] = array(
drupal_get_path('module', 'b2share') . '/css/b2share.css',
);
// metadata fields of the form
$element+= array(
'#type' => 'fieldset',
'#title' => t('Publish data on B2Share'),
'#collapsible' => $collapsible,
'#collapsed' => $collapsed,
'#tree' => TRUE,
);
// works
if (preg_match("/add/i", $cur_path)) {
//drupal_set_message(t('A node is being added ' . $cur_path));
$element['info_box'] = array(
'#markup' => '<p>A file can only be uploaded to B2Share after the metadata record was successfully created.</p>'
);
break;
}
// this part is probably obselete
elseif (preg_match("/ajax/i", $cur_path)) {
//drupal_set_message(t('An ajax call is happening '. $cur_path));
}
else {
//drupal_set_message(t('A node is being edited '. $cur_path));
}
// get field values of node
if ($node = menu_get_object()) {
$nid = $node->nid;
$loaded_node = node_load($nid);
$mynode_uuid = $node->field_uuid[LANGUAGE_NONE][0]['value'];
$abstract = $node->field_abstract[LANGUAGE_NONE][0]['value'];
$node_title = $node->title;
$node->field_person_creator[LANGUAGE_NONE][0]['value'];
$wrapper = entity_metadata_wrapper('node', $node);
$creator = $wrapper->field_person_creator->value();
$creator_json = '[';
for ($x = 0; $x < count($creator); $x++) {
$creator_json .= '{"creator_name":"';
$temp_nid = $creator[$x]->nid;
$temp_name = $creator[$x]->field_name;
$temp_name2 = $temp_name['und'];
$temp_name3 = $temp_name2[0];
$creator_json .= $temp_name3['family'] . ', ' . $temp_name3['given'] . '"}';
if ($x+1 < count($creator) && count($creator) != 1) {
$creator_json .= ',';
}
}
$creator_json .= ']';
//drupal_set_message(t('Creator json is ' . $creator_json));
}
$form['node_uuid'] = array(
'#type' => 'hidden',
'#default_value' => isset($items[$delta]['node_uuid']) ? $items[$delta]['node_uuid'] : $mynode_uuid,
);
$form['node_title'] = array(
'#type' => 'hidden',
'#default_value' => isset($items[$delta]['node_title']) ? $items[$delta]['node_title'] : $node_title,
);
$form['abstract'] = array(
'#type' => 'hidden',
'#default_value' => isset($items[$delta]['abstract']) ? $items[$delta]['abstract'] : $abstract,
);
$form['creator'] = array(
'#type' => 'hidden',
'#default_value' => isset($items[$delta]['creator']) ? $items[$delta]['creator'] : $creator_json,
);
// fetch settings
$settings = $instance['settings'];
$save_doi_value = variable_get('save_dois');
$collapsible = TRUE;
if ($settings['collapsible'] == 1) {
$collapsible = FALSE;
}
$collapsed = FALSE;
if ($settings['collapsed'] == 0) {
$collapsed = TRUE;
}
$element['file_select'] = array(
'#type' => 'managed_file',
'#title' => t('Select File'),
'#description' => t('Allowed extensions: gif png jpg jpeg txt csv zip 7z xls xlsx'),
'#default_value' => (isset($foo->picture->fid) ? $foo->picture->fid : ''),
'#upload_location' => variable_get('picture_upload_location'),
'#upload_validators' => array(
'file_validate_size' => array('MAX_FILE_SIZE'*1024*1024),
'file_validate_extensions' => array('gif png jpg jpeg txt csv zip 7z xls xlsx')
),
);
$element['open_access'] = array(
'#type' =>'checkbox',
'#title' => t('Open Access'),
'#default_value' => 1,
);
$element['submit_button'] = array(
'#type' => 'button',
'#name' => 'submit_button',
'#default_value' => t('Deposit file on B2Share'),
'#ajax' => array(
'event' => 'click',
'callback' => 'b2share_ajax_submit',
),
'#limit_validation_errors' => array(),
);
if ($save_doi_value == 1){
$element['doi'] = array(
'#title' => 'DOI',
'#description' => t('Put the DOI here or have one generated.'),
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['doi']) ? $items[$delta]['doi'] : '',
'#prefix' => '<div id="b2share_doi_textfield_div">',
'#suffix' => '</div>',
);
}
$element['pid'] = array(
'#title' => 'PID',
'#description' => t('Put the PID here or have one generated.'),
'#type' => 'textfield',
'#default_value' => isset($items[$delta]['pid']) ? $items[$delta]['pid'] : '',
'#prefix' => '<div id="b2share_pid_textfield_div">',
'#suffix' => '</div>',
);
break;
}
return $element;
}
/**
* Implements AJAX call for submission.
*
* communication with the B2Share API happens here
*/
function b2share_ajax_submit(&$form, &$form_state) {
// function for ignoring outdated security certificates
function b2share_ignore_certificates($ch) {
if (variable_get('ignore_outdate_certificates') == TRUE) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
}
// function for printing http codes and status messages
function b2share_print_codes_messages($http_code_visible, $status_message_visible, $phase_string, $http_status, $output) {
if ($http_code_visible == TRUE) {
drupal_set_message(t('The http status code for ' . $phase_string . ' is ' . $http_status));
}
if ($status_message_visible == TRUE) {
drupal_set_message(t('The status message for ' . $phase_string . ' is ' . $output));
}
if ($http_status == 401) {
drupal_set_message(t('There is a problem with the authentication. Please check your token.'), 'error');
}
}
// get field name
$temp_field_name = $form_state['values']['database_field_name'];
$temp_uuid = $form_state['values']['node_uuid'];
//drupal_set_message(t('The uuid value in the form is ' . $temp_nid ));
// function for printing the drupal status messages within an ajax call
function b2share_print_drupal_messages($form, $temp_field_name){
return array (
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace("#b2share_pid_textfield_div", render($form[$temp_field_name]['und'][0]['pid'])),
ajax_command_append("#b2share_pid_textfield_div", theme('status_messages')),
)
);
}
// check for existing values in the pid/doi field
if (!empty($form_state['values'][$temp_field_name]['und'][0]['pid'])){
drupal_set_message(t('There is already a value set for the pid.'), 'error');
return b2share_print_drupal_messages($form, $temp_field_name);
}
if (variable_get('save_dois') == 1){
if (!empty($form_state['values'][$temp_field_name]['und'][0]['doi'])){
drupal_set_message(t('There is already a value set for the doi.'), 'error');
return b2share_print_drupal_messages($form, $temp_field_name);
}
}
// Fetch token from settings
$temp_token = variable_get('b2share_token');
if ($temp_token == 'Example LKR35GP7TG') {
drupal_set_message(t('You need to set a token.'), 'error');
return b2share_print_drupal_messages($form, $temp_field_name);
}
// check for curl
$curl_version = function_exists('curl_version');
if ($curl_version == false ) {
drupal_set_message(t('Curl is NOT installed.'), 'error');
return b2share_print_drupal_messages($form, $temp_field_name);
}
// Fetch metadata information and file
// Load file from select form
$check_file = $form_state['values'][$temp_field_name]['und'][0]['file_select'];
$file = file_load($check_file['fid']);
if ((empty($file)) == true) {
drupal_set_message(t('Please select a file'), 'error');
return b2share_print_drupal_messages($form, $temp_field_name);
}
// get file information
$uri = $file->uri;
$file_realpath = drupal_realpath($uri);
$base_file_name = drupal_basename($uri);
// get field information
$open_access = $form_state['values'][$temp_field_name]['und'][0]['open_access'];
//$md_description = $form_state['values'][$temp_field_name]['und'][0]['description'];
$md_description = $form_state['values']['abstract'];
$md_title = $form_state['values']['node_title'];
//$md_title = $form_state['values'][$temp_field_name]['und'][0]['title'];
//$md_creator = $form_state['values'][$temp_field_name]['und'][0]['creator'];
$md_creator = $form_state['values']['creator'];
//$uuid = $form_state['values']['field_uuid']['und'][0]['uuid'];
//drupal_set_message(t('The uuid value is ' . $uuid ));
// check for a title
if (empty($md_title)) {
drupal_set_message(t('Please provide at least a title.'), 'error');
return b2share_print_drupal_messages($form, $temp_field_name);
}
if ($open_access == 1){
$open_access = "true";
}
else {
$open_access = "false";
}
$status_message_visible = variable_get('status_message_visible');
$http_code_visible = variable_get('http_code_visible');
// Build the query.
// Create a new deposition
$base_url = variable_get('b2share_url');
// check if trailing slash was added
$base_url = rtrim($base_url, '/') . '/';
$url = $base_url.'api/records/?access_token='.$temp_token;
if(!$url || !is_string($url) || ! preg_match('/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i', $url)){
drupal_set_message(t('Bad url'));
}
$community_value = variable_get('b2share_domain');
if (!$community_value) {
drupal_set_message(t('No community value was set. File will be uploaded to generic domain.'));
$community_value = "e9b9792e-79fb-4b07-b6b4-b9c2bd06d095";
}
// get available metadata and construct the json for transmitting the parameters
// mandatory metadata
$params_json = '{"titles":[{"title":"' . $md_title . '"}],';
$params_json .= '"community":"' . $community_value . '",';
$params_json .= '"open_access":' . $open_access . ',';
// optional metadata
if (!empty($md_description)) {
$params_json .= '"descriptions": [{"description": "' . $md_description . '","description_type": "Abstract"}],';
}
if (!empty($md_creator)) {
$params_json .= '"creators":' . $md_creator . ',';
}
// add community specific aspects for call
//$metadata_url = 'https://www.deims.org/dataset/' . $temp_uuid;
$metadata_url = "https://" . $_SERVER['SERVER_NAME'] . "/dataset/" . $temp_uuid;
$params_json .= '"community_specific":{"27193e5b-97e6-4f6f-8e87-3694589bcebe":{"metadata_url": "' . $metadata_url . '"';
//$params_json .= '"community_specific":{';
//$params_json .= '}}';
$params_json .= '}}}';
//drupal_set_message(t('The params_json is ' . $params_json));
// curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
b2share_ignore_certificates($ch);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept:application/json',
'Content-Type:application/json',
));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
// output the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT );
if ($output === false) {$output = curl_error($ch);}
curl_close($ch);
// Return status message
$phase_string = 'creating a draft';
b2share_print_codes_messages ($http_code_visible, $status_message_visible, $phase_string, $http_status, $output);
// catch case, that a draft couldn't be created for whatever reason
if ($http_status != 201) {
drupal_set_message(t('The draft could not be created :('), 'error');
return b2share_print_drupal_messages($form, $temp_field_name);
}
// Save response as php variable
$php_variable = drupal_json_decode($output);
$record_id = $php_variable['id'];
$return_links = $php_variable['links'];
$draft_id = $return_links['files'];
$record_id = $return_links['self'];
// PID of deposition
$upload_url = $draft_id.'/'.$base_file_name.'?access_token='.$temp_token;
// deposit file
$ch = curl_init($upload_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file_realpath));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept:application/json',
'Content-Type:application/json',
));
b2share_ignore_certificates($ch);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT);
curl_close($ch);
// Return status message
$phase_string = 'depositing the file';
b2share_print_codes_messages ($http_code_visible, $status_message_visible, $phase_string, $http_status, $output);
$patch_content='[{"op": "add", "path":"/publication_state", "value": "submitted"}]';
$publish_url = $record_id.'?access_token='.$temp_token;
// Patch Call in order to publish a draft
$ch = curl_init($publish_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $patch_content);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept:application/json',
'Content-Type:application/json-patch+json',
));
b2share_ignore_certificates($ch);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$output = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSent = curl_getinfo($ch, CURLINFO_HEADER_OUT);
if ($output === false) $output = curl_error($ch);
curl_close($ch);
// Return status message
$phase_string = 'publishing the record';
b2share_print_codes_messages ($http_code_visible, $status_message_visible, $phase_string, $http_status, $output);
// set pid and doi field values if deposition was successful
if ($http_status == 200) {
drupal_set_message(t('File was successfully deposited :) Please save your metadata record to store the generated PID'), 'status');
$php_variable = drupal_json_decode($output);
$files_content = $php_variable['metadata'];
if (variable_get('save_dois') == 1){
if (isset($files_content['DOI'])){
$form[$temp_field_name]['und'][0]['doi']['#value'] = $files_content['DOI'];
}
}
if (isset($files_content['ePIC_PID'])){
$form[$temp_field_name]['und'][0]['pid']['#value'] = $files_content['ePIC_PID'];
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace("#b2share_doi_textfield_div", render($form[$temp_field_name]['und'][0]['doi'])),
ajax_command_replace("#b2share_pid_textfield_div", render($form[$temp_field_name]['und'][0]['pid'])),
ajax_command_append("#b2share_pid_textfield_div", theme('status_messages')),
)
);
}
else {
drupal_set_message(t('The file was deposited, but no PID was returned :/ Please talk to the administrator of the B2Share instance. '), 'error');
return b2share_print_drupal_messages($form, $temp_field_name);
}
}
else {
drupal_set_message(t('Oh no. Something went wrong. No file was deposited :('), 'error');
return b2share_print_drupal_messages($form, $temp_field_name);
}
}
/**
* Implements hook_field_is_empty().
*/
function b2share_field_is_empty($item, $field) {
if (empty($item['b2share_fieldset']['pid'])) {
return TRUE;
}
return FALSE;
}
/**
* Implements hook_form_node_form_alter().
*
* Calls the function responsible for saving the generated values to the database
*/
function b2share_form_node_form_alter(&$form, &$form_state, $form_id) {
$form['actions']['submit']['#submit'][] = 'b2share_save_to_db';
}
/**
* Saves values from DOI/PID field to the database.
*/
function b2share_save_to_db($form, &$form_state) {
$values = array();
if (!array_key_exists('database_field_name', $form_state)) {
return;
}
$temp_field_name = $form_state['values']['database_field_name'];
if (variable_get('save_dois') == 1 && isset($form_state['values'][$temp_field_name]['und'][0]['doi'])){
$values['doi'] = $form_state['values'][$temp_field_name]['und'][0]['doi'];
}
if (isset($form_state['values'][$temp_field_name]['und'][0]['pid'])){
$values['pid'] = $form_state['values'][$temp_field_name]['und'][0]['pid'];
}
if (!isset($form_state['values'][$temp_field_name]['und'][0]['doi'])) {
$values['doi'] = '';
}
if ( (empty($values['doi'] == false) || (empty($values['pid']) == false)) ) {
$db_name_b2share = $form_state['values']['database_field_name'];
$b2share_table_name = 'field_data_'.$db_name_b2share;
$b2share_column_doi = $db_name_b2share.'_doi';
$b2share_column_pid = $db_name_b2share.'_pid';
$insert = db_insert($b2share_table_name)
-> fields(array(
$b2share_column_doi => $values['doi'],
$b2share_column_pid => $values['pid'],
'entity_id' => $form_state['node']->nid,
'entity_type' => 'node',
'bundle' => $form_state['node']->type,
'delta' => 0,
'language' => 'und',
))
->execute();
}
}
/**
* Implements hook_token_info().
*/
function b2share_token_info() {
$info = array();
// Define a new token type.
$info['types']['b2share'] = array(
'name' => t('B2Share tokens'),
'description' => t('A token type for my b2share.'),
);
// Define any new tokens.
$info['tokens']['b2share']['doi'] = array(
'name' => t('B2Share DOI'),
'description' => t('DOI generated by B2Share'),
);
$info['tokens']['b2share']['pid'] = array(
'name' => t('B2Share PID'),
'description' => t('PID generated by B2Share'),
);
return $info;
}
/**
* Implements hook_tokens().
*/
function b2share_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'b2share') {
// Loop through the available tokens.
foreach ($tokens as $name => $original) {
// Find our custom tokens by name.
switch ($name) {
case 'doi':
// Work out the value of our token.
$value = 'DOI TEST';
// Give our token its value!
$replacements[$original] = $value;
break;
case 'pid':
// Work out the value of our token.
$value = 'PID TEST';
// Give our token its value!
$replacements[$original] = $value;
break;
}
}
}
return $replacements;
}
/**
* Property callback.
*
* rules integration, fields are visible but values can't be set and its value can't copied to another field, no idea why
*
*/
function b2share_field_property_callback(&$info, $entity_type, $field, $instance, $field_type) {
$property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];
$property['getter callback'] = 'entity_metadata_field_verbatim_get';
$property['setter callback'] = 'entity_metadata_field_verbatim_set';
unset($property['query callback']);
$property['property info'] = array(
'doi' => array(
'type' => 'text',
'label' => t('DOI'),
'description' => t('Subfield of Field "'.$field['field_name'].'".'),
//'sanitized' => TRUE,
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
//'setter permission' => 'administer nodes',
//'raw getter callback' => 'entity_property_verbatim_get',
),
'pid' => array(
'type' => 'text',
'label' => t('PID'),
'description' => t('Subfield of Field "'.$field['field_name'].'".'),
//'sanitized' => TRUE,
'getter callback' => 'entity_property_verbatim_get',
'setter callback' => 'entity_property_verbatim_set',
//'setter permission' => 'administer nodes',
//'raw getter callback' => 'entity_property_verbatim_get',
),
/*
'format' => array(
'type' => 'token',
'label' => t('Text format'),
'options list' => 'entity_metadata_field_text_formats',
'getter callback' => 'entity_property_verbatim_get',
), */
);
unset($property['query callback']);
}