locum_config; $no_circ = $locum->csv_parser($locum_cfg['location_limits']['no_request']); $valid_search_types = array('title', 'author', 'keyword', 'subject', 'series', 'callnum', 'tags', 'reviews'); // TODO handle this more dynamically $sort = $getvars['sort']; $format = $getvars['search_format']; $location = $getvars['location']; $limit_avail = $getvars['limit_avail']; $pager_page_array = explode(',', $getvars['page']); $search_type = $actions[1]; $search_term = utf8_urldecode($actions[2]); // Begin thinking about RSS $hitlist_template = ($getvars['output'] == 'rss') ? 'sopac_results_hitlist_rss' : 'sopac_results_hitlist'; // If there is a proper search query, we get that data here. if (in_array($actions[1], $valid_search_types)) { $valid_search = TRUE; // Save the search URL in a cookie $_SESSION['search_url'] = $_SERVER['REQUEST_URI']; if ($getvars['perpage']) { $limit = $getvars['perpage']; } else if ($user->profile_perpage) { $limit = $user->profile_perpage; } else { $limit = variable_get('sopac_results_per_page', 10); } /* Not implemented yet if ($user->uid && $limit != $user->profile_perpage) { $field = db_fetch_object(db_query("SELECT * FROM profile_fields WHERE name = 'profile_perpage'")); db_query("INSERT INTO profile_values (fid, uid, value) VALUES (%d, %d, '%s') ON DUPLICATE KEY UPDATE value = '%s'", $field->fid, $user->uid, $limit, $limit); } */ //if ($addl_search_args['limit']) { // $limit = $addl_search_args['limit']; //} else { // $limit = variable_get('sopac_results_per_page', 20); //} // Initialize the pager if need be if ($pager_page_array[0]) { $page = $pager_page_array[0] + 1; } else { $page = 1; } $page_offset = $limit * ($page - 1); // Grab the faceted search arguments from the URL $facet_args = array(); if (count($getvars['facet_series'])) { $facet_args['facet_series'] = $getvars['facet_series']; } if (count($getvars['facet_lang'])) { $facet_args['facet_lang'] = $getvars['facet_lang']; } if (count($getvars['facet_year'])) { $facet_args['facet_year'] = $getvars['facet_year']; } if (count($getvars['facet_decade'])) { $facet_args['facet_decade'] = $getvars['facet_decade']; } if (count($getvars['age'])) { $facet_args['age'] = $getvars['age']; } // Get the search results from Locum $locum_results_all = $locum->search($search_type, $search_term, $limit, $page_offset, $sort, $format, $location, $facet_args, FALSE, $limit_avail); $num_results = $locum_results_all['num_hits']; $result_info['limit'] = $limit; $result_info['num_results'] = $num_results; $result_info['hit_lowest'] = $page_offset + 1; if (($page_offset + $limit) < $num_results) { $result_info['hit_highest'] = $page_offset + $limit; } else { $result_info['hit_highest'] = $num_results; } } // Construct the search form $search_form_cfg = variable_get('sopac_search_form_cfg', 'both'); $search_form = sopac_search_form($search_form_cfg); // If we get results back, we begin creating the hitlist if ($num_results > 0) { // We need to determine how many result pages there are. $pager_total[0] = ceil($num_results / $limit); $hitlist = ''; $hitnum = $page_offset + 1; // When limiting to available, sometimes the "Last" link takes the user beyond the number of // available items and errors out. This will step them back until they have at least 1 hit. if (!count($locum_results_all['results']) && $getvars['limit_avail']) { $uri_arr = explode('?', $_SERVER['REQUEST_URI']); $uri = $uri_arr[0]; $getvars_tmp = $getvars; if ($getvars_tmp['page']) { if ($getvars_tmp['page'] == 1) { $getvars_tmp['page'] = ''; } else { $getvars_tmp['page']--; } $pvars_tmp = trim(sopac_make_pagevars(sopac_parse_get_vars($getvars_tmp))); $gvar_indicator = $pvars_tmp ? '?' : ''; $step_link = $uri . $gvar_indicator . $pvars_tmp; header('Location: ' . $step_link); } } // Loop through results. foreach ($locum_results_all['results'] as $locum_result) { // Grab Stdnum $stdnum = $locum_result['stdnum']; // Grab item status from Locum $locum_result['status'] = $locum->get_item_status($locum_result['bnum']); // Get the cover image $cover_img_url = $locum_result['cover_img']; // Grab Syndetics reviews, etc.. $review_links = $locum->get_syndetics($locum_result['stdnum']); if (count($review_links)) { $locum_result['review_links'] = $review_links; } // Send it all off to the template $result_body .= theme($hitlist_template, $hitnum, $cover_img_url, $locum_result, $locum_cfg, $no_circ); $hitnum++; } $hitlist_pager = theme('pager', NULL, $limit, 0, NULL, 6); } else if ($valid_search) { $result_body .= theme('sopac_results_nohits', $locum_results_all, $locum->locum_config); } // Pull it all together into the search page template $result_page = $search_form . theme($output_template, $result_info, $hitlist_pager, $result_body, $locum_results_all, $locum->locum_config); // Check to see if we're doing RSS if ($getvars['output'] == 'rss') { print theme('sopac_results_rss', $result_info, $search_term, $search_type, $result_body, $locum_results_all, $locum->locum_config); exit(0); } else { $result_page = $search_form . theme('sopac_results', $result_info, $hitlist_pager, $result_body, $locum_results_all, $locum->locum_config); } $search_feed_url = sopac_update_url($_SERVER['REQUEST_URI'], 'output', 'rss'); drupal_add_feed($search_feed_url, 'Search for "' . $search_term . '"'); return '
'. t($result_page) .'
'; } /** * Prepares and returns the HTML for an item record. * Uses the following templates: sopac_record.tpl.php * * @return string Item record HTML */ function sopac_bib_record() { global $user; $locum = new locum_client; $insurge = new insurge_client; $actions = sopac_parse_uri(); $bnum = $actions[1]; // Load social function require_once('sopac_social.php'); $bnum_arr[] = $bnum; $reviews = $insurge->get_reviews(NULL, $bnum_arr, NULL); $i = 0; foreach ($reviews['reviews'] as $insurge_review) { $rev_arr[$i]['rev_id'] = $insurge_review['rev_id']; $rev_arr[$i]['bnum'] = $insurge_review['bnum']; if ($insurge_review['uid']) { $rev_arr[$i]['uid'] = $insurge_review['uid']; } $rev_arr[$i]['timestamp'] = $insurge_review['rev_create_date']; $rev_arr[$i]['rev_title'] = $insurge_review['rev_title']; $rev_arr[$i]['rev_body'] = $insurge_review['rev_body']; $i++; } $no_circ = $locum->csv_parser($locum->locum_config['location_limits']['no_request']); $item = $locum->get_bib_item($bnum, TRUE); $item_status = $locum->get_item_status($bnum); if ($item['bnum']) { // Grab Syndetics reviews, etc.. $review_links = $locum->get_syndetics($item['stdnum']); if (count($review_links)) { $item['review_links'] = $review_links; } // Get and patron reviews if (!$insurge->check_reviewed($user->uid, $item['bnum']) && $user->uid) { $rev_form = drupal_get_form('sopac_review_form', $item['bnum']); } else { $rev_form = NULL; } // Build the page $result_page = theme('sopac_record', $item, $item_status, $locum->locum_config, $no_circ, &$locum, $rev_arr, $rev_form); } else { $result_page = t('This record does not exist.'); } return ''. t($result_page) .'
'; } /** * Formulates and returns the search tracker block HTML. * Uses the following templates: sopac_search_block.tpl.php * * @return string Search tracker block HTML */ function sopac_search_block($locum_results_all, $locum_cfg) { global $user; $getvars = sopac_parse_get_vars(); $uri = sopac_parse_uri(); $format = $getvars['search_format']; $term_arr = explode('?', trim(preg_replace('/\//', ' ', $uri[2]))); $search['term'] = trim($term_arr[0]); $search['type'] = trim($uri[1]); $search['sortby'] = $getvars['sort'] ? $getvars['sort'] : t('Most relevant'); $search['format'] = count($getvars['search_format']) && ($getvars['search_format'][0] != 'all') ? $getvars['search_format'] : array(); $search['series'] = count($getvars['facet_series']) ? $getvars['facet_series'] : array(); $search['lang'] = count($getvars['facet_lang']) ? $getvars['facet_lang'] : array(); $search['year'] = count($getvars['facet_year']) ? $getvars['facet_year'] : array(); $search['decade'] = count($getvars['facet_decade']) ? $getvars['facet_decade'] : array(); $search['age'] = count($getvars['age']) ? $getvars['age'] : array(); return theme('sopac_search_block', $search, $locum_results_all, $locum_cfg, $user); } /** * If nothing is in the author field, we try the addl author field. * Oh, and we present first name first. Like it oughta. * * @param string $author Author string as presented up from Locum * @param string $addl_author_ser Serialized additional author string as presented up from Locum * @return string The formatted author string */ function sopac_author_format($author, $addl_author_ser) { if ($author) { $author_arr = explode(',', $author); $new_author_str = trim($author_arr[1]) . ' ' . trim($author_arr[0]); } else if ($addl_author_ser) { $addl_author = unserialize($addl_author_ser); if ($addl_author[0]) { $author_arr = explode(',', $addl_author[0]); $new_author_str = trim($author_arr[1]) . ' ' . trim($author_arr[0]); } } if ($new_author_str) { //$new_author_str = ereg_replace("[^A-Za-z\x20-\x7F '.-]", '', $new_author_str ); $new_author_str = preg_replace('/ - /', ' ', $new_author_str); } else { $new_author_str = ''; } return $new_author_str; } /** * Create the "Did you mean" link * * @param array $locum_result Locum result array as passed up from Locum * @return string Suggestion link */ function suggestion_link($locum_result) { $pagevars = sopac_make_pagevars(sopac_parse_get_vars()); $url_prefix = variable_get('sopac_url_prefix', 'cat/seek'); $sugg_link = '/' . $url_prefix . '/search/' . $locum_result['type'] . '/' . $locum_result['suggestion'] . '?' . $pagevars; return $sugg_link; } /** * This function will return the appropriate request link based on whether the user is logged in, has a verified card, or not * * @return string HTML string for the request link */ function sopac_put_request_link($bnum) { global $user; profile_load_profile(&$user); if ($user->uid) { if (sopac_bcode_isverified(&$user)) { // User is logged in and has a verified card number $link = '/' . variable_get('sopac_url_prefix', 'cat/seek') . '/request/' . $bnum; $link_text = t('Request this item'); } else if ($user->profile_pref_cardnum) { // User is logged in but does not have a verified card number $link = '/user/' . $user->uid; $link_text = t('Verify your card to request this item'); } else { // User is logged in but does not have a card number. $link = '/user/' . $user->uid; $link_text = t('Register your card to request this item'); } } else { $link = '/user/login?' . drupal_get_destination(); $link_text = t('Please log in to request this item'); } return '' . $link_text . ''; } /** * Returns the search URL, only if the user is coming directly from the search page. * * @return string|bool Search URL or FALSE */ function sopac_prev_search_url($override = FALSE) { if (!$_SESSION['search_url']) { return FALSE; } $referer = substr($_SERVER['HTTP_REFERER'], 7 + strlen($_SERVER['HTTP_HOST'])); $search = $_SESSION['search_url']; if ((($search == $referer) || $override) && $_SESSION['search_url']) { return $search; } else { return FALSE; } } /** * Requests a particular item via locum then displays the results of that request * * @return string Request result */ function sopac_request_item() { global $user; // avoid php errors when debugging $varname = $request_result_msg = $request_error_msg = $item_form = $bnum = NULL; $button_txt = t('Request Selected Item'); profile_load_profile(&$user); if ($user->uid && sopac_bcode_isverified(&$user)) { if ($_POST['sub_type'] == $button_txt) { if ($_POST['varname']) { $varname = $_POST['varname']; } else { $request_error_msg = t('You need to select an item to request.'); } } // support multi-branch & user home branch $actions = sopac_parse_uri(); $bnum = $actions[1]; $pickup_arg = $actions[2] ? $actions[2] : NULL; $stored_pickup_options = variable_get('sopac_home_selector_options', array()); if (!$pickup_arg && count($stored_pickup_options)) { $hold_result['choose_location']['options'] = $stored_pickup_options; } else { $pickup_name = $actions[3] ? $actions[3] : NULL; $locum = new locum_client; $bib_item = $locum->get_bib_item($bnum); $hold_result = $locum->place_hold($user->profile_pref_cardnum, $bnum, $varname, $user->locum_pass, $pickup_arg); } if ($hold_result['success']) { // handling multi-branch scenario $request_result_msg = t('You have successfully requested a copy of ') . ' ' . $bib_item['title'] . ''; if ($pickup_name) { $request_result_msg .= t(' for pickup at ') . $pickup_name; } } // more multibranch elseif (is_array($hold_result['choose_location'])) { // pickup location $form_data = array( 'options' => $hold_result['choose_location']['options'], 'bnum' => $bnum, ); $request_result_msg = drupal_build_form('sopac_hold_location_form', $form_data); } else { $request_result_msg = t('We were unable to fulfill your request for ') . '' . $bib_item['title'] . ''; } if ($hold_result['error']) { $request_result_msg = $hold_result['error']; } if ($hold_result['selection'] && !$hold_result['success']) { $requestable = 0; $header = array('', t('Location'), t('Call Number'), t('Status')); foreach ($hold_result['selection'] as $selection) { $status = $selection['status']; if ($selection['varname']) { $radio = ''; $non_circ = NULL; $requestable++; } else { $radio = ''; $status = '' . $status . ''; } $rows[] = array( $radio, $selection['location'], $selection['callnum'], $status, ); } if ($requestable) { $submit_button = ''; $request_result_msg = t('Please select the item you would like to request.'); } else { $submit_button = NULL; $request_result_msg = ''; $request_error_msg = t('There are no copies of this item available for circulation.'); } if ($submit_button){ $rows[] = array( 'data' => array(array('data' => $submit_button, 'colspan' => 4)), 'class' => 'req_button' ); } $item_form = ''; } // TODO - add a tally for top items data recovery } else { $request_error_msg = t("You must have a valid library card number registered with our system."); } $result_page = theme('sopac_request', $request_result_msg, $request_error_msg, $item_form, $bnum); return ''. t($result_page) .'
'; } // allow user to select branch at which to pickup hold function sopac_hold_location_page() { $output = drupal_get_form('sopac_hold_location_form'); return $output; } function sopac_hold_location_form($form_data = null) { global $user; if (isset($form_data['bnum'])) { $form_data['storage']['options'] = $form_data['options']; $form_data['storage']['bnum'] = $form_data['bnum']; } $options = $form_data['storage']['options']; $form = array(); $form['#action'] = '/hold/location'; $form['hold_location'] = array( '#type' => 'select', '#title' => t('Choose a pickup location'), '#options' => $options, ); if (isset($user->profile_pref_home_branch)) { $options = array_flip($options); if (array_key_exists($user->profile_pref_home_branch, $options)) { $form['hold_location']['#default_value'] = $options[$user->profile_pref_home_branch]; } } $form['op'] = array( '#type' => 'submit', '#value' => t('Submit'), ); return $form; } function sopac_hold_location_form_submit($form, &$form_state) { $location_name = $form['hold_location']['#options'][$form_state['values']['hold_location']]; //drupal_set_message(t('You chose ' . $location_name)); $bnum = $form_state['storage']['bnum']; unset($form_state['storage']); drupal_goto('catalog/request/' . $bnum . '/' . $form_state['values']['hold_location'] . '/' . $location_name); } /** * Returns the url string to use in the save search link. * * @return string URL */ function sopac_savesearch_url() { $search_url = '/' . variable_get('sopac_url_prefix', 'cat/seek') . '/savesearch' . substr($_SERVER['REQUEST_URI'], 8 + strlen(variable_get('sopac_url_prefix', 'cat/seek'))); return $search_url; } /** * Formulates the basic search form array * * @return array Drupal search form array */ function sopac_search_form_basic() { $locum = new locum(); $locum_cfg = $locum->locum_config; $getvars = sopac_parse_get_vars(); $actions = sopac_parse_uri(); $search_args_raw = explode('?', $actions[2]); $search_args = trim($search_args_raw[0]); $stype_selected = $actions[1] ? 'cat_' . $actions[1] : 'cat_keyword'; $sformat_selected = $_GET['search_format'] ? $_GET['search_format'] : 'all'; $stypes = array( 'cat_keyword' => t('Keyword'), 'cat_title' => t('Title'), 'cat_author' => t('Author'), 'cat_series' => t('Series'), 'cat_tags' => t('Tags'), 'cat_reviews' => t('Reviews'), 'cat_subject' => t('Subject'), 'cat_callnum' => t('Call Number'), ); $sortopts = array( 'relevance' => t('Relevance'), 'newest' => t('Newest First'), 'oldest' => t('Oldest First'), ); foreach ($locum_cfg['format_groups'] as $sfmt => $sfmt_codes) { $sformats[preg_replace('/,[ ]*/', '|', trim($sfmt_codes))] = ucfirst($sfmt); } if (is_null($prompt)) { $prompt = t('Enter your keywords'); } // Initialize the form $form = array( '#action' => '/search_handler', '#attributes' => array('class' => 'search-form'), ); // Start creating the basic search form $form['basic'] = array('#type' => 'item'); $form['basic']['inline'] = array('#prefix' => '