Index: trunk/core/units/general/helpers/search_helper.php =================================================================== diff -u -r4225 -r4228 --- trunk/core/units/general/helpers/search_helper.php (.../search_helper.php) (revision 4225) +++ trunk/core/units/general/helpers/search_helper.php (.../search_helper.php) (revision 4228) @@ -12,17 +12,76 @@ function splitKeyword($keyword) { $quotes_re = '/([+\-]?)"(.*?)"/'; - $no_qutes_re = '/([+\-]?)([^ ]+)/'; + $no_quotes_re = '/([+\-]?)([^ ]+)/'; $quoted_kw = preg_match_all($quotes_re, $keyword, $res); foreach ($res[2] as $index => $kw) $final[$kw] = $res[1][$index]; $keyword = preg_replace($quotes_re, '', $keyword); - $not_quoted_kw = preg_match_all($no_qutes_re, $keyword, $res); + $not_quoted_kw = preg_match_all($no_quotes_re, $keyword, $res); foreach ($res[2] as $index => $kw) $final[$kw] = $res[1][$index]; return $final; } + function getPositiveKeywords($keyword) + { + $keywords = $this->splitKeyword($keyword); + + $ret = Array(); + foreach ($keywords as $keyword => $sign) { + if ($sign == '+' || $sign == '') { + $ret[] = $keyword; + } + } + return $ret; + } + + function buildWhereClause($keyword, $fields) + { + $keywords = $this->splitKeyword($keyword); + + $normal_conditions = Array(); + $plus_conditions = Array(); + $minus_conditions = Array(); + + foreach ($keywords as $keyword => $sign) { + switch ($sign) { + case '+': + $plus_conditions[] = implode(' LIKE "%'.$keyword.'%" OR ', $fields).' LIKE "%'.$keyword.'%"'; + break; + + case '-': + foreach ($fields as $field) { + $condition[] = $field.' NOT LIKE "%'.$keyword.'%" OR '.$field.' IS NULL'; + } + $minus_conditions[] = '('.implode(') AND (', $condition).')'; + break; + + case '': + $normal_conditions[] = implode(' LIKE "%'.$keyword.'%" OR ', $fields).' LIKE "%'.$keyword.'%"'; + break; + } + } + + // building where clause + if ($normal_conditions) { + $where_clause = '('.implode(') OR (', $normal_conditions).')'; + } + else { + $where_clause = '1'; + } + + if ($plus_conditions) { + $where_clause = '('.$where_clause.') AND ('.implode(') AND (', $plus_conditions).')'; + } + + if ($minus_conditions) { + $where_clause = '('.$where_clause.') AND ('.implode(') AND (', $minus_conditions).')'; + } + + return $where_clause; + } + } ?> \ No newline at end of file