The libraries interlibrary loan form module
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.3 KiB

<?php
/**
* @file
* functions for database access.
*/
/**
* Log an ILL request.
*
* @param array $payload
* The request payload as an associative array (SharePoint webhook format).
* @param array|string $response
* The response from the ILL service or an error message.
*/
function upei_roblib_ill_log_request(array $payload, $response) {
$time_submitted = time();
$relais_message = is_array($response) ? ($response['ConfirmMessage'] ?? ($response['Problem']['Message'] ?? '')) : (string) $response;
// Parse patron name into first/last.
$patron_firstname = '';
$patron_lastname = '';
if (!empty($payload['patronName'])) {
$parts = explode(' ', $payload['patronName'], 2);
$patron_firstname = trim($parts[0] ?? '');
$patron_lastname = trim($parts[1] ?? '');
}
$connection = \Drupal::service('database');
try {
$connection->insert('upei_roblib_ill_request')
->fields([
'patron_id' => $payload['patronBarcode'] ?? '',
'patron_firstname' => $patron_firstname,
'patron_lastname' => $patron_lastname,
'patron_type' => $payload['patronType'] ?? '',
'patron_department' => $payload['patronDepartment'] ?? '',
'patron_email' => $payload['patronEmail'] ?? '',
'notes' => $payload['notes'] ?? '',
'genre' => $payload['format'] ?? '',
'doi' => $payload['doi_isbn'] ?? '',
'author' => $payload['author'] ?? '',
'citation_date' => $payload['year'] ?? '',
'title' => $payload['title'] ?? '',
'atitle' => $payload['articleTitle'] ?? '',
'issn' => $payload['issn'] ?? '',
'isbn' => $payload['isbn'] ?? '',
'article_author' => $payload['author'] ?? '',
'volume' => $payload['volume'] ?? '',
'issue' => $payload['issue'] ?? '',
'pages_requested' => $payload['pagesRequested'] ?? '',
'time_submitted' => $time_submitted,
'relais_request_id' => is_array($response) && isset($response['RequestNumber']) ? $response['RequestNumber'] : '-1',
'relais_message' => substr($relais_message, 0, 254),
])->execute();
} catch (Exception $e) {
\Drupal::logger('upei_roblib_ill')->error('Error logging ILL request @msg',
array(
'@msg' => $e->getMessage(),
));
}
}