Browse Source

removed unneeded files and form settings

9.x-3.0
Paul Pound 1 week ago
parent
commit
096d940d61
  1. 80
      includes/admin.form.inc
  2. 167
      includes/relais.inc
  3. 50
      src/Form/RoblibIllSettingsForm.php

80
includes/admin.form.inc

@ -1,80 +0,0 @@
<?php
/**
* @file
* the administrative form for upei_roblib_ill
*/
function upei_roblib_ill_admin_form($form, &$form_state) {
$form = [];
$form['upei_roblib_ill_add_url'] = [
'#required' => TRUE,
'#type' => 'textfield',
'#size' => 200,
'#title' => t('The Relais AddRequest URL'),
'#description' => t('The URL for the Relais ILL server AddRequest service, the default is
https://caul-cbua.relais-host.com/portal-service/request/add'),
'#default_value' => variable_get('upei_roblib_ill_add_url', 'https://caul-cbua.relais-host.com/portal-service/request/add'),
];
$form['upei_roblib_ill_auth_url'] = [
'#required' => TRUE,
'#type' => 'textfield',
'#size' => 200,
'#title' => t('The Relais Auth URL'),
'#description' => t('The URL for the Relais ILL server Auth service (retrieve the aid), the default is
https://caul-cbua.relais-host.com/portal-service/user/authentication'),
'#default_value' => variable_get('upei_roblib_ill_auth_url', 'https://caul-cbua.relais-host.com/portal-service/user/authentication'),
];
$form['upei_roblib_ill_library_symbol'] = [
'#required' => TRUE,
'#type' => 'textfield',
'#size' => 200,
'#title' => t('Your Relais Library Symbol'),
'#description' => t('Your Relais Library Symbol'),
'#default_value' => variable_get('upei_roblib_ill_library_symbol'),
];
$form['upei_roblib_ill_relais_key'] = [
'#required' => TRUE,
'#type' => 'textfield',
'#size' => 200,
'#title' => t('The Relais API key'),
'#description' => t('The API key used to communicate with the Relais API, Contact Relais International to get your key'),
'#default_value' => variable_get('upei_roblib_ill_relais_key'),
];
$form['upei_roblib_ill_doi_openurl_pid'] = [
'#required' => TRUE,
'#type' => 'textfield',
'#size' => 200,
'#title' => t('OpenURL PID'),
'#description' => t('An identifier to call yourself, for the OpenURL endpoint. To use this service you first need to register for an account here: http://www.crossref.org/requestaccount/'),
'#default_value' => variable_get('upei_roblib_ill_doi_openurl_pid'),
];
$form['upei_roblib_ill_contact_email'] = [
'#required' => TRUE,
'#type' => 'textfield',
'#size' => 200,
'#title' => t('Contact Email'),
'#description' => t('The email address we want to show after a user has submitted an ILL request'),
'#default_value' => variable_get('upei_roblib_ill_contact_email', 'ill@upei.ca'),
];
$form['upei_roblib_ill_contact_phone'] = [
'#required' => TRUE,
'#type' => 'textfield',
'#size' => 200,
'#title' => t('Contact Phone Number'),
'#description' => t('The phone number we want to show to the user after a user has submitted an ILL request'),
'#default_value' => variable_get('upei_roblib_ill_contact_phone', '902-566-0445'),
];
$form['upei_roblib_ill_header_message'] = [
'#required' => TRUE,
'#type' => 'textarea',
'#title' => t('ILL Header Message'),
'#description' => t('The message that appears at the top of the ILL form, recently used for Covid messages. Leave this blank for no message to appear at the top of the form.'),
'#default_value' => variable_get('upei_roblib_ill_header_message', ''),
];
return system_settings_form($form);
}

167
includes/relais.inc

@ -1,167 +0,0 @@
<?php
/**
* @file
* Contains the functions to send and receive data to/from Relais.
*/
use Drupal\Core\Url;
use Drupal\Core\Link;
function upei_roblib_ill_get_pub_type($genre) {
switch ($genre) {
case 'book' :
return 'B';
case 'chapter' :
return 'I';
default :
return 'J';
}
}
/**
* Submit an ILL AddRequest to relais.
*
* @param array $form_state
* A drupal form_state array
* @param string $aid
* A relais authentication id (token)
*
* @return array|mixed
*/
function upei_roblib_ill_add_request($form_state, $aid) {
\Drupal::moduleHandler()->loadInclude('upei_roblib_ill', 'inc', 'includes/db');
\Drupal::moduleHandler()->loadInclude('upei_roblib_ill', 'inc', 'includes/utilities');
$config = \Drupal::config('upei_roblib_ill.settings');
$url = trim($config->get('ill_add_url'));
$relais_arr = upei_roblib_ill_build_relais_arr($form_state);
$relais_json = json_encode($relais_arr);
$options = [
'body' => $relais_json,
'timeout' => 15,
'headers' => ['Content-Type' => 'application/json'],
];
if (!isset($aid) || is_array($aid)) {
upei_roblib_ill_log_request($relais_arr, $aid);
\Drupal::messenger()->addMessage(t('Error retrieving authentication token, @message', ['@message' => $aid['Problem']['Message']]), 'error');
return ['ConfirmMessage' => t('There was an error processing your request, @msg', ['@msg' => $aid['Problem']['Message']])];
}
//$result = drupal_http_request($url . '?aid=' . $aid, $options);
try {
$response = \Drupal::httpClient()->post($url . '?aid=' . $aid, $options);
//$response_data = (string) $response->getBody();
}
catch (Exception $e) {
return ['ConfirmMessage' => 'Error Communicating with Relais, ' . $e->getMessage()];
}
$response_arr = json_decode($response->getBody(),TRUE);
if (!isset($response_arr)) {
return ['ConfirmMessage' => 'Error Communicating with Relais, no data returned.'];
}
$response_arr['ConfirmMessage'] = isset($response_arr['Problem']['ErrorMessage']) ? $response_arr['Problem']['ErrorMessage'] : $response_arr['ConfirmMessage'];
upei_roblib_ill_log_request($relais_arr, $response_arr);
return $response_arr;
}
/**
* @param $form_state
*
* @return array
*/
function upei_roblib_ill_build_relais_arr($form_state) {
\Drupal::moduleHandler()->loadInclude('upei_roblib_ill', 'inc', 'includes/utilities');
$storage = $form_state->getStorage();
$ill_request = $storage['request'];
$values = $form_state->getValues();
$config = \Drupal::config('upei_roblib_ill.settings');
$relais_arr = [
"SupplyingLibrarySymbol" => $config->get('ill_library_symbol'),
];
$pub_type = upei_roblib_ill_get_pub_type($ill_request['Genre']);
$relais_arr['BibliographicInfo'] = upei_roblib_ill_clean_array($ill_request);
$relais_arr['DeliveryAddress'] = upei_roblib_ill_clean_array($values);
if ($pub_type != 'B') {
// this is not a book but is a journal or chapter we will send electronically and as copy
$relais_arr['ElectronicDelivery']['DeliveryAddress'] = $relais_arr['ElectronicDelivery']['MessagingAddress'] =
$relais_arr['DeliveryAddress']['DeliveryAddress'];
}
$request_info = upei_roblib_ill_request_info_array($relais_arr['BibliographicInfo'], $values['notes']);
$relais_arr['RequestInfo'] = $request_info;
$relais_arr['PublisherInfo']['PublicationDate'] = isset($ill_request['Date']) ?
$ill_request['Date'] : '';
$relais_arr['PublisherInfo']['PublicationType'] = $pub_type;
return $relais_arr;
}
/**
* Request an aid from Relais.
*
* @param string $barcode
* A patron_id, campus_id or barcode to identify a user
*
* @return string/array
* Returns a Relais authentication id (token) or an array containing the
* Relais response error which should include the error message if
* authentication fails
*/
function upei_roblib_ill_authenticate($barcode, $surname) {
//$url = variable_get('upei_roblib_ill_auth_url', 'https://caul-cbua.relais-host.com/portal-service/user/authentication');
$config = \Drupal::config('upei_roblib_ill.settings');
$url = $config->get('ill_auth_url');
$json_arr = [];
$json_arr['ApiKey'] = $config->get('ill_relais_key');
$json_arr['UserGroup'] = 'patron';
$json_arr['LibrarySymbol'] = $config->get('ill_library_symbol');
$json_arr['PatronId'] = $barcode;
$request_json = json_encode($json_arr);
$options = [
'body' => $request_json,
'timeout' => 15,
'headers' => ['Content-Type' => 'application/json'],
];
try {
$response = \Drupal::httpClient()->post($url, $options);
}
catch (Exception $e) {
$human_readable = '';
if($e->getCode() == '401') {
$human_readable = t('There may have been an issue with your Campus ID, '
. 'Please verify it is correct. ');
}
$response_data['Problem']['Message'] = $e->getMessage();
$response_data['Problem']['HR_Message'] = upei_roblib_ill_build_err_msg($human_readable);
return $response_data;
}
$response_data = json_decode($response->getBody(),TRUE);
$status_code = $response->getStatusCode();
if ($status_code == '200') {
if (isset($response_data['Problem']) || strtolower($response_data['LastName']) !== strtolower($surname)) {
$err_message = isset($response_data['Problem']['Message']) ? $response_data['Problem']['Message'] : '';
$response_data['Problem']['Message'] = upei_roblib_ill_build_err_msg($err_message);
return $response_data;
}
$aid = $response_data['AuthorizationId'];
}
return isset($aid) ? $aid : $response_data;
}
function upei_roblib_ill_build_err_msg($msg_from_server) {
$campus_id = Link::fromTextAndUrl(t('Campus ID'), Url::fromUri('http://www.upei.ca/vpaf/campuscard'));
//$campus_id = l(t('Campus ID'), 'http://www.upei.ca/vpaf/campuscard');
$ill_email = Link::fromTextAndUrl('ill@upei.ca', Url::fromUri('mailto:ill@upei.ca'));
//$ill_email = l(t('ill@upei.ca'), 'mailto:ill@upei.ca');
$phone = Link::fromTextAndUrl('902-566-0583', Url::fromUri('tel:902-566-0353'));
//$phone = l(t('902-566-0583'), 'tel:902-566-0353');
$server_response = !empty($msg_from_server) ? 'Server Response: ' . $msg_from_server : '';
return t('Oops. Something went wrong.<br />Check the "Your Last Name" and "Your Campus ID" fields - those two need to match what is on file
. (Your @campus_id appears as the NUMBER near the middle of your campus card). If you do not have a @campus_id, please contact the Robertson Library Service Desk, or,
call @phone. Note: if you are a student taking online courses only, please email @ill_email.<br />@msg_from_server',
[
'@campus_id' => $campus_id->toString(),
'@ill_email' => $ill_email->toString(),
'@phone' => $phone->toString(),
'@msg_from_server' => $server_response,
]);
}

50
src/Form/RoblibIllSettingsForm.php

@ -10,57 +10,35 @@ use Drupal\Core\Form\FormStateInterface;
*
* @author ppound
*/
class RoblibIllSettingsForm extends FormBase {
class RoblibIllSettingsForm extends FormBase
{
/**
* {@inheritdoc}
*/
public function getFormId() {
public function getFormId()
{
return 'roblib_ill_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
public function buildForm(array $form, FormStateInterface $form_state)
{
$config = \Drupal::config('upei_roblib_ill.settings');
$form = [];
$form['ill_add_url'] = [
'#required' => TRUE,
'#type' => 'textfield',
'#size' => 200,
'#title' => t('The Relais AddRequest URL'),
'#description' => t('The URL for the Relais ILL server AddRequest service, the default is
https://caul-cbua.relais-host.com/portal-service/request/add'),
'#default_value' => $config->get('ill_add_url'),
];
$form['ill_auth_url'] = [
'#required' => TRUE,
'#type' => 'textfield',
'#size' => 200,
'#title' => t('The Relais Auth URL'),
'#description' => t('The URL for the Relais ILL server Auth service (retrieve the aid), the default is
https://caul-cbua.relais-host.com/portal-service/user/authentication'),
'#default_value' => $config->get('ill_auth_url'),
];
$form['ill_library_symbol'] = [
'#required' => TRUE,
'#type' => 'textfield',
'#size' => 200,
'#title' => t('Your Relais Library Symbol'),
'#title' => t('Your ILL Library Symbol'),
'#description' => t('Your Relais Library Symbol'),
'#default_value' => $config->get('ill_library_symbol'),
];
$form['ill_relais_key'] = [
'#required' => TRUE,
'#type' => 'textfield',
'#size' => 200,
'#title' => t('The Relais API key'),
'#description' => t('The API key used to communicate with the Relais API, Contact Relais International to get your key'),
'#default_value' => $config->get('ill_relais_key'),
];
$form['ill_doi_openurl_pid'] = [
'#required' => TRUE,
'#type' => 'textfield',
@ -120,7 +98,7 @@ class RoblibIllSettingsForm extends FormBase {
'#size' => 200,
];
$form['actions']['submit'] = [
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Save'),
'#button_type' => 'primary',
@ -132,14 +110,16 @@ class RoblibIllSettingsForm extends FormBase {
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
public function validateForm(array &$form, FormStateInterface $form_state)
{
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state)
{
$config = \Drupal::configFactory()->getEditable('upei_roblib_ill.settings');
$config->set('ill_add_url', $form_state->getValue('ill_add_url'))->save();
$config->set('ill_auth_url', $form_state->getValue('ill_auth_url'))->save();
@ -160,4 +140,4 @@ class RoblibIllSettingsForm extends FormBase {
$config->set('rapid_ill_branch_name', $form_state->getValue('rapid_ill_branch_name'))->save();
}
}
}
Loading…
Cancel
Save