/** * Adds a summary row to the given data table * * @param Piwik_DataTable $table */ public function filter($table) { if ($table->getRowsCount() <= $this->startRowToSummarize + 1) { return; } $table->filter('Sort', array($this->columnToSortByBeforeTruncating, 'desc')); $rows = $table->getRows(); $count = $table->getRowsCount(); $newRow = new Piwik_DataTable_Row(); for ($i = $this->startRowToSummarize; $i < $count; $i++) { if (!isset($rows[$i])) { // case when the last row is a summary row, it is not indexed by $cout but by Piwik_DataTable::ID_SUMMARY_ROW $summaryRow = $table->getRowFromId(Piwik_DataTable::ID_SUMMARY_ROW); //FIXME: I'm not sure why it could return false, but it was reported in: http://forum.piwik.org/read.php?2,89324,page=1#msg-89442 if ($summaryRow) { $newRow->sumRow($summaryRow, $enableCopyMetadata = false); } } else { $newRow->sumRow($rows[$i], $enableCopyMetadata = false); } } $newRow->setColumns(array('label' => $this->labelSummaryRow) + $newRow->getColumns()); if ($this->deleteRows) { $table->filter('Limit', array(0, $this->startRowToSummarize)); } $table->addSummaryRow($newRow); unset($rows); }
public function filter($table) { if($table->getRowsCount() <= $this->startRowToSummarize + 1) { return; } $table->filter('Sort', array( $this->columnToSortByBeforeTruncating, 'desc')); $rows = $table->getRows(); $count = $table->getRowsCount(); $newRow = new Piwik_DataTable_Row(); for($i = $this->startRowToSummarize; $i < $count; $i++) { if(!isset($rows[$i])) { // case when the last row is a summary row, it is not indexed by $cout but by Piwik_DataTable::ID_SUMMARY_ROW $summaryRow = $table->getRowFromId(Piwik_DataTable::ID_SUMMARY_ROW); $newRow->sumRow($summaryRow); } else { $newRow->sumRow($rows[$i]); } } $newRow->setColumns(array('label' => $this->labelSummaryRow) + $newRow->getColumns()); $table->filter('Limit', array(0, $this->startRowToSummarize)); $table->addSummaryRow($newRow); unset($rows); }
/** * Simple test of the DataTable_Row * * @group Core * @group DataTable */ public function testRow() { $columns = array('test_column' => 145, 00 => new Piwik_Timer(), 'super' => array('this column has an array value, amazing')); $metadata = array('logo' => 'piwik.png', 'super' => array('this column has an array value, amazing')); $arrayRow = array(Piwik_DataTable_Row::COLUMNS => $columns, Piwik_DataTable_Row::METADATA => $metadata, 'fake useless key' => 38959, 43905724897.0 => 'value'); $row = new Piwik_DataTable_Row($arrayRow); $this->assertEquals($columns, $row->getColumns()); $this->assertEquals($metadata, $row->getMetadata()); $this->assertNull($row->getIdSubDataTable()); }
/** * Helper function to test if two rows are equal. * * Two rows are equal * - if they have exactly the same columns / metadata * - if they have a subDataTable associated, then we check that both of them are the same. * * @param Piwik_DataTable_Row row1 to compare * @param Piwik_DataTable_Row row2 to compare * * @return bool */ public static function isEqual(Piwik_DataTable_Row $row1, Piwik_DataTable_Row $row2) { //same columns $cols1 = $row1->getColumns(); $cols2 = $row2->getColumns(); $diff1 = array_udiff($cols1, $cols2, array(__CLASS__, 'compareElements')); $diff2 = array_udiff($cols2, $cols1, array(__CLASS__, 'compareElements')); if ($diff1 != $diff2) { return false; } $dets1 = $row1->getMetadata(); $dets2 = $row2->getMetadata(); ksort($dets1); ksort($dets2); if ($dets1 != $dets2) { return false; } // either both are null // or both have a value if (!(is_null($row1->getIdSubDataTable()) && is_null($row2->getIdSubDataTable()))) { $subtable1 = Piwik_DataTable_Manager::getInstance()->getTable($row1->getIdSubDataTable()); $subtable2 = Piwik_DataTable_Manager::getInstance()->getTable($row2->getIdSubDataTable()); if (!Piwik_DataTable::isEqual($subtable1, $subtable2)) { return false; } } return true; }
/** * Add a row to the table and rebuild the index if necessary * * @param Piwik_DataTable_Row $row to add at the end of the array */ public function addRow(Piwik_DataTable_Row $row) { // if there is a upper limit on the number of allowed rows and the table is full, // add the new row to the summary row if ($this->maximumAllowedRows > 0 && $this->getRowsCount() >= $this->maximumAllowedRows - 1) { if ($this->summaryRow === null) { $this->addSummaryRow(new Piwik_DataTable_Row(array(Piwik_DataTable_Row::COLUMNS => $row->getColumns()))); $this->summaryRow->setColumn('label', self::LABEL_SUMMARY_ROW); } else { $this->summaryRow->sumRow($row, $enableCopyMetadata = false); } return $this->summaryRow; } $this->rows[] = $row; if (!$this->indexNotUpToDate && $this->rebuildIndexContinuously) { $label = $row->getColumn('label'); if ($label !== false) { $this->rowsIndexByLabel[$label] = count($this->rows) - 1; } $this->indexNotUpToDate = false; } return $row; }
/** * Helper function to test if two rows are equal. * * Two rows are equal * - if they have exactly the same columns / details * - if they have a subDataTable associated, then we check that both of them are the same. * * @param Piwik_DataTable_Row row1 to compare * @param Piwik_DataTable_Row row2 to compare * * @return bool */ public static function isEqual(Piwik_DataTable_Row $row1, Piwik_DataTable_Row $row2) { //same columns $cols1 = $row1->getColumns(); $cols2 = $row2->getColumns(); uksort($cols1, 'strnatcasecmp'); uksort($cols2, 'strnatcasecmp'); if ($cols1 != $cols2) { return false; } $dets1 = $row1->getDetails(); $dets2 = $row2->getDetails(); ksort($dets1); ksort($dets2); // same details if ($dets1 != $dets2) { return false; } // either both are null // or both have a value if (!(is_null($row1->getIdSubDataTable()) && is_null($row2->getIdSubDataTable()))) { $subtable1 = Piwik_DataTable_Manager::getInstance()->getTable($row1->getIdSubDataTable()); $subtable2 = Piwik_DataTable_Manager::getInstance()->getTable($row2->getIdSubDataTable()); if (!Piwik_DataTable::isEqual($subtable1, $subtable2)) { return false; } } return true; }