Index: trunk/core/kernel/db/dblist.php =================================================================== diff -u -N -r2711 -r2737 --- trunk/core/kernel/db/dblist.php (.../dblist.php) (revision 2711) +++ trunk/core/kernel/db/dblist.php (.../dblist.php) (revision 2737) @@ -32,7 +32,15 @@ */ class kDBList extends kDBBase { + /** + * Holds totals for fields specified in config + * + * @var Array + */ + var $Totals = Array(); + + /** * Description * * @var array @@ -149,7 +157,8 @@ * * @return kDBList */ - function kDBList() { + function kDBList() + { parent::kDBBase(); $this->OrderFields = Array(); @@ -318,6 +327,51 @@ return true; } + function CalculateTotals() + { + $this->Totals = Array(); + + $fields = Array(); + foreach($this->Fields as $field_name => $field_options) + { + $totals = getArrayValue($field_options, 'totals'); + if(!$totals) continue; + + $field_expression = isset($this->CalculatedFields[$field_name]) ? $this->CalculatedFields[$field_name] : '`'.$this->TableName.'`.`'.$field_name.'`'; + $fields[$field_name] = $totals.'('.$field_expression.') AS '.$field_name.'_'.$totals; + } + + if(!$fields) return false; + + $sql = $this->GetSelectSQL(true, false); + $fields = implode(', ', $fields); + + if ( preg_match("/DISTINCT(.*?)FROM(?!_)/is",$sql,$regs ) ) + { + $sql = preg_replace("/^.*SELECT DISTINCT(.*?)FROM(?!_)/is", 'SELECT '.$fields.' FROM', $sql); + } + else + { + $sql = preg_replace("/^.*SELECT(.*?)FROM(?!_)/is", 'SELECT '.$fields.' FROM ', $sql); + } + + $totals = $this->Conn->Query($sql); + + foreach($totals as $totals_row) + { + foreach($totals_row as $total_field => $field_value) + { + if(!isset($this->Totals[$total_field])) $this->Totals[$total_field] = 0; + $this->Totals[$total_field] += $field_value; + } + } + } + + function getTotal($field, $total_function) + { + return $this->Totals[$field.'_'.$total_function]; + } + /** * Builds full select query except for LIMIT clause *