Skip to content

Commit f842ebe

Browse files
authored
Merge pull request #6142 from NaturalHistoryMuseum/paul/gbif-description
Add a description field to GBIF submissions
2 parents 3fbdc6c + 9b089c5 commit f842ebe

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

sites/all/modules/custom/gbif_registry/classes/Registry.class.inc

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,22 +186,48 @@ class Registry
186186
}
187187

188188
/**
189-
* http put
189+
* Make an HTTP PUT request with JSON
190190
*
191-
* FIXME - Need to add authentication to this.
192-
* FIXME - Should this be data, or a filename?
191+
* @param string $path The path to PUT to
192+
* @param resource|string $data The request body
193+
* @return string|true False if failed, true if successful but
194+
* empty resposnse, otherwise the response body
193195
*/
194-
public function put($path, $file_handle)
196+
public function put($path, $data)
195197
{
196-
$ch = curl_init($this->registryURL . $path);
197-
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
198-
curl_setopt($ch, CURLOPT_HEADER, 0);
199-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
200-
curl_setopt($ch, CURLOPT_PUT, true);
201-
curl_setopt($ch, CURLOPT_INFILE, $file_handle);
202-
//curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filename));
203-
$response = curl_exec($ch);
198+
if(is_resource($data)) {
199+
$data = stream_get_contents($data);
200+
}
204201

202+
$response = drupal_http_request(
203+
$this->registryURL . $path,
204+
[
205+
'method' => 'PUT',
206+
'headers' => [
207+
'Authorization' => 'Basic ' . base64_encode("{$this->user}:{$this->password}"),
208+
'Content-Type' => 'application/json',
209+
'Accept' => 'application/json'
210+
],
211+
'data' => $data
212+
]
213+
);
214+
215+
$httpcode = $response->code;
216+
$http_is_successful = $httpcode >= 200 && $httpcode < 400;
217+
$error = $response->error;
218+
219+
if ($error || !$http_is_successful) {
220+
watchdog(
221+
'GBIF',
222+
'Could not save GBIF registry object - Error: %error HTTP status: %httpcode',
223+
array('%error' => $error, '%httpcode' => $httpcode),
224+
WATCHDOG_ERROR
225+
);
226+
return false;
227+
} else {
228+
// Make sure we don't return a falsy value if response is empty (e.g. a 204)
229+
return $response->data ?: true;
230+
}
205231
}
206232

207233
/**

sites/all/modules/custom/gbif_registry/classes/RegistryObject.class.inc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,9 @@ class RegistryObject extends RegistryChild
211211
foreach ($this->getAsArray() as $key => $value) {
212212
$current_data[$key] = $value;
213213
}
214-
$f = tmpfile();
215-
fwrite($f, json_encode($current_data));
216-
fseek($f, 0);
214+
215+
$f = json_encode($current_data);
217216
Registry::singleton()->put($this->getURLPrefix() . "/{$this->key}", $f);
218-
unlink($filename);
219217
} else {
220218
// No key, we simply need to create a new object, and save the key.
221219

sites/all/modules/custom/scratchpads/scratchpads_gbif_registry_client/scratchpads_gbif_registry_client.pages.inc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ function scratchpads_gbif_registry_client_admin_settings()
3232

3333
$nonBio = !variable_get('biological_vids');
3434

35+
$default_description = implode(' | ', array_filter([
36+
variable_get('site_slogan', ''),
37+
strip_tags(variable_get('front_page_welcome_message', ['value'=>''])['value'])
38+
]));
39+
3540
$form += array(
3641
'scratchpads_gbif_registry_client_registration_enabled' => array(
3742
'#type' => 'checkbox',
@@ -40,6 +45,12 @@ function scratchpads_gbif_registry_client_admin_settings()
4045
'#description' => t("Whether to register this scratchpad with GBIF."),
4146
'#disabled' => $nonBio
4247
),
48+
'scratchpads_gbif_registry_client_description' => array(
49+
'#type' => 'textarea',
50+
'#title' => t('Dataset description'),
51+
'#default_value' => variable_get('scratchpads_gbif_registry_client_description', $default_description),
52+
'#description' => t('A summary of the dataset. This will appear on the GBIF web page for your dataset and in GBIF search results.')
53+
),
4354
'scratchpads_gbif_registry_client_registration_licence' => array(
4455
'#type' => 'select',
4556
'#options' => $licence_options,
@@ -61,18 +72,43 @@ function scratchpads_gbif_registry_client_admin_settings()
6172
}
6273

6374
$form['#submit'][] = 'scratchpads_gbif_registry_client_admin_settings_form_submit';
75+
$form['#validate'][] = 'scratchpads_gbif_registry_client_admin_settings_form_validate';
6476

6577
// use the system_settings_form function so that we don't have to implement our own submit function
6678
return system_settings_form($form);
6779
}
6880

81+
/**
82+
* Validation handler for settings form
83+
*/
84+
function scratchpads_gbif_registry_client_admin_settings_form_validate($form, &$form_state) {
85+
$enabled = $form_state['values']['scratchpads_gbif_registry_client_registration_enabled'];
86+
$description = $form_state['values']['scratchpads_gbif_registry_client_description'];
87+
88+
if($enabled && !$description) {
89+
form_set_error('scratchpads_gbif_registry_client_description', t('You must proide a description to submit your dataset to GBIF.'));
90+
}
91+
}
92+
6993
/**
7094
* Submit handler for the scratchpads_gbif_registry_client_admin_settings form.
7195
*/
7296
function scratchpads_gbif_registry_client_admin_settings_form_submit($form, &$form_state)
7397
{
7498
variable_del('scratchpads_gbif_registry_client_last_update');
7599

100+
$new_desc = $form_state['values']['scratchpads_gbif_registry_client_description'];
101+
102+
if($form['scratchpads_gbif_registry_client_description']['#default_value'] !== $new_desc) {
103+
$uuid = variable_get('scratchpads_gbif_registry_client_uuid', false);
104+
if($uuid) {
105+
GBIFRegistry::singleton();
106+
$dataset = new GBIF\Dataset($uuid);
107+
$dataset->setDescription($new_desc);
108+
$dataset->save();
109+
}
110+
}
111+
76112
if(module_exists('dwcarchiver')) {
77113
dwcarchiver_rebuild('gbif-dwca', false);
78114
menu_rebuild();

0 commit comments

Comments
 (0)