/**
  * 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);
 }
 /**
  * Returns true if it is likely that the data for this report has been purged and if the
  * user should be told about that.
  * 
  * In order for this function to return true, the following must also be true:
  * - The data table for this report must either be empty or not have been fetched.
  * - The period of this report is not a multiple period.
  * - The date of this report must be older than the delete_reports_older_than config option.
  * @return bool
  */
 public function hasReportBeenPurged()
 {
     $strPeriod = Piwik_Common::getRequestVar('period', false);
     $strDate = Piwik_Common::getRequestVar('date', false);
     if ($strPeriod !== false && $strDate !== false && (is_null($this->dataTable) || $this->dataTable->getRowsCount() == 0)) {
         // if range, only look at the first date
         if ($strPeriod == 'range') {
             $idSite = Piwik_Common::getRequestVar('idSite', '');
             if (intval($idSite) != 0) {
                 $site = new Piwik_Site($idSite);
                 $timezone = $site->getTimezone();
             } else {
                 $timezone = 'UTC';
             }
             $period = new Piwik_Period_Range('range', $strDate, $timezone);
             $reportDate = $period->getDateStart();
         } else {
             if (Piwik_Archive::isMultiplePeriod($strDate, $strPeriod)) {
                 return false;
             } else {
                 $reportDate = Piwik_Date::factory($strDate);
             }
         }
         $reportYear = $reportDate->toString('Y');
         $reportMonth = $reportDate->toString('m');
         if (class_exists('Piwik_PrivacyManager') && Piwik_PrivacyManager::shouldReportBePurged($reportYear, $reportMonth)) {
             return true;
         }
     }
     return false;
 }
 /**
  * @param Piwik_DataTable $table
  * @return int
  */
 public function filter($table)
 {
     $rows = $table->getRows();
     foreach ($rows as $key => $row) {
         // A row is deleted if
         // 1 - its label doesnt contain the pattern
         // AND 2 - the label is not found in the children
         $patternNotFoundInChildren = false;
         try {
             $idSubTable = $row->getIdSubDataTable();
             $subTable = Piwik_DataTable_Manager::getInstance()->getTable($idSubTable);
             // we delete the row if we couldn't find the pattern in any row in the
             // children hierarchy
             if ($this->filter($subTable) == 0) {
                 $patternNotFoundInChildren = true;
             }
         } catch (Exception $e) {
             // there is no subtable loaded for example
             $patternNotFoundInChildren = true;
         }
         if ($patternNotFoundInChildren && !Piwik_DataTable_Filter_Pattern::match($this->patternToSearch, $this->patternToSearchQuoted, $row->getColumn($this->columnToFilter), $invertedMatch = false)) {
             $table->deleteRow($key);
         }
     }
     return $table->getRowsCount();
 }
Exemple #4
0
 /**
  * Computes the output for the given data table
  *
  * @param Piwik_DataTable  $table
  * @return string
  */
 protected function renderTable($table)
 {
     if ($table instanceof Piwik_DataTable_Array) {
         foreach ($table->getArray() as $date => $subtable) {
             if ($subtable->getRowsCount()) {
                 $this->buildTableStructure($subtable, '_' . $table->getKeyName(), $date);
             }
         }
     } else {
         if ($table->getRowsCount()) {
             $this->buildTableStructure($table);
         }
     }
     $out = $this->renderDataTable();
     return $out;
 }
Exemple #5
0
 /**
  * Returns true if both DataTable are exactly the same.
  * Used in unit tests.
  * 
  * @param Piwik_DataTable $table1
  * @param Piwik_DataTable $table2
  * @return bool
  */
 public static function isEqual(Piwik_DataTable $table1, Piwik_DataTable $table2)
 {
     $rows1 = $table1->getRows();
     $rows2 = $table2->getRows();
     $table1->rebuildIndex();
     $table2->rebuildIndex();
     if ($table1->getRowsCount() != $table2->getRowsCount()) {
         return false;
     }
     foreach ($rows1 as $row1) {
         $row2 = $table2->getRowFromLabel($row1->getColumn('label'));
         if ($row2 === false || !Piwik_DataTable_Row::isEqual($row1, $row2)) {
             return false;
         }
     }
     return true;
 }
Exemple #6
0
 /**
  * we test the count rows and the count rows recursive version
  * on a Complex array (rows with 2 and 3 levels only)
  * 
  * the recursive count returns 
  *         the sum of the number of rows of all the subtables 
  *         + the number of rows in the parent table
  * 
  * @group Core
  * @group DataTable
  */
 public function testCountRowsComplex()
 {
     $idcol = Piwik_DataTable_Row::COLUMNS;
     $idsubtable = Piwik_DataTable_Row::DATATABLE_ASSOCIATED;
     // table to go in the SUB table of RoW1
     $tableSubOfSubOfRow1 = new Piwik_DataTable();
     $rows1sub = array(array($idcol => array('label' => 'google')), array($idcol => array('label' => 'google78')), array($idcol => array('label' => 'googlaegge')), array($idcol => array('label' => 'gogeoggle')), array($idcol => array('label' => 'goaegaegaogle')), array($idcol => array('label' => 'ask')), array($idcol => array('label' => '238975247578949')));
     $tableSubOfSubOfRow1->addRowsFromArray($rows1sub);
     // table to go in row1
     $tableSubOfRow1 = new Piwik_DataTable();
     $rows1 = array(array($idcol => array('label' => 'google'), $idsubtable => $tableSubOfSubOfRow1), array($idcol => array('label' => 'ask')), array($idcol => array('label' => '238975247578949')));
     $tableSubOfRow1->addRowsFromArray($rows1);
     // table to go in row2
     $tableSubOfRow2 = new Piwik_DataTable();
     $rows2 = array(array($idcol => array('label' => 'google')), array($idcol => array('label' => 'ask')), array($idcol => array('label' => '238975247578949')), array($idcol => array('label' => 'agaegaesk')), array($idcol => array('label' => '23g  8975247578949')));
     $tableSubOfRow2->addRowsFromArray($rows2);
     // main parent table
     $table = new Piwik_DataTable();
     $rows = array(array($idcol => array('label' => 'row1')), array($idcol => array('label' => 'row2'), $idsubtable => $tableSubOfRow1), array($idcol => array('label' => 'row3'), $idsubtable => $tableSubOfRow2));
     $table->addRowsFromArray($rows);
     $this->assertEquals(count($rows), $table->getRowsCount());
     $countAllRows = count($rows) + count($rows1) + count($rows2) + count($rows1sub);
     $this->assertEquals($countAllRows, $table->getRowsCountRecursive());
 }
 public function test_whenRowsInRandomOrderButSortSpecified_shouldComputeSummaryRowAfterSort()
 {
     $table = new Piwik_DataTable();
     $table->addRow($this->getRow3());
     $table->addRow($this->getRow2());
     $table->addRow($this->getRow4());
     $table->addRow($this->getRow1());
     $table->addRow($this->getRow0());
     $filter = new Piwik_DataTable_Filter_AddSummaryRow($table, 2, Piwik_DataTable::LABEL_SUMMARY_ROW, $columnToSortBy = 'nb');
     $this->assertEqual($table->getRowsCount(), 3);
     $expectedRow = new Piwik_DataTable_Row(array(Piwik_DataTable_Row::COLUMNS => array('label' => Piwik_DataTable::LABEL_SUMMARY_ROW, 'nb' => 111)));
     $this->assertTrue(Piwik_DataTable_Row::isEqual($table->getLastRow(), $expectedRow));
 }