Index: trunk/core/kernel/db/dblist.php =================================================================== diff -u -N -r3703 -r4287 --- trunk/core/kernel/db/dblist.php (.../dblist.php) (revision 3703) +++ trunk/core/kernel/db/dblist.php (.../dblist.php) (revision 4287) @@ -147,6 +147,8 @@ */ var $HavingFilter = Array(FLT_SYSTEM => null, FLT_NORMAL => null, FLT_SEARCH => null, FLT_VIEW => null); + var $AggregateFilter = Array(FLT_SYSTEM => null, FLT_NORMAL => null); + var $GroupByFields = Array(); var $Queried = false; @@ -166,18 +168,17 @@ $this->WhereFilter[FLT_NORMAL] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_OR); $this->WhereFilter[FLT_SEARCH] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_OR); - $this->WhereFilter[FLT_SEARCH]->setType(FLT_TYPE_OR); - $this->WhereFilter[FLT_VIEW] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_AND); $this->HavingFilter[FLT_SYSTEM] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_AND); $this->HavingFilter[FLT_NORMAL] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_OR); - + $this->HavingFilter[FLT_SEARCH] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_OR); - $this->HavingFilter[FLT_SEARCH]->setType(FLT_TYPE_OR); - $this->HavingFilter[FLT_VIEW] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_AND); + $this->AggregateFilter[FLT_SYSTEM] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_AND); + $this->AggregateFilter[FLT_NORMAL] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_OR); + $this->PerPage = -1; } @@ -192,7 +193,10 @@ */ function addFilter($name, $clause, $filter_type = WHERE_FILTER, $filter_scope = FLT_SYSTEM) { - $filter_name = ($filter_type == WHERE_FILTER) ? 'WhereFilter' : 'HavingFilter'; + $filter_source = Array( WHERE_FILTER => 'WhereFilter', + HAVING_FILTER => 'HavingFilter', + AGGREGATE_FILTER => 'AggregateFilter'); + $filter_name = $filter_source[$filter_type]; $filter =& $this->$filter_name; $filter =& $filter[$filter_scope]; @@ -209,7 +213,10 @@ */ function removeFilter($name, $filter_type = WHERE_FILTER, $filter_scope = FLT_SYSTEM) { - $filter_name = ($filter_type == WHERE_FILTER) ? 'WhereFilter' : 'HavingFilter'; + $filter_source = Array( WHERE_FILTER => 'WhereFilter', + HAVING_FILTER => 'HavingFilter', + AGGREGATE_FILTER => 'AggregateFilter'); + $filter_name = $filter_source[$filter_type]; $filter =& $this->$filter_name; $filter =& $filter[$filter_scope]; @@ -224,23 +231,24 @@ */ function clearFilters($user=true,$system=true,$search=true,$view=true) { - if($system) - { + if ($system) { $this->WhereFilter[FLT_SYSTEM]->clearFilters(); $this->HavingFilter[FLT_SYSTEM]->clearFilters(); + $this->AggregateFilter[FLT_SYSTEM]->clearFilters(); } - if($user) - { + + if ($user) { $this->WhereFilter[FLT_NORMAL]->clearFilters(); $this->HavingFilter[FLT_NORMAL]->clearFilters(); + $this->AggregateFilter[FLT_NORMAL]->clearFilters(); } - if($search) - { + + if ($search) { $this->WhereFilter[FLT_SEARCH]->clearFilters(); $this->HavingFilter[FLT_SEARCH]->clearFilters(); } - if($view) - { + + if ($view) { $this->WhereFilter[FLT_VIEW]->clearFilters(); $this->HavingFilter[FLT_VIEW]->clearFilters(); } @@ -397,7 +405,7 @@ function GetSelectSQL($for_counting=false,$system_filters_only=false) { $q = parent::GetSelectSQL($this->SelectClause); - if(!$for_counting) $q = $this->addCalculatedFields($q); + $q = !$for_counting ? $this->addCalculatedFields($q, 0) : str_replace('%2$s', '', $q); $where = $this->GetWhereClause($for_counting,$system_filters_only); $having = $this->GetHavingClause($for_counting,$system_filters_only); @@ -412,12 +420,19 @@ return str_replace('%1$s',$this->TableName,$q); } - function extractCalculatedFields($clause) + /** + * Enter description here... + * + * @param string $clause where clause to extract calculated fields from + * @param int $aggregated 0 - having + aggregated, 1 - having only, 2 - aggregated only + * @return string + */ + function extractCalculatedFields($clause, $aggregated = 1) { - if ( is_array($this->CalculatedFields) ) { - foreach($this->CalculatedFields as $field_name => $field_expression) - { - $clause = preg_replace('/`'.$field_name.'`/', $field_expression, $clause); + $fields = $this->getCalculatedFields($aggregated); + if (is_array($fields) && count($fields) > 0) { + foreach ($fields as $field_name => $field_expression) { + $clause = preg_replace('/[`]{0,1}'.$field_name.'[`]{0,1}/', $field_expression, $clause); } } return $clause; @@ -449,7 +464,7 @@ if( $for_counting ) // add system_having and view_having to where { - $where->addFilter('system_having', $this->extractCalculatedFields( $this->HavingFilter[FLT_SYSTEM]->getSQL() ) ); + $where->addFilter('system_having', $this->extractCalculatedFields($this->HavingFilter[FLT_SYSTEM]->getSQL()) ); if (!$system_filters_only) $where->addFilter('view_having', $this->extractCalculatedFields( $this->HavingFilter[FLT_VIEW]->getSQL() ) ); } @@ -476,16 +491,24 @@ * Returns HAVING clause of the query * * @param bool $for_counting don't return having filter in case if this is counting sql + * @param bool $system_filters_only return only system having filters + * @param int $aggregated 0 - aggregated and having, 1 - having only, 2 - aggregated only * @return string * @access public */ - function GetHavingClause($for_counting=false, $system_filters_only=false) + function GetHavingClause($for_counting=false, $system_filters_only=false, $aggregated = 0) { - if( $for_counting ) return ''; + if ($for_counting) { + return $this->extractCalculatedFields($this->AggregateFilter[FLT_SYSTEM]->getSQL(), 2); + } $having =& $this->Application->makeClass('kMultipleFilter'); $having->addFilter('system_having', $this->HavingFilter[FLT_SYSTEM] ); + if ($aggregated == 0) { + $having->addFilter('system_aggregated', $this->AggregateFilter[FLT_SYSTEM] ); + } + if (!$system_filters_only) { $having->addFilter('view_having', $this->HavingFilter[FLT_VIEW] ); $search_w = $this->WhereFilter[FLT_SEARCH]->getSQL();