Index: trunk/kernel/units/general/cat_event_handler.php =================================================================== diff -u -r4216 -r4228 --- trunk/kernel/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 4216) +++ trunk/kernel/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 4228) @@ -667,12 +667,14 @@ $join_clauses[] = ' LEFT JOIN '.$custom_table.' custom_data ON '.$product_table.'.ResourceId = custom_data.ResourceId'; } - foreach($field_list as $key => $field) - { + // what field in search config becomes what field in sql (key - new field, value - old field (from searchconfig table)) + $search_config_map = Array(); + + foreach ($field_list as $key => $field) { $options = $object->getFieldOptions($field); $local_table = TABLE_PREFIX.$search_config[$field]['TableName']; $weight_sum += $search_config[$field]['Priority']; // counting weight sum; used when making relevance clause - + // processing multilingual fields if (getArrayValue($options, 'formatter') == 'kMultiLanguage') { $field_list[$key] = 'l'.$lang.'_'.$field; @@ -704,8 +706,10 @@ $alias_counter++; $alias = 't'.$alias_counter; - + $field_list[$key] = $alias.'.'.$exploded[1]; + $search_config_map[ $field_list[$key] ] = $field; + $join_clause = str_replace('{ForeignTable}', $alias, $search_config[$field]['JoinClause']); $join_clause = str_replace('{LocalTable}', $product_table, $join_clause); @@ -720,115 +724,14 @@ $field_list[$key] = 'l'.$lang.'_cust_'.array_search($field_list[$key], $custom_fields); } $field_list[$key] = $local_table.'.'.$field_list[$key]; + $search_config_map[ $field_list[$key] ] = $field; } } // keyword string processing - $normal_keywords = Array(); - $plus_keywords = Array(); - $minus_keywords = Array(); - - for($i = 0; $i < strlen($keywords); $i++) - { - if(substr($keywords, $i, 1) == ' ') continue; - $extra_skip = 0; - switch(substr($keywords, $i, 1)) - { - case '+': - if(substr($keywords, $i + 1, 1) == '"') - { - $keyword_start = $i + 2; - $keyword_end = strpos($keywords, '"', $i + 2); - $extra_skip = 2; - } - else - { - $keyword_start = $i + 1; - $keyword_end = strpos($keywords, ' ', $i + 1); - $extra_skip = 0; - } - $target_array =& $plus_keywords; - break; - case '-': - if(substr($keywords, $i + 1, 1) == '"') - { - $keyword_start = $i + 2; - $keyword_end = strpos($keywords, '"', $i + 2); - $extra_skip = 2; - } - else - { - $keyword_start = $i + 1; - $keyword_end = strpos($keywords, ' ', $i + 1); - $extra_skip = 0; - } - $target_array =& $minus_keywords; - break; - case '"': - $keyword_start = $i + 1; - $keyword_end = strpos($keywords, '"', $i + 1); - $extra_skip = 1; - $target_array =& $normal_keywords; - break; - default: - $keyword_start = $i; - $keyword_end = strpos($keywords, ' ', $i + 1); - $target_array =& $normal_keywords; - } - - if($keyword_end === false) - { - $keyword_end = strlen($keywords); - } - $keyword_length = $keyword_end - $keyword_start; - $keyword = substr($keywords, $keyword_start, $keyword_length); - - if(strlen($keyword) >= $this->Application->ConfigValue('Search_MinKeyword_Length')) - { - $target_array[] = addcslashes($keyword, '"'); - } - - $i += $keyword_length + $extra_skip; - } - - // preparing conditions - $normal_conditions = Array(); - $plus_conditions = Array(); - $minus_conditions = Array(); - foreach($normal_keywords as $keyword) - { - $normal_conditions[] = implode(' LIKE "%'.$keyword.'%" OR ', $field_list).' LIKE "%'.$keyword.'%"'; - } - foreach($plus_keywords as $keyword) - { - $plus_conditions[] = implode(' LIKE "%'.$keyword.'%" OR ', $field_list).' LIKE "%'.$keyword.'%"'; - } - foreach($minus_keywords as $keyword) - { - foreach($field_list as $field) - { - $condition[] = $field.' NOT LIKE "%'.$keyword.'%" OR '.$field.' IS NULL'; - } - $minus_conditions[] = '('.implode(') AND (', $condition).')'; - } - - // 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).')'; - } + $search_helper =& $this->Application->recallObject('SearchHelper'); + $where_clause = $search_helper->buildWhereClause($keywords, $field_list); + $where_clause = $where_clause.' AND '.$product_table.'.Status=1'; if($this->Application->GetVar('Action') == 'm_simple_subsearch') // subsearch, In-portal @@ -846,21 +749,16 @@ } } - // building having clause - - // making relevance clause - $positive_words = array_merge($normal_keywords, $plus_keywords); + $positive_words = $search_helper->getPositiveKeywords($keywords); $this->Application->StoreVar('highlight_keywords', serialize($positive_words)); $revelance_parts = Array(); reset($search_config); - foreach($field_list as $field) - { + foreach ($field_list as $field) { $config_elem = each($search_config); - $weight = $search_config[$field]['Priority']; + $weight = $search_config[ $search_config_map[$field] ]['Priority']; $revelance_parts[] = 'IF('.$field.' LIKE "%'.implode(' ', $positive_words).'%", '.$weight_sum.', 0)'; - foreach($positive_words as $keyword) - { + foreach ($positive_words as $keyword) { $revelance_parts[] = 'IF('.$field.' LIKE "%'.$keyword.'%", '.$config_elem['value']['Priority'].', 0)'; } } @@ -872,17 +770,14 @@ $relevance_clause .= ' + (CachedRating + 1) / (MAX(CachedRating) + 1) * '.$rel_rating; // building final search query - if( !$this->Application->GetVar('INPORTAL_ON') ) - { + if (!$this->Application->GetVar('INPORTAL_ON')) { $this->Conn->Query('DROP TABLE IF EXISTS '.$search_table); // erase old search table if clean k4 event } - if($this->Conn->Query('SHOW TABLES LIKE "'.$search_table.'"')) - { + if ($this->Conn->Query('SHOW TABLES LIKE "'.$search_table.'"')) { $select_intro = 'INSERT INTO '.$search_table.' (Relevance, ItemId, ResourceId, ItemType, EdPick) '; } - else - { + else { $select_intro = 'CREATE TABLE '.$search_table.' AS '; }