$sortBy = $_POST["sortBy"];
if ($sortBy == "default") {
    if ($searchType == "author") {
        $sortBy = "author";
    } else {
        $sortBy = "title";
    }
}
$searchText = trim($_POST["searchText"]);
# remove redundant whitespace
$searchText = preg_replace('/\\s+/', " ", $searchText);
if ($searchType == "barcodeNmbr") {
    $sType = OBIB_SEARCH_BARCODE;
    $words[] = $searchText;
} else {
    $words = explodeQuoted($searchText);
    if ($searchType == "author") {
        $sType = OBIB_SEARCH_AUTHOR;
    } elseif ($searchType == "subject") {
        $sType = OBIB_SEARCH_SUBJECT;
    } elseif ($searchType == "all") {
        $sType = OBIB_SEARCH_ALL;
    } else {
        $sType = OBIB_SEARCH_TITLE;
    }
}
// limit search results to collections and materials
$collecs = array();
if (is_array($_POST['collec'])) {
    foreach ($_POST['collec'] as $value) {
        array_push($collecs, $value);
function explodeSearches($searchcode)
{
    // JALG from ALX 11-07-2015
    global $BibSearchesId;
    mb_regex_encoding(OBIB_CHARSET);
    $searchparts = mb_split('\\n', $searchcode);
    $searches = array();
    $i = 0;
    for ($i = 0; $i < count($searchparts); $i++) {
        $dcol = mb_strpos($searchparts[$i], ':', 0, $encoding = OBIB_CHARSET);
        if (!$dcol) {
            continue;
        }
        $typename = mb_substr($searchparts[$i], 0, $dcol, $encoding = OBIB_CHARSET);
        if (!isset($BibSearchesId[$typename])) {
            continue;
        }
        $stype = $BibSearchesId[$typename];
        $stext = mb_substr($searchparts[$i], $dcol + 1);
        $stext = mb_ereg_replace('((^[[:blank:][:cntrl:]]+)|([[:blank:][:cntrl:]]+$))', '', $stext);
        $stext = rawurldecode($stext);
        $swords = explodeQuoted($stext);
        //$swords = array($stext);
        $item = new BiblioSearchQueryItem($stype, $swords);
        $searches[] = $item;
    }
    return $searches;
}
 function search($keyword, $start = 0, $limit = 10, $type = 'title')
 {
     $words = explodeQuoted($keyword, $extended = true);
     $cond = '';
     // Decode Arithmetric Clause
     foreach ($words as $key => $w) {
         $words[$key] = str_replace('%space_bar%', ' ', $w);
     }
     if ($type == 'author') {
         $sortBy = 'author';
     } else {
         $sortBy = 'title';
     }
     if ($type == 'author') {
         $field_cond = array('author');
     } else {
         if ($type == 'subject') {
             $field_cond = array('topic1', 'topic2', 'topic3', 'topic4', 'topic5');
         } else {
             // Otherwise set default search by title
             $field_cond = array('title', 'title_remainder');
         }
     }
     $exclusion = '';
     foreach ($words as $w) {
         if (strpos($w, '%') !== false) {
             $buffer = '';
             $endp = -1;
             while (($p = strpos($w, '%', $endp + 1)) !== false) {
                 $endp = strpos($w, '%', $p + 1);
                 if ($endp === false) {
                     break;
                 }
                 $pre_word = substr($w, 0, $p);
                 $word_end = strpos($w, '%', $endp + 1);
                 if ($word_end === false) {
                     $post_word = substr($w, $endp + 1);
                 } else {
                     $post_word = substr($w, $endp + 1, $word_end - ($endp + 1));
                 }
                 switch ($condition = strtoupper(substr($w, $p + 1, $endp - ($p + 1)))) {
                     case 'AND':
                     case 'OR':
                     case 'NOT':
                         break;
                     default:
                         $condition = 'OR';
                 }
                 if ($condition == 'NOT') {
                     if (empty($exclusion)) {
                         // Pre word in NOT clause should be default clause.
                         if (!empty($pre_word)) {
                             if (empty($buffer)) {
                                 $buffer = "(__FIELD__ LIKE '%{$pre_word}%')";
                             } else {
                                 $buffer = "(__FIELD__ LIKE '%{$pre_word}%' OR ({$buffer}))";
                             }
                         }
                     }
                     $exclusion .= '(';
                     foreach ($field_cond as $f) {
                         $exclusion .= "{$f} NOT LIKE '%{$post_word}%' AND ";
                     }
                     $exclusion = substr($exclusion, 0, -4) . ") AND ";
                 } else {
                     if (empty($buffer)) {
                         $buffer = "(__FIELD__ LIKE '%{$pre_word}%' {$condition} __FIELD__ LIKE '%{$post_word}%')";
                     } else {
                         $buffer = "({$buffer} {$condition} __FIELD__ LIKE '%{$post_word}%')";
                     }
                 }
             }
             if (!empty($buffer)) {
                 foreach ($field_cond as $f) {
                     $cond .= str_replace('__FIELD__', $f, $buffer) . " OR ";
                 }
             }
         } else {
             // Decode some characters.
             $w = str_replace('[percent_mrk]', '%', $w);
             $w = str_replace("%space_bar%", " ", $w);
             foreach ($field_cond as $f) {
                 $cond .= "{$f} LIKE '%{$w}%' OR ";
             }
         }
     }
     $cond = substr($cond, 0, -4);
     if (!empty($exclusion)) {
         if (empty($cond)) {
             $cond = substr($exclusion, 0, -5);
         } else {
             $cond = "({$cond}) AND " . substr($exclusion, 0, -5);
         }
     }
     $q = "FROM biblio";
     $joined = "LEFT JOIN collection_dm c ON collection_cd=c.code\n      LEFT JOIN material_type_dm m ON material_cd=m.code \n      LEFT JOIN biblio_field f ON biblio.bibid=f.bibid AND tag=902 AND subfield_cd='a'";
     $where = "WHERE " . $cond . " ORDER BY " . mysql_real_escape_string($sortBy);
     $counter = $this->_query("SELECT count(*) c {$q} {$where}", false);
     $row = $this->_conn->fetchRow();
     $result['rows'] = $row[c];
     $field = "f.field_data cover, biblio.bibid id, title, author, CONCAT(call_nmbr1, call_nmbr2, call_nmbr3) call_no, \n      c.description collection, m.description material";
     $handler = $this->_query("SELECT {$field} {$q} {$joined} {$where} LIMIT " . (0 + $start) . "," . (0 + $limit), false);
     while ($row = $this->_conn->fetchRow()) {
         $row['cover'] = "http://" . $_SERVER['HTTP_HOST'] . dirname(dirname($_SERVER['SCRIPT_NAME'])) . COVER_PATH . '/' . $row['cover'];
         $result['data'][] = $row;
     }
     return serialize($result);
 }
        $sortBy = "author";
    } else {
        $sortBy = "title";
    }
}
$searchText = trim(rawurldecode($_POST["searchText"]));
# remove redundant whitespace
$searchText = preg_replace('/\\s+/', " ", $searchText);
$sType = OBIB_SEARCH_TITLE;
if (isset($BibSearchesId[$searchType])) {
    $sType = $BibSearchesId[$searchType];
}
if ($sType == OBIB_SEARCH_BARCODE) {
    $words[] = $searchText;
} else {
    $words = FixWordsPercent(explodeQuoted($searchText));
}
$look4type = $_REQUEST["look4type"];
if (isset($BibSearchesId[$look4type])) {
    $resType = $BibSearchesId[$look4type];
} else {
    $resType = OBIB_SEARCH_TITLE;
}
$searches = NULL;
if (isset($_REQUEST["searches"])) {
    $searches = explodeSearches($_REQUEST["searches"]);
}
#****************************************************************************
#*  Search database
#****************************************************************************
$biblioQ = new BiblioSearchQuery();