Totally Zenned
- Join Date:
- Jan 2005
- Posts:
- 1,285
- Plugin Contributions:
- 0
Language-specific Search Operators
As my site's language is not english, I didn't like the fact that I needed to enter "and" or "or" when doing product searches, so I made a minor change to function zen_parse_search_string.
It will now make use of another constant, which should be translated for each language.
This way, I can enter for instance "gift og certificate" and "gift eller certificate" ("og" means "and", "eller" means "or").
I also added "&" and "|" as operators ("and" and "or").
First, add the following lines to includes/languages/<language>.php
define('OPERATOR_AND', 'and');
define('OPERATOR_OR', 'or');
(and translate 'and' and 'or' to your language)
Then simply replace the following code from includes/functions/functions_general.php:
(around line 364)
// add default logical operators if needed
$temp = array();
for($i=0; $i<(count($objects)-1); $i++) {
$temp[] = $objects[$i];
if ( ($objects[$i] != 'and') &&
($objects[$i] != 'or') &&
($objects[$i] != '(') &&
($objects[$i+1] != 'and') &&
($objects[$i+1] != 'or') &&
($objects[$i+1] != ')') ) {
$temp[] = ADVANCED_SEARCH_DEFAULT_OPERATOR;
}
}
$temp[] = $objects[$i];
$objects = $temp;
$keyword_count = 0;
$operator_count = 0;
$balance = 0;
for($i=0; $i<count($objects); $i++) {
if ($objects[$i] == '(') $balance --;
if ($objects[$i] == ')') $balance ++;
if ( ($objects[$i] == 'and') || ($objects[$i] == 'or') ) {
$operator_count ++;
} elseif ( ($objects[$i]) && ($objects[$i] != '(') && ($objects[$i] != ')') ) {
$keyword_count ++;
}
}
if ( ($operator_count < $keyword_count) && ($balance == 0) ) {
return true;
} else {
return false;
}
}
////
with this code:
// add default logical operators if needed
$temp = array();
for($i=0; $i<(count($objects)-1); $i++) {
$temp[] = $objects[$i];
if ( ($objects[$i] != OPERATOR_AND) &&
($objects[$i] != '&') &&
($objects[$i] != OPERATOR_OR) &&
($objects[$i] != '|') &&
($objects[$i] != '(') &&
($objects[$i+1] != OPERATOR_AND) &&
($objects[$i+1] != OPERATOR_OR) &&
($objects[$i+1] != '&') &&
($objects[$i+1] != '|') &&
($objects[$i+1] != ')') ) {
$temp[] = ADVANCED_SEARCH_DEFAULT_OPERATOR;
}
}
$temp[] = $objects[$i];
$objects = $temp;
$keyword_count = 0;
$operator_count = 0;
$balance = 0;
for($i=0; $i<count($objects); $i++) {
if ($objects[$i] == '(') $balance --;
if ($objects[$i] == ')') $balance ++;
if ( ($objects[$i] == OPERATOR_AND) || ($objects[$i] == '&') ) {$objects[$i] = 'and'; $operator_count ++;}
elseif ( ($objects[$i] == OPERATOR_OR) || ($objects[$i] == '|') ) {$objects[$i] = 'or'; $operator_count ++;}
elseif ( ($objects[$i]) && ($objects[$i] != '(') && ($objects[$i] != ')') ) {
$keyword_count ++;
}
}
if ( ($operator_count < $keyword_count) && ($balance == 0) ) {
return true;
} else {
return false;
}
}
////