<commit_before><?php
/**
  * Displays form for submitting list of barcodes
  *
  * @author <NAME> <<EMAIL>>
  * @version 2014-04-15
  * @since 2014-02-22
  *
  */
require_once 'config.php';

// Get all barcodes entered into form and put them into an array
$item_ids = explode("\n", $_REQUEST['barcodes']);

// Get data for each barcode using SIRSI API calls
$data = get_data($item_ids);

// Format the data to only include necessary information for our application
$clean_data = format_data($data);

// Give output to template for display to user
$html = array('title' => 'Home', 'html' => $clean_data);
template::display('report.tmpl', $html);


/***************************
 *        FUNCTIONS        *
 ***************************/

/**
 * Constructs API call(s) and returns data for all barcodes
 *
 * @param array Array with all barcodes entered into form
 * @return array Array with just relevant data from API calls included
 **/
function get_data($item_ids) {
  // Loop through all barcodes
  // Sirsi technically allows aggregating multiple calls but does not return all necessary information for each returned result for this to be useful for us
  foreach($item_ids AS $item_id) {
    $item_id = trim($item_id);
    // Create search parameters for Sirsi API
    $parameters = 'marcEntryFilter=ALL&includeItemInfo=true&includeCallNumberSummary=true&json=true&itemID=' . $item_id;
    // Construct URL for Sirsi API call
    $url = config::API_BASE_URL . config::API_SEARCH_URL . $parameters . '&clientID=' . config::CLIENT_ID;
    // Get the results of the API call
    $data = get_json($url);
    if($data == '') {
      echo '<p>There was a problem connecting to the server. Please try again later.</p>';
      die();
    }
    // Sirsi spits out bad JSON if there is an error
    // Correct the invalid JSON if there is an error
    if(config::BAD_FAULT_JSON) {
      $data = str_replace('faultResponse', '"faultResponse"', $data);
    }
    // Convert the object to a PHP array
    $data = json_decode($data, true);
    // Store all API call results in one array
    $all_data[] = array('item_id' => $item_id, 'data' => $data);
  }
  return $all_data;
}


/**
 * Extracts relevant information from results of all API calls
 *
 * @param array Array with data from all API calls
 * @return array Array with just relevant data from API calls included
 **/
function format_data($data) {
  foreach($data AS $record) {
    $item_id = $record['item_id'];
    $title_info = $record['data']['TitleInfo'][0];
    // If no result found
    if(empty($title_info)) {
      $all_records[] = array('error' => 'No item found for ' . $item_id . '.');
    // If result is found
    } else {
      $title_id = $title_info['titleID'];
      // Home location and call number
      foreach($title_info['CallInfo'] AS $CallInfo) {
        $record_call_number = $CallInfo['callNumber'];
        foreach($CallInfo['ItemInfo'] AS $ItemInfo) {
          $record_item_id = $ItemInfo['itemID'];
          if($record_item_id == $item_id) {
            $call_number = $record_call_number;
            $home_location = $ItemInfo['homeLocationID'];
          }
          // Indicate whether the item is a SERIAL because base call number
          // does not include subfield z (can remove once SIRSI updates APIs
          // to match documentation)
          if($ItemInfo['itemTypeID'] == 'SERIAL') {
            $call_number = $record_call_number . '<br/><span class="warning">(SERIAL)</span>';
          } // Non-BOOK items have a different location for call number data
          elseif($ItemInfo['itemTypeID'] != 'BOOK') {
            $call_number = $record_call_number;
          }
        }
      }
      // MARC record data
      $marc = $record['data']['TitleInfo'][0]['BibliographicInfo']['MarcEntryInfo'];
      foreach($marc AS $field) {
        $label           = $field['label'];
        $entryID         = $field['entryID'];
        $indicators      = $field['indicators'];
        $text            = $field['text'];
        $unformattedText = $field['unformattedText'];
        if($entryID == '001') { $control_number = $text; }

        if($entryID == '020') { $isbn = $text; }
        if($entryID == '100') { $author = $text; }
        if($entryID == '245') { $title = $text; }
        // Vernacular title
        if($entryID == '880' && strpos($unformattedText, '|6245-01|') !== false) { $linked_title = $text; }
        if($entryID == '260') {
          $publisher = $text;
          $pub_array = explode('|c', $unformattedText);
          $pub_date  = trim($pub_array[1], '. ');
        }

      }

      $all_records[] = array('item_id' => $item_id, 'title_id' => $title_id, 'home_location' => $home_location, 'call_number' => $call_number, 'control_number' => $control_number, 'isbn' => $isbn, 'author' => $author, 'title' => $title, 'linked_title' => $linked_title, 'pub_date' => $pub_date, 'publisher' => $publisher);

      // Reset all variables for next item being searched
      $item_id        = null;
      $title_id       = null;
      $home_location  = null;
      $call_number    = null;
      $control_number = null;
      $isbn           = null;
      $author         = null;
      $title          = null;
      $linked_title   = null;
      $pub_date       = null;
      $publisher      = null;
    }
  }
  return $all_records;
}


/**
 * Uses CURL to grab the JSON from SIRSI
 * From http://25labs.com/alternative-for-file_get_contents-using-curl/
 *
 * @param string URL of API call
 * @return string Data returned by API call
 **/<commit_msg>[FIX] Remove dashes from pub dates<commit_after><?php
/**
  * Displays form for submitting list of barcodes
  *
  * @author <NAME> <<EMAIL>>
  * @version 2014-04-15
  * @since 2014-02-22
  *
  */
require_once 'config.php';

// Get all barcodes entered into form and put them into an array
$item_ids = explode("\n", $_REQUEST['barcodes']);

// Get data for each barcode using SIRSI API calls
$data = get_data($item_ids);

// Format the data to only include necessary information for our application
$clean_data = format_data($data);

// Give output to template for display to user
$html = array('title' => 'Home', 'html' => $clean_data);
template::display('report.tmpl', $html);


/***************************
 *        FUNCTIONS        *
 ***************************/

/**
 * Constructs API call(s) and returns data for all barcodes
 *
 * @param array Array with all barcodes entered into form
 * @return array Array with just relevant data from API calls included
 **/
function get_data($item_ids) {
  // Loop through all barcodes
  // Sirsi technically allows aggregating multiple calls but does not return all necessary information for each returned result for this to be useful for us
  foreach($item_ids AS $item_id) {
    $item_id = trim($item_id);
    // Create search parameters for Sirsi API
    $parameters = 'marcEntryFilter=ALL&includeItemInfo=true&includeCallNumberSummary=true&json=true&itemID=' . $item_id;
    // Construct URL for Sirsi API call
    $url = config::API_BASE_URL . config::API_SEARCH_URL . $parameters . '&clientID=' . config::CLIENT_ID;
    // Get the results of the API call
    $data = get_json($url);
    if($data == '') {
      echo '<p>There was a problem connecting to the server. Please try again later.</p>';
      die();
    }
    // Sirsi spits out bad JSON if there is an error
    // Correct the invalid JSON if there is an error
    if(config::BAD_FAULT_JSON) {
      $data = str_replace('faultResponse', '"faultResponse"', $data);
    }
    // Convert the object to a PHP array
    $data = json_decode($data, true);
    // Store all API call results in one array
    $all_data[] = array('item_id' => $item_id, 'data' => $data);
  }
  return $all_data;
}


/**
 * Extracts relevant information from results of all API calls
 *
 * @param array Array with data from all API calls
 * @return array Array with just relevant data from API calls included
 **/
function format_data($data) {
  foreach($data AS $record) {
    $item_id = $record['item_id'];
    $title_info = $record['data']['TitleInfo'][0];
    // If no result found
    if(empty($title_info)) {
      $all_records[] = array('error' => 'No item found for ' . $item_id . '.');
    // If result is found
    } else {
      $title_id = $title_info['titleID'];
      // Home location and call number
      foreach($title_info['CallInfo'] AS $CallInfo) {
        $record_call_number = $CallInfo['callNumber'];
        foreach($CallInfo['ItemInfo'] AS $ItemInfo) {
          $record_item_id = $ItemInfo['itemID'];
          if($record_item_id == $item_id) {
            $call_number = $record_call_number;
            $home_location = $ItemInfo['homeLocationID'];
          }
          // Indicate whether the item is a SERIAL because base call number
          // does not include subfield z (can remove once SIRSI updates APIs
          // to match documentation)
          if($ItemInfo['itemTypeID'] == 'SERIAL') {
            $call_number = $record_call_number . '<br/><span class="warning">(SERIAL)</span>';
          } // Non-BOOK items have a different location for call number data
          elseif($ItemInfo['itemTypeID'] != 'BOOK') {
            $call_number = $record_call_number;
          }
        }
      }
      // MARC record data
      $marc = $record['data']['TitleInfo'][0]['BibliographicInfo']['MarcEntryInfo'];
      foreach($marc AS $field) {
        $label           = $field['label'];
        $entryID         = $field['entryID'];
        $indicators      = $field['indicators'];
        $text            = $field['text'];
        $unformattedText = $field['unformattedText'];
        if($entryID == '001') { $control_number = $text; }

        if($entryID == '020') { $isbn = $text; }
        if($entryID == '100') { $author = $text; }
        if($entryID == '245') { $title = $text; }
        // Vernacular title
        if($entryID == '880' && strpos($unformattedText, '|6245-01|') !== false) { $linked_title = $text; }
        if($entryID == '260') {
          $publisher = $text;
          $pub_array = explode('|c', $unformattedText);
          $pub_date  = trim($pub_array[1], '-. ');
        }

      }

      $all_records[] = array('item_id' => $item_id, 'title_id' => $title_id, 'home_location' => $home_location, 'call_number' => $call_number, 'control_number' => $control_number, 'isbn' => $isbn, 'author' => $author, 'title' => $title, 'linked_title' => $linked_title, 'pub_date' => $pub_date, 'publisher' => $publisher);

      // Reset all variables for next item being searched
      $item_id        = null;
      $title_id       = null;
      $home_location  = null;
      $call_number    = null;
      $control_number = null;
      $isbn           = null;
      $author         = null;
      $title          = null;
      $linked_title   = null;
      $pub_date       = null;
      $publisher      = null;
    }
  }
  return $all_records;
}


/**
 * Uses CURL to grab the JSON from SIRSI
 * From http://25labs.com/alternative-for-file_get_contents-using-curl/
 *
 * @param string URL of API call
 * @return string Data returned by API call
 **/
{'fraction_non_alphanumeric': 0.14953354861858628, 'fraction_numerical': 0.008073196986006458, 'mean_word_length': 2.772927241962775, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4344417', 'n_tokens_mistral': 3464, 'n_tokens_neox': 3374, 'n_words': 971}