Index: trunk/kernel/include/searchitems.php =================================================================== diff -u -N -r4683 -r6093 --- trunk/kernel/include/searchitems.php (.../searchitems.php) (revision 4683) +++ trunk/kernel/include/searchitems.php (.../searchitems.php) (revision 6093) @@ -5,7 +5,7 @@ "word word" : contents between the quotes are treated as a single entity +/-"word word" is supported ignore words are not case sensitive -*/ +*/ class clsSearchLog extends clsItemDB { function clsSearchLog($id=NULL) @@ -21,12 +21,12 @@ function LoadFromDatabase($Id) { global $Errors; - + if(!isset($Id)) { $Errors->AddError("error.AppError",NULL,'Internal error: LoadFromDatabase id',"",get_class($this),"LoadFromDatabase"); return false; - } + } $sql = sprintf("SELECT * FROM ".$this->tablename." WHERE ".$this->IdField()." = '%s'",$Id); $result = $this->adodbConnection->Execute($sql); if ($result === false) @@ -40,7 +40,7 @@ $this->SetFromArray($data); $this->Clean(); return true; - } + } } class clsSearchLogList extends clsItemCollection @@ -105,12 +105,12 @@ function LoadFromDatabase($Id) { global $Errors; - + if(!isset($Id)) { $Errors->AddError("error.AppError",NULL,'Internal error: LoadFromDatabase id',"",get_class($this),"LoadFromDatabase"); return false; - } + } $sql = sprintf("SELECT * FROM ".$this->tablename." WHERE ".$this->IdField()." = '%s'",$Id); $result = $this->adodbConnection->Execute($sql); if ($result === false) @@ -124,7 +124,7 @@ $this->SetFromArray($data); $this->Clean(); return true; - } + } } class clsEmailLogList extends clsItemCollection @@ -183,7 +183,7 @@ var $SourceTable; var $Relationships; var $Ignored_Words; - var $CatClause; + var $CatClause; var $Keywords; var $Phrase = ""; var $SearchType; @@ -226,30 +226,30 @@ { $limit = "LIMIT $Start,$PerPage"; } - $sql = "SELECT * FROM ".$this->ResultTable." ".$limit; + $sql = "SELECT * FROM ".$this->ResultTable." ".$limit; $this->Clear(); $rs = $this->adodbConnection->Execute($sql); return $this->Query_Item($sql); } - + function SetCategoryClause($whereclause) { $this->CatClause=$whereclause; } function AddRelationship($JoinTable,$JoinExpression=NULL) { - - $this->Relationships[$JoinTable]=$JoinExpression; + + $this->Relationships[$JoinTable]=$JoinExpression; } function SetKeywords($keywords) { - $this->Phrase=$keywords; + $this->Phrase=$keywords; $this->keywords = GetKeywords($keywords); } - + function AddSimpleCustomFields($main_prefix) { $table_to_module = Array( 'Category' => 'In-Portal', @@ -258,39 +258,50 @@ 'PortalUser' => 'In-Portal', 'News' => 'In-News'); $module = $table_to_module[$this->SourceTable]; - + $sql = 'SELECT Priority, CustomFieldId FROM '.TABLE_PREFIX.'SearchConfig WHERE ModuleName = '.$this->Conn->qstr($module).' AND SimpleSearch = 1 AND CustomFieldId > 0'; - + $search_config = $this->Conn->GetCol($sql, 'CustomFieldId'); if ($search_config) { $item_table = $this->Application->getUnitOption($main_prefix, 'TableName'); $custom_table = $this->Application->getUnitOption($main_prefix.'-cdata', 'TableName'); $this->AddRelationship($custom_table.' AS '.$main_prefix.'_custom_data', $main_prefix.'_custom_data.ResourceId = '.$item_table.'.ResourceId'); - + $ml_formatter =& $this->Application->recallObject('kMultiLanguage'); foreach ($search_config as $custom_id => $custom_priority) { $field_name = $main_prefix.'_custom_data.'.$ml_formatter->LangFieldName('cust_'.$custom_id); $this->FieldList[] = $field_name; $this->FieldWeight[$field_name] = $custom_priority; } } - } + } function AddSimpleFields($main_prefix) { $sql = "SELECT * FROM ".GetTablePrefix()."SearchConfig WHERE TableName='".$this->SourceTable."' AND SimpleSearch=1 AND CustomFieldId IS NULL"; //echo $sql; - foreach($this->Relationships as $Table=>$clause) + foreach($this->Relationships as $Table=>$clause) { if(strlen($Table)>0 && $Table != "Category") $sql .= " OR TableName='".$Table."'"; } $rs = $this->adodbConnection->Execute($sql); - + + $ml_formatter =& $this->Application->recallObject('kMultiLanguage'); + $lang = $this->Application->GetVar('m_lang'); while($rs && !$rs->EOF) { - $f = GetTablePrefix().$rs->fields["TableName"].".".$rs->fields["FieldName"]; + $config = $this->Application->getUnitOption($main_prefix, 'Fields'); + if (isset($config[$rs->fields["FieldName"]]) && $config[$rs->fields["FieldName"]]['formatter'] == 'kMultiLanguage') { + $f = GetTablePrefix().$rs->fields["TableName"].".".'l'.$lang.'_'.$rs->fields["FieldName"]; + $f_prim = GetTablePrefix().$rs->fields["TableName"].".".'l'.$this->Application->GetDefaultLanguageId().'_'.$rs->fields["FieldName"]; + $this->FieldList[] = $f_prim; + $this->FieldWeight[$f_prim] = $rs->fields["Priority"]; + } + else { + $f = GetTablePrefix().$rs->fields["TableName"].".".$rs->fields["FieldName"]; + } $this->FieldList[] = $f; $this->FieldWeight[$f] = $rs->fields["Priority"]; $rs->MoveNext(); @@ -304,14 +315,14 @@ /* build required keywords string */ if(count($this->keywords["required"])>0) - { - $required = $this->keywords["required"]; + { + $required = $this->keywords["required"]; for($i=0;$i0) { if($i>0) - { + { $or =" OR "; } else @@ -321,7 +332,7 @@ } } if(strlen($w)>0) - { + { $req_where = "(". $w.")"; } else @@ -330,19 +341,19 @@ $w = ""; $not_where=""; if(count($this->keywords["notallowed"])>0) - { + { $words = $this->keywords["notallowed"]; for($i=0;$i0) { if($i>0) - { + { $or =" AND "; } else $or = ""; - $w .= $or." ".$FieldName." NOT LIKE '%".$words[$i]."%'"; + $w .= $or." ".$FieldName." NOT LIKE '%".$words[$i]."%'"; $this->ExcludeWhereClause[$words[$i]][$FieldName] = '('.$FieldName." NOT LIKE '%".$words[$i]."%'"; if (substr($FieldName, 0, strlen(GetTablePrefix())) == GetTablePrefix()) { @@ -352,33 +363,33 @@ { $this->ExcludeWhereClause[$words[$i]][$FieldName] .= ' OR '.$FieldName.' IS NULL)'; // for custom fields } - } + } } if(strlen($w)>0) - { + { $not_where = "(".$w.")"; } else $not_where = ""; } - + $w=""; $normal = $this->keywords["normal"]; if(count($normal)>0) - { + { for($i=0;$i0) - { + { + if (strlen($normal[$i])>0) + { if($i>0) - { + { $or =" OR "; } else $or = ""; $w .= "$or $FieldName LIKE '%".$normal[$i]."%'"; } - } + } /*if(count($required)>0) $w .= " OR "; for($i=0;$i0) - { + { $where = "(".$w.")"; } else $where = ""; } $complete = BuildWhereClause($where, $req_where, $not_where, $this); - if ($complete) + if ($complete) { $this->WhereClauses[$FieldName]="(".$complete.")"; } - $this->Ignored_Words=$this->keywords["ignored"]; + $this->Ignored_Words=$this->keywords["ignored"]; } - + function PerformSearch($ItemType,$OrderBy=NULL,$InitTable=FALSE, $idlist=NULL) { static $SelectSQL, $OldItemType; @@ -425,15 +436,15 @@ $this->SourceTable = GetTablePrefix().$ctype->Get("SourceTable"); $result=0; $PopField = $ctype->Get("PopField"); - $RateField = $ctype->Get("RateField"); + $RateField = $ctype->Get("RateField"); //print_pre($this->keywords); - + if(!strlen($SelectSQL)) - { + { $typestr = str_pad($ItemType,2,"0",STR_PAD_LEFT); $SelectSQL = "SELECT "; $ifs = array(); - $weightsum = 0; + $weightsum = 0; foreach($this->FieldWeight as $w) { $weightsum += $w; } @@ -445,51 +456,51 @@ $s = array(); if(!$single) - { + { $full = trim(implode(" ",$this->keywords["normal"])); - $s[] = " (IF ($f LIKE '%$full%', ".$weightsum.", 0))"; + $s[] = " (IF ($f LIKE '%$full%', ".$weightsum.", 0))"; } foreach($this->keywords["normal"] as $k) - { + { if($k != $full || $single) - { + { $temp = " (IF ($f LIKE '%$k%', ".$weight.", 0))"; $s[] = $temp; } } - + foreach($this->keywords["required"] as $k) { if($this->RequiredRelevance>0) $weight = $this->FieldWeight[$f] + ($this->FieldWeight[$f]*($this->RequiredRelevance/100)); - + if($k != $full || $single) { $s[] = " (IF ($f LIKE '%$k%', ".$weight.", 0))"; } } - // echo "
";print_r($s); echo "
"; + // echo "
";print_r($s); echo "
"; $txt = implode("+",$s); //echo $txt."
\n"; - $ifs[] = $txt; + $ifs[] = $txt; unset($s); } // echo "
";print_r($ifs); echo "
"; /* add relevance formula for weighting hits & popularity */ - + if($weightsum==0) $weightsum=1; if(strlen($PopField)>0 && $this->PctPop>0) - { + { $popcalc = " + ((($PopField + 1) / (max($PopField)+1)*".$this->PctPop."))"; } else $popcalc = ""; if(strlen($RateField)>0 && $this->PctRating>0) - { + { $ratecalc = " + ((($RateField + 1) / (max($RateField)+1)*".$this->PctRating."))"; } else @@ -503,11 +514,11 @@ $relcalc = "0"; $SelectSQL .= $relcalc.$popcalc.$ratecalc." as Relevance, "; - + $SelectSQL .= $this->SourceTable.".".$idField." as ItemId, ".$this->SourceTable.".ResourceId as ResourceId, CONCAT($typestr) as ItemType, EditorsPick as EdPick FROM ".$this->SourceTable." "; - + foreach($this->Relationships as $JoinTable=>$OnClause) - { + { $SelectSQL .= "LEFT JOIN $JoinTable ON $OnClause "; } $first=1; @@ -523,26 +534,26 @@ } else { $this->AddSearchWhereClause($field); - } + } } if (count($this->WhereClauses)) { - $normal_where = '('; // not - or + + $normal_where = '('; // not - or + $normal_where .= implode(" or ",$this->WhereClauses); $normal_where .= ')'; } else { $normal_where = ''; } - + $where = $normal_where; if (count($this->ExcludeWhereClause)>0) { foreach ($this->ExcludeWhereClause as $section) - { + { foreach ($section as $clause_part) { if (strlen($where)) @@ -553,7 +564,7 @@ } } } - + if (count($this->RequireWhereClause)>0) { $requirewhere .= '('; @@ -569,43 +580,43 @@ $where_add .= $clause_part; } $where_add .= ')'; - if (strlen($requirewhere) > 1) $requirewhere .= ' AND '; + if (strlen($requirewhere) > 1) $requirewhere .= ' AND '; $requirewhere .= $where_add; } $requirewhere .= ')'; - if (strlen($where)) + if (strlen($where)) { $where .= ' AND '; } $where .= $requirewhere; } - - + + if(is_array($idlist)) { if (strlen($where)) { $where .= ' AND '; - } + } $where .= '('.$this->SourceTable.'.ResourceId IN ('.implode(',',$idlist).'))'; } } - + if (!strlen($where)) { $where = '1'; } $SelectSQL .= " WHERE ".$where; - + $SelectSQL .= " GROUP BY $idField "; - + //echo $SelectSQL."

\n"; - + if($InitTable == TRUE) { $this->adodbConnection->Execute("DROP TABLE IF EXISTS ".$this->ResultTable); //$indexSQL = "(INDEX(Relevance), INDEX(ItemId), INDEX(ItemType), INDEX sorting (EdPick,Relevance)) "; - $full_sql = "CREATE TABLE ".$this->ResultTable." ".$indexSQL.$SelectSQL; + $full_sql = "CREATE TABLE ".$this->ResultTable." ".$indexSQL.$SelectSQL; //echo "Full SQL:".$full_sql."
\n"; $this->adodbConnection->Execute($full_sql); //echo $this->adodbConnection->ErrorMsg()."
\n"; @@ -621,7 +632,7 @@ //Here we need to remove found items which was found by HTML tags matching keywords //$this->adodbConnection->Execute("DELETE FROM ".$this->ResultTable." WHERE ItemId = 13 AND ItemType = 4"); } - + function BuildIndexes() { $sql = "ALTER TABLE ".$this->ResultTable." ADD INDEX (Relevance), "; @@ -647,7 +658,7 @@ } } -function count_words($string) +function count_words($string) { // below line added to make contiguous spaces count as one space if(strlen($string)) @@ -662,19 +673,19 @@ function GetKeywords($phrase) { global $KeywordIgnore, $objConfig; - + //echo "Passed Phrase: $phrase
"; if(count($KeywordIgnore)==0) GetIgnoreList(); $keywords["normal"]= array(); $keywords["required"]= array(); $keywords["notallowed"] = array(); - $keywords["ignored"] = array(); + $keywords["ignored"] = array(); $min_length = $objConfig->Get('Search_MinKeyword_Length'); if (!strlen($phrase)) { return $keywords; } - + $w_array = array(); $phrase=trim($phrase); //if(count_words($phrase)>1) @@ -691,30 +702,30 @@ break; $exact_word=substr($phrase, $i+1, ($exact_match_close-$i)-1); $exact_word = str_replace("\\", "", $exact_word); - $exact_word = trim($exact_word); + $exact_word = trim($exact_word); $i=$exact_match_close; if($exact_word) - { + { if(strlen($token)==0) $token="|"; - $w_array[$ce]=$token.addslashes($exact_word); + $w_array[$ce]=$token.addslashes($exact_word); $token=""; $ce++; $exact_word=""; } break; - + case '+': if(strlen($exact_word)==0) - { + { $token = "+"; } else $exact_word .= "+"; break; case '-': if(strlen($exact_word)==0) - { + { $token = "-"; } else @@ -723,7 +734,7 @@ case " ": case ",": if($exact_word) - { + { if(strlen($token)==0) $token="|"; if($token=="|") @@ -734,7 +745,7 @@ $ce++; } else - { + { $w_array[$ce]=$token.addslashes($exact_word); $ce++; } @@ -745,7 +756,7 @@ $ce++; } $token=""; - $exact_word=""; + $exact_word=""; } break; @@ -763,12 +774,12 @@ $ce++; } else - { + { $w_array[$ce]=$token.addslashes($exact_word); $ce++; } } - for ($i=0;$i