This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
govcms_ckan.admin.inc
121 lines (105 loc) · 4.05 KB
/
govcms_ckan.admin.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
<?php
/**
* @file
* Code for the admin pages of the govCMS CKAN module.
*/
/**
* Create settings form for govCMS CKAN.
*
* @return array
* Form API definition.
*/
function govcms_ckan_settings_form() {
$form = array();
$form['govcms_ckan_endpoint_url'] = array(
'#type' => 'textfield',
'#title' => t('Endpoint Url'),
'#description' => t('Specify the endpoint url. Example https://data.gov.au (please note no trailing slash)'),
'#weight' => 0,
'#size' => 100,
'#required' => TRUE,
'#default_value' => variable_get('govcms_ckan_endpoint_url', ''),
);
$form['govcms_ckan_api_key'] = array(
'#type' => 'textfield',
'#title' => t('Api Key'),
'#description' => t('Optionally specify an API key.'),
'#weight' => 1,
'#size' => 100,
'#default_value' => variable_get('govcms_ckan_api_key', ''),
);
$form['govcms_ckan_auth_header'] = array(
'#type' => 'textfield',
'#title' => t('Authorisation header'),
'#description' => t('Optionally override the HTTP authorisation header if required. Default is "Authorization"'),
'#weight' => 2,
'#size' => 100,
'#default_value' => variable_get('govcms_ckan_auth_header', ''),
);
$form['govcms_ckan_advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 10,
);
$form['govcms_ckan_advanced']['govcms_ckan_cache_ttl'] = array(
'#type' => 'textfield',
'#title' => t('Cache expiry'),
'#description' => t('How long results from CKAN get cached after a successful request. In seconds'),
'#size' => 100,
'#default_value' => variable_get('govcms_ckan_cache_ttl', GOVCMS_CKAN_CACHE_TTL),
);
$form['govcms_ckan_advanced']['govcms_ckan_cache_fail_ttl'] = array(
'#type' => 'textfield',
'#title' => t('Failed request cache expiry'),
'#description' => t('How long results from CKAN get cached after a failed request. In seconds'),
'#size' => 100,
'#default_value' => variable_get('govcms_ckan_cache_fail_ttl', GOVCMS_CKAN_CACHE_FAIL_TTL),
);
$form['govcms_ckan_advanced']['govcms_ckan_dataset_record_limit'] = array(
'#type' => 'textfield',
'#title' => t('Dataset record limit'),
'#description' => t('The maximum number records that will be fetched with a single dataset result request'),
'#size' => 100,
'#default_value' => variable_get('govcms_ckan_dataset_record_limit', GOVCMS_CKAN_DATASET_RECORD_LIMIT),
);
$form['#validate'][] = 'govcms_ckan_settings_form_validate';
return system_settings_form($form);
}
/**
* Form validate handler for govCMS CKAN settings form.
*
* @see govcms_ckan_settings_form()
*/
function govcms_ckan_settings_form_validate($form, &$form_state) {
// If an API key is in use, enforce https.
if (!empty($form_state['values']['govcms_ckan_api_key'])) {
if (file_uri_scheme($form_state['values']['govcms_ckan_endpoint_url']) != 'https') {
form_set_error('govcms_ckan_endpoint_url', t('If using an API key, the endpoint url must use HTTPS.'));
}
}
// Get a client instance.
module_load_include('inc', 'govcms_ckan', 'src/GovCmsCkanClient');
$client = new GovCmsCkanClient(
$form_state['values']['govcms_ckan_endpoint_url'],
$form_state['values']['govcms_ckan_api_key'],
$form_state['values']['govcms_ckan_auth_header']);
// Test the connection for a valid response.
if (!empty($form_state['values']['govcms_ckan_api_key'])) {
$response_code = $client->testConnection('action/dashboard_activity_list', array('limit' => 1));
}
else {
$response_code = $client->testConnection('action/package_list', array('limit' => 1));
}
// If we don't get a 200 (success), we have problems connecting.
if ($response_code != 200) {
if ($response_code == 403) {
form_set_error('govcms_ckan_api_key', t('API return "Not Authorised" please check your API key.'));
}
else {
form_set_error('govcms_ckan_endpoint_url', t('Could not establish a connection to the endpoint. Error: @code',
array('@code' => $response_code)));
}
}
}