-
Notifications
You must be signed in to change notification settings - Fork 13
/
dkan_dataset.forms.inc
791 lines (712 loc) · 34.6 KB
/
dkan_dataset.forms.inc
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
<?php
/**
* @file
* Form elements for DKAN.
*/
include_once 'includes/getRemoteFileInfo.php';
/**
* Implements hook_field_group_build_pre_render_alter().
*/
function dkan_dataset_field_group_build_pre_render_alter(&$element) {
// Moving path into primary group here since we can't do it on the content
// type form. Rearanging weights so path is under title.
if (isset($element['#form_id']) && $element['#form_id'] == 'dataset_node_form') {
$element['group_primary']['path'] = $element['path'];
$element['group_primary']['title']['#weight'] = '-10';
$element['group_primary']['path']['#weight'] = "1";
$element['group_primary']['body']['#weight'] = "2";
$element['group_primary']['field_tags']['#weight'] = "3";
$element['group_primary']['og_group_ref']['#weight'] = "4";
unset($element['path']);
}
}
/**
* Helper function to get the node title without loading the whole node object.
*/
function dkan_dataset_get_node_title($nid) {
return db_query('SELECT title FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
}
/**
* Returns an array with the available group options for the current user.
*/
function dkan_dataset_get_group_options() {
$available_groups = array();
if (user_access('administer group')) {
// If the user is a group administrator then show all groups as available options.
$groups = og_get_all_group();
} else {
// If the user is not an administrator then only display the groups that the user is a member of.
// If a user is not specified for 'og_get_groups_by_user' then it defaults to the current user.
$groups = og_get_groups_by_user(NULL, 'node');
}
if ($groups) {
foreach ($groups as $group_id) {
$available_groups[$group_id] = dkan_dataset_get_node_title($group_id);
}
}
return $available_groups;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function dkan_dataset_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'dataset_node_form') {
$form['#attached']['css'][] = drupal_get_path('module', 'dkan_dataset') . '/css/dkan_dataset.css';
$form['#attached']['js'][] = drupal_get_path('module', 'dkan_dataset') . '/js/dkan_forms.js';
beautytips_add_beautytips();
$form['#after_build']['dkan_dataset'] = 'dkan_dataset_resource_form_after_build';
// Slugify pathauto in a jazzy but brittle manner.
if (variable_get('dkan_dataset_form_pathauto_alter', 1)) {
$form['#attached']['js'][] = libraries_get_path('slugify') . '/jquery.slugify.js';
$form['#attached']['js'][] = drupal_get_path('module', 'dkan_dataset') . '/js/dkan_forms.slugify.js';
// Removing path from vertiable tabs.
unset($form['path']['#group']);
unset($form['path']['#title']);
$form['path']['#weight'] = "-1";
// Always use pathauto.
$form['path']['pathauto']['#default_value'] = TRUE;
$form['path']['pathauto']['#access'] = FALSE;
$form['path']['alias']['#default_value'] = preg_replace('/dataset\//', '', $form['path']['alias']['#default_value']);
$form['path']['#collapsible'] = FALSE;
$form['path']['#collapsed'] = FALSE;
$alias = $form['path']['alias']['#default_value'] ? $form['path']['alias']['#default_value'] : t('[dataset]');
$form['path']['#prefix'] = '<div id="field-tags-wrapper">
<div id="url-edit-preview">
<div id="url-edit-label"><strong>URL</strong></div>
<div id="url-edit-prefix">' . check_plain($_SERVER['HTTP_HOST']) . '/dataset/</div>' .
'<span id="url-slug">' . $alias . '</span>' . $form['path']['alias']['#default_value'] . '<button class="btn btn-small">Edit</button>
</div>';
$form['path']['#suffix'] = '</div>';
$form['path']['alias']['#prefix'] = '<span class="add-on">/dataset/</span>';
$form['path']['alias']['#title'] = 'URL';
}
// Adding descriptions.
$form['title']['#attributes']['placeholder'] = t('eg. A descriptive title');
// Get langcode for body.
$body_langcode = dkan_dataset_form_field_language($form, 'body');
$form['body'][$body_langcode][0]['#prefix'] = '<div id="prefix">';
$form['body'][$body_langcode][0]['#suffix'] = '</div>';
$form['path']['alias']['#description'] = '';
$form['path']['alias']['#title'] = '';
// Get langcode for field_tags.
$field_tags_langcode = dkan_dataset_form_field_language($form, 'field_tags');
$form['field_tags'][$field_tags_langcode]['#description'] = t('eg. economy, mental health, government');
// Get langcode for field_license.
$field_license_langcode = dkan_dataset_form_field_language($form, 'field_license');
// Add icon to license field.
$form['field_license'][$field_license_langcode]['#description'] = '<i class="icon-large icon-info-sign"></i>' . t('License definitions and additional information can be found at ') . '<a href="http://opendefinition.org">opendefinition.org</a>';
// Get langcode for field_additional_info.
if (isset($form['field_additional_info'])) {
$field_additional_info_langcode = dkan_dataset_form_field_language($form, 'field_additional_info');
foreach ($form['field_additional_info'][$field_additional_info_langcode] as $key => $values) {
if (is_int($key)) {
$form['field_additional_info'][$field_additional_info_langcode][$key]['first']['#attributes']['placeholder'] = t('key');
$form['field_additional_info'][$field_additional_info_langcode][$key]['second']['#attributes']['placeholder'] = t('value');
}
}
}
if (isset($form['og_group_ref'])) {
// Get langcode for og_group_ref.
$og_group_ref_langcode = dkan_dataset_form_field_language($form, 'og_group_ref');
// Modify title of Publisher field.
$form['og_group_ref'][$og_group_ref_langcode]['#title'] = t('Groups');
$groups = dkan_dataset_get_group_options();
$form['og_group_ref'][$og_group_ref_langcode]['#options'] = $groups;
// Don't show groups if user doesn't have any groups to add.
if (empty($groups)) {
$form['og_group_ref']['#access'] = FALSE;
}
}
if (arg(1) == 'add') {
// We don't need to see revision form when creating a new node.
$form['revision_information']['#access'] = FALSE;
$form['prefix']['#type'] = 'item';
$form['prefix']['#weight'] = '-20';
$form['prefix']['#markup'] = dkan_form_create_stages('dataset-create');
$form['actions']['next'] = array(
'#type' => 'submit',
'#value' => t('Next: Add data'),
'#weight' => -5,
'#submit' => array('node_form_submit'),
);
$form['actions']['next']['#submit'] = array('node_form_submit', 'dkan_dataset_dataset_form_submit');
$form['actions']['terms'] = array(
'#type' => 'item',
'#markup' => '<div id="action-info">' . t('Important: By submitting content, you agree to release your contributions under the Open Database License.') . '</div>',
);
unset($form['actions']['submit']);
}
elseif (arg(2) == 'edit' && $query = drupal_get_query_parameters()) {
if (isset($query['additional'])) {
$form['prefix']['#type'] = 'item';
$form['prefix']['#weight'] = '-20';
$form['prefix']['#markup'] = dkan_form_create_stages('dataset-additional', arg(1));
if (isset($form['actions']['submit'])) {
$form['actions']['submit']['#submit'] = array('dkan_dataset_dataset_body_submit', 'node_form_submit');
}
}
}
else {
$form['actions']['submit']['#value'] = t('Finish');
// This is here for AJAX compatibililty but causing other problems; commenting out for now
// $form['#action'] = url('node/add/dataset');
}
// Mark all POD related required fields as required except for 'Publisher'
// that handled separately if POD validation is enabled on DKAN Dataset Forms.
if (variable_get('dkan_dataset_form_pod_validation')) {
// Body.
$body_langcode = dkan_dataset_form_field_language($form, 'body');
$form['body'][$body_langcode][0]['#required'] = TRUE;
// Tags.
$field_tags_langcode = dkan_dataset_form_field_language($form, 'field_tags');
$form['field_tags'][$field_tags_langcode]['#required'] = TRUE;
// Contact email.
$field_contact_email_langcode = dkan_dataset_form_field_language($form, 'field_contact_email');
$form['field_contact_email'][$field_contact_email_langcode][0]['value']['#required'] = TRUE;
// Contact name.
$field_contact_name_langcode = dkan_dataset_form_field_language($form, 'field_contact_name');
$form['field_contact_name'][$field_contact_name_langcode][0]['value']['#required'] = TRUE;
// Public access level.
$field_public_access_level_langcode = dkan_dataset_form_field_language($form, 'field_public_access_level');
$form['field_public_access_level'][$field_public_access_level_langcode]['#required'] = TRUE;
// License.
$field_license_langcode = dkan_dataset_form_field_language($form, 'field_license');
// Set 'Open Data Commons Open Database License (ODbL)' as default value.
$valid_pod_license = true;
$selected_license_code = $form['field_license'][$field_license_langcode]['#default_value'];
// If the license is not specified then it's not a POD valid license.
if ($selected_license_code === 'notspecified') {
$valid_pod_license = false;
} else {
$available_licenses = dkan_dataset_content_types_license_subscribed_values();
// If the license is not available then is not a POD valid license.
if (!isset($available_licenses[$selected_license_code])) {
$valid_pod_license = false;
drupal_set_message(
t('The dataset had a license value that is not valid on Project Open Data so it was changed automatically. ') .
t('Please check the new value before saving any changes.')
);
}
}
if (!$valid_pod_license) {
$form['field_license'][$field_license_langcode]['#default_value'] = 'odc-odbl';
}
}
// Make 'Rights' field visible and required if 'Public Access Level' is set
// to 'restricted' or 'private'.
$access_level_values = array(
array('value' => t('restricted')),
array('value' => t('private'))
);
$field_public_access_level_langcode = dkan_dataset_form_field_language($form, 'field_public_access_level');
$form['field_rights']['#states'] = array(
'visible' => array(
'[name="field_public_access_level[' . $field_public_access_level_langcode . ']"]' => array($access_level_values)),
'required' => array(
'[name="field_public_access_level[' . $field_public_access_level_langcode . ']"]' => array($access_level_values))
);
// Mark 'Groups' field as required if the group validation is enabled on DKAN Dataset forms.
if (variable_get('dkan_dataset_form_group_validation') && module_exists('dkan_dataset_groups')) {
// Groups.
$og_group_ref_langcode = dkan_dataset_form_field_language($form, 'og_group_ref');
$form['og_group_ref'][$og_group_ref_langcode]['#required'] = TRUE;
// If there are no groups then display a warning letting the user know that he won't be able
// to create the dataset since the 'Publisher' field is a POD required field.
$available_groups = $form['og_group_ref'][$og_group_ref_langcode]['#options'];
if (empty($available_groups)) {
// Show a warning message.
dkan_dataset_display_groups_warning();
}
}
// If 'Validate dataset form according to Project Open Data' is enabled on dataset form settings then
// move all POD required fields to the first step if the form is a multistep form.
$multistep_form_enabled = variable_get('dkan_dataset_form_additional_info', 1);
$pod_validation_enabled = variable_get('dkan_dataset_form_pod_validation');
if ($multistep_form_enabled && $pod_validation_enabled) {
$form['#group_children']['field_contact_name'] = 'group_primary';
$form['#group_children']['field_contact_email'] = 'group_primary';
$form['#group_children']['field_public_access_level'] = 'group_primary';
$form['#group_children']['field_rights'] = 'group_primary';
}
// Hide field groups if multi step form is enabled and the dataset API is not being used.
if (variable_get('dkan_dataset_form_additional_info', 1 ) && (current_path() != 'api/dataset/node')) {
$query = drupal_get_query_parameters();
$arg2 = arg(2);
$additional = isset($form['#additional_processed']) && $form['#additional_processed'] ? TRUE : FALSE;
if ((isset($form['#groups']['group_primary']->disabled) && $form['#groups']['group_primary']->disabled) || isset($query['additional'])) {
field_group_hide_field_groups($form, array('group_primary'));
if (module_exists('open_data_federal_extras')) {
field_group_hide_field_groups($form, array('group_odfie_pod'));
}
}
elseif (isset($form['#action']) && $form['#action'] == url('node/add/dataset') && !$additional && $arg2 != 'edit') {
field_group_hide_field_groups($form, array('group_additional'));
}
}
$form['#validate'][] = 'dkan_dataset_dataset_node_form_validate';
}
if ($form_id == 'resource_node_form') {
drupal_add_library('system', 'jquery.bbq');
$form['#attached']['css'][] = drupal_get_path('module', 'dkan_dataset') . '/css/dkan_dataset.css';
$form['#attached']['js'][] = drupal_get_path('module', 'dkan_dataset') . '/js/dkan_forms.js';
if (isset($form['feeds'])) {
$form['feeds']['#access'] = FALSE;
}
$form['#after_build']['dkan_dataset'] = 'dkan_dataset_resource_form_after_build';
// Get langcode for field_dataset_ref.
$field_dataset_ref_langcode = dkan_dataset_form_field_language($form, 'field_dataset_ref');
$form['field_dataset_ref'][$field_dataset_ref_langcode]['#description'] = t('Dataset that this resource is attached to.');
$dataset_options = $form['field_dataset_ref'][$field_dataset_ref_langcode]['#options'];
$fix_apostrophes = function($value) {
return html_entity_decode($value, ENT_QUOTES, "UTF-8");
};
$form['field_dataset_ref'][$field_dataset_ref_langcode]['#options'] = array_map($fix_apostrophes, $dataset_options);
// Get langcode for field_upload.
$field_upload_langcode = dkan_dataset_form_field_language($form, 'field_upload');
$form['field_upload'][$field_upload_langcode][0]['#title'] = '';
$form['field_upload'][$field_upload_langcode][0]['view']['#collapsible'] = TRUE;
$form['field_upload'][$field_upload_langcode][0]['view']['#collasped'] = TRUE;
$file_upload_validators = $form['field_upload'][$field_upload_langcode][0]['#file_resup_upload_validators'];
$form['field_upload'][$field_upload_langcode][0]['#description'] = theme('file_upload_help', array('upload_validators' => $file_upload_validators));
// Get langcode for field_link_file.
$field_link_file_langcode = dkan_dataset_form_field_language($form, 'field_link_file');
$field_link_remote_file_langcode = dkan_dataset_form_field_language($form, 'field_link_remote_file');
$form['field_link_file'][$field_link_file_langcode][0]['#description'] = 'Link to a CSV file that will be imported into the DKAN datastore.';
$form['field_link_remote_file'][$field_link_file_langcode][0]['#description'] = t('Link to a file hosted on a remote server. CSV files can be imported into the DKAN datastore.');
// Get langcode for field_link_api.
$field_link_api_langcode = dkan_dataset_form_field_language($form, 'field_link_api');
$form['field_link_api'][$field_link_api_langcode][0]['#description'] = t('Link to a public API or information source.');
// Get langcode for field_format.
$field_link_api_langcode = dkan_dataset_form_field_language($form, 'field_format');
$form['field_format'][$field_link_api_langcode]['#description'] = 'Leave blank to auto-detect resource format';
$form['title']['#attributes']['placeholder'] = t('eg. January Gold Prices');
$form['title']['#required'] = TRUE;
$form['field_link_api'][$field_link_api_langcode][0]['#title'] = '';
$form['field_link_file'][$field_link_file_langcode][0]['#title'] = '';
$form['field_link_remote_file'][$field_link_file_langcode][0]['#title'] = '';
// Resources inherit groups from parent dataset.
// Hide field so users cannot manually modify that information.
$form['og_group_ref']['#access'] = FALSE;
if ($query = drupal_get_query_parameters()) {
if (isset($query['dataset'])) {
$form['field_dataset_ref'][$field_dataset_ref_langcode]['#default_value'] = $query['dataset'];
$form['prefix']['#type'] = 'item';
$form['prefix']['#weight'] = '-20';
$form['prefix']['#markup'] = dkan_form_create_stages('resource-create', $query['dataset']);
}
}
elseif (isset($form['#node']->field_dataset_ref['nid'])) {
$form['field_dataset_ref'][$field_dataset_ref_langcode]['#default_value'] = $form['#node']->field_dataset_ref['nid'];
$form['prefix']['#type'] = 'item';
$form['prefix']['#weight'] = '-20';
$form['prefix']['#markup'] = dkan_form_create_stages('resource-create', $form['#node']->field_dataset_ref['nid']);
}
$form['#validate'][] = 'dkan_dataset_resource_node_form_validate';
// This saves us on the ajax reloading.
if (isset($query['dataset'])) {
$form['#action'] = url('node/add/resource', array('query' => array('dataset' => $query['dataset'])));
}
else {
$form['#action'] = url('node/add/resource');
if (drupal_match_path(current_path(), 'node/*/edit')) {
$form['#action'] = url('node/' . $form['#node']->nid . '/edit');
}
}
if (variable_get('dkan_dataset_form_additional_info', 1 )) {
if (isset($query['dataset']) || (isset($form_state['input']['field_dataset_ref']['und'][0]))) {
$form['actions']['additional'] = array(
'#type' => 'submit',
'#value' => t('Next: Additional Info'),
'#weight' => -5,
'#submit' => array('node_form_submit'),
);
$form['actions']['additional']['#submit'] = array('node_form_submit', 'dkan_dataset_resource_additional_form_submit');
}
}
$form['actions']['another'] = array(
'#type' => 'submit',
'#value' => t('Save and add another'),
'#weight' => 10,
'#submit' => array('node_form_submit'),
);
$form['actions']['another']['#submit'] = array('node_form_submit', 'dkan_dataset_resource_form_submit');
}
}
/**
* Resource node form submit handler.
*/
function dkan_dataset_resource_form_submit($form, &$form_state) {
if ($query = drupal_get_query_parameters()) {
if (isset($query['dataset'])) {
drupal_goto('node/add/resource', array('query' => array('dataset' => $query['dataset'])));
}
}
}
/**
* Resource next form submit handler.
*/
function dkan_dataset_resource_additional_form_submit($form, &$form_state) {
if ($query = drupal_get_query_parameters()) {
if (isset($query['dataset'])) {
drupal_goto('node/' . $query['dataset'] . '/edit', array('query' => array('additional' => 1)));
}
}
}
/**
* Resource node form submit handler.
*/
function dkan_dataset_dataset_body_submit($form, &$form_state) {
// Fixes body getting dropped in "Additional info" form.
if (isset($form_state['build_info']['args'][0]->body['und']['0']['value'])) {
if ($form_state['build_info']['args'][0]->body['und']['0']['value']) {
$form_state['values']['body']['und'][0]['value'] = $form_state['build_info']['args'][0]->body['und']['0']['value'];
$form_state['values']['body']['und'][0]['summary'] = $form_state['build_info']['args'][0]->body['und']['0']['summary'];
$form_state['values']['body']['und'][0]['format'] = $form_state['build_info']['args'][0]->body['und']['0']['format'];
}
}
}
/**
* Dataset node form submit handler.
*/
function dkan_dataset_dataset_form_submit($form, &$form_state) {
if (isset($form_state['node']->nid)) {
drupal_goto('node/add/resource', array('query' => array('dataset' => $form_state['node']->nid)));
}
}
/**
* Validation function for dataset node form.
*/
function dkan_dataset_dataset_node_form_validate($form, &$form_state) {
// Throw an error if the user has no groups available but the strict POD validation
// is enabled in DKAN Dataset Forms.
// The check about the field being required and empty won't work because the 'Groups'
// field is hidden when the user is not associated with any groups.
if (variable_get('dkan_dataset_form_group_validation') && module_exists('dkan_dataset_groups')) {
// Get available groups from the field options.
$field_og_group_ref_langcode = dkan_dataset_form_field_language($form, 'og_group_ref');
$available_groups = $form['og_group_ref'][$field_og_group_ref_langcode]['#options'];
// If there are no options then display an error.
if (empty($available_groups)) {
dkan_dataset_display_groups_warning();
form_set_error('', 'Error: You tried to submit a dataset with no groups assigned.');
}
}
// Display an error if the 'Public Access Level' field value is not 'public' and the 'Rights' field is empty.
$field_public_access_level_langcode = dkan_dataset_form_field_language($form, 'field_public_access_level');
$public_access_level = $form_state['values']['field_public_access_level'][$field_public_access_level_langcode][0]['value'];
$field_rights_langcode = dkan_dataset_form_field_language($form, 'field_rights');
$rights = $form_state['values']['field_rights'][$field_rights_langcode][0]['value'];
if ($public_access_level != 'public' && empty($rights)) {
form_set_error('field_rights', 'Rights field is required.');
}
}
/**
* Display groups warning on dataset form.
*/
function dkan_dataset_display_groups_warning() {
if (!user_access('administer group')) {
drupal_set_message(t("A dataset must be added to a specific group, but you don’t belong to any groups.
Ask a supervisor or site administrator to add you to a group or
promote you to site manager to add datasets."), 'warning');
}
}
/**
* Validation function for resource node form.
*/
function dkan_dataset_resource_node_form_validate($form, &$form_state) {
// Get langcode for field_link_file.
$field_link_file_langcode = dkan_dataset_form_field_language($form, 'field_link_file');
$field_link_remote_file_langcode = dkan_dataset_form_field_language($form, 'field_link_remote_file');
// Get langcode for field_link_api.
$field_link_api_langcode = dkan_dataset_form_field_language($form, 'field_link_api');
// Get langcode for field_upload.
$field_upload_langcode = dkan_dataset_form_field_language($form, 'field_upload');
// Get langcode for field_format.
$field_format_langcode = dkan_dataset_form_field_language($form, 'field_format');
// See if file link, api link, or file upload.
$link = isset($form_state['values']['field_link_file'][$field_link_file_langcode]) ? $form_state['values']['field_link_file'][$field_link_file_langcode][0]['url'] : NULL;
$api = isset($form_state['values']['field_link_api']) ? $form_state['values']['field_link_api'][$field_link_api_langcode][0]['url'] : NULL;
$upload_fid = $form_state['values']['field_upload'][$field_upload_langcode][0]['fid'];
$link_fid = isset($form_state['values']['field_link_remote_file'][$field_upload_langcode]) ? $form_state['values']['field_link_remote_file'][$field_upload_langcode][0]['fid'] : NULL;
$format_name = isset($form_state['values']['field_format'][$field_upload_langcode][0]) ? $form_state['values']['field_format'][$field_upload_langcode][0]['name'] : NULL;
$format_tid = isset($form_state['values']['field_format'][$field_upload_langcode][0]) ? $form_state['values']['field_format'][$field_upload_langcode][0]['tid'] : NULL;
$old_format_tid = isset($form['#node']->field_format[$field_upload_langcode][0]) ? $form['#node']->field_format[$field_upload_langcode][0]['tid'] : NULL;
if ($format_tid && !$format_name) {
$new_tax = taxonomy_term_load($format_tid);
$format_name = $new_tax->name;
}
$type = NULL;
// A format has been entered.
if ($format_name) {
// Previous format is the same, make no change.
if ($old_format_tid == $format_tid) {
$type = $format_name;
}
// No previous format, so user entered the format.
elseif (!$old_format_tid) {
$type = $format_name;
}
else {
// User entered new taxonomy term that is different from previous.
if ($old_tax = taxonomy_term_load($old_format_tid)) {
if ($old_tax->name != $format_name) {
$type = $format_name;
}
}
}
}
// Ensure that only one type of resource is uploaded or linked to.
if ($link && ($api || $upload_fid || $link_fid) || $api && ($link || $upload_fid || $link_fid) || ($upload_fid && $api || $link) || ($link_fid && $api || $link)) {
form_set_error('field_link_file', 'Only one resource type can be added.');
}
// Autodetect file type if a new one is not provided.
if (!$type) {
if ($upload_fid || $link_fid || $api) {
if ($upload_fid) {
$file = file_load($upload_fid);
$type = isset($file->filemime) ? dkan_dataset_discovery_extension($file->filemime) : '';
}
elseif ($link_fid) {
$file = file_load($link_fid);
// If you have a file that provides a content-type header or filemime of
// octet-stream we can look if the file has a filename with an extension
// we can use.
if (isset($file->filemime) && $file->filemime == 'application/octet-stream') {
$name = explode(".", $file->filename);
if (count($name) > 1) {
$type = $name[count($name) - 1];
}
}
else {
$type = isset($file->filemime) ? dkan_dataset_discovery_extension($file->filemime) : '';
}
}
// @todo Probably remove all this
elseif ($api) {
$file = new dkanDataset\getRemoteFileInfo($api, t('!site_name crawler', array('!site_name' => variable_get('site_name'))) , TRUE, file_directory_temp());
$filemime = $file->getType();
$type = dkan_dataset_discovery_extension($filemime);
}
// Check for geojson
if ($type == 'json' && isset($file)) {
$response = drupal_http_request($file->uri, array('timeout' => 5));
// dpm($response);
if ($response->code == '200') {
$data = $response->data;
$decoded = drupal_json_decode($data);
$geojson_types = array('FeatureCollection', 'GeometryCollection', 'Feature', 'Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon');
foreach ($geojson_types as $geojson_type) {
foreach ($decoded as $key => $value) {
if ($geojson_type === $value) {
$type = 'geojson';
}
}
}
}
}
}
}
$vocabs = taxonomy_vocabulary_get_names();
$type = isset($type) ? $type : 'data';
if (isset($vocabs['format'])) {
$form_state['values']['field_format'][$field_format_langcode][0]['vid'] = $vocabs['format']->vid;
if ($tax = taxonomy_get_term_by_name($type, 'format')) {
$tax = array_values($tax);
$form_state['values']['field_format'][$field_format_langcode][0]['tid'] = $tax[0]->tid;
$form_state['values']['field_format'][$field_format_langcode][0]['name'] = $tax[0]->name;
}
else {
$form_state['values']['field_format'][$field_format_langcode][0]['tid'] = 'autocreate';
$form_state['values']['field_format'][$field_format_langcode][0]['name'] = $type;
}
}
}
/**
* Implements hook_recline_format_alter().
*/
function dkan_dataset_recline_format_alter(&$format, $entity) {
if (isset($entity->field_format['und'][0]['taxonomy_term']->name)) {
$format = $entity->field_format['und'][0]['taxonomy_term']->name;
}
}
/**
* After build function for resource and dataset nodes.
*/
function dkan_dataset_resource_form_after_build($form, &$form_state) {
// Hide 'Other' options if strict POD validation is enabled.
if (isset($form['field_license']) && variable_get('dkan_dataset_form_pod_validation')) {
unset($form['field_license']['und']['select']['#options']['select_or_other']);
unset($form['field_license']['und']['select']['#options']['_none']);
}
if (isset($form['field_link_file'])) {
// Get langcode for field_link_file.
$field_link_file_langcode = dkan_dataset_form_field_language($form, 'field_link_file');
$field_link_remote_file_langcode = dkan_dataset_form_field_language($form, 'field_link_file');
// Get langcode for field_link_api.
$field_link_api_langcode = dkan_dataset_form_field_language($form, 'field_link_api');
$form['field_link_file'][$field_link_file_langcode][0]['url']['#attributes']['placeholder'] = 'eg. http://example.com/gold-prices-jan-2011.csv';
$form['field_link_remote_file'][$field_link_file_langcode][0]['filefield_dkan_remotefile']['url']['#attributes']['placeholder'] = 'eg. http://example.com/gold-prices.csv';
$form['field_link_remote_file'][$field_link_file_langcode][0]['filefield_dkan_remotefile']['select']['#suffix'] = '';
$form['field_link_api'][$field_link_api_langcode][0]['url']['#attributes']['placeholder'] = 'eg. http://example.com/gold-prices-jan-2011';
}
if (isset($form['field_format'])) {
// Get langcode for field_format.
$field_format_langcode = dkan_dataset_form_field_language($form, 'field_format');
$form['field_format'][$field_format_langcode]['textfield']['#attributes']['placeholder'] = 'eg. csv, json';
$form['field_format'][$field_format_langcode]['textfield']['#description'] = t('File format will be autodetected');
}
// Get langcode for body.
$body_langcode = dkan_dataset_form_field_language($form, 'body');
$form['body'][$body_langcode][0]['value']['#attributes']['placeholder'] = t('eg. Some useful notes about the data');
return $form;
}
module_load_include('inc', 'node', 'node.pages');
/**
* Creates initial resource node form.
*/
function dkan_dataset_prepare_resource_form($nid) {
$type = 'resource';
global $user;
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => $type,
);
$node->field_dataset_ref['nid'] = $nid;
$new_form_state = array();
$new_form_state['build_info']['args'][] = $node;
// Don't pull from cache.
$new_form_state['input'] = array();
$resource_form = drupal_build_form($type . '_node_form', $new_form_state);
$resource_form['#action'] = url('node/add/resource', array('query' => array('dataset' => $nid)));
return $resource_form;
}
/**
* Implements hook_block_info().
*/
function dkan_dataset_block_info() {
$blocks['dkan_dataset_dataset_help'] = array(
'info' => t('What are datasets?'),
);
$blocks['dkan_dataset_resource_help'] = array(
'info' => t('What is data?'),
);
$blocks['dkan_dataset_resource_nodes'] = array(
'info' => t('Resources'),
);
$blocks['dkan_dataset_visualizations_list'] = array(
'info' => t('Visualizations'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function dkan_dataset_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'dkan_dataset_dataset_help':
$block['subject'] = t('What are datasets?');
$block['content'] = dkan_dataset_dataset_form_help();
break;
case 'dkan_dataset_resource_help':
$block['subject'] = t('What is data?');
$block['content'] = dkan_dataset_resource_form_help();
break;
case 'dkan_dataset_resource_nodes':
$block['subject'] = t('Resources');
$block['content'] = dkan_dataset_resource_nodes();
break;
case 'dkan_dataset_visualizations_list':
$block['subject'] = t('Visualizations');
$block['content'] = dkan_dataset_visualizations_list();
break;
}
return $block;
}
/**
* Adds block for dataset edit form.
*/
function dkan_dataset_dataset_form_help() {
return t('Datasets are simply used to group related pieces of data. These can then be found under a single url with a description and licensing information.');
}
/**
* Adds block for resource edit form.
*/
function dkan_dataset_resource_form_help() {
return t('Data can be any file or link to a file containing useful data.');
}
/**
* Returns resources within a dataset.
*/
function dkan_dataset_resource_nodes() {
$output = '';
$nodes = array();
$current_node = menu_get_object();
if (isset($current_node->type) && $current_node->type == 'resource' && isset($current_node->field_dataset_ref[LANGUAGE_NONE][0])) {
$wrapper = entity_metadata_wrapper('node', $current_node);
$target = $wrapper->field_dataset_ref->value();
$target_id = $target[0]->nid;
if ($target_id) {
$nodes = dkan_dataset_get_resource_nodes($target_id);
}
}
else {
if ($query = drupal_get_query_parameters()) {
if (isset($query['dataset'])) {
$nodes = dkan_dataset_get_resource_nodes($query['dataset']);
}
}
}
if ($nodes) {
foreach ($nodes as $node) {
$attributes = array('attributes' => array('class' => array('list-group-item')),'html' => true);
$links[] = l('<span>' . $node->title . '</span>', 'node/' . $node->nid, $attributes);
}
if (!$current_node) {
$links[] = '';
}
$output = theme('item_list', array('items' => $links, 'attributes' => array('class' => array('list-group'))));
}
return $output;
}
/**
* Returns list of visualizations based on a resource.
*/
function dkan_dataset_visualizations_list() {
$output = '';
$current_node = menu_get_object();
if (isset($current_node->type) && $current_node->type == 'resource') {
if($current_node->uuid) {
// Find all visualizations created from this resource.
$nodes = dkan_dataset_get_visualization_entities($current_node->uuid);
}
if ($nodes) {
foreach ($nodes as $node) {
$attributes = array('attributes' => array('class' => array('list-group-item')),'html' => true);
// Fetch title and type.
$result = db_query("SELECT type, title FROM {eck_visualization} WHERE id = :id", array(':id' => $node->nid));
foreach ($result as $row) {
// Add visualization to the list.
$links[] = l($row->title, 'visualization/' . $row->type . '/' . $node->nid, $attributes);
}
}
if (!$current_node) {
$links[] = '';
}
$output = theme('item_list', array('items' => $links, 'attributes' => array('class' => array('list-group'))));
}
}
return $output;
}
/**
* Returns the display language for the fields attached to the given form.
*/
function dkan_dataset_form_field_language($form, $field_name = NULL) {
$langcode = 'und';
if ($field_name && isset($form[$field_name]) && !empty($form[$field_name]['#language'])) {
$langcode = $form[$field_name]['#language'];
}
$langcode = field_valid_language($langcode, FALSE);
return $langcode;
}