groupByColumn = $groupByColumn; $this->reduceFunction = $reduceFunction; $this->parameters = $parameters; } /** * Applies the reduce function to each row and merges rows w/ the same reduce result. * * @param Piwik_DataTable $table */ public function filter($table) { $groupByRows = array(); $nonGroupByRowIds = array(); foreach ($table->getRows() as $rowId => $row) { // skip the summary row if ($rowId == Piwik_DataTable::ID_SUMMARY_ROW) { continue; } // reduce the group by column of this row $groupByColumnValue = $row->getColumn($this->groupByColumn); $parameters = array_merge(array($groupByColumnValue), $this->parameters); $groupByValue = call_user_func_array($this->reduceFunction, $parameters); if (!isset($groupByRows[$groupByValue])) { // if we haven't encountered this group by value before, we mark this row as a // row to keep, and change the group by column to the reduced value. $groupByRows[$groupByValue] = $row; $row->setColumn($this->groupByColumn, $groupByValue); } else { // if we have already encountered this group by value, we add this row to the // row that will be kept, and mark this one for deletion $groupByRows[$groupByValue]->sumRow($row, $copyMeta = true, $table->getColumnAggregationOperations()); $nonGroupByRowIds[] = $rowId; } } // delete the unneeded rows. $table->deleteRows($nonGroupByRowIds); } }