clsItemDB();
$this->tablename = GetTablePrefix()."StatItem";
$this->id_field = "StatItemId";
$this->NoResourceId = 1;
$this->GotValue = FALSE;
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);
return true;
}
function ExecuteResetSQL()
{
$sql = $this->Get("ValueSQL");
$sql = str_replace("<%prefix%>",GetTablePrefix(),$sql);
$this->adodbConnection->Execute($sql);
}
function ExecuteValueSQL()
{
global $ADODB_FETCH_MODE;
$ADODB_FETCH_MODE = 3;
$this->CurrentSQL = $this->Get("ValueSQL");
// 1. replace prefix to actual one
$this->CurrentSQL = str_replace("<%prefix%>", GetTablePrefix(), $this->CurrentSQL);
// 2. replace config variable to it's value
while( ($tag = $this->FindTag()) != false )
{
if( !array_key_exists( trim($tag), $this->TagCache ) )
{
// unprocessed tag -> parse it to get result
$this->TagCache[ trim($tag) ] = $this->ProcessTag($tag);
}
$tagResult = $this->TagCache[ trim($tag) ]; // return result from cache
$this->CurrentSQL = str_replace('<%'.$tag.'%>', $tagResult, $this->CurrentSQL);
}
$values = array();
$rs = $this->adodbConnection->Execute($this->CurrentSQL);
if($rs && !$rs->EOF)
{
$value = $rs->fields[0];
if($this->PostFormatting)
{
switch($this->PostFormatting)
{
case 'number': // simple-specific postformatting
$value = LangNumber($value, $this->PostFormattingParams['precision']);
break;
// extended postformatting
case 'COUNT':
$value = $rs->RecordCount();
break;
case 'SUM':
$field_sum = 0;
$rs->MoveFirst();
while(!$rs->EOF)
{
$field_sum += $rs->fields[ $this->PostFormattingParams['field'] ];
$rs->MoveNext();
}
$value = $field_sum;
if($this->PostFormattingParams['format_as'] == 'file') $value = size($value);
break;
// other type of information (not from db)
case 'SysFileSize':
$value = size( dir_size($GLOBALS['pathtoroot']) );
break;
default: // simple-default postformatting
$value = adodb_date($this->PostFormatting, $value);
break;
}
$this->PostFormatting = false;
$this->PostFormattingParams = Array();
}
$this->Set("Value", $value);
}
$ADODB_FETCH_MODE = 2;
$this->GotValue=TRUE;
}
// -- new: begin --
function FindTag()
{
// finds tag in current sql & returns it if found, false otherwise
$tagOpen = '<%'; $tagClose = '%>'; $tagOpenLen = strlen($tagOpen);
$startPos = strpos($this->CurrentSQL, $tagOpen);
if( $startPos !== false )
{
$endPos = strpos($this->CurrentSQL, $tagClose, $startPos);
return ($endPos > $startPos) ? substr($this->CurrentSQL, $startPos + $tagOpenLen, $endPos - $startPos - $tagOpenLen) : false;
}
return false;
}
function ProcessTag($tag)
{
$db =& $this->adodbConnection;
$tmp_params = explode(' ', $tag); // 1st - function, 2nd .. nth - params
$tag_name = array_shift($tmp_params);
foreach($tmp_params as $param)
{
$param = explode('=', $param);
$param_value = substr($param[1], 1, strlen($param[1]) - 2);
$param_value = str_replace('+',' ', $param_value);
$tag_params[ $param[0] ] = $param_value;
}
switch($tag_name)
{
case 'm:config': // rerieves config value for specific parameter (parameter name passed)
$sql = 'SELECT VariableValue FROM '.GetTablePrefix()."ConfigurationValues WHERE VariableName = '%s'";
return $db->GetOne( sprintf($sql, $tag_params['name']) );
break;
case 'm:post_format':
switch($tag_params['type'])
{
case 'date': $this->PostFormatting = GetDateFormat(); break;
case 'time': $this->PostFormatting = GetTimeFormat(); break;
case 'currency':
$this->PostFormatting = 'number';
$this->PostFormattingParams['precision'] = $tag_params['precision'];
break;
}
return $tag_params['field'];
break;
case 'm:custom_action':
$this->PostFormatting = $tag_params['action'];
return ($tag_params['sql'] == 'empty') ? 'SELECT 1' : $tag_params['sql'];
break;
//m:sql_action sql="SHOW TABLES" action="COUNT" field="*"
case 'm:sql_action':
$this->PostFormatting = $tag_params['action'];
$this->PostFormattingParams = $tag_params;
return $tag_params['sql'];
break;
case 'link:hit_count':
$type = $tag_params['type'];
$sql = 'SELECT Hits FROM '.GetTablePrefix().'Link ';
if($type == 'top') // by now only top is supported
{
$top_links_count = $this->ProcessTag('m:config name="Link_MinPopRating"'); // 5 - default
$sql .= 'ORDER BY Hits DESC LIMIT 0,'.$top_links_count;
$rs = $db->Execute($sql);
if($rs->RecordCount() > 0)
{
$rs->MoveLast();
return $rs->fields['Hits'];
}
else
return 0;
}
break;
case 'article:hit_count':
$type = $tag_params['type'];
$sql = 'SELECT CachedRating FROM '.GetTablePrefix().'News ';
if($type == 'top') // by now only top is supported
{
$top_articles_count = $this->ProcessTag('m:config name="News_MaxHotNumber"');
$min_votes = $this->ProcessTag('m:config name="News_MinPopVotes"');
$sql .= 'WHERE CachedVotesQty > '.$min_votes.' ORDER BY CachedRating DESC LIMIT 0,'.$top_articles_count;
$rs = $db->Execute($sql);
if($rs->RecordCount() > 0)
{
$rs->MoveLast();
return $rs->fields['CachedRating'];
}
else
return 0;
}
break;
case 'topic:hit_count':
$type = $tag_params['type'];
$sql = 'SELECT Views FROM '.GetTablePrefix().'Topic ';
if($type == 'top') // by now only top is supported
{
$top_posts_count = $this->ProcessTag('m:config name="Topic_MinPopRating"');
$sql .= ' ORDER BY Views DESC LIMIT 0,'.$top_posts_count;
$rs = $db->Execute($sql);
if($rs->RecordCount() > 0)
{
$rs->MoveLast();
return $rs->fields['Views'];
}
else
return 0;
}
break;
case 'modules:get_current':
return $this->Get('Module');
break;
}
}
// -- new: end --
function GetValue()
{
if(!$this->GotValue)
$this->ExecuteValueSQL();
return $this->Get("Value");
}
function ParseObject($element)
{
return $this->parsetag($element->name);
}
function parsetag($tag)
{
if(is_object($tag))
{
$tagname = $tag->name;
}
else
$tagname = $tag;
switch($tagname)
{
case "stats_module":
$ret = $this->Get("Module");
break;
case "stats_label":
$ret = admin_language($this->Get("ListLabel"));
break;
case "stats_adminlabel":
$ret = admin_language($this->Get("ListLabel"));
brea;
case "stats_value":
$ret = $this->GetValue();
break;
}
return $ret;
}
}
class clsStatList extends clsItemCollection
{
var $Page;
var $PerPageVar;
function clsStatList()
{
$this->clsItemCollection();
$this->SourceTable = GetTablePrefix()."StatItem";
$this->classname = "clsStatItem";
$this->Page=1;
$this->PerPageVar = "Perpage_StatItem";
$this->AdminSearchFields = array("ListLabel", "Module", "Value");
}
function ProcessList($where, $orderBy, $use_limit = false)
{
// fills list with data
$this->LoadStatItems($where,$orderBy, $use_limit );
for($i=0; $i < $this->NumItems(); $i++)
{
$s =& $this->GetItemRefByIndex($i);
$s->Set("ListLabel",admin_language($s->Get("ListLabel")));
}
$this->ExecuteValue();
}
function ExecuteValue()
{
for($i=0;$i<$this->NumItems();$i++)
{
$s =& $this->GetItemRefByIndex($i);
$s->ExecuteValueSQL();
}
if($this->debuglevel) echo "LastSQL: ".$s->CurrentSQL.'
';
}
function ExecuteResetSQL()
{
foreach($this->Items as $s) $s->ExecuteResetSQL();
}
function LoadStatItems($where=NULL,$orderBy, $use_limit = false)
{
global $objSession, $objConfig;
$sql = "SELECT * FROM ".$this->SourceTable;
if(strlen(trim($where)))
$sql .= " WHERE ".$where;
if(strlen(trim($orderBy)))
$sql .= " ORDER BY ".$orderBy;
if($use_limit)
$sql .= " ".GetLimitSQL($this->Page,$objConfig->Get($this->PerPageVar));
if($this->debuglevel) echo "StatGetSQL: $sql
";
$this->Query_Item($sql);
}
}
?>