-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdkan_datastore.pages.inc
182 lines (168 loc) · 5.29 KB
/
dkan_datastore.pages.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
<?php
/**
* @file
* Callbacks for datastore pages.
*/
/**
* Callback for Data API instructions.
*/
function dkan_datastore_datastore_api($node) {
$status = dkan_datastore_status($node);
switch ($status) {
case DKAN_DATASTORE_EXISTS:
$datastore = dkan_datastore_go($node->uuid);
$output = $datastore->apiForm();
return $output;
case DKAN_DATASTORE_FILE_EXISTS:
drupal_set_message(t('You need to add your file to the datastore in order to use the DATA API'));
$redirect_path = 'node/' . $node->nid . '/datastore';
break;
default:
drupal_set_message(t('This resource can not be added to the datastore'));
$redirect_path = 'node/' . $node->nid;
break;
}
drupal_goto($redirect_path);
}
/**
* Callback for download tab.
*/
function dkan_datastore_download($node) {
$node_wrapper = entity_metadata_wrapper('node', $node);
$link_type = dkan_datastore_resource_link_type($node);
$url = $link_type['url'];
$type = $link_type['type'];
if($type == 'upload') {
return drupal_goto($url);
} else if($type == 'remote') {
dkan_datastore_proxy($node);
} else if($type == 'link'){
return drupal_goto($url);
} else {
drupal_set_message(t('No download available for this resource'));
return '';
}
}
/**
* Proxy remote resources.
*/
function dkan_datastore_proxy($node) {
$allowed_types = array('csv');
$node_wrapper = entity_metadata_wrapper('node', $node);
$remote_file = $node_wrapper->field_link_remote_file->value();
$uri = $remote_file['uri'];
$filename = $remote_file['filename'];
$mime = $remote_file['filemime'];
$type = recline_get_data_type($remote_file['filemime']);
if(in_array($type, $allowed_types)) {
drupal_add_http_header('Content-Type', $mime);
drupal_add_http_header('Content-Disposition', 'attachment; filename=' . $filename);
print file_get_contents($uri);
}
}
/**
* Callback for back link.
*/
function dkan_datastore_back($node) {
$node_wrapper = entity_metadata_wrapper('node', $node);
$dataset = $node_wrapper->field_dataset_ref->value();
if ($dataset) {
drupal_goto('node/' . $dataset->nid);
}
return '';
}
/**
* Callback for 'Add Resouce' tab.
*/
function dkan_datastore_add_resource($node) {
drupal_goto('node/add/resource', array('query' => array('dataset' => $node->nid)));
}
/**
* Render a datastore import form on node/id/import pages.
*/
function dkan_datastore_import_tab_form($form, &$form_state, $node) {
$datastore = dkan_datastore_go($node->uuid);
$form = $datastore->manageForm($form_state);
$form['#datastore'] = $datastore;
return $form;
}
/**
* Submit function for import tab.
*/
function dkan_datastore_import_tab_form_submit($form, &$form_state) {
$datastore = $form['#datastore'];
$datastore->manageFormSubmit($form_state);
}
/**
* Render a datastore delete form.
*
* Used on both node pages and configuration pages.
* Therefore $node may be missing.
*/
function dkan_datastore_delete_tab_form($form, &$form_state, $importer_id, $node = NULL) {
$datastore = dkan_datastore_go($node->uuid);
$form = $datastore->deleteForm($form_state);
$form['#submit'] = array('feeds_delete_tab_form_submit');
return $form;
}
/**
* Render a datastore unlock form.
*
* Used on both node pages and configuration pages.
* Therefore $node may be missing.
*/
function dkan_datastore_unlock_tab_form($form, &$form_state, $importer_id, $node = NULL) {
module_load_include('inc', 'feeds', 'feeds.pages');
if (empty($node)) {
$source = feeds_source($importer_id);
$form['#redirect'] = 'import/' . $source->id;
}
else {
$importer_id = feeds_get_importer_id($node->type);
$source = feeds_source($importer_id, $node->nid);
$form['#redirect'] = 'node/' . $source->feed_nid;
}
// Form cannot pass on source object.
$form['#importer_id'] = $source->id;
$form['#feed_nid'] = $source->feed_nid;
$form['source_status'] = array(
'#type' => 'fieldset',
'#title' => t('Status'),
'#tree' => TRUE,
'#value' => feeds_source_status($source),
);
$form = confirm_form($form, t('Unlock this importer?'), $form['#redirect'], '', t('Delete'), t('Cancel'), 'confirm feeds update');
if ($source->progressImporting() == FEEDS_BATCH_COMPLETE && $source->progressClearing() == FEEDS_BATCH_COMPLETE) {
$form['source_locked'] = array(
'#type' => 'markup',
'#title' => t('Not Locked'),
'#tree' => TRUE,
'#markup' => t('This importer is not locked, therefore it cannot be unlocked.'),
);
$form['actions']['submit']['#disabled'] = TRUE;
$form['actions']['submit']['#value'] = t('Unlock (disabled)');
}
else {
$form['actions']['submit']['#value'] = t('Unlock');
}
$form['#submit'] = array('feeds_unlock_tab_form_submit');
return $form;
}
/**
* Render a datastore drop form on node/id/drop pages.
*/
function dkan_datastore_drop_tab_form($form, &$form_state, $node) {
$datastore = dkan_datastore_go($node->uuid);
$form = $datastore->dropForm($form_state);
$form['#datastore'] = $datastore;
return $form;
}
/**
* Submit function for drop tab.
*/
function dkan_datastore_drop_tab_form_submit($form, &$form_state) {
$datastore = $form['#datastore'];
$form_state['#tables_to_drop'] = $form['#tables_to_drop'];
$datastore->dropFormSubmit($form_state);
$form_state['redirect'] = $form['#redirect'];
}