diff --git a/public/refile/check.php b/public/refile/check.php index 07781ad..51cd8a1 100755 --- a/public/refile/check.php +++ b/public/refile/check.php @@ -1,22 +1,69 @@ - - +function almaGetXml($url) +{ + $ch = curl_init($url); + + if (!$ch) { + return array( + 'ok' => false, + 'status' => 0, + 'body' => '', + 'error' => 'Unable to initialize cURL.' + ); + } + + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_TIMEOUT, 20); + + $body = curl_exec($ch); + $error = curl_error($ch); + $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); + + curl_close($ch); + + if ($body === false) { + return array( + 'ok' => false, + 'status' => $status, + 'body' => '', + 'error' => $error !== '' ? $error : 'Unknown cURL error.' + ); + } + + return array( + 'ok' => ($status >= 200 && $status < 300), + 'status' => $status, + 'body' => $body, + 'error' => '' + ); +} +$getbarcode = isset($_GET['barcode']) ? trim($_GET['barcode']) : ''; +$postBarcode = isset($_POST['barcode']) ? trim($_POST['barcode']) : ''; +$formBarcode = ($postBarcode !== '') ? $postBarcode : $getbarcode; +?> + + Item Information - - + - - + +
@@ -25,17 +72,17 @@

Check Item Status by Barcode

-
- - - class="form-control" id="barcode" name="barcode" placeholder="Enter barcode" required> +
@@ -44,271 +91,224 @@ class="form-control" id="barcode" name="barcode" placeholder="Enter barcode" req
- Please enter a barcode.
"; + } else { + $candidateBarcodes = array(); -//////// Check if barcode uses X, remove it, check again, error message if nothing works. Uses function above - // Check if the URL redirects - $redirectUrl = getFinalRedirectUrl($url); + $barcodeWithX = $barcodeInput; + if (substr($barcodeWithX, -1) !== 'X') { + $barcodeWithX .= 'X'; + } + $candidateBarcodes[] = $barcodeWithX; - if ($redirectUrl) { - // echo "Success, ".$barcode." redirects to: " . $redirectUrl; - } else { - // Modify the item barcode by removing the 'X' and reconstruct the URL - $item_barcode_no_x = str_replace('X', '', $barcode); - $urlWithoutX = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/items?item_barcode=" . $item_barcode_no_x . "&apikey=" . $api_key; - - // Check if the modified URL redirects - $redirectUrl = getFinalRedirectUrl($urlWithoutX); - // if redirect works, set new $url - if ($redirectUrl) { - // echo "Removed the X, and ".$item_barcode_no_x." works: " . $redirectUrl; - $url = $urlWithoutX; - } else { - echo "
Item record does not exist
"; - exit; + if (strtolower(substr($barcodeInput, 0, 1)) === 'p' && strlen($barcodeInput) === 6) { + $candidateBarcodes[] = $barcodeInput . 'X'; } - } -///// end check barcode - // Initialize cURL session for the first request - $ch = curl_init(); + $barcodeWithoutX = str_replace('X', '', $barcodeWithX); + if ($barcodeWithoutX !== '') { + $candidateBarcodes[] = $barcodeWithoutX; + } - // Set cURL options - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects - curl_setopt($ch, CURLOPT_HEADER, false); // Exclude headers in output + $candidateBarcodes = array_values(array_unique($candidateBarcodes)); - // Execute the cURL session - $response = curl_exec($ch); + $baseApi = 'https://api-na.hosted.exlibrisgroup.com/almaws/v1/items?item_barcode='; + $itemResponse = null; + $url = ''; + $xml = false; - // Check for errors - if ($response === false) { - echo "
cURL Error: " . curl_error($ch) . "
"; - } else { - // Load the response into a SimpleXML object for easy parsing - $xml = simplexml_load_string($response); + foreach ($candidateBarcodes as $candidateBarcode) { + $testUrl = $baseApi . rawurlencode($candidateBarcode) . '&apikey=' . rawurlencode($api_key); + $response = almaGetXml($testUrl); - if ($xml === false) { + if ($response['ok'] && trim($response['body']) !== '') { + $testXml = @simplexml_load_string($response['body']); - echo "
Failed to parse XML response.
"; - } else { + if ($testXml !== false && isset($testXml->item_data->barcode)) { + $itemResponse = $response; + $url = $testUrl; + $xml = $testXml; + break; + } + } + } - // Extract , <barcode>, <internal_note_3>, and additional data - $title = (string) $xml->bib_data->title; - $item_barcode = (string) $xml->item_data->barcode; - $internalNote3 = (string) $xml->item_data->internal_note_3; - $internalNote1 = (string) $xml->item_data->internal_note_1; - $description = (string) $xml->item_data->description; - $in_temp_location = (string) $xml->holding_data->in_temp_location; - $temp_library = (string) $xml->holding_data->temp_library; - $temp_location = (string) $xml->holding_data->temp_location; - $provenance = (string) $xml->item_data->provenance; - $provenance_desc = (string) $xml->item_data->provenance['desc']; - $process_type = (string) $xml->item_data->process_type; - $holding_id = (string) $xml->holding_data->holding_id; - $pid = (string) $xml->item_data->pid; - $mms_id = (string) $xml->bib_data->mms_id; - $modification_date = (string) $xml->item_data->modification_date; + if ($xml === false) { + echo "<div class='alert alert-danger mt-3 text-center'>Item record does not exist</div>"; + } else { + $title = isset($xml->bib_data->title) ? (string)$xml->bib_data->title : ''; + $item_barcode = isset($xml->item_data->barcode) ? (string)$xml->item_data->barcode : ''; + $internalNote3 = isset($xml->item_data->internal_note_3) ? (string)$xml->item_data->internal_note_3 : ''; + $internalNote1 = isset($xml->item_data->internal_note_1) ? (string)$xml->item_data->internal_note_1 : ''; + $description = isset($xml->item_data->description) ? (string)$xml->item_data->description : ''; + $in_temp_location = isset($xml->holding_data->in_temp_location) ? (string)$xml->holding_data->in_temp_location : ''; + $temp_library = isset($xml->holding_data->temp_library) ? (string)$xml->holding_data->temp_library : ''; + $temp_location = isset($xml->holding_data->temp_location) ? (string)$xml->holding_data->temp_location : ''; + $provenance_desc = isset($xml->item_data->provenance['desc']) ? (string)$xml->item_data->provenance['desc'] : ''; + $process_type = isset($xml->item_data->process_type) ? (string)$xml->item_data->process_type : ''; + $holding_id = isset($xml->holding_data->holding_id) ? (string)$xml->holding_data->holding_id : ''; + $pid = isset($xml->item_data->pid) ? (string)$xml->item_data->pid : ''; + $mms_id = isset($xml->bib_data->mms_id) ? (string)$xml->bib_data->mms_id : ''; + $modification_date = isset($xml->item_data->modification_date) ? (string)$xml->item_data->modification_date : ''; $modification_date = str_replace('Z', '', $modification_date); - // Display the results using Bootstrap styling echo "<div class='row mt-3'> - <div class='col-md-8 offset-md-2'> - <div class='card bg-light'> - <div class='card-header bg-dark text-white text-center'> - <h4>Item Details for Barcode: $item_barcode</h4> - </div> - <div class='card-body'>"; - - echo '<h6 class="text-center alert alert-success" >Item Information</h6><ul class="list-group">'; - echo "<ul class='list-group'>"; - echo " <li class='list-group-item'><strong>Title:</strong> " . htmlspecialchars($title) . "</li>"; - echo "<li class='list-group-item'><strong>Barcode:</strong> " . htmlspecialchars($item_barcode) . "</li>"; - echo "<li class='list-group-item'><strong>Tray/Shelf Barcode:</strong> " . htmlspecialchars($internalNote1) . "</li>"; - echo "<li class='list-group-item'><strong>Description:</strong> " . htmlspecialchars($description) . "</li>"; - echo "<li class='list-group-item'><strong>MMS ID:</strong> " . htmlspecialchars($mms_id) . "</li>"; - echo "<li class='list-group-item'><strong>Owning Library:</strong> " . htmlspecialchars($provenance_desc) . "</li>"; - echo "<li class='list-group-item'><strong>Last Modified:</strong> " . htmlspecialchars($modification_date) . "</li>"; - - echo '</ul><br /><h6 class="text-center alert alert-primary">Refile Information</h6><ul class="list-group">'; - echo "<li class='list-group-item'><strong>Internal Note 3:</strong> " . htmlspecialchars($internalNote3) . "</li>"; - echo "<li class='list-group-item'><strong>In Temp Location:</strong> " . htmlspecialchars($in_temp_location) . "</li>"; - echo "<li class='list-group-item'><strong>Temp Location:</strong> " . htmlspecialchars($temp_location) . "</li>"; - echo "<li class='list-group-item'><strong>Temp Library:</strong> " . htmlspecialchars($temp_library) . "</li>"; - echo "<li class='list-group-item'><strong>Process Type:</strong> <span class='text-danger'>" . htmlspecialchars($process_type) . "</span></li>"; - - // Second API call to fetch loan details - $loan_url = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/" . $mms_id . "/holdings/" . $holding_id . "/items/" . $pid . "/loans?apikey=" . $api_key; - curl_setopt($ch, CURLOPT_URL, $loan_url); - $loan_response = curl_exec($ch); - - if ($loan_response === false) { - echo "<div class='alert alert-danger mt-3'>cURL Error: " . curl_error($ch) . "</div>"; - } else { - // Load the response into a SimpleXML object - $loan_xml = simplexml_load_string($loan_response); - - if ($loan_xml === false) { + <div class='col-md-8 offset-md-2'> + <div class='card bg-light'> + <div class='card-header bg-dark text-white text-center'> + <h4>Item Details for Barcode: " . h($item_barcode) . "</h4> + </div> + <div class='card-body'>"; - echo "<div class='alert alert-danger mt-3'>Failed to parse loan XML response.</div>"; + echo '<h6 class="text-center alert alert-success">Item Information</h6>'; + echo "<ul class='list-group'>"; + echo "<li class='list-group-item'><strong>Title:</strong> " . h($title) . "</li>"; + echo "<li class='list-group-item'><strong>Barcode:</strong> " . h($item_barcode) . "</li>"; + echo "<li class='list-group-item'><strong>Tray/Shelf Barcode:</strong> " . h($internalNote1) . "</li>"; + echo "<li class='list-group-item'><strong>Description:</strong> " . h($description) . "</li>"; + echo "<li class='list-group-item'><strong>MMS ID:</strong> " . h($mms_id) . "</li>"; + echo "<li class='list-group-item'><strong>Owning Library:</strong> " . h($provenance_desc) . "</li>"; + echo "<li class='list-group-item'><strong>Last Modified:</strong> " . h($modification_date) . "</li>"; + echo "</ul>"; + + echo '<br><h6 class="text-center alert alert-primary">Refile Information</h6>'; + echo "<ul class='list-group'>"; + echo "<li class='list-group-item'><strong>Internal Note 3:</strong> " . h($internalNote3) . "</li>"; + echo "<li class='list-group-item'><strong>In Temp Location:</strong> " . h($in_temp_location) . "</li>"; + echo "<li class='list-group-item'><strong>Temp Location:</strong> " . h($temp_location) . "</li>"; + echo "<li class='list-group-item'><strong>Temp Library:</strong> " . h($temp_library) . "</li>"; + echo "<li class='list-group-item'><strong>Process Type:</strong> <span class='text-danger'>" . h($process_type) . "</span></li>"; + + $loan_url = ''; + + if ($mms_id !== '' && $holding_id !== '' && $pid !== '') { + $loan_url = 'https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/' + . rawurlencode($mms_id) + . '/holdings/' . rawurlencode($holding_id) + . '/items/' . rawurlencode($pid) + . '/loans?apikey=' . rawurlencode($api_key); + + $loanResponse = almaGetXml($loan_url); + + if (!$loanResponse['ok']) { + echo "<div class='alert alert-danger mt-3'>Loan API Error"; + if ($loanResponse['status'] > 0) { + echo " (HTTP " . (int)$loanResponse['status'] . ")"; + } + echo ".</div>"; } else { - // Check if the item is checked in (no loans) - $total_record_count = (int) $loan_xml['total_record_count']; - if ($total_record_count === 0) { - echo '<li class="list-group-item"><strong>Status:</strong> <span class="text-success">Item Checked In</span></li>'; - } else { - - // Access the single <item_loan> elements if item is still checked out - $item_loan = $loan_xml->item_loan; - - if ($item_loan) { - // Extract the required values from the single item_loan element - $due_date = (string) $item_loan->due_date; - $loan_status = (string) $item_loan->loan_status; - $process_status = (string) $item_loan->process_status; - $user_id = (string) $item_loan->user_id; - - // Display the extracted values - if ($loan_status == 'Active') { - echo " - - </ul> - <br /><h6 class='text-center alert alert-info'>Loan Information</h6><ul class='list-group'> - - <li class='list-group-item'><strong>Loan Status: </strong><span class='text-danger font-italic'>Checked Out</span></li>"; - } else { - echo "<li class='list-group-item'><strong>Loan Status:</strong> " . htmlspecialchars($loan_status) . "</li>"; - } + $loan_xml = @simplexml_load_string($loanResponse['body']); -// Mapping user_id to "Deliver To" values - $deliverToMapping = [ - "01WRLC_AMU-UNIV_LIB" => "AU", - "01WRLC_AMULAW-PLL" => "AULAW", - "01WRLC_CAA-CUMULLEN" => "CU", - "01WRLC_CAA-CUOLL" => "CU (for Lima)", - "01WRLC_DOC-DCVN" => "DC", - "01WRLC_DOCLAW-UDCLAW" => "DCLaw", - "01WRLC_GAL-GALLAUDET" => "GA", - "01WRLC_GML-FENWICK" => "GM", - "01WRLC_GML-ARLINGTON" => "GMA", - "01WRLC_GML-MERCER" => "GMP", - "01WRLC_GUNIV-lau" => "GT", - "01WRLC_GUNIV-qatar" => "GT", - "01WRLC_GUNIV-kie" => "GT-Bioethics", - "01WRLC_GUNIV-bfcsc" => "GT-Booth", - "01WRLC_GUNIV-mccourt" => "GT-McCourt", - "01WRLC_GUNIV-maindel" => "GT-OD", - "01WRLC_GUNIV-scs" => "GT-SCS", - "01WRLC_GUNIV-wdst" => "GT-WTL", - "01WRLC_GUNIV-sci" => "GTB", - "01WRLC_GUNIVLAW-GUL" => "GTL", - "01WRLC_GWA-gelman" => "GW", - "01WRLC_GWA-SCRC" => "GW-SC", - "01WRLC_GWA-eckles" => "GWE", - "01WRLC_GWA-vstcl" => "GWN", - "01WRLC_GWAHLTH-GW_VSTC_Library_Delivery" => "GWN", - "01WRLC_GWA-zOffCampus" => "GWOC", - "01WRLC_GWAHLTH-GWAHLTH" => "HI", - "01WRLC_SCF-SCF" => "HQ", - "01WRLC_HOW-HUF" => "HU", - "01WRLC_HOW-HUB" => "HU", - "01WRLC_HOW-HUMS" => "HU", - "01WRLC_HOW-HUS" => "HU", - "01WRLC_HOW-HULS" => "HU-HS", - "01WRLC_HOW-HL" => "HUWC", - "01WRLC_GWALAW-GWALAW" => "JB", - "01WRLC_MAR-main" => "MU", - "01WRLC_MAR-bcle" => "MUB", - "01WRLC_SCF-Sheehan Library" => "TR", - ]; - -// Sample $user_id variable - //$user_id = "01WRLC_AMU-UNIV_LIB"; - -// Check if $user_id exists in the mapping and display the corresponding "Deliver To" value - if (isset($deliverToMapping[$user_id])) { - echo "<li class='list-group-item'><strong>Borrower:</strong> <span class='badge badge-warning' style='font-size:1.2em;'>Deliver To: " . $deliverToMapping[$user_id] . "</span></li>"; + if ($loan_xml === false) { + echo "<div class='alert alert-danger mt-3'>Failed to parse loan XML response.</div>"; + } else { + $total_record_count = isset($loan_xml['total_record_count']) ? (int)$loan_xml['total_record_count'] : 0; + if ($total_record_count === 0) { + echo '<li class="list-group-item"><strong>Status:</strong> <span class="text-success">Item Checked In</span></li>'; + } else { + $item_loan = isset($loan_xml->item_loan) ? $loan_xml->item_loan : null; + + if ($item_loan) { + $due_date = isset($item_loan->due_date) ? (string)$item_loan->due_date : ''; + $loan_status = isset($item_loan->loan_status) ? (string)$item_loan->loan_status : ''; + $process_status = isset($item_loan->process_status) ? (string)$item_loan->process_status : ''; + $user_id = isset($item_loan->user_id) ? (string)$item_loan->user_id : ''; + + echo "</ul>"; + echo "<br><h6 class='text-center alert alert-info'>Loan Information</h6>"; + echo "<ul class='list-group'>"; + + if ($loan_status === 'Active') { + echo "<li class='list-group-item'><strong>Loan Status: </strong><span class='text-danger font-italic'>Checked Out</span></li>"; + } else { + echo "<li class='list-group-item'><strong>Loan Status:</strong> " . h($loan_status) . "</li>"; + } + + $deliverToMapping = array( + "01WRLC_AMU-UNIV_LIB" => "AU", + "01WRLC_AMULAW-PLL" => "AULAW", + "01WRLC_CAA-CUMULLEN" => "CU", + "01WRLC_CAA-CUOLL" => "CU (for Lima)", + "01WRLC_DOC-DCVN" => "DC", + "01WRLC_DOCLAW-UDCLAW" => "DCLaw", + "01WRLC_GAL-GALLAUDET" => "GA", + "01WRLC_GML-FENWICK" => "GM", + "01WRLC_GML-ARLINGTON" => "GMA", + "01WRLC_GML-MERCER" => "GMP", + "01WRLC_GUNIV-lau" => "GT", + "01WRLC_GUNIV-qatar" => "GT", + "01WRLC_GUNIV-kie" => "GT-Bioethics", + "01WRLC_GUNIV-bfcsc" => "GT-Booth", + "01WRLC_GUNIV-mccourt" => "GT-McCourt", + "01WRLC_GUNIV-maindel" => "GT-OD", + "01WRLC_GUNIV-scs" => "GT-SCS", + "01WRLC_GUNIV-wdst" => "GT-WTL", + "01WRLC_GUNIV-sci" => "GTB", + "01WRLC_GUNIVLAW-GUL" => "GTL", + "01WRLC_GWA-gelman" => "GW", + "01WRLC_GWA-SCRC" => "GW-SC", + "01WRLC_GWA-eckles" => "GWE", + "01WRLC_GWA-vstcl" => "GWN", + "01WRLC_GWAHLTH-GW_VSTC_Library_Delivery" => "GWN", + "01WRLC_GWA-zOffCampus" => "GWOC", + "01WRLC_GWAHLTH-GWAHLTH" => "HI", + "01WRLC_SCF-SCF" => "HQ", + "01WRLC_HOW-HUF" => "HU", + "01WRLC_HOW-HUB" => "HU", + "01WRLC_HOW-HUMS" => "HU", + "01WRLC_HOW-HUS" => "HU", + "01WRLC_HOW-HULS" => "HU-HS", + "01WRLC_HOW-HL" => "HUWC", + "01WRLC_GWALAW-GWALAW" => "JB", + "01WRLC_MAR-main" => "MU", + "01WRLC_MAR-bcle" => "MUB", + "01WRLC_SCF-Sheehan Library" => "TR" + ); + + if (isset($deliverToMapping[$user_id])) { + echo "<li class='list-group-item'><strong>Borrower:</strong> <span class='badge badge-warning' style='font-size:1.2em;'>Deliver To: " . h($deliverToMapping[$user_id]) . "</span></li>"; + } else { + echo "<li class='list-group-item'><strong>Borrower (User ID):</strong> " . h($user_id) . "</li>"; + } + + echo "<li class='list-group-item'><strong>Due Date:</strong> " . h($due_date) . "</li>"; + echo "<li class='list-group-item'><strong>Process Status:</strong> " . h($process_status) . "</li>"; } else { - echo "<li class='list-group-item'><strong>Borrower (User ID):</strong> " . htmlspecialchars($user_id) . "</li>"; + echo "<li class='list-group-item text-danger'>No item_loan found in the XML response.</li>"; } - - // echo "<li class='list-group-item'><strong>User ID (Borrower):</strong> " . htmlspecialchars($user_id) . "</li>"; - echo "<li class='list-group-item'><strong>Due Date:</strong> " . htmlspecialchars($due_date) . "</li>"; - echo "<li class='list-group-item'><strong>Process Status:</strong> " . htmlspecialchars($process_status) . "</li>"; - } else { - echo 'No item_loan found in the XML response.'; } } } + } else { + echo "<li class='list-group-item text-warning'>Loan lookup unavailable: missing bib/item identifiers.</li>"; } - echo "</ul>"; // End of the list - echo " - <br /> - <div class='btn-group mb-4 offset-md-4' style='width:240px;'> - <a class='btn btn-info btn-sm' style='width:120px;' target='_blank' href='" . $url . "'>XML</a> - <a class='btn btn-warning btn-sm' style='width:120px;' target='_blank' href='" . $loan_url . "'>Loan XML</a></div> - <p class='text-center'>Does everything look ok?</p> - <a class='btn btn-danger offset-md-5 mb-2' style='width:130px;' href='check_in.php?barcode=" . htmlspecialchars($item_barcode) . "'>Check-In Item</a> - </div> - </div> - </div> - </div>"; + echo "</ul>"; + echo "<br>"; + echo "<div class='btn-group mb-4 offset-md-4' style='width:240px;'>"; + echo "<a class='btn btn-info btn-sm' style='width:120px;' target='_blank' href='" . h($url) . "'>XML</a>"; + if ($loan_url !== '') { + echo "<a class='btn btn-warning btn-sm' style='width:120px;' target='_blank' href='" . h($loan_url) . "'>Loan XML</a>"; + } + echo "</div>"; + echo "<p class='text-center'>Does everything look ok?</p>"; + echo "<a class='btn btn-danger offset-md-5 mb-2' style='width:130px;' href='check_in.php?barcode=" . rawurlencode($item_barcode) . "'>Check-In Item</a>"; + echo "</div></div></div></div>"; } } - // Close the cURL session - curl_close($ch); } - ?> - <p class="text-center">Example barcode: 32882012348937X</p> + <p class="text-center">Example barcode: 32882012348937X</p> </div> - - - - <!-- Bootstrap JS, Popper.js, and jQuery --> - <?php include 'include/footer.php';?> + <?php include 'include/footer.php'; ?> </body> - </html> \ No newline at end of file diff --git a/public/refile/refile_update.php b/public/refile/refile_update.php index f4a57cf..6c9e55f 100755 --- a/public/refile/refile_update.php +++ b/public/refile/refile_update.php @@ -1,17 +1,155 @@ -<?php include 'include/access.php';?><!DOCTYPE html> +<?php +include 'include/access.php'; +include 'include/apikey.php'; + +function h($value) +{ + return htmlspecialchars((string)(isset($value) ? $value : ''), ENT_QUOTES, 'UTF-8'); +} + +function almaRequest($url, $method = 'GET', $body = null, $headers = array()) +{ + $ch = curl_init(); + + if (!$ch) { + return array( + 'ok' => false, + 'status' => 0, + 'body' => '', + 'error' => 'Unable to initialize cURL.' + ); + } + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + + if ($method === 'PUT') { + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); + } elseif ($method === 'POST') { + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); + } + + if (!empty($headers)) { + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + } + + $responseBody = curl_exec($ch); + $curlError = curl_error($ch); + $statusCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); + + curl_close($ch); + + if ($responseBody === false) { + return array( + 'ok' => false, + 'status' => $statusCode, + 'body' => '', + 'error' => $curlError !== '' ? $curlError : 'Unknown cURL error.' + ); + } + + return array( + 'ok' => ($statusCode >= 200 && $statusCode < 300), + 'status' => $statusCode, + 'body' => $responseBody, + 'error' => '' + ); +} + +function loadXmlFromResponse($response) +{ + if (!is_array($response) || empty($response['ok']) || trim($response['body']) === '') { + return false; + } + + return @simplexml_load_string($response['body']); +} + +function findItemXmlByBarcode($barcodeInput, $apiKey) +{ + $candidateBarcodes = array(); + + $barcodeInput = trim((string)$barcodeInput); + + if ($barcodeInput === '') { + return array( + 'ok' => false, + 'url' => '', + 'xml' => false, + 'error' => 'Blank barcode.' + ); + } + + $barcodeWithX = $barcodeInput; + if (substr($barcodeWithX, -1) !== 'X') { + $barcodeWithX .= 'X'; + } + $candidateBarcodes[] = $barcodeWithX; + + if (strtolower(substr($barcodeInput, 0, 1)) === 'p' && strlen($barcodeInput) === 6) { + $candidateBarcodes[] = $barcodeInput . 'X'; + } + + $barcodeWithoutX = str_replace('X', '', $barcodeWithX); + if ($barcodeWithoutX !== '') { + $candidateBarcodes[] = $barcodeWithoutX; + } + + $candidateBarcodes = array_values(array_unique($candidateBarcodes)); + + foreach ($candidateBarcodes as $candidateBarcode) { + $url = 'https://api-na.hosted.exlibrisgroup.com/almaws/v1/items?item_barcode=' + . rawurlencode($candidateBarcode) + . '&apikey=' . rawurlencode($apiKey); + + $response = almaRequest($url, 'GET'); + $xml = loadXmlFromResponse($response); + + if ($xml !== false && isset($xml->item_data->barcode)) { + return array( + 'ok' => true, + 'url' => $url, + 'xml' => $xml, + 'error' => '' + ); + } + } + + return array( + 'ok' => false, + 'url' => '', + 'xml' => false, + 'error' => 'Item record does not exist.' + ); +} + +$barcodeValue = isset($_POST['barcode']) ? trim($_POST['barcode']) : (isset($_GET['barcode']) ? trim($_GET['barcode']) : ''); +$trayBarcodeValue = isset($_POST['trayBarcode']) ? trim($_POST['trayBarcode']) : (isset($_GET['trayBarcode']) ? trim($_GET['trayBarcode']) : ''); + +$rows = array(); +$name = ''; + +if (isset($_SESSION['user_id'])) { + $name = $_SESSION['user_id']; +} elseif (isset($GLOBALS['name'])) { + $name = $GLOBALS['name']; +} +?><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Item Information - - + - - + +
@@ -21,19 +159,26 @@

Barcode Entry

-
- +
- +
@@ -42,27 +187,12 @@
-

Item Details

@@ -75,418 +205,301 @@ Status '; -//// function to check the URL. Add above code. - function getFinalRedirectUrl($url) - { - // Initialize cURL session - $ch = curl_init($url); - // Set cURL options to follow redirects - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADER, true); - // Execute cURL request - curl_exec($ch); - // Check the final URL after following redirects - $finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); - // Close cURL session - curl_close($ch); - // Return final URL if it is different from the input URL, indicating a redirect - return $finalUrl !== $url ? $finalUrl : false; - } -//// End Function - - // API URL to get item data - $url = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/items?item_barcode=" . $barcode . "&apikey=" . $api_key; - - //////// Check if barcode uses X, remove it, check again, error message if nothing works. Uses function above - // Check if the URL redirects - $redirectUrl = getFinalRedirectUrl($url); + $itemLookup = findItemXmlByBarcode($barcode, $api_key); - if ($redirectUrl) { - // echo "Success, ".$barcode." redirects to: " . $redirectUrl; + if (!$itemLookup['ok'] || $itemLookup['xml'] === false) { + echo '
'; + echo "
Item record does not exist
"; } else { - // Modify the item barcode by removing the 'X' and reconstruct the URL - $item_barcode_no_x = str_replace('X', '', $barcode); - $urlWithoutX = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/items?item_barcode=" . $item_barcode_no_x . "&apikey=" . $api_key; - - // Check if the modified URL redirects - $redirectUrl = getFinalRedirectUrl($urlWithoutX); - // if redirect works, set new $url - if ($redirectUrl) { - // echo "Removed the X, and ".$item_barcode_no_x." works: " . $redirectUrl; - $url = $urlWithoutX; + $xml = $itemLookup['xml']; + $url = $itemLookup['url']; + + $title = isset($xml->bib_data->title) ? (string)$xml->bib_data->title : ''; + $item_barcode = isset($xml->item_data->barcode) ? (string)$xml->item_data->barcode : ''; + $internalNote1 = isset($xml->item_data->internal_note_1) ? (string)$xml->item_data->internal_note_1 : ''; + $internalNote1 = substr($internalNote1, 0, 12); + $internalNote3 = isset($xml->item_data->internal_note_3) ? (string)$xml->item_data->internal_note_3 : ''; + $mms_id = isset($xml->bib_data->mms_id) ? (string)$xml->bib_data->mms_id : ''; + $holding_id = isset($xml->holding_data->holding_id) ? (string)$xml->holding_data->holding_id : ''; + $pid = isset($xml->item_data->pid) ? (string)$xml->item_data->pid : ''; + + $loan_url = ''; + $total_record_count = 0; + $status = ''; + $due_date = ''; + $process_status = ''; + + if ($mms_id !== '' && $holding_id !== '' && $pid !== '') { + $loan_url = 'https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/' + . rawurlencode($mms_id) + . '/holdings/' . rawurlencode($holding_id) + . '/items/' . rawurlencode($pid) + . '/loans?apikey=' . rawurlencode($api_key); + + $loanResponse = almaRequest($loan_url, 'GET'); + $loan_xml = loadXmlFromResponse($loanResponse); + + if ($loan_xml === false) { + $status = 'Loan status unavailable'; + } else { + $total_record_count = isset($loan_xml['total_record_count']) ? (int)$loan_xml['total_record_count'] : 0; + + if ($total_record_count === 0) { + $status = 'Item Checked In'; + } else { + $item_loan = isset($loan_xml->item_loan) ? $loan_xml->item_loan : null; + + if ($item_loan) { + $status = 'Checked Out'; + $due_date = isset($item_loan->due_date) ? (string)$item_loan->due_date : ''; + $process_status = isset($item_loan->process_status) ? (string)$item_loan->process_status : ''; + } else { + $status = 'Loan status unavailable'; + } + } + } } else { - echo "
Item record does not exist
"; - exit; + $status = 'Loan status unavailable'; } - } -///// end check barcode - // Initialize cURL session for the first request - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADER, false); - - // Execute the cURL session and fetch the response - $response = curl_exec($ch); - - if ($response === false) { - echo "
cURL Error: " . curl_error($ch) . "
"; - exit; - } - - // Load the response into a SimpleXML object for easy parsing - $xml = simplexml_load_string($response); - - if ($xml === false) { - echo "
Failed to parse XML response for item information.
"; - exit; - } - - // Extract relevant fields - $title = (string) $xml->bib_data->title; - $item_barcode = (string) $xml->item_data->barcode; - $internalNote1 = (string) $xml->item_data->internal_note_1; - $internalNote1 = substr($internalNote1, 0, 12); - $internalNote3 = (string) $xml->item_data->internal_note_3; - $mms_id = (string) $xml->bib_data->mms_id; - $holding_id = (string) $xml->holding_data->holding_id; - $pid = (string) $xml->item_data->pid; - - // Second API call to get loan details - $loan_url = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/" . $mms_id . "/holdings/" . $holding_id . "/items/" . $pid . "/loans?apikey=" . $api_key; - - // $loan_url = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/" . $mms_id . "/loans?apikey=" . $api_key; - curl_setopt($ch, CURLOPT_URL, $loan_url); - $loan_response = curl_exec($ch); - - if ($loan_response === false) { - echo "
cURL Error: " . curl_error($ch) . "
"; - exit; - } - - // Load the loan response into a SimpleXML object - $loan_xml = simplexml_load_string($loan_response); - - if ($loan_xml === false) { - echo "
Failed to parse loan XML response for loan details.
"; - exit; - } - - // Check if the item is checked in (no loans) - $total_record_count = (int) $loan_xml['total_record_count']; - $status = ''; - $due_date = ''; - $process_status = ''; - - if ($total_record_count === 0) { - $status = 'Item Checked In'; - } else { - // Extract loan details if item is checked out - $item_loan = $loan_xml->item_loan; - if ($item_loan) { - $status = 'Checked Out'; - $due_date = (string) $item_loan->due_date; - $process_status = (string) $item_loan->process_status; + $rows[] = array( + 'trayBarcode' => $trayBarcode, + 'title' => $title, + 'item_barcode' => $item_barcode, + 'internalNote1' => $internalNote1, + 'internalNote3' => $internalNote3, + 'mms_id' => $mms_id, + 'holding_id' => $holding_id, + 'pid' => $pid, + ); + + echo ""; + + if ($trayBarcode === $internalNote1) { + echo '' . h($trayBarcode) . ' - Match'; + } else { + if (!empty($trayBarcode) && !empty($internalNote1)) { + $barcodeFile = $trayBarcode; + $barcodeAlma = $internalNote1; + $itembarcode = $item_barcode; + + $google_form_url = "https://docs.google.com/forms/u/0/d/e/1FAIpQLSfdqhD8VPq8X13niOSL-y7146PkmYtzJW0v7U-Sr94EmJOtyA/formResponse"; + + $form_data = array( + "entry.1671538415" => $barcodeFile, + "entry.1478552555" => $barcodeAlma, + "entry.860961451" => $itembarcode, + ); + + $googleResponse = almaRequest( + $google_form_url, + 'POST', + http_build_query($form_data) + ); + + echo '' + . h($trayBarcode) . ' (from File) - ' + . h($internalNote1) . ' (from Alma) - Does Not Match.
' + . 'Mismatch Recorded' + . '
'; + } else { + $missing = array(); + + if (empty($trayBarcode)) { + $missing[] = 'trayBarcode (from File)'; + } + + if (empty($internalNote1)) { + $missing[] = 'internalNote1 (from Alma)'; + } + + $missing_message = implode(' and ', $missing) . ' missing'; + + $barcodeFile = !empty($trayBarcode) ? $trayBarcode : 'MISSING'; + $barcodeAlma = !empty($internalNote1) ? $internalNote1 : 'MISSING'; + $itembarcode = $item_barcode; + + $google_form_url = "https://docs.google.com/forms/u/0/d/e/1FAIpQLSfdqhD8VPq8X13niOSL-y7146PkmYtzJW0v7U-Sr94EmJOtyA/formResponse"; + + $form_data = array( + "entry.1671538415" => $barcodeFile, + "entry.1478552555" => $barcodeAlma, + "entry.860961451" => $itembarcode, + ); + + $googleResponse = almaRequest( + $google_form_url, + 'POST', + http_build_query($form_data) + ); + + echo 'Error: ' + . h($missing_message) + . '.
Missing Data Recorded
'; + } } - } - // Store the row data for later use - $rows[] = [ - 'trayBarcode' => $trayBarcode, - 'title' => $title, - 'item_barcode' => $item_barcode, - 'internalNote1' => $internalNote1, - 'internalNote3' => $internalNote3, - 'mms_id' => $mms_id, - 'holding_id' => $holding_id, - 'pid' => $pid, - ]; - - // Display the results in table rows - echo ""; - - if ($trayBarcode == $internalNote1) { - echo - '' . htmlspecialchars($trayBarcode) . ' - Match'; - } else { - -///////If mismatch, save the info to a Google Spreadsheet -if (!empty($trayBarcode) && !empty($internalNote1)) { - - if ($trayBarcode == $internalNote1) { - // Both available and match - echo '' . htmlspecialchars($trayBarcode) . ' - Match'; - } else { - // Both available but do not match - $barcode = $trayBarcode; - $barcodeAlma = $internalNote1; - $itembarcode = $item_barcode; - - // Google Form URL - $google_form_url = "https://docs.google.com/forms/u/0/d/e/1FAIpQLSfdqhD8VPq8X13niOSL-y7146PkmYtzJW0v7U-Sr94EmJOtyA/formResponse"; - - // Google Form fields - $form_data = [ - "entry.1671538415" => $barcode, - "entry.1478552555" => $barcodeAlma, - "entry.860961451" => $itembarcode, - ]; - - // Initialize cURL session - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $google_form_url); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($form_data)); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - - // Execute cURL session - $response = curl_exec($ch); - curl_close($ch); - - echo '' . htmlspecialchars($trayBarcode) . ' (from File) - ' . htmlspecialchars($internalNote1) . ' (from Alma) - Does Not Match.
Mismatch Recorded
'; - } - -} else { - - // One or both variables are missing - $missing = []; + echo "" . h($title) . ""; + echo "" . h($item_barcode) . ""; + echo "" . h($internalNote3) . ""; - if (empty($trayBarcode)) { - $missing[] = 'trayBarcode (from File)'; - } - - if (empty($internalNote1)) { - $missing[] = 'internalNote1 (from Alma)'; - } - - $missing_message = implode(' and ', $missing) . ' missing'; - - $barcode = !empty($trayBarcode) ? $trayBarcode : 'MISSING'; - $barcodeAlma = !empty($internalNote1) ? $internalNote1 : 'MISSING'; - $itembarcode = $item_barcode; - - // Google Form URL - $google_form_url = "https://docs.google.com/forms/u/0/d/e/1FAIpQLSfdqhD8VPq8X13niOSL-y7146PkmYtzJW0v7U-Sr94EmJOtyA/formResponse"; - - // Google Form fields - $form_data = [ - "entry.1671538415" => $barcode, - "entry.1478552555" => $barcodeAlma, - "entry.860961451" => $itembarcode, - ]; - - // Initialize cURL session - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $google_form_url); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($form_data)); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - - // Execute cURL session - $response = curl_exec($ch); - curl_close($ch); - - echo 'Error: ' . htmlspecialchars($missing_message) . '.
Missing Data Recorded
'; -}} - - echo "" . htmlspecialchars($title) . ""; - echo "" . htmlspecialchars($item_barcode) . ""; - echo "" . htmlspecialchars($internalNote3) . ""; - - if ($total_record_count !== 0) { - echo "" . $status . "
Due: " . htmlspecialchars($due_date) . "
Processing Data: " . htmlspecialchars($process_status) . "
"; - } else { - echo "" . $status . ""; - } - - echo ""; - - echo ''; - echo ''; - - // Close the cURL session - curl_close($ch); + if ($total_record_count !== 0) { + echo "" . $status . "
Due: " . h($due_date) . "
Processing Data: " . h($process_status) . "
"; + } else { + echo "" . $status . ""; + } - // Show Proceed button if rows were fetched - if (!empty($rows)) { - echo '
'; - echo ''; - echo ''; - echo ''; - echo ''; - echo ' Clear'; - echo '
'; + echo ""; + echo ''; + echo ''; + + if (!empty($rows)) { + echo '
'; + echo ''; + echo ''; + echo ''; + echo ''; + echo ' Clear'; + echo '
'; + } } } -// Handle the Proceed action if (isset($_POST['proceed'])) { - // $rows = unserialize($_POST['rows']); - $mms_id = $_POST['mms_id']; - $holding_id = $_POST['holding_id']; - $pid = $_POST['pid']; - $barcode = $_POST['barcode']; - - // foreach ($rows as $row) { - // Construct the URL for GET request - $getUrl = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/" . $mms_id . "/holdings/" . $holding_id . "/items/" . $pid . "?apikey=" . $api_key; - - // Initialize cURL for the GET request - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $getUrl); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - $getResponse = curl_exec($ch); - curl_close($ch); - - // Load the response into a SimpleXML object - $xml = simplexml_load_string($getResponse); - - // Update the field to blank - $internalNote3 = $xml->item_data->internal_note_3; - $internalNote3[0] = ''; // Update the value to blank - - //Modify Temp Location fields to reset - // In temp location - true or false - $custom_in_temp_location = 'false'; - $xml->holding_data->in_temp_location = $custom_in_temp_location; - - // In temp location - true or false - $custom_temp_library = ''; - $xml->holding_data->temp_library = $custom_temp_library; - - // In temp location - true or false - $custom_temp_location = ''; - $xml->holding_data->temp_location = $custom_temp_location; - - // Convert XML back to string for the PUT request - $xmlString = $xml->asXML(); - - // Construct the URL for PUT request - $putUrl = $getUrl; // Same URL for PUT request - - // Initialize cURL for the PUT request - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $putUrl); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/xml']); - curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlString); - $putResponse = curl_exec($ch); - curl_close($ch); + $mms_id = isset($_POST['mms_id']) ? trim($_POST['mms_id']) : ''; + $holding_id = isset($_POST['holding_id']) ? trim($_POST['holding_id']) : ''; + $pid = isset($_POST['pid']) ? trim($_POST['pid']) : ''; + $barcode = isset($_POST['barcode']) ? trim($_POST['barcode']) : ''; - // } - - // Redisplay the updated data - // We can reuse the same code as above to fetch and display updated item details - echo '
'; - echo '
'; - echo '

Updated Item Details Clear

'; - echo ''; - echo ' - - - - - - - '; - - // foreach ($rows as $row) { - $url = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/items?item_barcode=" . $barcode . "&apikey=" . $api_key; - -// Initialize cURL session for GET request to fetch updated data - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - $response = curl_exec($ch); - - // Parse the response XML - $xml = simplexml_load_string($response); - - // Extract updated fields - $title = (string) $xml->bib_data->title; - $item_barcode = (string) $xml->item_data->barcode; - $internalNote1 = (string) $xml->item_data->internal_note_1; - $internalNote1_full = (string) $xml->item_data->internal_note_1; - $internalNote1 = substr($internalNote1, 0, 12); - $process_type = (string) $xml->item_data->process_type; - $internalNote3 = (string) $xml->item_data->internal_note_3; - $mms_id = (string) $xml->bib_data->mms_id; - - - /////// ***** Record to NDJSON File ******** ////////////// -// File path for the NDJSON file -$file = __DIR__ . '/refile.ndjson'; - -// Optional: ensure $process_type is defined before this line -$process_type_full = ' - ' . $process_type; - -// Define the variables for a new entry -$jsonDate = date('Y-m-d H:i:s'); // Current timestamp -$jsonName = $name; // Replace this with actual name variable -$jsonBarcode = $item_barcode; // Replace this with actual barcode variable -$jsonTrayBarcode = $internalNote1; // Tray barcode - -if (isset($process_type) && $process_type !== '') { - $jsonStatus = 'Item In Place - ' . $process_type; -} else { - $jsonStatus = 'Item In Place'; -} - -$jsonStep = '2'; // Define the step the row is part of - -// Create a new entry -$newEntry = [ - 'date' => $jsonDate, - 'name' => $jsonName, - 'barcode' => $jsonBarcode, - 'tray barcode' => $jsonTrayBarcode, - 'status' => $jsonStatus, - 'step' => $jsonStep, -]; - -// Encode as JSON (compact) and append to file -$jsonLine = json_encode($newEntry, JSON_UNESCAPED_SLASHES); -if ($jsonLine === false) { - die('Error: JSON encoding failed. ' . json_last_error_msg()); -} - -if (file_put_contents($file, $jsonLine . "\n", FILE_APPEND | LOCK_EX) === false) { - die('Error: Unable to write to the NDJSON file.'); -} -/////// ***** End Record to NDJSON File ******** ////////////// - - - // Display updated details in table - echo ""; - echo ''; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - curl_close($ch); + if ($mms_id === '' || $holding_id === '' || $pid === '' || $barcode === '') { + echo "
Missing data required to proceed.
"; + } else { + $getUrl = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/bibs/" + . rawurlencode($mms_id) + . "/holdings/" . rawurlencode($holding_id) + . "/items/" . rawurlencode($pid) + . "?apikey=" . rawurlencode($api_key); - // } + $getResponse = almaRequest($getUrl, 'GET'); + $xml = loadXmlFromResponse($getResponse); - echo '
Tray BarcodeTitleItem BarcodeInternal Note 3MMS IDStatus
' . htmlspecialchars($internalNote1) . '" . htmlspecialchars($title) . "" . htmlspecialchars($item_barcode) . "" . htmlspecialchars($internalNote3) . "" . htmlspecialchars($mms_id) . "Updated
'; - echo '
'; + if ($xml === false) { + echo "
Unable to retrieve item record for update.
"; + } else { + if (isset($xml->item_data->internal_note_3)) { + $xml->item_data->internal_note_3 = ''; + } + + if (isset($xml->holding_data->in_temp_location)) { + $xml->holding_data->in_temp_location = 'false'; + } + + if (isset($xml->holding_data->temp_library)) { + $xml->holding_data->temp_library = ''; + } + + if (isset($xml->holding_data->temp_location)) { + $xml->holding_data->temp_location = ''; + } + + $xmlString = $xml->asXML(); + + $putResponse = almaRequest( + $getUrl, + 'PUT', + $xmlString, + array('Content-Type: application/xml') + ); + + if (!$putResponse['ok']) { + echo "
Update failed"; + if (!empty($putResponse['status'])) { + echo " (HTTP " . (int)$putResponse['status'] . ")"; + } + echo ".
"; + } else { + echo '
'; + echo '
'; + echo '

Updated Item Details Clear

'; + echo ''; + echo ' + + + + + + + '; + + $updatedLookup = findItemXmlByBarcode($barcode, $api_key); + + if (!$updatedLookup['ok'] || $updatedLookup['xml'] === false) { + echo ""; + } else { + $xml = $updatedLookup['xml']; + + $title = isset($xml->bib_data->title) ? (string)$xml->bib_data->title : ''; + $item_barcode = isset($xml->item_data->barcode) ? (string)$xml->item_data->barcode : ''; + $internalNote1 = isset($xml->item_data->internal_note_1) ? (string)$xml->item_data->internal_note_1 : ''; + $internalNote1 = substr($internalNote1, 0, 12); + $process_type = isset($xml->item_data->process_type) ? (string)$xml->item_data->process_type : ''; + $internalNote3 = isset($xml->item_data->internal_note_3) ? (string)$xml->item_data->internal_note_3 : ''; + $mms_id = isset($xml->bib_data->mms_id) ? (string)$xml->bib_data->mms_id : ''; + + $file = __DIR__ . '/refile.ndjson'; + + $jsonDate = date('Y-m-d H:i:s'); + $jsonName = $name; + $jsonBarcode = $item_barcode; + $jsonTrayBarcode = $internalNote1; + + if (isset($process_type) && $process_type !== '') { + $jsonStatus = 'Item In Place - ' . $process_type; + } else { + $jsonStatus = 'Item In Place'; + } + + $jsonStep = '2'; + + $newEntry = array( + 'date' => $jsonDate, + 'name' => $jsonName, + 'barcode' => $jsonBarcode, + 'tray barcode' => $jsonTrayBarcode, + 'status' => $jsonStatus, + 'step' => $jsonStep, + ); + + $jsonLine = json_encode($newEntry, JSON_UNESCAPED_SLASHES); + + if ($jsonLine === false) { + echo "
JSON encoding failed: " . h(json_last_error_msg()) . "
"; + } else { + if (file_put_contents($file, $jsonLine . "\n", FILE_APPEND | LOCK_EX) === false) { + echo "
Unable to write to the NDJSON file.
"; + } + } + + echo ""; + echo ''; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + } + + echo '
Tray BarcodeTitleItem BarcodeInternal Note 3MMS IDStatus
Unable to reload updated item data.
' . h($internalNote1) . '" . h($title) . "" . h($item_barcode) . "" . h($internalNote3) . "" . h($mms_id) . "Updated
'; + echo '
'; + } + } + } } - ?> - - + - \ No newline at end of file diff --git a/public/refile/refile_upload.php b/public/refile/refile_upload.php index 571f32a..312af10 100755 --- a/public/refile/refile_upload.php +++ b/public/refile/refile_upload.php @@ -1,59 +1,170 @@ - false, + 'status' => 0, + 'body' => '', + 'error' => 'Unable to initialize cURL.' + ); + } + + curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADER, true); - $response = curl_exec($ch); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); - if (curl_errno($ch)) { - echo "
cURL Error: " . curl_error($ch) . "
"; - curl_close($ch); - return false; + if ($method === 'PUT') { + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); + } elseif ($method === 'POST') { + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); + } + + if (!empty($headers)) { + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } - $finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); + $responseBody = curl_exec($ch); + $curlError = curl_error($ch); + $statusCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); - return ($finalUrl !== $url) ? $finalUrl : false; + + if ($responseBody === false) { + return array( + 'ok' => false, + 'status' => $statusCode, + 'body' => '', + 'error' => $curlError !== '' ? $curlError : 'Unknown cURL error.' + ); + } + + return array( + 'ok' => ($statusCode >= 200 && $statusCode < 300), + 'status' => $statusCode, + 'body' => $responseBody, + 'error' => '' + ); } +function loadXmlFromResponse($response) +{ + if (!is_array($response) || empty($response['ok']) || trim($response['body']) === '') { + return false; + } -?> + return @simplexml_load_string($response['body']); +} + +function findItemXmlByBarcode($barcodeInput, $apiKey) +{ + $candidateBarcodes = array(); + + $barcodeInput = trim((string)$barcodeInput); + + if ($barcodeInput === '') { + return array( + 'ok' => false, + 'url' => '', + 'xml' => false, + 'error' => 'Blank barcode.' + ); + } + + $barcodeWithX = $barcodeInput; + if (substr($barcodeWithX, -1) !== 'X') { + $barcodeWithX .= 'X'; + } + $candidateBarcodes[] = $barcodeWithX; + + if (strtolower(substr($barcodeInput, 0, 1)) === 'p' && strlen($barcodeInput) === 6) { + $candidateBarcodes[] = $barcodeInput . 'X'; + } + + $barcodeWithoutX = str_replace('X', '', $barcodeWithX); + if ($barcodeWithoutX !== '') { + $candidateBarcodes[] = $barcodeWithoutX; + } + + $candidateBarcodes = array_values(array_unique($candidateBarcodes)); + + foreach ($candidateBarcodes as $candidateBarcode) { + $url = 'https://api-na.hosted.exlibrisgroup.com/almaws/v1/items?item_barcode=' + . rawurlencode($candidateBarcode) + . '&apikey=' . rawurlencode($apiKey); + + $response = almaRequest($url, 'GET'); + $xml = loadXmlFromResponse($response); + if ($xml !== false && isset($xml->item_data->barcode)) { + return array( + 'ok' => true, + 'url' => $url, + 'xml' => $xml, + 'error' => '' + ); + } + } + return array( + 'ok' => false, + 'url' => '', + 'xml' => false, + 'error' => 'Item record does not exist.' + ); +} +$rows = array(); +$name = ''; +$xmlData = false; + +if (isset($_SESSION['user_id'])) { + $name = $_SESSION['user_id']; +} elseif (isset($GLOBALS['name'])) { + $name = $GLOBALS['name']; +} +?> Item Information - - + + display: block; + width: 100%; + height: 44px; + padding: .375rem .75rem; + font-size: 1rem; + font-weight: 400; + line-height: 1.5; + color: #495057; + background-color: #fff; + background-clip: padding-box; + border: 1px solid #ced4da; + border-radius: .25rem; + transition: border-color .15s ease-in-out, box-shadow .15s ease-in-out; + } + - - + +
@@ -63,509 +174,398 @@ function getFinalRedirectUrl($url)

Upload Barcode File

-
-
-
Be patient. It can take time to load a large list of item records.
+
+ +
+
+ Be patient. It can take time to load a large list of item records. +
- - '; } - - // Redisplay the updated data - // We can reuse the same code as above to fetch and display updated item details - echo '
'; - echo '
'; - // Count the rows in $rows and display next to "Updated Item Details" - $updated_row_count = count($rows); - echo '

Updated Item Details Clear

'; - - echo ''; - echo ' - - - - - - - '; - - foreach ($rows as $row) { - $url = "https://api-na.hosted.exlibrisgroup.com/almaws/v1/items?item_barcode=" . $row['item_barcode'] . "&apikey=" . $api_key; - - // Initialize cURL session for GET request to fetch updated data - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - $response = curl_exec($ch); - curl_close($ch); - - // Parse the response XML - $xml = simplexml_load_string($response); - - // Extract updated fields - $title = (string) $xml->bib_data->title; - $item_barcode = (string) $xml->item_data->barcode; - $internalNote3 = (string) $xml->item_data->internal_note_3; - $internalNote1 = (string) $xml->item_data->internal_note_1; - $mms_id = (string) $xml->bib_data->mms_id; - $process_type = (string) $xml->item_data->process_type; - - // Display updated details in table - echo ""; - echo ''; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - -/////// ***** Record to NDJSON File ******** ////////////// -// File path for the NDJSON file -$file = __DIR__ . '/refile.ndjson'; - -// Optional: ensure $process_type is defined before this line -$process_type_full = ' - ' . $process_type; - -// Define the variables for a new entry -$jsonDate = date('Y-m-d H:i:s'); // Current timestamp -$jsonName = $name; // Replace this with actual name variable -$jsonBarcode = $item_barcode; // Replace this with actual barcode variable -$jsonTrayBarcode = $internalNote1; // Tray barcode - -if (isset($process_type) && $process_type !== '') { - $jsonStatus = 'Item In Place - ' . $process_type; -} else { - $jsonStatus = 'Item In Place'; -} - -$jsonStep = '2'; // Define the step the row is part of - -// Create a new entry -$newEntry = [ - 'date' => $jsonDate, - 'name' => $jsonName, - 'barcode' => $jsonBarcode, - 'tray barcode' => $jsonTrayBarcode, - 'status' => $jsonStatus, - 'step' => $jsonStep, -]; - -// Encode as JSON (compact) and append to file -$jsonLine = json_encode($newEntry, JSON_UNESCAPED_SLASHES); -if ($jsonLine === false) { - die('Error: JSON encoding failed. ' . json_last_error_msg()); -} - -if (file_put_contents($file, $jsonLine . "\n", FILE_APPEND | LOCK_EX) === false) { - die('Error: Unable to write to the NDJSON file.'); -} } - -/////// ***** End Record to NDJSON File ******** ////////////// - - echo '
Tray BarcodeTitleItem BarcodeInternal Note 3MMS IDStatus
' . htmlspecialchars($row['trayBarcode']) . '" . htmlspecialchars($title) . "" . htmlspecialchars($item_barcode) . "" . htmlspecialchars($internalNote3) . "" . htmlspecialchars($mms_id) . "Updated
'; - echo '
'; } ?> -

Upload a .txt file with barcodes

-

Format should be:
- Tray Barcode
- Item Barcode
- Tray Barcode
- Item Barcode
+

Upload a .txt file with barcodes

+

Format should be:
+ Tray Barcode
Item Barcode
+ Tray Barcode
Item Barcode
Etc... -

+

- - + + \ No newline at end of file