@ -14,7 +14,8 @@
use Drupal\Component\Utility\Xss;
use Drupal\Component\Utility\Html;
function upei_roblib_ill_check_arr_item(& $value, $key) {
function upei_roblib_ill_check_arr_item(& $value, $key)
{
if (is_array($value)) {
array_walk($value, 'upei_roblib_ill_check_arr_item');
}
@ -33,7 +34,8 @@ function upei_roblib_ill_check_arr_item(&$value, $key) {
* An array where the keys in the array that have empty values are removed and
* drupal specific elements are removed.
*/
function upei_roblib_ill_clean_array($values) {
function upei_roblib_ill_clean_array($values)
{
$arr = array_filter($values);
if (isset($arr['ISSN'])) {
$arr['ISSN'] = [$arr['ISSN']];
@ -71,6 +73,95 @@ function upei_roblib_ill_clean_array($values) {
return $arr;
}
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,
]);
}
/**
* Lookup the user in Evergreen and verify the surname matches.
*
* @param string $campus_id
* The user's campus ID / barcode.
* @param string $surname
* The surname to check against.
*
* @return bool
* TRUE if the surname matches, FALSE otherwise.
*/
function upei_roblib_ill_authenticate($campus_id, $surname): bool
{
if (empty($campus_id) || empty($surname)) {
return FALSE;
}
$config = \Drupal::config('upei_roblib_ill.settings');
$evergreen_api_url = $config->get('evergreen_api_url');
if (empty($evergreen_api_url)) {
\Drupal::logger('upei_roblib_ill')->error('Evergreen API URL is not configured.');
return FALSE;
}
// Construct the PatronAPI dump URL.
// Format: {evergreen_api_url}/api/patronapi/{campus_id}/dump
$url = rtrim($evergreen_api_url, '/') . '/api/patronapi/' . urlencode($campus_id) . '/dump';
try {
$response = \Drupal::httpClient()->get($url, [
'timeout' => 10,
]);
$data = (string)$response->getBody();
// The PatronAPI dump endpoint returns data in a plain text/HTML format
// looking like `PATRN NAME[p]=Smith, John`.
if (preg_match('/PATRN NAME\[p\]=([^\[< ]+)/i', $data, $matches)) {
$patron_name = trim($matches[1]);
// Usually "Lastname, Firstname"
$name_parts = explode(',', $patron_name);
if (count($name_parts) > 0) {
$evergreen_surname = trim($name_parts[0]);
// Case-insensitive comparison of the extracted surname and provided surname.
if (strcasecmp($evergreen_surname, trim($surname)) === 0) {
return TRUE;
}
}
}
// Log failure for debugging.
\Drupal::logger('upei_roblib_ill')->warning('Evergreen authentication failed for @campus_id. Expected surname: @surname', [
'@campus_id' => $campus_id,
'@surname' => $surname,
]);
return FALSE;
}
catch (\Exception $e) {
\Drupal::logger('upei_roblib_ill')->error('Failed to connect to Evergreen API: @message', [
'@message' => $e->getMessage(),
]);
return FALSE;
}
}
/**
* Allows us to ask for _REQUEST variables that may or may not exist without
* generating a php warning. Also does some formatting for certain variables.
@ -81,30 +172,33 @@ function upei_roblib_ill_clean_array($values) {
* @return string
* The value of the $_REQUEST variable or an empty string.
*/
function upei_roblib_ill_get_request_variable($variable) {
function upei_roblib_ill_get_request_variable($variable)
{
if ($variable == 'author' & & empty($_REQUEST[$variable])) {
//google scholar usually sends auinit aulast instead of author
$initial = isset($_REQUEST['auinit']) ? Html::escape($_REQUEST['auinit']) : NULL;
$last_name = isset($_REQUEST['aulast']) ? Html::escape($_REQUEST['aulast']) : NULL;
$initial = isset($_REQUEST['auinit']) ?Html::escape($_REQUEST['auinit']) : NULL;
$last_name = isset($_REQUEST['aulast']) ?Html::escape($_REQUEST['aulast']) : NULL;
return !empty($last_name) ? $last_name . ',' . $initial : '';
}
if ($variable == 'issn' & & !empty($_REQUEST[$variable])) {
// ebsco sometimes sends garbage as issns verify this is a valid issn before displaying the value in the form.
return preg_match('/^\d{4}-?\d{3}[\dxX]$/', $_REQUEST[$variable]) ? Html::escape($_REQUEST[$variable]) : '';
return preg_match('/^\d{4}-?\d{3}[\dxX]$/', $_REQUEST[$variable]) ?Html::escape($_REQUEST[$variable]) : '';
}
// ebsco sometimes returns bookitem we only understand chapter
if (($variable == 'genre' & & !empty($_REQUEST[$variable])) & & $_REQUEST[$variable] == 'bookitem') {
return 'chapter';
}
return isset($_REQUEST[$variable]) ? Html::escape($_REQUEST[$variable]) : '';
return isset($_REQUEST[$variable]) ?Html::escape($_REQUEST[$variable]) : '';
}
function upei_roblib_ill_get_doi_from_request() {
function upei_roblib_ill_get_doi_from_request()
{
return (!empty($_REQUEST['doi'])) ? $_REQUEST['doi'] : upei_roblib_ill_get_doi_from_id();
}
function upei_roblib_ill_get_doi_from_id() {
function upei_roblib_ill_get_doi_from_id()
{
if (!empty($_REQUEST['ID'])) {
return (strpos($_REQUEST['ID'], 'doi:') !== 0) ? '' : $_REQUEST['ID'];
}
@ -120,7 +214,8 @@ function upei_roblib_ill_get_doi_from_id() {
* @return array
* The requestInfo portion of a Relais request
*/
function upei_roblib_ill_request_info_array($values, $notes) {
function upei_roblib_ill_request_info_array($values, $notes)
{
$requestInfo = [];
$genre = $values['Genre'];
$requestInfo['DateSubmitted'] = date('Y-m-d H:i:s');
@ -128,12 +223,12 @@ function upei_roblib_ill_request_info_array($values, $notes) {
if ($genre == 'book') {
$requestInfo['ServiceType'] = 'L';
$requestInfo['ServiceLabel'] = "R";
//$requestInfo['RequestSource'] = "C";
//$requestInfo['RequestSource'] = "C";
}
else {
$requestInfo['ServiceType'] = 'X';
$requestInfo['ServiceLabel'] = "R";
//$requestInfo['RequestSource'] = "C";
//$requestInfo['RequestSource'] = "C";
}
return $requestInfo;
}
@ -146,7 +241,8 @@ function upei_roblib_ill_request_info_array($values, $notes) {
*
* @return string
*/
function upei_roblib_format_biblio_info($form_state) {
function upei_roblib_format_biblio_info($form_state)
{
$values = $form_state->getValues();
$data = upei_roblib_ill_clean_array($values);
$rows = [];
@ -164,4 +260,4 @@ function upei_roblib_format_biblio_info($form_state) {
];
$html_output = \Drupal::service('renderer')->render($output);
return $html_output;
}
}