clsParsedItem();
$this->tablename = GetTablePrefix()."SearchConfig";
$this->id_field = "SearchConfigId";
$this->NoResourceId=1;
$this->TagPrefix="searchfield";
if($id)
$this->LoadFromDatabase($id);
}
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)
{
$Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"LoadFromDatabase");
return false;
}
$data = $result->fields;
$this->SetFromArray($data);
$this->Clean();
return true;
}
function GetWhereClause($verb,$value)
{
//$value = $this->adodbConnection->qstr(stripslashes($value));
$where = "";
if(strlen($this->Get("ForeignField"))>0)
{
$field = $this->Get("ForeignField");
}
else
{
$field = $this->Get("FieldName");
}
if (is_array($value))
{
foreach ($value as $value_item) {
$where .= '('.$field.' = '.$this->adodbConnection->qstr($value_item).') OR ';
}
return preg_replace('/(.*) OR $/', '\\1', $where);
}
switch($verb)
{
case "is":
if(strlen($this->Get("IsWhere")))
{
$where = str_replace("{Value}",$value,$this->Get("IsWhere"));
}
else
$where = $field." = '".$value."'";
break;
case "isnot":
if(strlen($this->Get("IsNotWhere")))
{
$where = str_replace("{Value}",$value,$this->Get("IsNotWhere"));
}
else
$where = $field." != '".$value."'";
break;
case "contains":
if(strlen($this->Get("ContainsWhere")))
{
$where = str_replace("{Value}",$value,$this->Get("ContainsWhere"));
}
else
$where = $field." LIKE '%".$value."%'";
break;
case "notcontains":
if(strlen($this->Get("NotContainsWhere")))
{
$where = str_replace("{Value}",$value,$this->Get("NotContainsWhere"));
}
else
$where = $field." NOT LIKE '%".$value."%'";
break;
}
return $where;
}
function ParseObject($element)
{
global $objConfig, $objSession, $objUsers, $objCatList, $var_list_update, $var_list,
$bb_var_list_update, $m_var_list_update,$FormValues;
$extra_attribs = ExtraAttributes($element->attributes);
if (strtolower($element->name)==$this->TagPrefix)
{
$field = strtolower($element->attributes["_field"]);
switch($field)
{
case "name":
$ret = $this->Get("FieldName");
break;
case "lang_name":
$ret = language($this->Get("DisplayName"));
break;
case "id":
$ret = $this->Get("SearchConfigId");
break;
case "header":
$hdr = $this->Get("ConfigHeader");
if ($hdr) $ret = language($hdr);
break;
case "table":
$ret = $this->Get("TableName");
break;
case "simple":
$ret = (int)$this->Get("SimpleSearch");
if($ret)
{
if(strlen($element->attributes["_text"]))
$ret = language($element->attributes["_text"]);
if(strlen($element->attributes["_plaintext"]))
$ret = $element->attributes["_plaintext"];
}
break;
case "advanced":
$ret = (int)$this->Get("AdvancedSearch");
if($ret)
{
if(strlen($element->attributes["_text"]))
$ret = language($element->attributes["_text"]);
if(strlen($element->attributes["_plaintext"]))
$ret = $element->attributes["_plaintext"];
}
break;
case "verb_select":
$datatype = $this->Get("FieldType");
switch($datatype)
{
case "text":
case 'textarea':
$fieldname = "verb[".$this->Get("FieldName")."]";
$ret = "";
break;
case "boolean":
$ret = "";
break;
case 'select':
case 'radio':
case 'checkbox':
// $ret = '';
$fieldname = 'verb['.$this->Get('FieldName').']';
$ret = '";
break;
}
break;
case 'form_input':
$datatype = $this->Get("FieldType");
$name = $this->Get("FieldName");
switch($datatype)
{
case "text":
case 'textarea':
$ret = "";
break;
case 'select':
$current_value = $FormValues[$name];
$values = $this->parseValuesList();
$ret = '';
break;
case 'radio':
$current_value = $FormValues[$name];
$values = $this->parseValuesList();
$ret = '';
$option_tpl = ' %s ';
foreach ($values as $value_key => $value_name) {
$selected = ($current_value == $value_key) ? ' checked' : '';
$ret .= sprintf($option_tpl, $value_key, $selected, $value_name);
}
break;
case 'checkbox':
$current_value = $FormValues[$name];
$values = $this->parseValuesList();
$ret = '';
$option_tpl = ' %s ';
foreach ($values as $value_key => $value_name) {
$selected = ($current_value == $value_key) ? ' checked' : '';
$ret .= sprintf($option_tpl, $value_key, $selected, $value_name);
}
break;
/*case 'password':
break;*/
case "boolean":
$ret = '
';
break;
case 'range':
$ret = '';
break;
}
break;
case "andor_radio":
$name = $this->Get("FieldName");
$field = "andor[$name]";
$checked = Array(1 => '', 2 => '');
$checked[ $FormValues[$field] ] = ' checked';
if(!$FormValues[$field]) $checked[1] = ' checked';
$ret = '';
return $ret;
break;
}
}
return $ret;
}
function getHumanField()
{
return language( $this->Get('DisplayName') );
}
function getHumanVerb($verb)
{
return language('lu_advsearch_'.$verb);
}
function getHumanValue($value)
{
$data_type = $this->Get('FieldType');
switch ($data_type) {
case 'text':
case 'textarea':
$ret = $value;
break;
case 'select':
case 'radio':
case 'checkbox':
$values = $this->parseValuesList();
$ret = $values[$value];
break;
case 'boolean':
$values = Array(-1 => 'lu_advsearch_any', 1 => 'lu_true', 0 => 'lu_false');
$ret = language($values[$value]);
break;
}
return $ret;
}
/**
* If custom field contains values list, then parse it into array
*
* @return Array
*/
function parseValuesList()
{
$sql = 'SELECT ValueList
FROM '.GetTablePrefix().'CustomField
WHERE CustomFieldId = '.$this->Get('CustomFieldId');
$list = $this->adodbConnection->GetOne($sql);
if (!$list) return Array();
$ret = Array();
$values = explode(',', $list);
foreach ($values as $mixed_value) {
list($value_key, $value_name) = explode('=', $mixed_value);
$value_key = trim($value_key);
$value_name = trim($value_name);
if (substr($value_name, 0, 1) == '+')
{
$ret[$value_key] = substr($value_name, 1, strlen($value_name));
}
else {
$ret[$value_key] = language($value_name);
}
}
return $ret;
}
}
class clsSearchConfigList
{
var $Module;
var $Items;
var $SourceTable;
var $classname;
var $adodbConnection;
function clsSearchConfigList($module=NULL)
{
$this->SourceTable = GetTablePrefix()."SearchConfig";
$this->classname = "clsSearchConfig";
$this->adodbConnection = &GetADODBConnection();
$this->Items = array();
if($module)
$this->LoadModule($module);
}
function Clear()
{
unset($this->Items);
$this->Items = array();
}
function ResetModule()
{
$this->Clear();
$sql = "UPDATE ".$this->SourceTable." SET SimpleSearch=0, AdvancedSearch=0 WHERE ModuleName='".$this->Module."'";
$this->adodbConnection->Execute($sql);
}
function LoadModule($mod)
{
$this->Clear();
$this->Module = $mod;
$sql = "SELECT * FROM ".$this->SourceTable." WHERE ModuleName ='$mod' ORDER BY DisplayOrder";
return $this->Query_Item($sql);
}
function GetNextDisplayOrder($mod)
{
$ret = 0;
$sql = "SELECT MAX(DisplayOrder) AS do FROM ".$this->SourceTable." WHERE ModuleName = '$mod'";
$rs = $this->adodbConnection->Execute($sql);
if($rs && !$rs->EOF)
$ret = $rs->fields["do"]+1;
return $ret;
}
function AddItemFromArray($data)
{
$class = new $this->classname;
$class->SetFromArray($data);
//foreach($data as $prop => $value)
// $class->Set($prop,$value);
array_push($this->Items,$class);
return $class;
}
function Query_Item($sql, $limit=NULL)
{
global $Errors;
if(isset($limit))
$result = $this->adodbConnection->SelectLimit($sql, $limit);
else
$result = $this->adodbConnection->Execute($sql);
if ($result === false)
{
$Errors->AddError("error.DatabaseError",NULL,$this->adodbConnection->ErrorMsg(),"",get_class($this),"Query_Item");
return false;
}
$this->Clear();
while ($result && !$result->EOF)
{
$data = $result->fields;
$this->AddItemFromArray($data);
$result->MoveNext();
}
return $this->Items;
}
function GetItem($ID)
{
$found=FALSE;
if(is_array($this->Items))
{
foreach($this->Items as $i)
{
if($i->UniqueID()==$ID)
{
$found=TRUE;
break;
}
}
}
if(!$found)
{
$i = NULL;
$i = new $this->classname();
//$i->idfield = $i->IdField(); //set the id field before we change tables
$i->tablename = $this->SourceTable;
$i->LoadFromDatabase($ID);
array_push($this->Items, $i);
}
return $i;
}
function GetItemByName($Table,$Name)
{
$found=FALSE;
if(is_array($this->Items))
{
foreach($this->Items as $i)
{
if($i->Get("TableName")==$Table && $i->Get("FieldName")==$Name)
{
$found=TRUE;
break;
}
}
}
if(!$found)
{
$i = NULL;
$rs = $this->adodbConnection->Execute("SELECT * FROM ".$this->SourceTable." WHERE Tablename='$Table' AND FieldName='$Name'");
if($rs && !$rs->EOF)
{
$i = new $this->classname();
//$i->idfield = $i->IdField(); //set the id field before we change tables
$i->tablename = $this->SourceTable;
$i->SetFromArray($rs->fields);
}
array_push($this->Items, $i);
}
return $i;
}
function EditFieldSettings($FieldId,$SimpleSearch,$AdvancedSearch,$Priority)
{
$f = $this->GetItem($FieldId);
//echo ""; print_r($f); echo "
";
$f->Set("SimpleSearch",(int)$SimpleSearch);
$f->Set("AdvancedSearch",(int)$AdvancedSearch);
$f->Set("Priority",(int)$Priority);
$f->Update();
return $f;
}
function &AddSearchField($Table,$FieldName,$Module,$Simple,$Adv,$Description,$DisplayName,$ConfigHeader,$DisplayOrder=0,$Priority=0,$FieldType="text",$JoinClause=NULL,$IsWhere=NULL,$IsNotWhere=NULL,$ContainsWhere=NULL,$NotContainsWhere=NULL,$CustomId=0, $ForeignField = NULL)
{
$f = new clsSearchConfig();
if($DisplayOrder==0)
$DisplayOrder = $this->GetNextDisplayOrder($Module);
$f->Set(Array("TableName","ModuleName","SimpleSearch","AdvancedSearch","Description","DisplayName","ConfigHeader",
"DisplayOrder","Priority","FieldType","FieldName", "ForeignField"),
Array($Table,$Module,$Simple,$Adv,$Description,$DisplayName,$ConfigHeader,$DisplayOrder,$Priority,$FieldType,$FieldName, $ForeignField));
$f->Set("CustomFieldId",$CustomId);
if(strlen($JoinClause))
$f->Set("JoinClause",$JoinClause);
if(strlen($IsWhere))
$f->Set("IsWhere",$IsWhere);
if(strlen($IsNotWhere))
$f->Set("IsNotWhere",$IsNotWhere);
if(strlen($ContainsWhere))
$f->Set("ContainsWhere",$ContainsWhere);
if(strlen($NotContainsWhere))
$f->Set("NotContainsWhere",$NotContainsWhere);
$f->Create();
return $f;
}
function GetHeadingList()
{
$res = array();
foreach($this->Items as $i)
{
$header = $i->Get("ConfigHeader");
$res[$header]=1;
}
reset($res);
return array_keys($res);
}
function GetHeadingItems($heading)
{
$res = array();
foreach($this->Items as $i)
{
if($i->Get("ConfigHeader")==$heading)
array_push($res,$i);
}
return $res;
}
}
?>