Пример #1
0
/**
 * Builds an array of product search terms from an array of input (handles advanced language searching, category selections)
 *
 * @param array Array of search input
 * @return array Formatted search input array
 */
function BuildProductSearchTerms($input)
{
    $regex_chars = array('/\\//', '/\\^/', '/\\$/', '/\\./', '/\\[/', '/\\]/', '/\\|/', '/\\(/', '/\\)/', '/\\?/', '/\\*/', '/\\+/', '/\\{/', '/\\}/');
    $regex_replace_chars = array('\\/', '\\^', '\\$', '\\.', '\\[', '\\]', '\\|', '\\(', '\\)', '\\?', '\\*', '\\+', '\\{', '\\}');
    //wirror_2010_10_21:search enhancement
    $ismartSearch = isset($input['is_smart_search']) && $input['is_smart_search'] == 1 ? true : false;
    $input['search_query'] = '';
    if (isset($input['search_query'])) {
        $input['search_query'] = html_entity_decode($input['search_query']);
    }
    $input['search_query'] = trim($input['search_query']);
    $spl_strings = array("+", ":", ";", "!", "@", "#", "\$", "%", "^", "*", "\\");
    $input['search_query'] = str_replace($spl_strings, " ", $input['search_query']);
    $input['search_query'] = preg_replace('/[ ]{2,}|[\\t]/', ' ', trim($input['search_query']));
    $srch_arr = array();
    if (!empty($input['search_query'])) {
        $srch_arr = explode(" ", $input['search_query']);
    }
    $make = "";
    $model = "";
    $submodel = "";
    $startyear = "";
    $endyear = "";
    $compare_catg_name = "";
    // this variable is used to check the position of catgory and brand
    $compare_brand_name = "";
    // this variable is used to check the position of catgory and brand
    array_walk($srch_arr, 'lower');
    $new_srch_str = implode("','", $srch_arr);
    /*-- finding the qualifiers----*/
    $qualifier_count = 0;
    $GLOBALS['v_cols'] = array();
    $GLOBALS['p_cols'] = array();
    $GLOBALS['visible_pqvq'] = array();
    /*-- end of finding the qualifiers----*/
    $count_srch = count($srch_arr);
    $flag_brand = 0;
    $flag_brand_series = 0;
    $flag_catg = 0;
    $flag_sub_catg = 0;
    // 0 for parent catg & 1 for sub-catg
    $flag_alt_names = 0;
    // 0 for not matching alternate names & 1 for matching
    if ($count_srch > 0) {
        // if there are any search criterias, then we need to check it in category list.
        $brandname = array();
        $brandaltname = array();
        $brand_qry = "select brandid, brandname, brandaltkeyword from [|PREFIX|]brands order by CHAR_LENGTH(brandname) desc";
        $brand_res = $GLOBALS['ISC_CLASS_DB']->Query($brand_qry);
        while ($brand_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($brand_res)) {
            $brandname[$brand_arr['brandid']] = $brand_arr['brandname'];
            $brandaltname[$brand_arr['brandid']] = $brand_arr['brandaltkeyword'];
        }
        $catg_qry = "select * from [|PREFIX|]categories order by catparentid ASC , CHAR_LENGTH(catname) DESC";
        $catg_res = $GLOBALS['ISC_CLASS_DB']->Query($catg_qry);
        $catgoryname = array();
        // array for category names
        $categoryaltname = array();
        // array for alternate keywords
        $catuniversal = array();
        while ($catg_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($catg_res)) {
            $catgoryname[$catg_arr['categoryid']]['catname'] = $catg_arr['catname'];
            $catgoryname[$catg_arr['categoryid']]['catparentid'] = $catg_arr['catparentid'];
            $categoryaltname[$catg_arr['categoryid']]['catname'] = $catg_arr['catname'];
            $categoryaltname[$catg_arr['categoryid']]['altkeywords'] = $catg_arr['cataltkeyword'];
            $categoryaltname[$catg_arr['categoryid']]['catparentid'] = $catg_arr['catparentid'];
            $catuniversal[$catg_arr['categoryid']] = $catg_arr['catuniversal'];
        }
        $GLOBALS['categories_all'] = $catgoryname;
        //wirror_2010_10_21: search enhancement
        if (!$ismartSearch) {
            $common_qry = "select brandname as typename, 'brand' as type, brandid as 'id' from [|PREFIX|]brands\n\t\t\t\t\t\t\t\tUNION ALL\n\t\t\t\t\t\t\t\t( select catname as typename, 'category' as type, categoryid as 'id' from [|PREFIX|]categories c order by catparentid )\n\t\t\t\t\t\t\t\torder by CHAR_LENGTH(typename) DESC , typename asc ";
            $common_res = $GLOBALS['ISC_CLASS_DB']->Query($common_qry);
            while ($common_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($common_res)) {
                $brand_name = strtolower($common_arr['typename']);
                $in_str = "";
                $track_srch_key = array();
                for ($k = 0; $k < $count_srch; $k++) {
                    $srchkey = preg_replace($regex_chars, $regex_replace_chars, $srch_arr[$k]);
                    if ($in_str == "") {
                        $match = "/(^{$srchkey}(\\s{1})|^{$srchkey}\\b)/i";
                    } else {
                        $match = "/(((\\s{1}){$srchkey}(\\s{1}))|((\\s{1}){$srchkey}\$))/i";
                    }
                    if (preg_match($match, $brand_name)) {
                        $track_srch_key[] = $k;
                        if (!empty($in_str)) {
                            $in_str .= " " . $srch_arr[$k];
                        } else {
                            $in_str .= $srch_arr[$k];
                        }
                        if ($brand_name == $in_str) {
                            // if matched early, then need to break it here.
                            break;
                        }
                    }
                }
                if ($in_str == '') {
                    for ($k = 0; $k < $count_srch; $k++) {
                        $srchkey = preg_replace($regex_chars, $regex_replace_chars, rtrim($srch_arr[$k], 's'));
                        if ($in_str == "") {
                            $match = "/(^{$srchkey}(\\s{1})|^{$srchkey}\\b)/i";
                        } else {
                            $match = "/(((\\s{1}){$srchkey}(\\s{1}))|((\\s{1}){$srchkey}\$))/i";
                        }
                        if (preg_match($match, $brand_name)) {
                            $track_srch_key[] = $k;
                            if (!empty($in_str)) {
                                $in_str .= " " . rtrim($srch_arr[$k], 's');
                            } else {
                                $in_str .= rtrim($srch_arr[$k], 's');
                            }
                            if ($brand_name == $in_str) {
                                // if matched early, then need to break it here.
                                break;
                            }
                        }
                    }
                }
                if ($brand_name == $in_str) {
                    foreach ($track_srch_key as $key => $val) {
                        unset($srch_arr[$val]);
                    }
                    $srch_arr = array_values($srch_arr);
                    if ($common_arr['type'] == 'brand') {
                        $compare_brand_name = $in_str;
                        $brandid = $common_arr['id'];
                        $input['brand'] = $brand_name;
                        $flag_brand = 1;
                        $GLOBALS['ISC_SRCH_BRAND_NAME'] = $brand_name;
                    } else {
                        if ($common_arr['type'] == 'category') {
                            $catg_name = strtolower($common_arr['typename']);
                            $key = $common_arr['id'];
                            if ($catgoryname[$key]['catparentid'] != 0) {
                                // checking its not a parent catg ?
                                $flag_sub_catg = 1;
                            }
                            $compare_catg_name = $in_str;
                            $catg_id = $key;
                            $flag_catg = 1;
                            $input['catuniversal'] = $catuniversal[$key];
                        }
                    }
                    break;
                }
            }
        }
        $count_srch = count($srch_arr);
        /*---- Brand Names --------*/
        //wirror_2010_10_21: search enhancement
        if ($flag_brand == 0 && !$ismartSearch) {
            foreach ($brandname as $bkey => $bvalue) {
                $brand_name = strtolower($bvalue);
                $in_str = "";
                $track_srch_key = array();
                for ($k = 0; $k < $count_srch; $k++) {
                    if (stristr($brand_name, rtrim($srch_arr[$k], 's'))) {
                        $track_srch_key[] = $k;
                        if (!empty($in_str)) {
                            $in_str .= " " . rtrim($srch_arr[$k], 's');
                        } else {
                            $in_str .= rtrim($srch_arr[$k], 's');
                        }
                    }
                }
                if ($brand_name == $in_str or $brand_name == $in_str . 's') {
                    //condition for brand ending with 's'
                    $compare_brand_name = $in_str;
                    foreach ($track_srch_key as $key => $val) {
                        unset($srch_arr[$val]);
                    }
                    $srch_arr = array_values($srch_arr);
                    $brandid = $bkey;
                    $input['brand'] = $bvalue;
                    $flag_brand = 1;
                    $GLOBALS['ISC_SRCH_BRAND_NAME'] = ucwords($bvalue);
                    break;
                }
            }
        }
        /*---- ALternate Names For Brands --------*/
        if ($flag_brand == 0) {
            foreach ($brandaltname as $baltkey => $baltvalue) {
                if (!empty($baltvalue)) {
                    $balt_name = strtolower($baltvalue);
                    $brandalternate_names = explode(",", $balt_name);
                    usort($brandalternate_names, 'altname_sort');
                    for ($i = 0; $i < count($brandalternate_names); $i++) {
                        $in_str = "";
                        $track_srch_key = array();
                        $brand_name = $brandname[$baltkey];
                        for ($k = 0; $k < $count_srch; $k++) {
                            if (stristr($brandalternate_names[$i], rtrim($srch_arr[$k], 's'))) {
                                $track_srch_key[] = $k;
                                if (!empty($in_str)) {
                                    $in_str .= " " . rtrim($srch_arr[$k], 's');
                                } else {
                                    $in_str .= rtrim($srch_arr[$k], 's');
                                }
                            }
                        }
                        if (ltrim($brandalternate_names[$i]) == $in_str || ltrim($brandalternate_names[$i]) == $in_str . 's') {
                            $compare_brand_name = $in_str;
                            foreach ($track_srch_key as $key => $val) {
                                unset($srch_arr[$val]);
                            }
                            $srch_arr = array_values($srch_arr);
                            $brandid = $baltkey;
                            $input['brand'] = $brand_name;
                            $flag_brand = 1;
                            $GLOBALS['ISC_SRCH_BRAND_NAME'] = ucwords($brand_name);
                            break;
                        }
                    }
                }
            }
        }
        if ($flag_brand == 0) {
            $brandseriesname = array();
            $brandseriesaltname = array();
            $brseriesparent = array();
            $series_qry = "select seriesid , s.brandid , brandname , seriesname , seriesaltkeyword from [|PREFIX|]brand_series s left join [|PREFIX|]brands b on b.brandid = s.brandid order by CHAR_LENGTH(seriesname) desc";
            $series_res = $GLOBALS['ISC_CLASS_DB']->Query($series_qry);
            while ($series_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($series_res)) {
                $brseriesparent[$series_arr['seriesid']] = $series_arr['brandid'];
                $brandseriesname[$series_arr['seriesid']] = $series_arr['seriesname'];
                $brandseriesaltname[$series_arr['seriesid']] = $series_arr['seriesaltkeyword'];
            }
            /* -------- for series names ----------- */
            //wirror_2010_10_21: search enhancement
            if (!$ismartSearch) {
                foreach ($brandseriesname as $bskey => $bsvalue) {
                    $series_name = strtolower($bsvalue);
                    $in_str = "";
                    $track_srch_key = array();
                    for ($k = 0; $k < $count_srch; $k++) {
                        if (stristr($series_name, rtrim($srch_arr[$k], 's'))) {
                            $track_srch_key[] = $k;
                            if (!empty($in_str)) {
                                $in_str .= " " . rtrim($srch_arr[$k], 's');
                            } else {
                                $in_str .= rtrim($srch_arr[$k], 's');
                            }
                        }
                    }
                    if ($series_name == $in_str || $series_name == $in_str . 's') {
                        $compare_brand_name = $in_str;
                        foreach ($track_srch_key as $key => $val) {
                            unset($srch_arr[$val]);
                        }
                        $srch_arr = array_values($srch_arr);
                        $brandid = $brseriesparent[$bskey];
                        $input['brand'] = $brandname[$brandid];
                        $input['series'] = $bsvalue;
                        $flag_brand = 1;
                        $flag_brand_series = 1;
                        // need to assign, to know the series has been selected
                        $GLOBALS['BRAND_SERIES_FLAG'] = 1;
                        $GLOBALS['ISC_SRCH_BRAND_NAME'] = ucwords($brandname[$brseriesparent[$bskey]]);
                        break;
                    }
                }
            }
            /* -------- for series alternate keywords ----------- */
            if ($flag_brand == 0) {
                foreach ($brandseriesaltname as $bsaltkey => $bsaltvalue) {
                    if (!empty($bsaltvalue)) {
                        $bsalt_name = strtolower($bsaltvalue);
                        $brandseriesalternate_names = explode(",", $bsalt_name);
                        usort($brandseriesalternate_names, 'altname_sort');
                        for ($i = 0; $i < count($brandseriesalternate_names); $i++) {
                            $in_str = "";
                            $track_srch_key = array();
                            $brand_name = $brandname[$brseriesparent[$bsaltkey]];
                            for ($k = 0; $k < $count_srch; $k++) {
                                if (stristr($brandseriesalternate_names[$i], rtrim($srch_arr[$k], 's'))) {
                                    $track_srch_key[] = $k;
                                    if (!empty($in_str)) {
                                        $in_str .= " " . rtrim($srch_arr[$k], 's');
                                    } else {
                                        $in_str .= rtrim($srch_arr[$k], 's');
                                    }
                                }
                            }
                            if (ltrim($brandseriesalternate_names[$i]) == $in_str || ltrim($brandseriesalternate_names[$i]) == $in_str . 's') {
                                $compare_brand_name = $in_str;
                                foreach ($track_srch_key as $key => $val) {
                                    unset($srch_arr[$val]);
                                }
                                $srch_arr = array_values($srch_arr);
                                $brandid = $brseriesparent[$bsaltkey];
                                $input['brand'] = $brandname[$brandid];
                                $input['series'] = $brandseriesname[$bsaltkey];
                                $flag_brand = 1;
                                $flag_brand_series = 1;
                                // need to assign, to know the series has been selected
                                $GLOBALS['BRAND_SERIES_FLAG'] = 1;
                                $GLOBALS['ISC_SRCH_BRAND_NAME'] = ucwords($brandname[$brandid]);
                                break;
                            }
                        }
                    }
                }
            }
        }
        $count_srch = count($srch_arr);
        if ($count_srch > 0 && $flag_catg == 0) {
            /* Checking first in category names */
            //wirror_2010_10_21: search enhancement
            if (!$ismartSearch) {
                foreach ($catgoryname as $key => $value) {
                    $catg_name = strtolower($catgoryname[$key]['catname']);
                    //$catg_name = strtolower($catgoryname[$i]);
                    $in_str = "";
                    $track_srch_key = array();
                    for ($k = 0; $k < $count_srch; $k++) {
                        if (stristr($catg_name, rtrim($srch_arr[$k], 's'))) {
                            $track_srch_key[] = $k;
                            if (!empty($in_str)) {
                                $in_str .= " " . rtrim($srch_arr[$k], 's');
                            } else {
                                $in_str .= rtrim($srch_arr[$k], 's');
                            }
                            if ($catg_name == $in_str || $catg_name == $in_str . 's') {
                                break;
                            }
                        }
                    }
                    if ($catg_name == $in_str || $catg_name == $in_str . 's') {
                        $compare_catg_name = $in_str;
                        for ($j = 0; $j < count($track_srch_key); $j++) {
                            unset($srch_arr[$track_srch_key[$j]]);
                        }
                        $srch_arr = array_values($srch_arr);
                        if ($catgoryname[$key]['catparentid'] != 0) {
                            // checking its not a parent catg ?
                            $flag_sub_catg = 1;
                        }
                        $catg_id = $key;
                        $flag_catg = 1;
                        $input['catuniversal'] = $catuniversal[$key];
                        break;
                    }
                }
            }
            /* Checking in category alternate names */
            if ($flag_catg == 0) {
                //NI CLOUD 2010-07-28
                //filter to get longest alt keywords
                $altcat = array();
                foreach ($categoryaltname as $altkey => $altvalue) {
                    $categoryaltname[$altkey]['altkeywords'] = trim($categoryaltname[$altkey]['altkeywords']);
                    if (!empty($categoryaltname[$altkey]['altkeywords'])) {
                        $catg_name = strtolower($categoryaltname[$altkey]['catname']);
                        $alternate_names = explode(",", $categoryaltname[$altkey]['altkeywords']);
                        usort($alternate_names, 'altname_sort');
                        for ($i = 0; $i < count($alternate_names); $i++) {
                            $in_str = "";
                            $track_srch_key = array();
                            $altname = strtolower(trim($alternate_names[$i]));
                            for ($k = 0; $k < $count_srch; $k++) {
                                if (strlen(stristr($altname, rtrim($srch_arr[$k], 's'))) > 0) {
                                    $track_srch_key[] = $k;
                                    if (!empty($in_str)) {
                                        $in_str .= " " . $srch_arr[$k];
                                    } else {
                                        $in_str .= $srch_arr[$k];
                                    }
                                    if ($altname == rtrim($in_str, 's')) {
                                        $altcat[] = $altname;
                                    }
                                }
                            }
                        }
                    }
                }
                $max = '';
                for ($m = 0; $m < count($altcat); $m++) {
                    if (strlen($max) < strlen($altcat[$m])) {
                        $max = $altcat[$m];
                    }
                }
                foreach ($categoryaltname as $altkey => $altvalue) {
                    $categoryaltname[$altkey]['altkeywords'] = trim($categoryaltname[$altkey]['altkeywords']);
                    if (!empty($categoryaltname[$altkey]['altkeywords'])) {
                        $catg_name = strtolower($categoryaltname[$altkey]['catname']);
                        $alternate_names = explode(",", $categoryaltname[$altkey]['altkeywords']);
                        usort($alternate_names, 'altname_sort');
                        for ($i = 0; $i < count($alternate_names); $i++) {
                            $in_str = "";
                            $track_srch_key = array();
                            $altname = strtolower(trim($alternate_names[$i]));
                            for ($k = 0; $k < $count_srch; $k++) {
                                if (stristr($max, rtrim($srch_arr[$k], 's'))) {
                                    $track_srch_key[] = $k;
                                    if (!empty($in_str)) {
                                        $in_str .= " " . $srch_arr[$k];
                                    } else {
                                        $in_str .= $srch_arr[$k];
                                    }
                                    if ($max == rtrim($in_str, 's')) {
                                        break;
                                    }
                                }
                            }
                            if ($altname == rtrim($in_str, 's') && strlen($altname) > 0) {
                                $compare_catg_name = rtrim($in_str, 's');
                                for ($j = 0; $j < count($track_srch_key); $j++) {
                                    unset($srch_arr[$track_srch_key[$j]]);
                                }
                                $srch_arr = array_values($srch_arr);
                                if ($categoryaltname[$altkey]['catparentid'] != 0) {
                                    // checking its not a parent catg ?
                                    $flag_sub_catg = 1;
                                }
                                $catg_id = $altkey;
                                $flag_catg = 1;
                                $input['catuniversal'] = $catuniversal[$altkey];
                                break;
                            }
                        }
                        if ($flag_catg == 1) {
                            break;
                        }
                    }
                }
            }
        }
    }
    /*----------------- for selecting category ---------------------*/
    if (isset($_GET['search_key']) || isset($_REQUEST['search'])) {
        if (in_array("chevy", $srch_arr)) {
            $input['make'] = "chevrolet";
            $key = array_search('chevy', $srch_arr);
            unset($srch_arr[$key]);
            $srch_arr = array_values($srch_arr);
        }
        $inner_qry = "";
        $flag_model = 0;
        if (count($srch_arr) > 0) {
            $query = " select distinct q.column_name , v.qid , q_value from isc_qualifier_value v left join isc_qualifier_names q on v.qid = q.qid where v.qid != 0 and ( ";
            foreach ($srch_arr as $key => $value) {
                if (empty($inner_qry)) {
                    $inner_qry .= " q_value like '%{$value}%' ";
                } else {
                    $inner_qry .= " OR q_value like '%{$value}%' ";
                }
            }
            if ($inner_qry == "") {
                $inner_qry .= " 1=1 ";
            }
            $query .= $inner_qry . " ) order by qid , CHAR_LENGTH(q_value) desc";
            $srch_res1 = $GLOBALS['ISC_CLASS_DB']->Query($query);
            $model_array = array();
            $model_qry = "";
            while ($srch_row1 = $GLOBALS['ISC_CLASS_DB']->Fetch($srch_res1)) {
                if ($srch_row1['qid'] == 1) {
                    if (!isset($input['make'])) {
                        $in_make = "";
                        $track_make_array = array();
                        for ($j = 0; $j < count($srch_arr); $j++) {
                            if (stristr($srch_row1['q_value'], $srch_arr[$j])) {
                                $track_make_array[] = $j;
                                if (!empty($in_make)) {
                                    $in_make .= " " . $srch_arr[$j];
                                } else {
                                    $in_make .= $srch_arr[$j];
                                }
                                if ($in_make == strtolower($srch_row1['q_value'])) {
                                    break;
                                }
                            }
                        }
                        if ($in_make == strtolower($srch_row1['q_value'])) {
                            $input['make'] = $srch_row1['q_value'];
                            for ($k = 0; $k < count($track_make_array); $k++) {
                                unset($srch_arr[$track_make_array[$k]]);
                            }
                            $srch_arr = array_values($srch_arr);
                        }
                    }
                } else {
                    if ($srch_row1['qid'] == 2) {
                        if (isset($input['make']) && $model_qry == "" && $input['make'] != "") {
                            $inner_qry = "";
                            $model_qry = "select distinct prodmodel from isc_import_variations where prodmake = '" . $input['make'] . "' and ( ";
                            foreach ($srch_arr as $key => $value) {
                                if (empty($inner_qry)) {
                                    $inner_qry .= " prodmodel like '%{$value}%' ";
                                } else {
                                    $inner_qry .= " OR prodmodel like '%{$value}%' ";
                                }
                            }
                            if (empty($inner_qry)) {
                                $inner_qry = " 1=1 ";
                            }
                            $model_qry .= $inner_qry . " )   order by CHAR_LENGTH(prodmodel) desc";
                            $model_res = $GLOBALS['ISC_CLASS_DB']->Query($model_qry);
                            while ($model_row = $GLOBALS['ISC_CLASS_DB']->Fetch($model_res)) {
                                if ($flag_model == 0) {
                                    $in_str = "";
                                    $track_srch_key = array();
                                    $count_model_str = 1;
                                    $temp_str = "";
                                    $model_array = array();
                                    $model_value = strtolower($model_row['prodmodel']);
                                    for ($k = 0; $k < count($srch_arr); $k++) {
                                        $srchkey = preg_replace($regex_chars, $regex_replace_chars, $srch_arr[$k]);
                                        if ($in_str == "") {
                                            $match = "/(^{$srchkey}(\\s{1})|^{$srchkey}\\b)/i";
                                        } else {
                                            $match = "/((\\s{1}){$srchkey}\\b)/i";
                                        }
                                        if (preg_match($match, $model_value)) {
                                            if ($count_model_str > 1) {
                                                $temp_str = $in_str . " " . $srch_arr[$k];
                                                if (($m = stripos($model_value, $temp_str)) !== false) {
                                                    $track_srch_key[] = $k;
                                                    $in_str .= " " . $srch_arr[$k];
                                                    $count_model_str++;
                                                }
                                            } else {
                                                $track_srch_key[] = $k;
                                                $in_str .= $srch_arr[$k];
                                                $count_model_str++;
                                            }
                                        }
                                    }
                                    //echo "<br>".$in_str;
                                    if ($in_str != "" && $in_str == $model_value) {
                                        $input['model'] = $in_str;
                                        for ($j = 0; $j < count($track_srch_key); $j++) {
                                            unset($srch_arr[$track_srch_key[$j]]);
                                        }
                                        $srch_arr = array_values($srch_arr);
                                        unset($model_array);
                                        $flag_model = 1;
                                        break;
                                    } else {
                                        if ($in_str != "") {
                                            if (!isset($input['model'])) {
                                                $model_array = $track_srch_key;
                                                $input['model'] = $in_str;
                                            } else {
                                                if ($m = stripos($in_str, $input['model']) !== false) {
                                                    $model_array = $track_srch_key;
                                                    $input['model'] = $in_str;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        } else {
                            if (!isset($input['make'])) {
                                if ($flag_model == 0) {
                                    $in_str = "";
                                    $track_srch_key = array();
                                    $count_model_str = 1;
                                    $temp_str = "";
                                    $model_array = array();
                                    $model_value = strtolower($srch_row1['q_value']);
                                    for ($k = 0; $k < count($srch_arr); $k++) {
                                        $srchkey = preg_replace($regex_chars, $regex_replace_chars, $srch_arr[$k]);
                                        if ($in_str == "") {
                                            $match = "/(^{$srchkey}(\\s{1})|^{$srchkey}\\b)/i";
                                        } else {
                                            $match = "/((\\s{1}){$srchkey}\\b)/i";
                                        }
                                        if (preg_match($match, $model_value)) {
                                            if ($count_model_str > 1) {
                                                $temp_str = $in_str . " " . $srch_arr[$k];
                                                if (($m = stripos($model_value, $temp_str)) !== false) {
                                                    $track_srch_key[] = $k;
                                                    $in_str .= " " . $srch_arr[$k];
                                                    $count_model_str++;
                                                }
                                            } else {
                                                $track_srch_key[] = $k;
                                                $in_str .= $srch_arr[$k];
                                                $count_model_str++;
                                            }
                                        }
                                    }
                                    if ($in_str != "" && $in_str == $model_value) {
                                        $input['model'] = $in_str;
                                        for ($j = 0; $j < count($track_srch_key); $j++) {
                                            unset($srch_arr[$track_srch_key[$j]]);
                                        }
                                        $srch_arr = array_values($srch_arr);
                                        unset($model_array);
                                        $flag_model = 1;
                                        break;
                                    } else {
                                        if ($in_str != "") {
                                            if (!isset($input['model'])) {
                                                $model_array = $track_srch_key;
                                                $input['model'] = $in_str;
                                            } else {
                                                if ($m = stripos($in_str, $input['model']) !== false) {
                                                    $model_array = $track_srch_key;
                                                    $input['model'] = $in_str;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        if ($srch_row1['qid'] == 4 || $srch_row1['qid'] == 5) {
                            /* this below patch is used to remove keywords selected from the search array where model is partial selected */
                            if (isset($model_array) && !empty($model_array) && $flag_model == 0) {
                                for ($j = 0; $j < count($model_array); $j++) {
                                    unset($srch_arr[$model_array[$j]]);
                                }
                                $srch_arr = array_values($srch_arr);
                                unset($model_array);
                            }
                            if (!isset($input['year'])) {
                                for ($j = 0; $j < count($srch_arr); $j++) {
                                    if ($srch_row1['q_value'] == $srch_arr[$j]) {
                                        $input['year'] = $srch_row1['q_value'];
                                        unset($srch_arr[$j]);
                                        $srch_arr = array_values($srch_arr);
                                        break;
                                    }
                                }
                            }
                        } else {
                            if ($srch_row1['qid'] != 6 && $srch_row1['qid'] != 7) {
                                /* this below patch is used to remove keywords selected from the search array where model is partial selected */
                                if (isset($model_array) && !empty($model_array) && $flag_model == 0) {
                                    for ($j = 0; $j < count($model_array); $j++) {
                                        unset($srch_arr[$model_array[$j]]);
                                    }
                                    $srch_arr = array_values($srch_arr);
                                    unset($model_array);
                                }
                                if (isset($input[$srch_row1['column_name']])) {
                                    continue;
                                }
                                $qualifier_names = explode(";", $srch_row1['q_value']);
                                for ($j = 0; $j < count($qualifier_names); $j++) {
                                    $in_str = "";
                                    $track_srch_key = array();
                                    $qualifier_name = strtolower($qualifier_names[$j]);
                                    for ($k = 0; $k < count($srch_arr); $k++) {
                                        //if(stristr($srch_row1['prodmodel'],$srch_arr[$j]))
                                        //if(strtolower($srch_row1['prodmodel']) == $srch_arr[$j])
                                        $srchkey = preg_replace($regex_chars, $regex_replace_chars, $srch_arr[$k]);
                                        if ($in_str == "") {
                                            $match = "/(^{$srchkey}(\\s{1})|^{$srchkey}\\b)/i";
                                        } else {
                                            $match = "/(((\\s{1}){$srchkey}(\\s{1}))|((\\s{1}){$srchkey}\$))/i";
                                        }
                                        if (preg_match($match, $qualifier_name)) {
                                            $track_srch_key[] = $k;
                                            if (!empty($in_str)) {
                                                $in_str .= " " . $srch_arr[$k];
                                            } else {
                                                $in_str .= $srch_arr[$k];
                                            }
                                            if ($qualifier_name == $in_str) {
                                                // if matched early, then need to break it here.
                                                break;
                                            }
                                        }
                                    }
                                    if ($qualifier_name == $in_str) {
                                        // if matched early, then need to break it here.
                                        break;
                                    }
                                }
                                if ($qualifier_name == $in_str) {
                                    $input[strtolower($srch_row1['column_name'])] = $qualifier_name;
                                    foreach ($track_srch_key as $key => $val) {
                                        unset($srch_arr[$val]);
                                    }
                                    $srch_arr = array_values($srch_arr);
                                }
                            }
                        }
                    }
                }
                if (count($srch_arr) == 0) {
                    break;
                }
            }
            if (isset($model_array) && !empty($model_array) && $flag_model == 0) {
                for ($j = 0; $j < count($model_array); $j++) {
                    unset($srch_arr[$model_array[$j]]);
                }
                $srch_arr = array_values($srch_arr);
                unset($model_array);
            }
            $bedsize_qry = " select distinct irregular_value as vqvalue from [|PREFIX|]bedsize_translation where irregular_value != 'Short bed' and irregular_value != 'Full Crew Cab' UNION select distinct generalize_value as vqvalue from [|PREFIX|]bedsize_translation where generalize_value != 'Short bed' and generalize_value != 'Mega Cab' order by char_length(vqvalue) desc ";
            $bedsize_res = $GLOBALS['ISC_CLASS_DB']->Query($bedsize_qry);
            $temp_bedsize = array();
            while ($bedsize_row = $GLOBALS['ISC_CLASS_DB']->Fetch($bedsize_res)) {
                $in_str = "";
                $track_srch_key = array();
                //$temp_bedsize = array();
                $count_model_str = 1;
                $bed_value = strtolower($bedsize_row['vqvalue']);
                for ($j = 0; $j < count($srch_arr); $j++) {
                    $srchkey = preg_replace($regex_chars, $regex_replace_chars, $srch_arr[$j]);
                    $match = "/(((\\s{1}){$srchkey}(\\s{1}))|(\\b{$srchkey}(\\s{1}))|((\\s{1}){$srchkey}\\b)|(^{$srchkey}\$))/i";
                    if (preg_match($match, $bed_value)) {
                        if ($count_model_str > 1) {
                            $track_srch_key[] = $j;
                            $in_str .= " " . $srch_arr[$j];
                            $count_model_str++;
                        } else {
                            $track_srch_key[] = $j;
                            $in_str .= $srch_arr[$j];
                            $count_model_str++;
                        }
                    }
                }
                if ($in_str != "" && $in_str == $bed_value) {
                    $input['vqbedsize'] = $in_str;
                    for ($j = 0; $j < count($track_srch_key); $j++) {
                        unset($srch_arr[$track_srch_key[$j]]);
                    }
                    $srch_arr = array_values($srch_arr);
                    unset($temp_bedsize);
                    break;
                } else {
                    if ($in_str != "") {
                        if (count($track_srch_key) == 1 && trim($in_str) == 'bed' && count($track_srch_key) > count($temp_bedsize)) {
                            $input['vqsbedsize'] = $in_str;
                            $temp_bedsize = $track_srch_key;
                        } else {
                            if (count($track_srch_key) > 1 && count($track_srch_key) > count($temp_bedsize)) {
                                $input['vqsbedsize'] = $in_str;
                                $temp_bedsize = $track_srch_key;
                            }
                        }
                    }
                }
            }
            if (isset($temp_bedsize) && !empty($temp_bedsize)) {
                for ($j = 0; $j < count($temp_bedsize); $j++) {
                    unset($srch_arr[$temp_bedsize[$j]]);
                }
                $srch_arr = array_values($srch_arr);
                unset($temp_bedsize);
            }
            $cabsize_qry = " select distinct irregular_value as vqvalue from [|PREFIX|]cabsize_translation\n\t\t\tUNION select distinct generalize_value as vqvalue from [|PREFIX|]cabsize_translation\n\t\t\torder by char_length(vqvalue) desc ";
            $cabsize_res = $GLOBALS['ISC_CLASS_DB']->Query($cabsize_qry);
            $temp_cabsize = array();
            while ($cabsize_row = $GLOBALS['ISC_CLASS_DB']->Fetch($cabsize_res)) {
                $in_str = "";
                $track_srch_key = array();
                $count_model_str = 1;
                $cab_value = strtolower($cabsize_row['vqvalue']);
                for ($j = 0; $j < count($srch_arr); $j++) {
                    $srchkey = preg_replace($regex_chars, $regex_replace_chars, $srch_arr[$j]);
                    $match = "/(((\\s{1}){$srchkey}(\\s{1}))|(\\b{$srchkey}(\\s{1}))|((\\s{1}){$srchkey}\\b)|(^{$srchkey}\$))/i";
                    if (preg_match($match, $cab_value)) {
                        if ($count_model_str > 1) {
                            $track_srch_key[] = $j;
                            $in_str .= " " . $srch_arr[$j];
                            $count_model_str++;
                        } else {
                            $track_srch_key[] = $j;
                            $in_str .= $srch_arr[$j];
                            $count_model_str++;
                        }
                    }
                }
                if ($in_str != "" && $in_str == $cab_value) {
                    $input['vqcabsize'] = $in_str;
                    for ($j = 0; $j < count($track_srch_key); $j++) {
                        unset($srch_arr[$track_srch_key[$j]]);
                    }
                    $srch_arr = array_values($srch_arr);
                    unset($temp_cabsize);
                    break;
                } else {
                    if ($in_str != "") {
                        if (count($track_srch_key) == 1 && trim($in_str) == 'cab' && count($track_srch_key) > count($temp_cabsize)) {
                            $input['vqscabsize'] = $in_str;
                            $temp_cabsize = $track_srch_key;
                        } else {
                            if (count($track_srch_key) > 1 && count($track_srch_key) > count($temp_bedsize)) {
                                $input['vqscabsize'] = $in_str;
                                $temp_cabsize = $track_srch_key;
                            }
                        }
                    }
                }
            }
            if (isset($temp_cabsize) && !empty($temp_cabsize)) {
                for ($j = 0; $j < count($temp_cabsize); $j++) {
                    unset($srch_arr[$temp_cabsize[$j]]);
                }
                $srch_arr = array_values($srch_arr);
                unset($temp_cabsize);
            }
            /*
            			 if(!isset($input['make']) && isset($input['model']) )
            			 {
            $get_make_qry = "select distinct make from isc_product_mmy where model like '".$input['model']."%' limit 0,1";
            $get_make_res = $GLOBALS['ISC_CLASS_DB']->Query($get_make_qry);
            $get_make_row = $GLOBALS['ISC_CLASS_DB']->FetchOne($get_make_res);
            $input['make'] = $get_make_row;
            }
            */
            if (!isset($input['year'])) {
                for ($i = 0; $i < count($srch_arr); $i++) {
                    if (is_numeric($srch_arr[$i])) {
                        if (strlen($srch_arr[$i]) == 2) {
                            $prodyr = $srch_arr[$i];
                            $curr_yr = date('y');
                            if ($prodyr >= 00 && $prodyr <= $curr_yr) {
                                $prodyr = 2000 + $prodyr;
                            } else {
                                $prodyr = 1900 + $prodyr;
                            }
                        } else {
                            $prodyr = $srch_arr[$i];
                        }
                        $year_qry = " select year1 , year2  from (select min(prodstartyear) as year1 from isc_import_variations where prodstartyear != '') sy , (select max(prodendyear) as year2 from isc_import_variations where prodendyear != 'ALL' ) ey where {$prodyr} between year1 and year2 ";
                        $year_result = $GLOBALS['ISC_CLASS_DB']->Query($year_qry);
                        $year_row = $GLOBALS['ISC_CLASS_DB']->CountResult($year_result);
                        if ($year_row == 1) {
                            $input['year'] = $prodyr;
                            unset($srch_arr[$i]);
                            $srch_arr = array_values($srch_arr);
                            break;
                        }
                    }
                }
            }
            // checking the partnumber from the remaining search parameters
            if (count($srch_arr) > 0) {
                $inner_qry = "";
                $product_code_query = " SELECT prodcode FROM [|PREFIX|]products p WHERE ";
                foreach ($srch_arr as $key => $value) {
                    if (empty($inner_qry)) {
                        $inner_qry .= " prodcode like '{$value}%' ";
                    } else {
                        $inner_qry .= " OR prodcode like '{$value}%' ";
                    }
                }
                $product_code_query .= $inner_qry;
                $product_code_res = $GLOBALS['ISC_CLASS_DB']->Query($product_code_query);
                while ($product_code_row = $GLOBALS['ISC_CLASS_DB']->Fetch($product_code_res)) {
                    $prod_code = strtolower($product_code_row['prodcode']);
                    $track_srch_key = array();
                    for ($j = 0; $j < count($srch_arr); $j++) {
                        if (stristr($prod_code, $srch_arr[$j])) {
                            if ($prod_code == $srch_arr[$j]) {
                                unset($srch_arr[$j]);
                                $srch_arr = array_values($srch_arr);
                                $input['partnumber'] = $prod_code;
                                $partnumber_flag = 0;
                                break;
                            } else {
                                $input['partnumber'] = $srch_arr[$j];
                                $partnumber_flag = 1;
                            }
                        }
                    }
                    if ($partnumber_flag == 0) {
                        break;
                    }
                }
            }
        }
        $input['search_string'] = implode(" ", $srch_arr);
        /*----------------- End for selecting category ---------------------*/
    }
    $searchTerms = array();
    $matches = array();
    $searchTerms['dynfilters'] = array();
    /*----------------- for selecting category ---------------------*/
    $searchTerms['flag_srch_category'] = 0;
    if (isset($input['partnumber'])) {
        $searchTerms['partnumber'] = $input['partnumber'];
    }
    if (isset($input['search'])) {
        $searchTerms['search'] = $input['search'];
    }
    if (isset($input['searchtext'])) {
        $searchTerms['searchtext'] = $input['searchtext'];
    }
    if (isset($input['search_string'])) {
        $searchTerms['search_string'] = $input['search_string'];
    }
    /* the below code is to find out whether category or brand is entered first */
    $check_position = 0;
    // 1 if catg is first and 0 if brand is first
    if ($flag_catg == 1 && $flag_brand == 1) {
        $catg_pos = stripos($input['search_query'], $compare_catg_name);
        $brand_pos = stripos($input['search_query'], $compare_brand_name);
        if ($catg_pos < $brand_pos) {
            $check_position = 1;
        } else {
            $input['category'] = $catg_name;
            $flag_catg = 0;
            // resetting to 0 as category is entered after brand so it should be passed in $input['category']
            $check_position = 0;
        }
    }
    if ($flag_brand == 1 && $check_position == 0) {
        $searchTerms['flag_srch_brand'] = $flag_brand_series;
    }
    if (isset($input['category'])) {
        if (!array_key_exists('srch_category', $searchTerms)) {
            $searchTerms['srch_category'] = array();
        }
        $searchTerms['category'] = $input['category'];
        $category_name = $input['category'];
        $get_catgories_qry = "SELECT categoryid FROM isc_categories WHERE (catname='" . $category_name . "' OR catparentid IN (SELECT categoryid FROM isc_categories WHERE catname='" . $category_name . "')) order by catparentid";
        $get_category_res = $GLOBALS['ISC_CLASS_DB']->Query($get_catgories_qry);
        while ($get_category_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($get_category_res)) {
            $searchTerms['srch_category'][] = $get_category_arr['categoryid'];
        }
        $GLOBALS['ISC_SRCH_CATG_NAME'] = ucwords($category_name);
        $GLOBALS['ISC_SRCH_CATG_ID'] = $searchTerms['srch_category'][0];
        if (isset($searchTerms['srch_category'][0]) && array_key_exists($searchTerms['srch_category'][0], $catuniversal)) {
            $input['catuniversal'] = $searchTerms['catuniversal'] = $catuniversal[$searchTerms['srch_category'][0]];
        }
    }
    if ($flag_catg == 1) {
        $GLOBALS['ISC_SRCH_CATG_NAME'] = ucwords($catg_name);
        $GLOBALS['ISC_SRCH_CATG_ID'] = $catg_id;
        /* the below patch has been added below for the catogories having the make as non-spec vehicle */
        /*if($catg_id == 17 OR $catg_id == 3 OR $catg_id == 7 OR $catg_id == 18 OR $catg_id == 1 OR $catg_id == 2) {
        	 $searchTerms['is_catg'] = 1;
        	 }*/
        $sub_catgid = array();
        if ($flag_sub_catg == 0) {
            // if its a parent catg, need to find the sub-categories
            $sub_catgqry = "select categoryid from isc_categories where catparentid = {$catg_id} ";
            $sub_catgres = $GLOBALS['ISC_CLASS_DB']->Query($sub_catgqry);
            if ($GLOBALS['ISC_CLASS_DB']->CountResult($sub_catgres) > 0) {
                // if there are no subcategories under a main category
                while ($sub_catgarr = $GLOBALS['ISC_CLASS_DB']->Fetch($sub_catgres)) {
                    $sub_catgid[] = $sub_catgarr['categoryid'];
                }
                $sub_catgid[] = $catg_id;
            } else {
                $flag_sub_catg = 1;
                $sub_catgid[] = $catg_id;
            }
        } else {
            // else it is a sub-category
            $sub_catgid[] = $catg_id;
        }
        $searchTerms['flag_srch_category'] = $flag_sub_catg;
        $searchTerms['srch_category'] = $sub_catgid;
        if (isset($input['catuniversal'])) {
            $searchTerms['catuniversal'] = $input['catuniversal'];
        }
    }
    /*----------------- End for selecting category ---------------------*/
    // Here we parse out any advanced search identifiers from the search query such as price:, :rating etc
    $advanced_params = array(GetLang('SearchLangPrice'), GetLang('SearchLangRating'), GetLang('SearchLangInStock'), GetLang('SearchLangFeatured'), GetLang('SearchLangFreeShipping'));
    if (isset($input['search_query'])) {
        $query = str_replace(array("&lt;", "&gt;"), array("<", ">"), $input['search_query']);
        foreach ($advanced_params as $param) {
            if ($param == GetLang('SearchLangPrice') || $param == GetLang('SearchLangRating')) {
                $match = sprintf("(<|>)?([0-9\\.%s]+)-?([0-9\\.%s]+)?", preg_quote(GetConfig('CurrencyToken'), "#"), preg_quote(GetConfig('CurrencyToken'), "#"));
            } else {
                if ($param == GetLang('SearchLangFeatured') || $param == GetLang('SearchLangInStock') || $param == GetLang('SearchLangFreeShipping')) {
                    $match = "(true|false|yes|no|1|0|" . preg_quote(GetLang('SearchLangYes'), "#") . "|" . preg_quote(GetLang('SearchLangNo'), "#") . ")";
                } else {
                    continue;
                }
            }
            preg_match("#\\s" . preg_quote($param, "#") . ":" . $match . '(\\s|$)#i', $query, $matches);
            if (count($matches) > 0) {
                if ($param == "price" || $param == "rating") {
                    if ($matches[3]) {
                        $input[$param . '_from'] = (double) $matches[2];
                        $input[$param . '_to'] = (double) $matches[3];
                    } else {
                        if ($matches[1] == "<") {
                            $input[$param . '_to'] = (double) $matches[2];
                        } else {
                            if ($matches[1] == ">") {
                                $input[$param . '_from'] = (double) $matches[2];
                            } else {
                                if ($matches[1] == "") {
                                    $input[$param] = (double) $matches[2];
                                }
                            }
                        }
                    }
                } else {
                    if ($param == "featured" || $param == "instock" || $param == "freeshipping") {
                        if ($param == "freeshipping") {
                            $param = "shipping";
                        }
                        if ($matches[1] == "true" || $matches[1] == "yes" || $matches[1] == 1) {
                            $input[$param] = 1;
                        } else {
                            $input[$param] = 0;
                        }
                    }
                }
                $matches[0] = str_replace(array("<", ">"), array("&lt;", "&gt;"), $matches[0]);
                $input['search_query'] = trim(preg_replace("#" . preg_quote(trim($matches[0]), "#") . "#i", "", $input['search_query']));
            }
        }
        // Pass the modified search query back
        $searchTerms['search_query'] = $input['search_query'];
    }
    if (isset($input['categoryid'])) {
        $input['category'] = $input['categoryid'];
    }
    /*
    if (isset($input['category'])) {
    if (!is_array($input['category'])) {
    $input['category'] = array($input['category']);
    }
    $searchTerms['category'] = $input['category'];
    }
    */
    if (isset($input['searchsubs']) && $input['searchsubs'] != "") {
        $searchTerms['searchsubs'] = $input['searchsubs'];
    }
    if (isset($input['price']) && $input['price'] != "") {
        $searchTerms['price'] = $input['price'];
    }
    if (isset($input['price_from']) && $input['price_from'] != "") {
        $searchTerms['price_from'] = $input['price_from'];
    }
    if (isset($input['price_to']) && $input['price_to'] != "") {
        $searchTerms['price_to'] = $input['price_to'];
    }
    if (isset($input['rating']) && $input['rating'] != "") {
        $searchTerms['rating'] = $input['rating'];
    }
    if (isset($input['rating_from']) && $input['rating_from'] != "") {
        $searchTerms['rating_from'] = $input['rating_from'];
    }
    if (isset($input['rating_to']) && $input['rating_to'] != "") {
        $searchTerms['rating_to'] = $input['rating_to'];
    }
    if (isset($input['featured']) && is_numeric($input['featured']) != "") {
        $searchTerms['featured'] = (int) $input['featured'];
    }
    if (isset($input['shipping']) && is_numeric($input['shipping']) != "") {
        $searchTerms['shipping'] = (int) $input['shipping'];
    }
    if (isset($input['instock']) && is_numeric($input['instock'])) {
        $searchTerms['instock'] = (int) $input['instock'];
    }
    if (isset($input['brand']) && $input['brand'] != "") {
        $searchTerms['brand'] = $input['brand'];
        $brand_query = "select brandid from [|PREFIX|]brands WHERE brandname='" . $searchTerms['brand'] . "'";
        $brand_result = $GLOBALS['ISC_CLASS_DB']->Query($brand_query);
        $GLOBALS['brandId'] = $GLOBALS['ISC_CLASS_DB']->FetchOne($brand_result);
        /* this below condition is added to get the brand name when clicked on brand after selecting any category */
        if (!isset($GLOBALS['ISC_SRCH_BRAND_NAME'])) {
            $GLOBALS['ISC_SRCH_BRAND_NAME'] = $searchTerms['brand'];
        }
    }
    if (isset($input['make']) && $input['make'] != "") {
        $searchTerms['make'] = $input['make'];
        unset($input['prodmake']);
    }
    /* this below 3 lines are added as db has the name as prodmodel for model and prodsubmodel as submodel*/
    if (isset($input['prodmodel']) && $input['prodmodel'] != "") {
        $input['model'] = $input['prodmodel'];
    }
    if (isset($input['model']) && $input['model'] != "" && (!isset($input['search_key']) || isset($input['search_key']) && $input['model'] != 'all')) {
        $searchTerms['model'] = $input['model'];
        unset($input['prodmodel']);
        $_REQUEST['model'] = $input['model'];
        /* the below flag is set to check whether exact model is searched or similar to the model is searched */
        if (isset($flag_model)) {
            $searchTerms['model_flag'] = $flag_model;
        }
    }
    /* the below condition is checked whether the flag is returning from the links in the side filter section in search result page */
    if (isset($input['model_flag']) && $input['model_flag'] != "") {
        $searchTerms['model_flag'] = $input['model_flag'];
    }
    if (isset($input['prodsubmodel']) && $input['prodsubmodel'] != "") {
        $input['submodel'] = $input['prodsubmodel'];
    }
    if (isset($input['submodel']) && $input['model'] != "") {
        $searchTerms['submodel'] = $input['submodel'];
        unset($input['prodsubmodel']);
    }
    if (isset($input['year']) && $input['year'] != "" && (!isset($input['search_key']) || isset($input['search_key']) && $input['year'] != 'all')) {
        $searchTerms['year'] = $input['year'];
    }
    /*if (isset($input['brand']) && $input['brand'] != "") {
    	 $searchTerms['brand'] = $input['brand'];
    	 }
    
    	 if (isset($input['from_price']) && $input['from_price'] != "") {
    	 $searchTerms['from_price'] = $input['from_price'];
    	 }
    
    	 if (isset($input['to_price']) && $input['to_price'] != "") {
    	 $searchTerms['to_price'] = $input['to_price'];
    	 }
    
    		if (isset($input['bedsize']) && $input['bedsize'] != "") {
    		$searchTerms['bedsize'] = $input['bedsize'];
    		}
    
    		if (isset($input['cover']) && $input['cover'] != "") {
    		$searchTerms['cover'] = $input['cover'];
    		}
    
    		if (isset($input['color']) && $input['color'] != "") {
    		$searchTerms['color'] = $input['color'];
    		}*/
    if (isset($input['subcategory']) && !empty($input['subcategory'])) {
        $sub_category = $input['subcategory'];
        $searchTerms['subcategory'] = $input['subcategory'];
        $sub_catg_qry = "select categoryid , catname from isc_categories where catname = '" . $GLOBALS['ISC_CLASS_DB']->Quote($sub_category) . "' and catparentid = '" . $GLOBALS['ISC_CLASS_DB']->Quote($GLOBALS['ISC_SRCH_CATG_ID']) . "'";
        $sub_catg_res = $GLOBALS['ISC_CLASS_DB']->Query($sub_catg_qry);
        $sub_catg_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($sub_catg_res);
        if ($GLOBALS['ISC_CLASS_DB']->CountResult($sub_catg_res) > 0) {
            $GLOBALS['subcategoryid'] = $sub_catg_arr['categoryid'];
        } else {
            $sub_catg_qry = "select categoryid , catname from isc_categories where categoryid = '" . $GLOBALS['ISC_CLASS_DB']->Quote($GLOBALS['ISC_SRCH_CATG_ID']) . "'";
            $sub_catg_res = $GLOBALS['ISC_CLASS_DB']->Query($sub_catg_qry);
            $sub_catg_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($sub_catg_res);
            $GLOBALS['subcategoryid'] = $sub_catg_arr['categoryid'];
        }
        /* the below query is written to get the main category anme when subcategory is selected directly */
        $main_catg_qry = "select p.categoryid , p.catname from isc_categories as p inner join isc_categories as c on p.categoryid = c.catparentid and c.catname = '" . $GLOBALS['ISC_CLASS_DB']->Quote($sub_category) . "' and c.catparentid = '" . $GLOBALS['ISC_CLASS_DB']->Quote($GLOBALS['ISC_SRCH_CATG_ID']) . "'";
        $main_catg_res = $GLOBALS['ISC_CLASS_DB']->Query($main_catg_qry);
        $main_catg_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($main_catg_res);
        $main_catg_rows = $GLOBALS['ISC_CLASS_DB']->CountResult($main_catg_res);
        if ($main_catg_rows == 0) {
            $main_catg_arr = $sub_catg_arr;
        }
        $GLOBALS['ISC_SRCH_CATG_NAME'] = ucwords($main_catg_arr['catname']);
        $GLOBALS['ISC_SRCH_CATG_ID'] = $main_catg_arr['categoryid'];
        //if(in_array($sub_category,$sub_catgid)) {
        //$sub_catgid = $sub_category;
        $searchTerms['srch_category'] = array($GLOBALS['subcategoryid']);
        $searchTerms['flag_srch_category'] = 1;
        if (isset($GLOBALS['subcategoryid']) && array_key_exists($GLOBALS['subcategoryid'], $catuniversal)) {
            $searchTerms['catuniversal'] = $catuniversal[$GLOBALS['subcategoryid']];
        }
        //}
    }
    // need to know the category name when brand and series are selected
    if (isset($input['series']) && !empty($input['series'])) {
        $searchTerms['series'] = $input['series'];
        $searchTerms['flag_srch_brand'] = 1;
        $GLOBALS['BRAND_SERIES_FLAG'] = 1;
        $main_catg_qry = "select s.seriesid from isc_brand_series s where s.seriesname = '" . $input['series'] . "' ";
        $main_catg_res = $GLOBALS['ISC_CLASS_DB']->Query($main_catg_qry);
        $main_catg_rows = $GLOBALS['ISC_CLASS_DB']->CountResult($main_catg_res);
        $main_catg_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($main_catg_res);
        $GLOBALS['seriesid'] = $main_catg_arr['seriesid'];
        if ($main_catg_rows > 0) {
            /*$GLOBALS['ISC_SRCH_CATG_NAME'] =  ucwords($main_catg_arr['catname']);
            		$GLOBALS['ISC_SRCH_CATG_ID'] =  $main_catg_arr['categoryid'];
            		$searchTerms['srch_category'] =  array($GLOBALS['ISC_SRCH_CATG_ID']);
            		$searchTerms['catuniversal'] = $catuniversal[$main_catg_arr['categoryid']];*/
        }
    }
    if (isset($GLOBALS['ISC_SRCH_CATG_ID'])) {
        $RelatedCatsQuery = "SELECT DISTINCT c.categoryid\n                                FROM isc_products p \n                                LEFT JOIN isc_categoryassociations ca ON ca.productid = p.productid \n                                LEFT JOIN isc_categories c ON c.categoryid = ca.categoryid ";
        if (isset($input['series']) && !empty($input['series'])) {
            $RelatedCatsQuery .= "\n                        LEFT JOIN isc_brands b ON prodbrandid = b.brandid \n                        LEFT JOIN isc_brand_series AS bs ON bs.seriesid = p.brandseriesid ";
        }
        $RelatedCatsQuery .= " WHERE 1=1 AND c.categoryid IS NOT NULL AND p.prodvisible='1' ";
        if (in_array($GLOBALS['ISC_SRCH_CATG_ID'], $searchTerms['srch_category'])) {
            // No subcatg under category
            $sidequalifier_query = "select distinct qn.qid , qn.column_name , qa.qualifier_visible from isc_qualifier_associations qa left join isc_qualifier_names qn on qn.qid = qa.qualifierid\n                    where qa.categoryid = " . $GLOBALS['ISC_SRCH_CATG_ID'];
            $RelatedCatsQuery .= " AND (c.categoryid = " . $GLOBALS['ISC_SRCH_CATG_ID'] . " || c.catparentid = " . $GLOBALS['ISC_SRCH_CATG_ID'] . ")";
        } else {
            if (!in_array($GLOBALS['ISC_SRCH_CATG_ID'], $searchTerms['srch_category']) && isset($input['subcategory'])) {
                //   product listing page
                $sidequalifier_query = "select distinct qn.qid , qn.column_name , qa.qualifier_visible from isc_qualifier_associations qa left join isc_qualifier_names qn on qn.qid = qa.qualifierid where qa.categoryid = " . $GLOBALS['subcategoryid'];
                $RelatedCatsQuery .= " AND c.categoryid = " . $GLOBALS['subcategoryid'] . "";
            }
        }
        if (isset($input['series']) && !empty($input['series'])) {
            if (isset($GLOBALS['brandId'])) {
                $brand_id = (int) $GLOBALS['brandId'];
                $RelatedCatsQuery .= " AND p.prodbrandid='" . $GLOBALS['ISC_CLASS_DB']->Quote($brand_id) . "' AND p.brandseriesid = " . $GLOBALS['seriesid'] . "";
            }
        }
        $RelatedCatsResult = $GLOBALS['ISC_CLASS_DB']->Query($RelatedCatsQuery);
        $RelatedCats = array();
        while ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($RelatedCatsResult)) {
            $RelatedCats[] = (int) $row['categoryid'];
        }
        $RelatedCats = implode(",", $RelatedCats);
        $qualifier_query = " SELECT\n                            DISTINCT qn.qid , qn.column_name , qa.qualifier_visible \n                            FROM isc_qualifier_associations qa \n                            LEFT JOIN isc_qualifier_names qn ON qn.qid = qa.qualifierid      \n                            WHERE qa.categoryid IN (" . $RelatedCats . ")";
        //OR qa.categoryid IN (SELECT catparentid FROM isc_categories WHERE categoryid IN (".$RelatedCats."))
        GetQualifierColumns($qualifier_query, $qualifier_columns, $VCols, $PCols);
        $GLOBALS['sidev_cols'] = $VCols;
        $GLOBALS['sidep_cols'] = $PCols;
        GetQualifierColumns($sidequalifier_query, $sidequalifier_columns, $SideVCols, $SidePCols);
        $GLOBALS['v_cols'] = $SideVCols;
        $GLOBALS['p_cols'] = $SidePCols;
    } else {
        $RelatedCatsQuery = "SELECT DISTINCT c.categoryid\n                                FROM isc_products p \n                                LEFT JOIN isc_categoryassociations ca ON ca.productid = p.productid \n                                LEFT JOIN isc_categories c ON c.categoryid = ca.categoryid ";
        if (isset($input['series']) && !empty($input['series'])) {
            $RelatedCatsQuery .= "\n                        LEFT JOIN isc_brands b ON prodbrandid = b.brandid \n                        LEFT JOIN isc_brand_series AS bs ON bs.seriesid = p.brandseriesid ";
        }
        $RelatedCatsQuery .= " WHERE 1=1 AND c.categoryid IS NOT NULL AND p.prodvisible='1' ";
        if (isset($input['series']) && !empty($input['series'])) {
            if (isset($GLOBALS['brandId'])) {
                $brand_id = (int) $GLOBALS['brandId'];
                $RelatedCatsQuery .= " AND p.prodbrandid='" . $GLOBALS['ISC_CLASS_DB']->Quote($brand_id) . "' AND p.brandseriesid = " . $GLOBALS['seriesid'] . "";
            }
        }
        //$RelatedCatsQuery = $GLOBALS['StartNumber'];
        $RelatedCatsResult = $GLOBALS['ISC_CLASS_DB']->Query($RelatedCatsQuery);
        $RelatedCats = array();
        while ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($RelatedCatsResult)) {
            $RelatedCats[] = (int) $row['categoryid'];
        }
        $RelatedCats = implode(",", $RelatedCats);
        $qualifier_query = "SELECT\n                                DISTINCT qn.qid , qn.column_name , qa.qualifier_visible \n                                FROM isc_qualifier_associations qa \n                                LEFT JOIN isc_qualifier_names qn ON qn.qid = qa.qualifierid      \n                                WHERE qa.categoryid IN (" . $RelatedCats . ")";
        //OR qa.categoryid IN (SELECT catparentid FROM isc_categories WHERE categoryid IN (".$RelatedCats."))
        $qualifier_result = $GLOBALS['ISC_CLASS_DB']->Query($qualifier_query);
        GetQualifierColumns($qualifier_query, $qualifier_columns, $VCols, $PCols);
        $GLOBALS['sidev_cols'] = $VCols;
        $GLOBALS['sidep_cols'] = $PCols;
    }
    $GLOBALS['v_cols'] = isset($GLOBALS['v_cols']) ? $GLOBALS['v_cols'] : array();
    $GLOBALS['p_cols'] = isset($GLOBALS['p_cols']) ? $GLOBALS['p_cols'] : array();
    $qualifier_count = count($GLOBALS['sidev_cols']) + count($GLOBALS['sidep_cols']);
    /*------------ For Dynamic filters ------------------*/
    for ($qid = 0; $qid < $qualifier_count; $qid++) {
        $dynval = strtolower($qualifier_columns[$qid]);
        if (isset($input[$dynval]) && !empty($input[$dynval])) {
            $q_value = $input[$dynval];
            $dynval = preg_replace("/^([a-zA-Z0-9]{2})/e", "strtoupper('\\1')", $dynval);
            // making the first 2 letters capital of the qualifier name
            $searchTerms['dynfilters'][$dynval] = $q_value;
        }
    }
    if (isset($input['vqbedsize'])) {
        unset($input['vqsbedsize']);
    } else {
        if (isset($input['vqsbedsize'])) {
            $searchTerms['vqsbedsize'] = $input['vqsbedsize'];
        }
    }
    if (isset($input['vqcabsize'])) {
        unset($input['vqscabsize']);
    } else {
        if (isset($input['vqscabsize'])) {
            $searchTerms['vqscabsize'] = $input['vqscabsize'];
        }
    }
    /*====================== Cookies section below ================================ */
    $number_of_days = 730;
    $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days;
    if (isset($input['vqbedsize'])) {
        setcookie("last_search_selection[bedsize]", $input['vqbedsize'], $date_of_expiry, "/");
    } else {
        setcookie("last_search_selection[bedsize]", '', $date_of_expiry, "/");
    }
    if (isset($input['vqcabsize'])) {
        setcookie("last_search_selection[cabsize]", $input['vqcabsize'], $date_of_expiry, "/");
    } else {
        setcookie("last_search_selection[cabsize]", '', $date_of_expiry, "/");
    }
    if (isset($_COOKIE['last_search_selection']) && !isset($searchTerms['make']) && !isset($GLOBALS['ISC_CLASS_REDEFINE_SEARCH']) && !isset($input['change'])) {
        if (!empty($_COOKIE['last_search_selection']['make'])) {
            $searchTerms['make'] = $_COOKIE['last_search_selection']['make'];
            if (isset($searchTerms['model'])) {
                $model_validate_qry = "select model from [|PREFIX|]product_mmy where ";
                if (!isset($searchTerms['model_flag']) || $searchTerms['model_flag'] == 1) {
                    $model_validate_qry .= " make = '" . $searchTerms['make'] . "' and model = '" . $searchTerms['model'] . "'";
                } else {
                    $model_validate_qry .= " make = '" . $searchTerms['make'] . "' and model like '" . $searchTerms['model'] . "%'";
                }
                $model_validate_res = $GLOBALS['ISC_CLASS_DB']->Query($model_validate_qry);
                if ($GLOBALS['ISC_CLASS_DB']->CountResult($model_validate_res) == 0) {
                    unset($searchTerms['model'], $searchTerms['model_flag'], $_REQUEST['model'], $_COOKIE['last_search_selection']['model']);
                }
            }
        }
        if (!empty($_COOKIE['last_search_selection']['make'])) {
            $searchTerms['make'] = $_COOKIE['last_search_selection']['make'];
        }
        // This condition is added as to check  if only model is searched, then no need to update cookie. so storing empty value
        if (!isset($searchTerms['make']) && isset($searchTerms['model'])) {
            setcookie("last_search_selection[model]", '', $date_of_expiry, "/");
        } else {
            if (isset($searchTerms['make']) && !empty($_COOKIE['last_search_selection']['model'])) {
                $searchTerms['model'] = $_COOKIE['last_search_selection']['model'];
                $_REQUEST['model'] = $searchTerms['model'];
            }
        }
        if (!isset($searchTerms['model']) && !empty($_COOKIE['last_search_selection']['model'])) {
            $searchTerms['model'] = $_COOKIE['last_search_selection']['model'];
            $_REQUEST['model'] = $searchTerms['model'];
        }
        /*else {
        		unset($searchTerms['model'],$searchTerms['model_flag'],$_REQUEST['model']);
        		}*/
        if (!isset($searchTerms['year']) && !empty($_COOKIE['last_search_selection']['year'])) {
            $searchTerms['year'] = $_COOKIE['last_search_selection']['year'];
            $_REQUEST['year'] = $searchTerms['year'];
        }
        if (array_key_exists('make', $searchTerms) && isset($searchTerms['make'])) {
            setcookie("last_search_selection[make]", $searchTerms['make'], $date_of_expiry, "/");
        } else {
            if (array_key_exists('make', $_COOKIE['last_search_selection']) && isset($_COOKIE['last_search_selection']['make'])) {
                $searchTerms['make'] = $_COOKIE['last_search_selection']['make'];
            }
        }
        if (array_key_exists('model', $searchTerms) && isset($searchTerms['model'])) {
            setcookie("last_search_selection[model]", $searchTerms['model'], $date_of_expiry, "/");
        } else {
            if (array_key_exists('model', $_COOKIE['last_search_selection']) && isset($_COOKIE['last_search_selection']['model'])) {
                $searchTerms['model'] = $_COOKIE['last_search_selection']['model'];
            }
        }
        if (array_key_exists('year', $searchTerms) && isset($searchTerms['year'])) {
            setcookie("last_search_selection[year]", $searchTerms['year'], $date_of_expiry, "/");
        } else {
            if (array_key_exists('year', $_COOKIE['last_search_selection']) && isset($_COOKIE['last_search_selection']['model'])) {
                $searchTerms['year'] = $_COOKIE['last_search_selection']['year'];
            }
        }
        //setcookie( "last_search_selection[MMY_KEY]", time(), $date_of_expiry  );
    } else {
        if (!isset($GLOBALS['ISC_CLASS_REDEFINE_SEARCH'])) {
            if (isset($searchTerms['make'])) {
                setcookie("last_search_selection[make]", $searchTerms['make'], $date_of_expiry, "/");
            } else {
                if (isset($_COOKIE['last_search_selection']) && isset($_COOKIE['last_search_selection']['make'])) {
                    $searchTerms['make'] = $_COOKIE['last_search_selection']['make'];
                }
            }
            if (isset($searchTerms['make']) && isset($searchTerms['model']) && (!isset($searchTerms['model_flag']) || $searchTerms['model_flag'] == 1)) {
                setcookie("last_search_selection[model]", $searchTerms['model'], $date_of_expiry, "/");
            } else {
                if (isset($_COOKIE['last_search_selection']) && isset($_COOKIE['last_search_selection']['model'])) {
                    $searchTerms['model'] = $_COOKIE['last_search_selection']['model'];
                }
            }
            if (isset($searchTerms['year'])) {
                setcookie("last_search_selection[year]", $searchTerms['year'], $date_of_expiry, "/");
            } else {
                if (isset($_COOKIE['last_search_selection']) && isset($_COOKIE['last_search_selection']['year'])) {
                    $searchTerms['year'] = $_COOKIE['last_search_selection']['year'];
                }
            }
            //setcookie( "last_search_selection[MMY_KEY]", time(), $date_of_expiry  );
        }
    }
    //NI CLOUD 2010-06-18
    //add YMM validating logic
    $query = "SELECT 'ALL' AS\n\t\t\t\t\tTYPE , MIN( prodstartyear ) AS MIN_StartYear, MAX( prodendyear ) AS MAX_EndYear\n\t\t\t\t\tFROM isc_import_variations v\n\t\t\t\t\tINNER JOIN isc_products p ON v.productid = p.productid\n\t\t\t\t\tWHERE p.prodvisible =1 and `prodstartyear` <> 'all' AND `prodendyear` <> 'all'  and `prodstartyear` <> '' AND `prodendyear` <> ''  and `prodstartyear` is not null AND `prodendyear` is not null\n\t\t\t\t\tUNION ALL SELECT 'MAKE' AS \n\t\t\t\t\tTYPE , MIN( prodstartyear ) AS MIN_StartYear, MAX( prodendyear ) AS MAX_EndYear\n\t\t\t\t\tFROM isc_import_variations v\n\t\t\t\t\tINNER JOIN isc_products p ON v.productid = p.productid\n\t\t\t\t\tWHERE p.prodvisible =1 and `prodstartyear` <> 'all' AND `prodendyear` <> 'all'  and `prodstartyear` <> '' AND `prodendyear` <> ''  and `prodstartyear` is not null AND `prodendyear` is not null\n\t\t\t\t\tAND prodmake = '" . (isset($searchTerms["make"]) ? $searchTerms["make"] : '') . "'\n\t\t\t\t\tGROUP BY prodmake\n\t\t\t\t\tUNION ALL SELECT 'MODAL' AS \n\t\t\t\t\tTYPE , MIN( prodstartyear ) AS MIN_StartYear, MAX( prodendyear ) AS MAX_EndYear\n\t\t\t\t\tFROM isc_import_variations v\n\t\t\t\t\tINNER JOIN isc_products p ON v.productid = p.productid\n\t\t\t\t\tWHERE p.prodvisible =1 and `prodstartyear` <> 'all' AND `prodendyear` <> 'all'  and `prodstartyear` <> '' AND `prodendyear` <> ''  and `prodstartyear` is not null AND `prodendyear` is not null\n\t\t\t\t\tAND prodmake = '" . (isset($searchTerms["make"]) ? $searchTerms["make"] : '') . "'\n\t\t\t\t\tAND prodmodel = '" . (isset($searchTerms["model"]) ? $searchTerms["model"] : '') . "'\n\t\t\t\t\tGROUP BY prodmake + prodmodel";
    $result = $GLOBALS['ISC_CLASS_DB']->Query($query);
    //validate model
    if ($GLOBALS['ISC_CLASS_DB']->CountResult($result) < 3) {
        unset($searchTerms["model"]);
        unset($searchTerms["model_flag"]);
        setcookie("last_search_selection[model]", '', $date_of_expiry, "/");
    }
    //validate make
    if ($GLOBALS['ISC_CLASS_DB']->CountResult($result) < 2) {
        unset($searchTerms["make"]);
    }
    //validate year
    while ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) {
        if (isset($searchTerms["year"]) && ($searchTerms["year"] > (int) $row["MAX_EndYear"] || $searchTerms["year"] < (int) $row["MIN_StartYear"])) {
            unset($searchTerms["year"]);
        }
    }
    return $searchTerms;
}
Пример #2
0
/**
 * Builds an array of product search terms from an array of input (handles advanced language searching, category selections)
 *
 * @param array Array of search input
 * @return array Formatted search input array
 */
function BuildProductSearchTerms($input)
{
    $input['search_query'] = html_entity_decode($input['search_query']);
    $input['search_query'] = trim($input['search_query']);
    $spl_strings = array("+", "_", ":", ";", "!", "@", "#", "\$", "%", "^", "*", "\\");
    $input['search_query'] = str_replace($spl_strings, " ", $input['search_query']);
    $input['search_query'] = preg_replace('/[ ]{2,}|[\\t]/', ' ', trim($input['search_query']));
    $srch_arr = array();
    if (!empty($input['search_query'])) {
        $srch_arr = explode(" ", $input['search_query']);
    }
    $make = "";
    $model = "";
    $submodel = "";
    $startyear = "";
    $endyear = "";
    $compare_catg_name = "";
    // this variable is used to check the position of catgory and brand
    $compare_brand_name = "";
    // this variable is used to check the position of catgory and brand
    array_walk($srch_arr, 'lower');
    $new_srch_str = implode("','", $srch_arr);
    /*-- finding the qualifiers----*/
    $qualifier_count = 0;
    $GLOBALS['v_cols'] = array();
    $GLOBALS['p_cols'] = array();
    $GLOBALS['visible_pqvq'] = array();
    /*-- end of finding the qualifiers----*/
    $count_srch = count($srch_arr);
    $flag_brand = 0;
    $flag_brand_series = 0;
    $flag_catg = 0;
    $flag_sub_catg = 0;
    // 0 for parent catg & 1 for sub-catg
    $flag_alt_names = 0;
    // 0 for not matching alternate names & 1 for matching
    if ($count_srch > 0) {
        // if there are any search criterias, then we need to check it in category list.
        $brandname = array();
        $brandaltname = array();
        $brand_qry = "select brandid, brandname, brandaltkeyword from [|PREFIX|]brands order by CHAR_LENGTH(brandname) desc";
        $brand_res = $GLOBALS['ISC_CLASS_DB']->Query($brand_qry);
        while ($brand_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($brand_res)) {
            $brandname[$brand_arr['brandid']] = $brand_arr['brandname'];
            $brandaltname[$brand_arr['brandid']] = $brand_arr['brandaltkeyword'];
        }
        /*---- Brand Names --------*/
        foreach ($brandname as $bkey => $bvalue) {
            $brand_name = strtolower($bvalue);
            $in_str = "";
            $track_srch_key = array();
            for ($k = 0; $k < $count_srch; $k++) {
                if (stristr($brand_name, $srch_arr[$k])) {
                    $track_srch_key[] = $k;
                    if (!empty($in_str)) {
                        $in_str .= " " . $srch_arr[$k];
                    } else {
                        $in_str .= $srch_arr[$k];
                    }
                }
            }
            if ($brand_name == $in_str) {
                $compare_brand_name = $in_str;
                foreach ($track_srch_key as $key => $val) {
                    unset($srch_arr[$val]);
                }
                $srch_arr = array_values($srch_arr);
                $brandid = $bkey;
                $input['brand'] = $bvalue;
                $flag_brand = 1;
                $GLOBALS['ISC_SRCH_BRAND_NAME'] = ucwords($bvalue);
                break;
            }
        }
        /*---- ALternate Names For Brands --------*/
        if ($flag_brand == 0) {
            foreach ($brandaltname as $baltkey => $baltvalue) {
                if (!empty($baltvalue)) {
                    $balt_name = strtolower($baltvalue);
                    $brandalternate_names = explode(",", $balt_name);
                    usort($brandalternate_names, 'altname_sort');
                    for ($i = 0; $i < count($brandalternate_names); $i++) {
                        $in_str = "";
                        $track_srch_key = array();
                        $brand_name = $brandname[$baltkey];
                        for ($k = 0; $k < $count_srch; $k++) {
                            if (stristr($brandalternate_names[$i], $srch_arr[$k])) {
                                $track_srch_key[] = $k;
                                if (!empty($in_str)) {
                                    $in_str .= " " . $srch_arr[$k];
                                } else {
                                    $in_str .= $srch_arr[$k];
                                }
                            }
                        }
                        if ($brandalternate_names[$i] == $in_str) {
                            $compare_brand_name = $in_str;
                            foreach ($track_srch_key as $key => $val) {
                                unset($srch_arr[$val]);
                            }
                            $srch_arr = array_values($srch_arr);
                            $brandid = $baltkey;
                            $input['brand'] = $brand_name;
                            $flag_brand = 1;
                            $GLOBALS['ISC_SRCH_BRAND_NAME'] = ucwords($brand_name);
                            break;
                        }
                    }
                }
            }
        }
        if ($flag_brand == 0) {
            $brandseriesname = array();
            $brandseriesaltname = array();
            $brseriesparent = array();
            $series_qry = "select seriesid , s.brandid , brandname , seriesname , seriesaltkeyword from [|PREFIX|]brand_series s left join [|PREFIX|]brands b on b.brandid = s.brandid order by CHAR_LENGTH(seriesname) desc";
            $series_res = $GLOBALS['ISC_CLASS_DB']->Query($series_qry);
            while ($series_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($series_res)) {
                $brseriesparent[$series_arr['seriesid']] = $series_arr['brandid'];
                $brandseriesname[$series_arr['seriesid']] = $series_arr['seriesname'];
                $brandseriesaltname[$series_arr['seriesid']] = $series_arr['seriesaltkeyword'];
            }
            /* -------- for series names ----------- */
            foreach ($brandseriesname as $bskey => $bsvalue) {
                $series_name = strtolower($bsvalue);
                $in_str = "";
                $track_srch_key = array();
                for ($k = 0; $k < $count_srch; $k++) {
                    if (stristr($series_name, $srch_arr[$k])) {
                        $track_srch_key[] = $k;
                        if (!empty($in_str)) {
                            $in_str .= " " . $srch_arr[$k];
                        } else {
                            $in_str .= $srch_arr[$k];
                        }
                    }
                }
                if ($series_name == $in_str) {
                    $compare_brand_name = $in_str;
                    foreach ($track_srch_key as $key => $val) {
                        unset($srch_arr[$val]);
                    }
                    $srch_arr = array_values($srch_arr);
                    $brandid = $brseriesparent[$bskey];
                    $input['brand'] = $brandname[$brandid];
                    $input['series'] = $bskey;
                    $flag_brand = 1;
                    $flag_brand_series = 1;
                    // need to assign, to know the series has been selected
                    $GLOBALS['BRAND_SERIES_FLAG'] = 1;
                    $GLOBALS['ISC_SRCH_BRAND_NAME'] = ucwords($brandname[$brseriesparent[$bskey]]);
                    break;
                }
            }
            /* -------- for series alternate keywords ----------- */
            if ($flag_brand == 0) {
                foreach ($brandseriesaltname as $bsaltkey => $bsaltvalue) {
                    if (!empty($bsaltvalue)) {
                        $bsalt_name = strtolower($bsaltvalue);
                        $brandseriesalternate_names = explode(",", $bsalt_name);
                        usort($brandseriesalternate_names, 'altname_sort');
                        for ($i = 0; $i < count($brandseriesalternate_names); $i++) {
                            $in_str = "";
                            $track_srch_key = array();
                            $brand_name = $brandname[$brseriesparent[$bsaltkey]];
                            for ($k = 0; $k < $count_srch; $k++) {
                                if (stristr($brandseriesalternate_names[$i], $srch_arr[$k])) {
                                    $track_srch_key[] = $k;
                                    if (!empty($in_str)) {
                                        $in_str .= " " . $srch_arr[$k];
                                    } else {
                                        $in_str .= $srch_arr[$k];
                                    }
                                }
                            }
                            if ($brandseriesalternate_names[$i] == $in_str) {
                                $compare_brand_name = $in_str;
                                foreach ($track_srch_key as $key => $val) {
                                    unset($srch_arr[$val]);
                                }
                                $srch_arr = array_values($srch_arr);
                                $brandid = $brseriesparent[$bsaltkey];
                                $input['brand'] = $brandname[$brandid];
                                $input['series'] = $bsaltkey;
                                $flag_brand = 1;
                                $flag_brand_series = 1;
                                // need to assign, to know the series has been selected
                                $GLOBALS['BRAND_SERIES_FLAG'] = 1;
                                $GLOBALS['ISC_SRCH_BRAND_NAME'] = ucwords($brandname[$brandid]);
                                break;
                            }
                        }
                    }
                }
            }
        }
        $count_srch = count($srch_arr);
        $catg_qry = "select * from isc_categories where catvisible=1 order by catparentid ASC , CHAR_LENGTH(catname) DESC";
        $catg_res = $GLOBALS['ISC_CLASS_DB']->Query($catg_qry);
        $catgoryname = array();
        // array for category names
        $categoryaltname = array();
        // array for alternate keywords
        $catuniversal = array();
        while ($catg_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($catg_res)) {
            $catgoryname[$catg_arr['categoryid']]['catname'] = $catg_arr['catname'];
            $catgoryname[$catg_arr['categoryid']]['catparentid'] = $catg_arr['catparentid'];
            $categoryaltname[$catg_arr['categoryid']]['catname'] = $catg_arr['catname'];
            $categoryaltname[$catg_arr['categoryid']]['altkeywords'] = $catg_arr['cataltkeyword'];
            $categoryaltname[$catg_arr['categoryid']]['catparentid'] = $catg_arr['catparentid'];
            $catuniversal[$catg_arr['categoryid']] = $catg_arr['catuniversal'];
        }
        if ($count_srch > 0) {
            /* Checking first in category names */
            foreach ($catgoryname as $key => $value) {
                $catg_name = strtolower($catgoryname[$key]['catname']);
                //$catg_name = strtolower($catgoryname[$i]);
                $in_str = "";
                $track_srch_key = array();
                for ($k = 0; $k < $count_srch; $k++) {
                    if (stristr($catg_name, $srch_arr[$k])) {
                        $track_srch_key[] = $k;
                        if (!empty($in_str)) {
                            $in_str .= " " . $srch_arr[$k];
                        } else {
                            $in_str .= $srch_arr[$k];
                        }
                        if ($catg_name == $in_str) {
                            break;
                        }
                    }
                }
                if ($catg_name == $in_str) {
                    $compare_catg_name = $in_str;
                    for ($j = 0; $j < count($track_srch_key); $j++) {
                        unset($srch_arr[$track_srch_key[$j]]);
                    }
                    $srch_arr = array_values($srch_arr);
                    if ($catgoryname[$key]['catparentid'] != 0) {
                        // checking its not a parent catg ?
                        $flag_sub_catg = 1;
                    }
                    $catg_id = $key;
                    $flag_catg = 1;
                    $input['catuniversal'] = $catuniversal[$key];
                    break;
                }
            }
            /* Checking in category alternate names */
            if ($flag_catg == 0) {
                foreach ($categoryaltname as $altkey => $altvalue) {
                    if (!empty($categoryaltname[$altkey]['altkeywords'])) {
                        $catg_name = strtolower($categoryaltname[$altkey]['catname']);
                        $alternate_names = explode(",", $categoryaltname[$altkey]['altkeywords']);
                        usort($alternate_names, 'altname_sort');
                        for ($i = 0; $i < count($alternate_names); $i++) {
                            $in_str = "";
                            $track_srch_key = array();
                            $altname = strtolower(trim($alternate_names[$i]));
                            for ($k = 0; $k < $count_srch; $k++) {
                                if (stristr($altname, $srch_arr[$k])) {
                                    $track_srch_key[] = $k;
                                    if (!empty($in_str)) {
                                        $in_str .= " " . $srch_arr[$k];
                                    } else {
                                        $in_str .= $srch_arr[$k];
                                    }
                                    if ($altname == $in_str) {
                                        break;
                                    }
                                }
                            }
                            if ($altname == $in_str) {
                                $compare_catg_name = $in_str;
                                for ($j = 0; $j < count($track_srch_key); $j++) {
                                    unset($srch_arr[$track_srch_key[$j]]);
                                }
                                $srch_arr = array_values($srch_arr);
                                if ($categoryaltname[$altkey]['catparentid'] != 0) {
                                    // checking its not a parent catg ?
                                    $flag_sub_catg = 1;
                                }
                                $catg_id = $altkey;
                                $flag_catg = 1;
                                $input['catuniversal'] = $catuniversal[$altkey];
                                break;
                            }
                        }
                        if ($flag_catg == 1) {
                            break;
                        }
                    }
                }
            }
        }
    }
    /*----------------- for selecting category ---------------------*/
    if (isset($_GET['search_key']) || isset($_REQUEST['search'])) {
        if (in_array("chevy", $srch_arr)) {
            $input['make'] = "chevrolet";
            $key = array_search('chevy', $srch_arr);
            unset($srch_arr[$key]);
            $srch_arr = array_values($srch_arr);
        }
        $inner_qry = "";
        $flag_model = 0;
        if (count($srch_arr) > 0) {
            $query = " select distinct q.column_name , v.qid , q_value from isc_qualifier_value v left join isc_qualifier_names q on v.qid = q.qid where ";
            foreach ($srch_arr as $key => $value) {
                if (empty($inner_qry)) {
                    $inner_qry .= " q_value like '%{$value}%' ";
                } else {
                    $inner_qry .= " OR q_value like '%{$value}%' ";
                }
            }
            $query .= $inner_qry . " order by qid , CHAR_LENGTH(q_value) desc";
            $srch_res1 = $GLOBALS['ISC_CLASS_DB']->Query($query);
            $model_array = array();
            while ($srch_row1 = $GLOBALS['ISC_CLASS_DB']->Fetch($srch_res1)) {
                if ($srch_row1['qid'] == 1) {
                    if (!isset($input['make'])) {
                        $in_make = "";
                        $track_make_array = array();
                        for ($j = 0; $j < count($srch_arr); $j++) {
                            if (stristr($srch_row1['q_value'], $srch_arr[$j])) {
                                $track_make_array[] = $j;
                                if (!empty($in_make)) {
                                    $in_make .= " " . $srch_arr[$j];
                                } else {
                                    $in_make .= $srch_arr[$j];
                                }
                                if ($in_make == strtolower($srch_row1['q_value'])) {
                                    break;
                                }
                            }
                        }
                        if ($in_make == strtolower($srch_row1['q_value'])) {
                            $input['make'] = $srch_row1['q_value'];
                            for ($k = 0; $k < count($track_make_array); $k++) {
                                unset($srch_arr[$track_make_array[$k]]);
                            }
                            $srch_arr = array_values($srch_arr);
                        }
                    }
                } else {
                    if ($srch_row1['qid'] == 2) {
                        if ($flag_model == 0) {
                            $in_str = "";
                            $track_srch_key = array();
                            for ($k = 0; $k < count($srch_arr); $k++) {
                                //echo $srch_row1['q_value']."-".$srch_arr[$k]."<br>";
                                // if(stristr($srch_row1['q_value'],$srch_arr[$k])) {
                                $match = "/([^a-z]{$srch_arr[$k]}(\\s{1})|(\\s{1}){$srch_arr[$k]}|^{$srch_arr[$k]})/i";
                                if (preg_match($match, $srch_row1['q_value'])) {
                                    $model_value = strtolower($srch_row1['q_value']);
                                    $cmp = strcasecmp($srch_arr[$k], $model_value);
                                    //echo $srch_arr[$k]."-".$cmp."-".$model_value."<br>";
                                    if ($srch_arr[$k] == $model_value) {
                                        if (isset($input['model']) && stristr($input['model'], $srch_arr[$k]) && strlen($input['model']) != strlen($srch_arr[$k])) {
                                            continue;
                                        } else {
                                            $input['model'] = $srch_arr[$k];
                                            //$_REQUEST['model'] = $srch_row1['q_value'];
                                            unset($srch_arr[$k]);
                                            $srch_arr = array_values($srch_arr);
                                            $flag_model = 1;
                                            break;
                                        }
                                    } else {
                                        if (stristr($model_value, $srch_arr[$k])) {
                                            $track_srch_key[] = $k;
                                            if (!empty($in_str) && $in_str != $srch_arr[$k]) {
                                                $in_str .= " " . $srch_arr[$k];
                                            } else {
                                                $in_str = $srch_arr[$k];
                                            }
                                            //echo $in_str."-".$model_value."-".$k."<br>";
                                            if ($in_str == $model_value) {
                                                for ($j = 0; $j < count($track_srch_key); $j++) {
                                                    unset($srch_arr[$track_srch_key[$j]]);
                                                }
                                                $srch_arr = array_values($srch_arr);
                                                $input['model'] = $in_str;
                                                //$_REQUEST['model'] = $srch_row1['q_value'];
                                                $flag_model = 1;
                                                break;
                                            } else {
                                                //echo strlen($in_str)."-".$in_str."=".$input['model']."-".strlen($input['model'])."    ".$k."<br>";
                                                if (isset($input['model'])) {
                                                    if (strlen($in_str) >= strlen($input['model'])) {
                                                        $input['model'] = $in_str;
                                                    }
                                                } else {
                                                    $input['model'] = $in_str;
                                                }
                                                $model_array = $track_srch_key;
                                                //break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        if ($srch_row1['qid'] == 4 || $srch_row1['qid'] == 5) {
                            if (!isset($input['year'])) {
                                for ($j = 0; $j < count($srch_arr); $j++) {
                                    if ($srch_row1['q_value'] == $srch_arr[$j]) {
                                        $input['year'] = $srch_row1['q_value'];
                                        unset($srch_arr[$j]);
                                        $srch_arr = array_values($srch_arr);
                                        break;
                                    }
                                }
                            }
                        } else {
                            /* this below patch is used to remove keywords selected from the search array where model is partial selected */
                            if (!empty($model_array) && $flag_model == 0) {
                                for ($j = 0; $j < count($model_array); $j++) {
                                    unset($srch_arr[$model_array[$j]]);
                                }
                                $srch_arr = array_values($srch_arr);
                                unset($model_array);
                            }
                            for ($j = 0; $j < count($srch_arr); $j++) {
                                //if(stristr($srch_row1['prodmodel'],$srch_arr[$j]))
                                //if(strtolower($srch_row1['prodmodel']) == $srch_arr[$j])
                                if (eregi('(^' . $srch_arr[$j] . ')', $srch_row1['q_value'])) {
                                    $model = $srch_arr[$j];
                                    if (isset($input[$srch_row1['column_name']])) {
                                        continue;
                                    }
                                    if ($srch_row1['qid'] == 4 || $srch_row1['qid'] == 5) {
                                        $input['year'] = $srch_arr[$j];
                                    } else {
                                        $input[$srch_row1['column_name']] = $srch_arr[$j];
                                    }
                                    //$key = array_search($srch_row1['q_value'],$srch_arr);
                                    unset($srch_arr[$j]);
                                    $srch_arr = array_values($srch_arr);
                                    break;
                                }
                            }
                        }
                    }
                }
                if (count($srch_arr) == 0) {
                    break;
                }
            }
            if (!isset($input['year'])) {
                for ($i = 0; $i < count($srch_arr); $i++) {
                    if (is_numeric($srch_arr[$i])) {
                        if (strlen($srch_arr[$i]) == 2) {
                            $prodyr = $srch_arr[$i];
                            $curr_yr = date('y');
                            if ($prodyr >= 00 && $prodyr <= $curr_yr) {
                                $prodyr = 2000 + $prodyr;
                            } else {
                                $prodyr = 1900 + $prodyr;
                            }
                        } else {
                            $prodyr = $srch_arr[$i];
                        }
                        $year_qry = " select year1 , year2  from (select min(prodstartyear) as year1 from isc_import_variations ) sy , (select max(prodendyear) as year2 from isc_import_variations where prodendyear != 'ALL' ) ey where {$prodyr} between year1 and year2 ";
                        $year_result = $GLOBALS['ISC_CLASS_DB']->Query($year_qry);
                        $year_row = $GLOBALS['ISC_CLASS_DB']->CountResult($year_result);
                        if ($year_row == 1) {
                            $input['year'] = $prodyr;
                            unset($srch_arr[$i]);
                            $srch_arr = array_values($srch_arr);
                            break;
                        }
                    }
                }
            }
            // checking the partnumber from the remaining search parameters
            if (count($srch_arr) > 0) {
                $inner_qry = "";
                $product_code_query = " SELECT prodcode FROM [|PREFIX|]products p WHERE ";
                foreach ($srch_arr as $key => $value) {
                    if (empty($inner_qry)) {
                        $inner_qry .= " prodcode like '%{$value}%' ";
                    } else {
                        $inner_qry .= " OR prodcode like '%{$value}%' ";
                    }
                }
                $product_code_query .= $inner_qry;
                $product_code_res = $GLOBALS['ISC_CLASS_DB']->Query($product_code_query);
                while ($product_code_row = $GLOBALS['ISC_CLASS_DB']->Fetch($product_code_res)) {
                    $prod_code = strtolower($product_code_row['prodcode']);
                    $track_srch_key = array();
                    for ($j = 0; $j < count($srch_arr); $j++) {
                        if (stristr($prod_code, $srch_arr[$j])) {
                            if ($prod_code == $srch_arr[$j]) {
                                unset($srch_arr[$j]);
                                $srch_arr = array_values($srch_arr);
                                $input['partnumber'] = $prod_code;
                                $partnumber_flag = 0;
                                break;
                            } else {
                                $input['partnumber'] = $srch_arr[$j];
                                $partnumber_flag = 1;
                            }
                        }
                    }
                    if ($partnumber_flag == 0) {
                        break;
                    }
                }
            }
        }
        /*----------------- End for selecting category ---------------------*/
    }
    $searchTerms = array();
    $matches = array();
    $searchTerms['dynfilters'] = array();
    /*----------------- for selecting category ---------------------*/
    $searchTerms['flag_srch_category'] = 0;
    if (isset($input['partnumber'])) {
        $searchTerms['partnumber'] = $input['partnumber'];
    }
    /* the below code is to find out whether category or brand is entered first */
    $check_position = 0;
    // 1 if catg is first and 0 if brand is first
    if ($flag_catg == 1 && $flag_brand == 1) {
        $catg_pos = stripos($input['search_query'], $compare_catg_name);
        $brand_pos = stripos($input['search_query'], $compare_brand_name);
        if ($catg_pos < $brand_pos) {
            $check_position = 1;
        } else {
            $input['category'] = $catg_name;
            $flag_catg = 0;
            // resetting to 0 as category is entered after brand so it should be passed in $input['category']
            $check_position = 0;
        }
    }
    if ($flag_brand == 1 && $check_position == 0) {
        $searchTerms['flag_srch_brand'] = $flag_brand_series;
    }
    if (isset($input['category'])) {
        $searchTerms['category'] = $input['category'];
        $category_name = $input['category'];
        $get_catgories_qry = "SELECT categoryid FROM isc_categories WHERE (catname='" . $category_name . "' OR catparentid IN (SELECT categoryid FROM isc_categories WHERE catname='" . $category_name . "')) AND catvisible='1'";
        $get_category_res = $GLOBALS['ISC_CLASS_DB']->Query($get_catgories_qry);
        while ($get_category_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($get_category_res)) {
            $searchTerms['srch_category'][] = $get_category_arr['categoryid'];
        }
        $GLOBALS['ISC_SRCH_CATG_NAME'] = ucwords($category_name);
        $GLOBALS['ISC_SRCH_CATG_ID'] = $searchTerms['srch_category'][0];
        $input['catuniversal'] = $searchTerms['catuniversal'] = $catuniversal[$searchTerms['srch_category'][0]];
    }
    if ($flag_catg == 1) {
        $GLOBALS['ISC_SRCH_CATG_NAME'] = ucwords($catg_name);
        $GLOBALS['ISC_SRCH_CATG_ID'] = $catg_id;
        /* the below patch has been added below for the catogories having the make as non-spec vehicle */
        /*if($catg_id == 17 OR $catg_id == 3 OR $catg_id == 7 OR $catg_id == 18 OR $catg_id == 1 OR $catg_id == 2) {
                 $searchTerms['is_catg'] = 1;
          }*/
        $sub_catgid = array();
        if ($flag_sub_catg == 0) {
            // if its a parent catg, need to find the sub-categories
            $sub_catgqry = "select categoryid from isc_categories where catparentid = {$catg_id} and catvisible=1";
            $sub_catgres = $GLOBALS['ISC_CLASS_DB']->Query($sub_catgqry);
            if ($GLOBALS['ISC_CLASS_DB']->CountResult($sub_catgres) > 0) {
                // if there are no subcategories under a main category
                while ($sub_catgarr = $GLOBALS['ISC_CLASS_DB']->Fetch($sub_catgres)) {
                    $sub_catgid[] = $sub_catgarr['categoryid'];
                }
                $sub_catgid[] = $catg_id;
            } else {
                $flag_sub_catg = 1;
                $sub_catgid[] = $catg_id;
            }
        } else {
            // else it is a sub-category
            $sub_catgid[] = $catg_id;
        }
        $searchTerms['flag_srch_category'] = $flag_sub_catg;
        $searchTerms['srch_category'] = $sub_catgid;
        if (isset($input['catuniversal'])) {
            $searchTerms['catuniversal'] = $input['catuniversal'];
        }
    }
    /*----------------- End for selecting category ---------------------*/
    // Here we parse out any advanced search identifiers from the search query such as price:, :rating etc
    $advanced_params = array(GetLang('SearchLangPrice'), GetLang('SearchLangRating'), GetLang('SearchLangInStock'), GetLang('SearchLangFeatured'), GetLang('SearchLangFreeShipping'));
    if (isset($input['search_query'])) {
        $query = str_replace(array("&lt;", "&gt;"), array("<", ">"), $input['search_query']);
        foreach ($advanced_params as $param) {
            if ($param == GetLang('SearchLangPrice') || $param == GetLang('SearchLangRating')) {
                $match = sprintf("(<|>)?([0-9\\.%s]+)-?([0-9\\.%s]+)?", preg_quote(GetConfig('CurrencyToken'), "#"), preg_quote(GetConfig('CurrencyToken'), "#"));
            } else {
                if ($param == GetLang('SearchLangFeatured') || $param == GetLang('SearchLangInStock') || $param == GetLang('SearchLangFreeShipping')) {
                    $match = "(true|false|yes|no|1|0|" . preg_quote(GetLang('SearchLangYes'), "#") . "|" . preg_quote(GetLang('SearchLangNo'), "#") . ")";
                } else {
                    continue;
                }
            }
            preg_match("#\\s" . preg_quote($param, "#") . ":" . $match . '(\\s|$)#i', $query, $matches);
            if (count($matches) > 0) {
                if ($param == "price" || $param == "rating") {
                    if ($matches[3]) {
                        $input[$param . '_from'] = (double) $matches[2];
                        $input[$param . '_to'] = (double) $matches[3];
                    } else {
                        if ($matches[1] == "<") {
                            $input[$param . '_to'] = (double) $matches[2];
                        } else {
                            if ($matches[1] == ">") {
                                $input[$param . '_from'] = (double) $matches[2];
                            } else {
                                if ($matches[1] == "") {
                                    $input[$param] = (double) $matches[2];
                                }
                            }
                        }
                    }
                } else {
                    if ($param == "featured" || $param == "instock" || $param == "freeshipping") {
                        if ($param == "freeshipping") {
                            $param = "shipping";
                        }
                        if ($matches[1] == "true" || $matches[1] == "yes" || $matches[1] == 1) {
                            $input[$param] = 1;
                        } else {
                            $input[$param] = 0;
                        }
                    }
                }
                $matches[0] = str_replace(array("<", ">"), array("&lt;", "&gt;"), $matches[0]);
                $input['search_query'] = trim(preg_replace("#" . preg_quote(trim($matches[0]), "#") . "#i", "", $input['search_query']));
            }
        }
        // Pass the modified search query back
        $searchTerms['search_query'] = $input['search_query'];
    }
    if (isset($input['categoryid'])) {
        $input['category'] = $input['categoryid'];
    }
    /*
    if (isset($input['category'])) {
    	if (!is_array($input['category'])) {
    		$input['category'] = array($input['category']);
    	}
    	$searchTerms['category'] = $input['category'];
    }
    */
    if (isset($input['searchsubs']) && $input['searchsubs'] != "") {
        $searchTerms['searchsubs'] = $input['searchsubs'];
    }
    if (isset($input['price']) && $input['price'] != "") {
        $searchTerms['price'] = $input['price'];
    }
    if (isset($input['price_from']) && $input['price_from'] != "") {
        $searchTerms['price_from'] = $input['price_from'];
    }
    if (isset($input['price_to']) && $input['price_to'] != "") {
        $searchTerms['price_to'] = $input['price_to'];
    }
    if (isset($input['rating']) && $input['rating'] != "") {
        $searchTerms['rating'] = $input['rating'];
    }
    if (isset($input['rating_from']) && $input['rating_from'] != "") {
        $searchTerms['rating_from'] = $input['rating_from'];
    }
    if (isset($input['rating_to']) && $input['rating_to'] != "") {
        $searchTerms['rating_to'] = $input['rating_to'];
    }
    if (isset($input['featured']) && is_numeric($input['featured']) != "") {
        $searchTerms['featured'] = (int) $input['featured'];
    }
    if (isset($input['shipping']) && is_numeric($input['shipping']) != "") {
        $searchTerms['shipping'] = (int) $input['shipping'];
    }
    if (isset($input['instock']) && is_numeric($input['instock'])) {
        $searchTerms['instock'] = (int) $input['instock'];
    }
    if (isset($input['brand']) && $input['brand'] != "") {
        $searchTerms['brand'] = $input['brand'];
        $brand_query = "select brandid from [|PREFIX|]brands WHERE brandname='" . $searchTerms['brand'] . "'";
        $brand_result = $GLOBALS['ISC_CLASS_DB']->Query($brand_query);
        $GLOBALS['brandId'] = $GLOBALS['ISC_CLASS_DB']->FetchOne($brand_result);
        /* this below condition is added to get the brand name when clicked on brand after selecting any category */
        if (!isset($GLOBALS['ISC_SRCH_BRAND_NAME'])) {
            $GLOBALS['ISC_SRCH_BRAND_NAME'] = $searchTerms['brand'];
        }
    }
    if (isset($input['make']) && $input['make'] != "") {
        $searchTerms['make'] = $input['make'];
        unset($input['prodmake']);
    }
    /* this below 3 lines are added as db has the name as prodmodel for model and prodsubmodel as submodel*/
    if (isset($input['prodmodel']) && $input['prodmodel'] != "") {
        $input['model'] = $input['prodmodel'];
    }
    if (isset($input['model']) && $input['model'] != "" && (!isset($input['search_key']) || isset($input['search_key']) && $input['model'] != 'all')) {
        $searchTerms['model'] = $input['model'];
        unset($input['prodmodel']);
        $_REQUEST['model'] = $input['model'];
        /* the below flag is set to check whether exact model is searched or similar to the model is searched */
        if (isset($flag_model)) {
            $searchTerms['model_flag'] = $flag_model;
        }
    }
    /* the below condition is checked whether the flag is returning from the links in the side filter section in search result page */
    if (isset($input['model_flag']) && $input['model_flag'] != "") {
        $searchTerms['model_flag'] = $input['model_flag'];
    }
    if (isset($input['prodsubmodel']) && $input['prodsubmodel'] != "") {
        $input['submodel'] = $input['prodsubmodel'];
    }
    if (isset($input['submodel']) && $input['model'] != "") {
        $searchTerms['submodel'] = $input['submodel'];
        unset($input['prodsubmodel']);
    }
    if (isset($input['year']) && $input['year'] != "" && (!isset($input['search_key']) || isset($input['search_key']) && $input['year'] != 'all')) {
        $searchTerms['year'] = $input['year'];
    }
    /*if (isset($input['brand']) && $input['brand'] != "") {
                $searchTerms['brand'] = $input['brand'];
            } 
            
            if (isset($input['from_price']) && $input['from_price'] != "") {
                $searchTerms['from_price'] = $input['from_price'];
            }
            
            if (isset($input['to_price']) && $input['to_price'] != "") {
                $searchTerms['to_price'] = $input['to_price'];
            }
    
    		if (isset($input['bedsize']) && $input['bedsize'] != "") {
                $searchTerms['bedsize'] = $input['bedsize'];
            } 
            
            if (isset($input['cover']) && $input['cover'] != "") {
                $searchTerms['cover'] = $input['cover'];
            }
            
            if (isset($input['color']) && $input['color'] != "") {
                $searchTerms['color'] = $input['color'];
            }*/
    if (isset($input['subcategory']) && !empty($input['subcategory'])) {
        $sub_category = $input['subcategory'];
        $searchTerms['subcategory'] = $input['subcategory'];
        $sub_catg_qry = "select categoryid , catname from isc_categories where catname = '" . $sub_category . "'";
        $sub_catg_res = $GLOBALS['ISC_CLASS_DB']->Query($sub_catg_qry);
        $sub_catg_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($sub_catg_res);
        $GLOBALS['subcategoryid'] = $sub_catg_arr['categoryid'];
        /* the below query is written to get the main category anme when subcategory is selected directly */
        $main_catg_qry = "select p.categoryid , p.catname from isc_categories as p inner join isc_categories as c on p.categoryid = c.catparentid and c.catname = '" . $sub_category . "'";
        $main_catg_res = $GLOBALS['ISC_CLASS_DB']->Query($main_catg_qry);
        $main_catg_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($main_catg_res);
        $main_catg_rows = $GLOBALS['ISC_CLASS_DB']->CountResult($main_catg_res);
        if ($main_catg_rows == 0) {
            $main_catg_arr = $sub_catg_arr;
        }
        $GLOBALS['ISC_SRCH_CATG_NAME'] = ucwords($main_catg_arr['catname']);
        $GLOBALS['ISC_SRCH_CATG_ID'] = $main_catg_arr['categoryid'];
        //if(in_array($sub_category,$sub_catgid)) {
        //$sub_catgid = $sub_category;
        $searchTerms['srch_category'] = array($GLOBALS['subcategoryid']);
        $searchTerms['flag_srch_category'] = 1;
        $searchTerms['catuniversal'] = $catuniversal[$GLOBALS['subcategoryid']];
        //}
    }
    // need to know the category name when brand and series are selected
    if (isset($input['series']) && !empty($input['series'])) {
        $searchTerms['series'] = $input['series'];
        $searchTerms['flag_srch_brand'] = 1;
        $GLOBALS['BRAND_SERIES_FLAG'] = 1;
        $main_catg_qry = "select s.seriesid from isc_brand_series s where s.seriesname = '" . $input['series'] . "' ";
        $main_catg_res = $GLOBALS['ISC_CLASS_DB']->Query($main_catg_qry);
        $main_catg_rows = $GLOBALS['ISC_CLASS_DB']->CountResult($main_catg_res);
        $main_catg_arr = $GLOBALS['ISC_CLASS_DB']->Fetch($main_catg_res);
        $GLOBALS['seriesid'] = $main_catg_arr['seriesid'];
        if ($main_catg_rows > 0) {
            /*$GLOBALS['ISC_SRCH_CATG_NAME'] =  ucwords($main_catg_arr['catname']);
            		$GLOBALS['ISC_SRCH_CATG_ID'] =  $main_catg_arr['categoryid'];
            		$searchTerms['srch_category'] =  array($GLOBALS['ISC_SRCH_CATG_ID']);
            		$searchTerms['catuniversal'] = $catuniversal[$main_catg_arr['categoryid']];*/
        }
    }
    if (isset($GLOBALS['ISC_SRCH_CATG_ID'])) {
        $RelatedCatsQuery = "SELECT DISTINCT c.categoryid \n                                FROM isc_products p \n                                LEFT JOIN isc_categoryassociations ca ON ca.productid = p.productid \n                                LEFT JOIN isc_categories c ON c.categoryid = ca.categoryid AND c.catvisible = 1 ";
        if (isset($input['series']) && !empty($input['series'])) {
            $RelatedCatsQuery .= "\n                        LEFT JOIN isc_brands b ON prodbrandid = b.brandid \n                        LEFT JOIN isc_brand_series AS bs ON bs.seriesid = p.brandseriesid ";
        }
        $RelatedCatsQuery .= " WHERE 1=1 AND c.categoryid IS NOT NULL AND p.prodvisible='1' ";
        if (in_array($GLOBALS['ISC_SRCH_CATG_ID'], $searchTerms['srch_category'])) {
            // No subcatg under category
            $sidequalifier_query = "select distinct qn.qid , qn.column_name , qa.qualifier_visible from isc_qualifier_associations qa left join isc_qualifier_names qn on qn.qid = qa.qualifierid \n                    where qa.categoryid = " . $GLOBALS['ISC_SRCH_CATG_ID'];
            $RelatedCatsQuery .= " AND (c.categoryid = " . $GLOBALS['ISC_SRCH_CATG_ID'] . " || c.catparentid = " . $GLOBALS['ISC_SRCH_CATG_ID'] . ")";
        } else {
            if (!in_array($GLOBALS['ISC_SRCH_CATG_ID'], $searchTerms['srch_category']) && isset($input['subcategory'])) {
                //   product listing page
                $sidequalifier_query = "select distinct qn.qid , qn.column_name , qa.qualifier_visible from isc_qualifier_associations qa left join isc_qualifier_names qn on qn.qid = qa.qualifierid where qa.categoryid = " . $GLOBALS['subcategoryid'];
                $RelatedCatsQuery .= " AND c.categoryid = " . $GLOBALS['subcategoryid'] . "";
            }
        }
        if (isset($input['series']) && !empty($input['series'])) {
            $brand_id = (int) $GLOBALS['brandId'];
            $RelatedCatsQuery .= " AND p.prodbrandid='" . $GLOBALS['ISC_CLASS_DB']->Quote($brand_id) . "' AND p.brandseriesid = " . $GLOBALS['seriesid'] . "";
        }
        $RelatedCatsResult = $GLOBALS['ISC_CLASS_DB']->Query($RelatedCatsQuery);
        $RelatedCats = array();
        while ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($RelatedCatsResult)) {
            $RelatedCats[] = (int) $row['categoryid'];
        }
        $RelatedCats = implode(",", $RelatedCats);
        $qualifier_query = " SELECT \n                            DISTINCT qn.qid , qn.column_name , qa.qualifier_visible \n                            FROM isc_qualifier_associations qa \n                            LEFT JOIN isc_qualifier_names qn ON qn.qid = qa.qualifierid      \n                            WHERE qa.categoryid IN (" . $RelatedCats . ")";
        //OR qa.categoryid IN (SELECT catparentid FROM isc_categories WHERE categoryid IN (".$RelatedCats."))
        GetQualifierColumns($qualifier_query, $qualifier_columns, $VCols, $PCols);
        $GLOBALS['sidev_cols'] = $VCols;
        $GLOBALS['sidep_cols'] = $PCols;
        GetQualifierColumns($sidequalifier_query, $sidequalifier_columns, $SideVCols, $SidePCols);
        $GLOBALS['v_cols'] = $SideVCols;
        $GLOBALS['p_cols'] = $SidePCols;
    } else {
        $RelatedCatsQuery = "SELECT DISTINCT c.categoryid \n                                FROM isc_products p \n                                LEFT JOIN isc_categoryassociations ca ON ca.productid = p.productid \n                                LEFT JOIN isc_categories c ON c.categoryid = ca.categoryid AND c.catvisible = 1 ";
        if (isset($input['series']) && !empty($input['series'])) {
            $RelatedCatsQuery .= "\n                        LEFT JOIN isc_brands b ON prodbrandid = b.brandid \n                        LEFT JOIN isc_brand_series AS bs ON bs.seriesid = p.brandseriesid ";
        }
        $RelatedCatsQuery .= " WHERE 1=1 AND c.categoryid IS NOT NULL AND p.prodvisible='1' ";
        if (isset($input['series']) && !empty($input['series'])) {
            $brand_id = (int) $GLOBALS['brandId'];
            $RelatedCatsQuery .= " AND p.prodbrandid='" . $GLOBALS['ISC_CLASS_DB']->Quote($brand_id) . "' AND p.brandseriesid = " . $GLOBALS['seriesid'] . "";
        }
        //$RelatedCatsQuery = $GLOBALS['StartNumber'];
        $RelatedCatsResult = $GLOBALS['ISC_CLASS_DB']->Query($RelatedCatsQuery);
        $RelatedCats = array();
        while ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($RelatedCatsResult)) {
            $RelatedCats[] = (int) $row['categoryid'];
        }
        $RelatedCats = implode(",", $RelatedCats);
        $qualifier_query = "SELECT \n                                DISTINCT qn.qid , qn.column_name , qa.qualifier_visible \n                                FROM isc_qualifier_associations qa \n                                LEFT JOIN isc_qualifier_names qn ON qn.qid = qa.qualifierid      \n                                WHERE qa.categoryid IN (" . $RelatedCats . ")";
        //OR qa.categoryid IN (SELECT catparentid FROM isc_categories WHERE categoryid IN (".$RelatedCats."))
        $qualifier_result = $GLOBALS['ISC_CLASS_DB']->Query($qualifier_query);
        GetQualifierColumns($qualifier_query, $qualifier_columns, $VCols, $PCols);
        $GLOBALS['sidev_cols'] = $VCols;
        $GLOBALS['sidep_cols'] = $PCols;
    }
    $GLOBALS['v_cols'] = isset($GLOBALS['v_cols']) ? $GLOBALS['v_cols'] : array();
    $GLOBALS['p_cols'] = isset($GLOBALS['p_cols']) ? $GLOBALS['p_cols'] : array();
    $qualifier_count = count($GLOBALS['sidev_cols']) + count($GLOBALS['sidep_cols']);
    /*------------ For Dynamic filters ------------------*/
    for ($qid = 0; $qid < $qualifier_count; $qid++) {
        $dynval = strtolower($qualifier_columns[$qid]);
        if (isset($input[$dynval]) && !empty($input[$dynval])) {
            $q_value = $input[$dynval];
            $dynval = preg_replace("/^([a-zA-Z0-9]{2})/e", "strtoupper('\\1')", $dynval);
            // making the first 2 letters capital of the qualifier name
            $searchTerms['dynfilters'][$dynval] = $q_value;
        }
    }
    /*====================== Cookies section below ================================ */
    $number_of_days = 730;
    $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days;
    if (isset($_COOKIE['last_search_selection']) && !isset($searchTerms['make']) && !isset($GLOBALS['ISC_CLASS_REDEFINE_SEARCH']) && !isset($input['change'])) {
        if (!empty($_COOKIE['last_search_selection']['make'])) {
            $searchTerms['make'] = $_COOKIE['last_search_selection']['make'];
        }
        if (!isset($searchTerms['model']) && !empty($_COOKIE['last_search_selection']['model'])) {
            $searchTerms['model'] = $_COOKIE['last_search_selection']['model'];
            $_REQUEST['model'] = $searchTerms['model'];
        } else {
            unset($searchTerms['model'], $searchTerms['model_flag'], $_REQUEST['model']);
        }
        if (!isset($searchTerms['year']) && !empty($_COOKIE['last_search_selection']['year'])) {
            $searchTerms['year'] = $_COOKIE['last_search_selection']['year'];
            $_REQUEST['year'] = $searchTerms['year'];
        }
        setcookie("last_search_selection[make]", isset($searchTerms['make']) ? $searchTerms['make'] : '', $date_of_expiry, "/");
        setcookie("last_search_selection[model]", isset($searchTerms['model']) ? $searchTerms['model'] : '', $date_of_expiry, "/");
        setcookie("last_search_selection[year]", isset($searchTerms['year']) ? $searchTerms['year'] : '', $date_of_expiry, "/");
        //setcookie( "last_search_selection[MMY_KEY]", time(), $date_of_expiry  );
    } else {
        if (!isset($GLOBALS['ISC_CLASS_REDEFINE_SEARCH'])) {
            setcookie("last_search_selection[make]", isset($searchTerms['make']) ? $searchTerms['make'] : '', $date_of_expiry, "/");
            if (isset($searchTerms['model']) && (!isset($searchTerms['model_flag']) || $searchTerms['model_flag'] == 1)) {
                setcookie("last_search_selection[model]", $searchTerms['model'], $date_of_expiry, "/");
            } else {
                setcookie("last_search_selection[model]", '', $date_of_expiry, "/");
            }
            setcookie("last_search_selection[year]", isset($searchTerms['year']) ? $searchTerms['year'] : '', $date_of_expiry, "/");
            //setcookie( "last_search_selection[MMY_KEY]", time(), $date_of_expiry  );
        }
    }
    return $searchTerms;
}