Index: trunk/core/kernel/kbase.php =================================================================== diff -u -r4029 -r4284 --- trunk/core/kernel/kbase.php (.../kbase.php) (revision 4029) +++ trunk/core/kernel/kbase.php (.../kbase.php) (revision 4284) @@ -174,7 +174,15 @@ */ var $CalculatedFields = Array(); + /** + * Calculated fields, that contain aggregated functions, e.g. COUNT, SUM, etc. + * + * @var Array + */ + var $AggregatedCalculatedFields = Array(); + + /** * Description * * @var string Item' database table name, without prefix @@ -262,27 +270,56 @@ return $query; } + /** + * Returns required mixing of aggregated & non-aggregated calculated fields + * + * @param int $aggregated 0 - having + aggregated, 1 - having only, 2 - aggregated only + * @return Array + */ + function getCalculatedFields($aggregated = 0) + { + switch ($aggregated) { + case 0: + $fields = array_merge($this->CalculatedFields, $this->AggregatedCalculatedFields); + break; + + case 1: + $fields = $this->CalculatedFields; + break; + + case 2: + $fields = $this->AggregatedCalculatedFields; + break; + + default: + $fields = Array(); + break; + } + + return $fields; + } + + /** * Insert calculated fields sql into query in place of %2$s, * return processed query. * * @param string $query + * @param int $aggregated 0 - having + aggregated, 1 - having only, 2 - aggregated only * @return string */ - function addCalculatedFields($query) + function addCalculatedFields($query, $aggregated = 1) { - if($this->CalculatedFields) - { + $fields = $this->getCalculatedFields($aggregated); + if ($fields) { $sql = Array(); - foreach($this->CalculatedFields as $field_name => $field_expression) - { + foreach ($fields as $field_name => $field_expression) { $sql[] = '('.$field_expression.') AS '.$field_name; } $sql = implode(',',$sql); return $this->Application->ReplaceLanguageTags( str_replace('%2$s', ','.$sql, $query) ); } - else - { + else { return str_replace('%2$s', '', $query); } } @@ -345,14 +382,20 @@ $this->setConfigFields( $this->Application->getUnitOption($this->Prefix, 'Fields') ); $this->setCustomFields( $this->Application->getUnitOption($this->Prefix, 'CustomFields', Array()) ); $this->setVirtualFields( $this->Application->getUnitOption($this->Prefix, 'VirtualFields') ); - $this->setCalculatedFields( $this->Application->getUnitOption($this->Prefix, 'CalculatedFields') ); + $this->setCalculatedFields( $this->Application->getUnitOption($this->Prefix, 'CalculatedFields', Array()) ); + $this->setAggragatedCalculatedFields( $this->Application->getUnitOption($this->Prefix, 'AggregatedCalculatedFields', Array()) ); } function setCalculatedFields($fields) { - $this->CalculatedFields = isset($fields[$this->Special]) ? $fields[$this->Special] : (isset($fields['']) ? $fields[''] : false); + $this->CalculatedFields = isset($fields[$this->Special]) ? $fields[$this->Special] : (isset($fields['']) ? $fields[''] : Array()); } + function setAggragatedCalculatedFields($fields) + { + $this->AggregatedCalculatedFields = isset($fields[$this->Special]) ? $fields[$this->Special] : (isset($fields['']) ? $fields[''] : Array()); + } + /** * Set's field names from table * from config