Browse Source

loading doi data working

9.x-1.0
ppound 3 years ago
parent
commit
54dde1a0a3
  1. 26
      includes/doi.inc
  2. 65
      src/Form/RoblibIllLoanForm.php
  3. 16
      upei_roblib_ill.install

26
includes/doi.inc

@ -18,22 +18,26 @@
* @return array * @return array
* A Drupal form with updated values. * A Drupal form with updated values.
*/ */
use Drupal\Core\Url;
function upei_roblib_ill_doi_get_data($form, $doi) { function upei_roblib_ill_doi_get_data($form, $doi) {
if (empty($doi)) { if (empty($doi)) {
$doi = 'Empty DOI'; $doi = 'Empty DOI';
} }
$response = upei_roblib_ill_doi_load($doi); $response = upei_roblib_ill_doi_load($doi);
if ($response->data && strpos($response->data, "Malformed DOI") !== FALSE) { $response_data = (string)$response->getBody();
if ($response_data && strpos($response_data, "Malformed DOI") !== FALSE) {
//try again without parsing the doi as sometimes parsing breaks it //try again without parsing the doi as sometimes parsing breaks it
$response = upei_roblib_ill_doi_load($doi, FALSE); $response = upei_roblib_ill_doi_load($doi, FALSE);
} }
$headers = array_change_key_case($response->headers); $headers = array_change_key_case($response->getHeaders());
if ($response->data && if ($response_data &&
strpos($headers['content-type'], 'text/html') === FALSE && strpos($headers['content-type'][0], 'text/html') === FALSE &&
strpos($response->data, "Malformed DOI") === FALSE strpos($response_data, "Malformed DOI") === FALSE
) { ) {
$crossref_xml = new DOMDocument('1.0'); $crossref_xml = new DOMDocument('1.0');
if (!$crossref_xml->loadXML($response->data)) { if (!$crossref_xml->loadXML($response_data)) {
drupal_set_message(t('Error parseing DOI response, @msg')); drupal_set_message(t('Error parseing DOI response, @msg'));
return $form; return $form;
} }
@ -159,9 +163,10 @@ function upei_roblib_ill_doi_name_from_url($doi_url) {
function upei_roblib_ill_doi_load($doi, $parse = TRUE) { function upei_roblib_ill_doi_load($doi, $parse = TRUE) {
// Allows for $id to pass a DOI url string or the DOI name. // Allows for $id to pass a DOI url string or the DOI name.
$id = $parse ? upei_roblib_ill_doi_name_from_url($doi) : $doi; $id = $parse ? upei_roblib_ill_doi_name_from_url($doi) : $doi;
$openurl = variable_get('upei_roblib_ill_doi_openurl', 'http://www.crossref.org/openurl'); $config = \Drupal::config('upei_roblib_ill.settings');
$openurl_pid = variable_get('upei_roblib_ill_doi_openurl_pid', 'user@example.com'); $openurl = 'http://www.crossref.org/openurl';
$url = url($openurl, [ $openurl_pid = $config->get('ill_doi_openurl_pid');
$url = Url::fromUri($openurl, [
'query' => [ 'query' => [
'noredirect' => 'true', 'noredirect' => 'true',
'pid' => $openurl_pid, 'pid' => $openurl_pid,
@ -169,6 +174,7 @@ function upei_roblib_ill_doi_load($doi, $parse = TRUE) {
'id' => ((strpos($id, 'doi:') === 0) ? $id : 'doi:' . $id), 'id' => ((strpos($id, 'doi:') === 0) ? $id : 'doi:' . $id),
], ],
]); ]);
return drupal_http_request($url); $response = \Drupal::httpClient()->get($url->toString());
return $response;
} }

65
src/Form/RoblibIllLoanForm.php

@ -27,18 +27,6 @@ class RoblibIllLoanForm extends FormBase {
return $this->ill_auth_form($form, $form_state); return $this->ill_auth_form($form, $form_state);
} }
return $this->ill_request_form($form, $form_state); return $this->ill_request_form($form, $form_state);
/* $values = $form_state->getValues();
if (empty($values)) {
$form_name = 'upei_roblib_ill_request_form';
}
else {
$form_name = 'upei_roblib_ill_auth_form';
}
$form_state->setValue('step', $from_name);
return $form_name === 'upei_roblib_ill_request_form' ? $this->ill_request_form($form, $form_state) :
$this->ill_auth_form($form, $form_state);
* */
} }
/** /**
@ -55,8 +43,8 @@ class RoblibIllLoanForm extends FormBase {
if (!$values['certify']) { if (!$values['certify']) {
$form_state->setErrorByName('certify', t('Please certify that this is item is being sought for a fair dealing purpose')); $form_state->setErrorByName('certify', t('Please certify that this is item is being sought for a fair dealing purpose'));
} }
$campus_id = $this->ill_form_message_id($values['campus_id']); $campus_id = $this->ill_form_message_id($values['campus_id']);
$aid = upei_roblib_ill_authenticate($campus_id, $values['Surname']); $aid = upei_roblib_ill_authenticate($campus_id, $values['Surname']);
if (is_array($aid) && isset($aid['Problem']['Message'])) { if (is_array($aid) && isset($aid['Problem']['Message'])) {
@ -72,22 +60,23 @@ class RoblibIllLoanForm extends FormBase {
$form_state->set('aid', $aid); $form_state->set('aid', $aid);
} }
} }
//if (empty($form_state->get('step')) && $form_state['triggering_element']['#value'] !== 'Lookup DOI' && empty($form_state->get('Title'))) { $trigger = $form_state->getTriggeringElement();
// form_set_error('Title', t('Journal/Book Title is required.')); if (empty($form_state->get('step')) && $trigger['#value'] !== 'Lookup DOI' && empty($form_state->getValue('Title'))) {
//} $form_state->setErrorByName('Title', t('Journal/Book Title is required.'));
}
} }
/** /**
* @param string $id * @param string $id
* The users campus id * The users campus id
* *
* @return mixed * @return mixed
* the campus id with leading zeros and leading/trailing whitespace removed. * the campus id with leading zeros and leading/trailing whitespace removed.
*/ */
function ill_form_message_id($id) { function ill_form_message_id($id) {
$id = ltrim($id, '0'); $id = ltrim($id, '0');
return trim($id); return trim($id);
} }
/** /**
* @param array $form * @param array $form
@ -96,10 +85,13 @@ function ill_form_message_id($id) {
* The current state of the form. * The current state of the form.
*/ */
public function submitStepOne(array &$form, FormStateInterface $form_state) { public function submitStepOne(array &$form, FormStateInterface $form_state) {
$form_state $trigger = $form_state->getTriggeringElement();
->set('step', 2) if ($trigger['#value'] != 'Lookup DOI') {
->set('request', $form_state->getValues()) $form_state
->setRebuild(TRUE); ->set('step', 2)
->set('request', $form_state->getValues())
->setRebuild(TRUE);
}
} }
/** /**
@ -122,6 +114,13 @@ function ill_form_message_id($id) {
]; */ ]; */
} }
public function doiCallback(array &$form, FormStateInterface $form_state) {
module_load_include('inc', 'upei_roblib_ill', 'includes/doi');
$doi = $form_state->getValue('doi');
$form = upei_roblib_ill_doi_get_data($form, $doi);
return $form;
}
/** /**
* The bibliographic portion of the ILL form. * The bibliographic portion of the ILL form.
* *
@ -177,7 +176,7 @@ function ill_form_message_id($id) {
'#type' => 'submit', '#type' => 'submit',
'#value' => 'Lookup DOI', '#value' => 'Lookup DOI',
'#ajax' => [ '#ajax' => [
'callback' => 'upei_roblib_ill_doi_callback', 'callback' => '::doiCallback',
'method' => 'replace', 'method' => 'replace',
'wrapper' => 'upei-roblib-ill-request-form', 'wrapper' => 'upei-roblib-ill-request-form',
'effect' => 'fade', 'effect' => 'fade',

16
upei_roblib_ill.install

@ -5,22 +5,6 @@
*/ */
/**
* Implements hook_uninstall().
*/
function upei_roblib_ill_uninstall() {
$variables = [
'upei_roblib_ill_add_url',
'upei_roblib_ill_auth_url',
'upei_roblib_ill_relais_key',
'upei_roblib_ill_library_symbol',
'upei_roblib_ill_contact_email',
'upei_roblib_ill_contact_phonel',
'upei_roblib_ill_header_message',
];
array_walk($variables, 'variable_del');
}
function upei_roblib_ill_update_7100() { function upei_roblib_ill_update_7100() {
db_change_field('upei_roblib_ill_request', 'isbn', 'isbn', db_change_field('upei_roblib_ill_request', 'isbn', 'isbn',
array( array(

Loading…
Cancel
Save