Index: trunk/core/kernel/db/dblist.php =================================================================== diff -u -N -r5547 -r6093 --- trunk/core/kernel/db/dblist.php (.../dblist.php) (revision 5547) +++ trunk/core/kernel/db/dblist.php (.../dblist.php) (revision 6093) @@ -153,6 +153,7 @@ var $Queried = false; var $Counted = false; + var $TotalsCalculated = false; /** * Creates kDBList @@ -178,6 +179,7 @@ $this->AggregateFilter[FLT_SYSTEM] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_AND); $this->AggregateFilter[FLT_NORMAL] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_OR); + $this->AggregateFilter[FLT_VIEW] =& $this->Application->makeClass('kMultipleFilter', FLT_TYPE_AND); $this->PerPage = -1; } @@ -389,10 +391,12 @@ $this->Totals[$total_field] += $field_value; } } + $this->TotalsCalculated = true; } function getTotal($field, $total_function) { + if (!$this->TotalsCalculated) $this->CalculateTotals(); return $this->Totals[$field.'_'.$total_function]; } @@ -417,7 +421,7 @@ if (!empty($having)) $q .= ' HAVING ' . $having; if ( !$for_counting && !empty($order) ) $q .= ' ORDER BY ' . $order; - return str_replace('%1$s',$this->TableName,$q); + return $this->replaceModePrefix( str_replace('%1$s', $this->TableName, $q) ); } /** @@ -432,7 +436,7 @@ $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); + $clause = preg_replace('/[,` ]{1}'.$field_name.'[` ]{1}/', $field_expression, $clause); } } return $clause; @@ -499,14 +503,22 @@ function GetHavingClause($for_counting=false, $system_filters_only=false, $aggregated = 0) { if ($for_counting) { - return $this->extractCalculatedFields($this->AggregateFilter[FLT_SYSTEM]->getSQL(), 2); + $aggregate_filter =& $this->Application->makeClass('kMultipleFilter'); + $aggregate_filter->addFilter('aggregate_system', $this->AggregateFilter[FLT_SYSTEM]); + if (!$system_filters_only) { + $aggregate_filter->addFilter('aggregate_view', $this->AggregateFilter[FLT_VIEW]); + } + return $this->extractCalculatedFields($aggregate_filter->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_aggregated', $this->AggregateFilter[FLT_VIEW] ); + } + $having->addFilter('system_aggregated', $this->AggregateFilter[FLT_SYSTEM]); } if (!$system_filters_only) {