Ejemplo n.º 1
0
 /**
  * Filters the given DataTable. Removes columns that are not desired from
  * each DataTable row.
  * 
  * @param Piwik_DataTable $table
  */
 public function filter($table)
 {
     $recurse = false;
     // only recurse if there are columns to remove/keep
     // remove columns specified in $this->columnsToRemove
     if (!empty($this->columnsToRemove)) {
         foreach ($table->getRows() as $row) {
             foreach ($this->columnsToRemove as $column) {
                 $row->deleteColumn($column);
             }
         }
         $recurse = true;
     }
     // remove columns not specified in $columnsToKeep
     if (!empty($this->columnsToKeep)) {
         foreach ($table->getRows() as $row) {
             foreach ($row->getColumns() as $name => $value) {
                 // label cannot be removed via whitelisting
                 if ($name != 'label' && !isset($this->columnsToKeep[$name])) {
                     $row->deleteColumn($name);
                 }
             }
         }
         $recurse = true;
     }
     // recurse
     if ($recurse) {
         foreach ($table->getRows() as $row) {
             $this->filterSubTable($row);
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * @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();
 }
Ejemplo n.º 3
0
 /**
  * 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);
             $nonGroupByRowIds[] = $rowId;
         }
     }
     // delete the unneeded rows.
     $table->deleteRows($nonGroupByRowIds);
 }
Ejemplo n.º 4
0
 /**
  * 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);
 }
Ejemplo n.º 5
0
 /**
  * @param Piwik_DataTable  $subTable
  */
 function __construct($subTable)
 {
     parent::__construct();
     foreach ($subTable->getRows() as $row) {
         $this->sumRow($row);
     }
 }
Ejemplo n.º 6
0
 /**
  * Template method called from self::manipulate.
  * Flatten each data table.
  *
  * @param Piwik_DataTable  $dataTable
  * @param bool             $date
  * @return Piwik_DataTable
  */
 protected function doManipulate(Piwik_DataTable $dataTable, $date = false)
 {
     $newDataTable = $dataTable->getEmptyClone();
     foreach ($dataTable->getRows() as $row) {
         $this->flattenRow($row, $newDataTable, $date);
     }
     return $newDataTable;
 }
 /**
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $key => $row) {
         $oldValue = $row->getMetadata($this->metadataToRead);
         $newValue = call_user_func($this->functionToApply, $oldValue);
         $row->addMetadata($this->metadataToAdd, $newValue);
     }
 }
Ejemplo n.º 8
0
 /**
  * Executes the filter and renames the defined columns
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $key => $row) {
         $oldColumns = $row->getColumns();
         $newColumns = $this->getRenamedColumns($oldColumns);
         $row->setColumns($newColumns);
         $this->filterSubTable($row);
     }
 }
Ejemplo n.º 9
0
 /**
  * Filters the given data table
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $key => $row) {
         $columnValue = $row->getColumn($this->columnToFilter);
         if (!call_user_func($this->function, $columnValue)) {
             $table->deleteRow($key);
         }
         $this->filterSubTable($row);
     }
 }
 /**
  * Filters the given data table
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $key => $row) {
         $columnValue = $row->getColumn($this->columnToFilter);
         if (!call_user_func_array($this->function, array_merge(array($columnValue), $this->functionParams))) {
             $table->deleteRow($key);
         }
         $this->filterSubTable($row);
     }
 }
Ejemplo n.º 11
0
 /**
  * Filtert Kollektionsdaten nach Strings. -> z.B. Unterscheidung Standard <-> Projekt
  * @param Piwik_DataTable $dataTable
  * @param String $filter
  * @param boolean $bool
  */
 private static function filterCollectionTableContent($dataTable, $filter, $bool)
 {
     $rows = $dataTable->getRows();
     Piwik_cdebug::clog('filterCollectionTableContent: rows: ' . count($rows));
     foreach ($rows as $i => $row) {
         $label = $row->getColumn('label');
         if (strpos($label, $filter) !== false xor $bool) {
             $dataTable->deleteRow($i);
         }
     }
 }
Ejemplo n.º 12
0
 /**
  * Decodes all columns of the given data table
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $row) {
         $value = $row->getColumn($this->columnToDecode);
         if ($value !== false) {
             $value = self::filterValue($value);
             $row->setColumn($this->columnToDecode, $value);
             $this->filterSubTable($row);
         }
     }
 }
Ejemplo n.º 13
0
 /**
  * Truncates the table after X rows and adds a summary row
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     $table->filter('AddSummaryRow', array($this->truncateAfter));
     $table->filter('ReplaceSummaryRowLabel');
     foreach ($table->getRows() as $row) {
         if ($row->isSubtableLoaded()) {
             $idSubTable = $row->getIdSubDataTable();
             $subTable = Piwik_DataTable_Manager::getInstance()->getTable($idSubTable);
             $subTable->filter('Truncate', array($this->truncateAfter));
         }
     }
 }
Ejemplo n.º 14
0
 /**
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $key => $row) {
         //instead search must handle
         // - negative search with -piwik
         // - exact match with ""
         // see (?!pattern) 	A subexpression that performs a negative lookahead search, which matches the search string at any point where a string not matching pattern begins.
         if (!self::match($this->patternToSearch, $this->patternToSearchQuoted, $row->getColumn($this->columnToFilter), $this->invertedMatch)) {
             $table->deleteRow($key);
         }
     }
 }
Ejemplo n.º 15
0
 /**
  * Executes the filter an adjusts all columns to fit the defined range
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $row) {
         $value = $row->getColumn($this->columnToFilter);
         if ($value !== false) {
             if ($value < self::$minimumValue) {
                 $row->setColumn($this->columnToFilter, self::$minimumValue);
             } elseif ($value > self::$maximumValue) {
                 $row->setColumn($this->columnToFilter, self::$maximumValue);
             }
         }
     }
 }
Ejemplo n.º 16
0
 /**
  * Executes a callback on every row of the supplied table and adds the result of
  * the callback as a new column to each row.
  * 
  * @param Piwik_DataTable  $table  The table to filter.
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $row) {
         $columnValues = array();
         foreach ($this->columns as $column) {
             $columnValues[] = $row->getColumn($column);
         }
         $parameters = array_merge($columnValues, $this->functionParameters);
         $value = call_user_func_array($this->functionToApply, $parameters);
         $row->setColumn($this->columnToAdd, $value);
         $this->filterSubTable($row);
     }
 }
 /**
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $key => $row) {
         if (!$this->applyToSummaryRow && $key == Piwik_DataTable::ID_SUMMARY_ROW) {
             continue;
         }
         $params = array();
         foreach ($this->metadataToRead as $name) {
             $params[] = $row->getMetadata($name);
         }
         $newValue = call_user_func_array($this->functionToApply, $params);
         $row->addMetadata($this->metadataToAdd, $newValue);
     }
 }
Ejemplo n.º 18
0
 /**
  * Filters the given data table
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $key => $row) {
         $oldValue = $row->getColumn($this->columnToRead);
         $parameters = array($oldValue);
         if (!is_null($this->functionParameters)) {
             $parameters = array_merge($parameters, $this->functionParameters);
         }
         if (!is_null($this->functionToApply)) {
             $newValue = call_user_func_array($this->functionToApply, $parameters);
         } else {
             $newValue = $oldValue;
         }
         $row->addMetadata($this->metadataToAdd, $newValue);
     }
 }
Ejemplo n.º 19
0
 /**
  * Updates the summary row label
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     $rows = $table->getRows();
     foreach ($rows as $row) {
         if ($row->getColumn('label') == Piwik_DataTable::LABEL_SUMMARY_ROW) {
             $row->setColumn('label', $this->newLabel);
             break;
         }
     }
     // recurse
     foreach ($rows as $row) {
         if ($row->isSubtableLoaded()) {
             $subTable = Piwik_DataTable_Manager::getInstance()->getTable($row->getIdSubDataTable());
             $this->filter($subTable);
         }
     }
 }
 /**
  * Filters the given data table
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $key => $row) {
         $existingValue = $row->getColumn($this->columnNameToAdd);
         if ($existingValue !== false) {
             continue;
         }
         $value = $this->getDividend($row);
         if ($value === false && $this->shouldSkipRows) {
             continue;
         }
         $divisor = $this->getDivisor($row);
         $formattedValue = $this->formatValue($value, $divisor);
         $row->addColumn($this->columnNameToAdd, $formattedValue);
         $this->filterSubTable($row);
     }
 }
Ejemplo n.º 21
0
 /**
  * Filters the given data table
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     $rowsIdToDelete = array();
     foreach ($table->getRows() as $key => $row) {
         $nbVisits = $this->getColumn($row, Piwik_Archive::INDEX_NB_VISITS);
         $nbActions = $this->getColumn($row, Piwik_Archive::INDEX_NB_ACTIONS);
         if ($nbVisits == 0 && $nbActions == 0 && $this->deleteRowsWithNoVisit) {
             // case of keyword/website/campaign with a conversion for this day,
             // but no visit, we don't show it
             $rowsIdToDelete[] = $key;
             continue;
         }
         $nbVisitsConverted = (int) $this->getColumn($row, Piwik_Archive::INDEX_NB_VISITS_CONVERTED);
         if ($nbVisitsConverted > 0) {
             $conversionRate = round(100 * $nbVisitsConverted / $nbVisits, $this->roundPrecision);
             try {
                 $row->addColumn('conversion_rate', $conversionRate . "%");
             } catch (Exception $e) {
                 // conversion_rate can be defined upstream apparently? FIXME
             }
         }
         if ($nbVisits == 0) {
             $actionsPerVisit = $averageTimeOnSite = $bounceRate = $this->invalidDivision;
         } else {
             // nb_actions / nb_visits => Actions/visit
             // sum_visit_length / nb_visits => Avg. Time on Site
             // bounce_count / nb_visits => Bounce Rate
             $actionsPerVisit = round($nbActions / $nbVisits, $this->roundPrecision);
             $averageTimeOnSite = round($this->getColumn($row, Piwik_Archive::INDEX_SUM_VISIT_LENGTH) / $nbVisits, $rounding = 0);
             $bounceRate = round(100 * $this->getColumn($row, Piwik_Archive::INDEX_BOUNCE_COUNT) / $nbVisits, $this->roundPrecision);
         }
         try {
             $row->addColumn('nb_actions_per_visit', $actionsPerVisit);
             $row->addColumn('avg_time_on_site', $averageTimeOnSite);
         } catch (Exception $e) {
         }
         try {
             $row->addColumn('bounce_rate', $bounceRate . "%");
         } catch (Exception $e) {
         }
         $this->filterSubTable($row);
     }
     $table->deleteRows($rowsIdToDelete);
 }
 /**
  * Filters the given data table
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $key => $row) {
         if (!$this->applyToSummaryRow && $key == Piwik_DataTable::ID_SUMMARY_ROW) {
             continue;
         }
         $oldValue = $row->getColumn($this->columnToRead);
         $parameters = array($oldValue);
         if (!is_null($this->functionParameters)) {
             $parameters = array_merge($parameters, $this->functionParameters);
         }
         if (!is_null($this->functionToApply)) {
             $newValue = call_user_func_array($this->functionToApply, $parameters);
         } else {
             $newValue = $oldValue;
         }
         if ($newValue !== false) {
             $row->addMetadata($this->metadataToAdd, $newValue);
         }
     }
 }
Ejemplo n.º 23
0
 /**
  * Filters the given data table
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $key => $row) {
         $extraColumnParameters = array();
         foreach ($this->extraColumnParameters as $columnName) {
             $extraColumnParameters[] = $row->getColumn($columnName);
         }
         foreach ($this->columnsToFilter as $column) {
             // when a value is not defined, we set it to zero by default (rather than displaying '-')
             $value = $this->getElementToReplace($row, $column);
             if ($value === false) {
                 $value = 0;
             }
             $parameters = array_merge(array($value), $extraColumnParameters);
             if (!is_null($this->functionParameters)) {
                 $parameters = array_merge($parameters, $this->functionParameters);
             }
             $newValue = call_user_func_array($this->functionToApply, $parameters);
             $this->setElementToReplace($row, $column, $newValue);
             $this->filterSubTable($row);
         }
     }
 }
Ejemplo n.º 24
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;
 }
Ejemplo n.º 25
0
 /**
  * This method loads in memory all the subtables for the main table called $name.
  * You have to give it the parent table $dataTableToLoad so we can lookup the sub tables ids to load.
  *
  * If $addMetadataSubtableId set to true, it will add for each row a 'metadata' called 'databaseSubtableId'
  *  containing the child ID of the subtable  associated to this row.
  *
  * @param string $name
  * @param Piwik_DataTable $dataTableToLoad
  * @param bool $addMetadataSubtableId
  */
 public function loadSubDataTables($name, Piwik_DataTable $dataTableToLoad, $addMetadataSubtableId = false)
 {
     // we have to recursively load all the subtables associated to this table's rows
     // and update the subtableID so that it matches the newly instanciated table
     foreach ($dataTableToLoad->getRows() as $row) {
         $subTableID = $row->getIdSubDataTable();
         if ($subTableID !== null) {
             $subDataTableLoaded = $this->getDataTable($name, $subTableID);
             $row->setSubtable($subDataTableLoaded);
             $this->loadSubDataTables($name, $subDataTableLoaded, $addMetadataSubtableId);
             // we edit the subtable ID so that it matches the newly table created in memory
             // NB: we dont overwrite the datatableid in the case we are displaying the table expanded.
             if ($addMetadataSubtableId) {
                 // this will be written back to the column 'idsubdatatable' just before rendering, see Renderer/Php.php
                 $row->addMetadata('idsubdatatable_in_db', $row->getIdSubDataTable());
             }
         }
     }
 }
Ejemplo n.º 26
0
 /**
  * General tests that tries to test the normal behaviour of DataTable
  * 
  * We create some tables, add rows, some of the rows link to sub tables
  * 
  * Then we serialize everything, and we check that the unserialize give the same object back
  * 
  * @group Core
  * @group DataTable
  */
 public function testGeneral()
 {
     /*
      * create some fake tables to make sure that the serialized array of the first TABLE
      * does not take in consideration those tables
      */
     $useless1 = new Piwik_DataTable();
     $useless1->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(13)));
     /*
      * end fake tables
      */
     /*
      * MAIN TABLE
      */
     $table = new Piwik_DataTable();
     $subtable = new Piwik_DataTable();
     $idtable = $table->getId();
     $idsubtable = $subtable->getId();
     /*
      * create some fake tables to make sure that the serialized array of the first TABLE
      * does not take in consideration those tables
      * -> we check that the DataTable_Manager is not impacting DataTable 
      */
     $useless2 = new Piwik_DataTable();
     $useless1->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(8487)));
     $useless3 = new Piwik_DataTable();
     $useless3->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(8487)));
     /*
      * end fake tables
      */
     $row = array(Piwik_DataTable_Row::COLUMNS => array(0 => 1554, 1 => 42, 2 => 657, 3 => 155744), Piwik_DataTable_Row::METADATA => array('logo' => 'test.png'));
     $row = new Piwik_DataTable_Row($row);
     $table->addRow($row);
     $table->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(0 => 1554, 1 => 42), Piwik_DataTable_Row::METADATA => array('url' => 'piwik.org')));
     $table->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(0 => 787877888787.0), Piwik_DataTable_Row::METADATA => array('url' => 'OUPLA ADDED'), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $subtable));
     /*
      * SUB TABLE
      */
     $row = array(Piwik_DataTable_Row::COLUMNS => array(0 => 1554), Piwik_DataTable_Row::METADATA => array('searchengine' => 'google'));
     $subtable->addRowFromArray($row);
     $row = array(Piwik_DataTable_Row::COLUMNS => array(0 => 84894), Piwik_DataTable_Row::METADATA => array('searchengine' => 'yahoo'));
     $subtable->addRowFromArray($row);
     $row = array(Piwik_DataTable_Row::COLUMNS => array(0 => 4898978989.0), Piwik_DataTable_Row::METADATA => array('searchengine' => 'ask'));
     $subtable->addRowFromArray($row);
     /*
      * SUB SUB TABLE
      */
     $subsubtable = new Piwik_DataTable();
     $subsubtable->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(245), Piwik_DataTable_Row::METADATA => array('yes' => 'subsubmetadata1')));
     $subsubtable->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(13), Piwik_DataTable_Row::METADATA => array('yes' => 'subsubmetadata2')));
     $row = array(Piwik_DataTable_Row::COLUMNS => array(0 => 666666666666666.0), Piwik_DataTable_Row::METADATA => array('url' => 'NEW ROW ADDED'), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $subsubtable);
     $subtable->addRowFromArray($row);
     $idsubsubtable = $subsubtable->getId();
     $serialized = $table->getSerialized();
     $this->assertEquals(array_keys($serialized), array($idsubsubtable, $idsubtable, 0));
     // In the next test we compare an unserialized datatable with its original instance.
     // The unserialized datatable rows will have positive DATATABLE_ASSOCIATED ids.
     // Positive DATATABLE_ASSOCIATED ids mean that the associated sub-datatables are not loaded in memory.
     // In this case, this is NOT true: we know that the sub-datatable is loaded in memory.
     // HOWEVER, because of datatable id conflicts happening in the datatable manager, it is not yet
     // possible to know, after unserializing a datatable, if its sub-datatables are loaded in memory.
     $expectedTableRows = array();
     foreach ($table->getRows() as $currentRow) {
         $expectedTableRow = clone $currentRow;
         $currentRowAssociatedDatatableId = $currentRow->c[Piwik_DataTable_Row::DATATABLE_ASSOCIATED];
         if ($currentRowAssociatedDatatableId != null) {
             // making DATATABLE_ASSOCIATED ids positive
             $expectedTableRow->c[Piwik_DataTable_Row::DATATABLE_ASSOCIATED] = -1 * $currentRowAssociatedDatatableId;
         }
         $expectedTableRows[] = $expectedTableRow;
     }
     $tableAfter = new Piwik_DataTable();
     $tableAfter->addRowsFromSerializedArray($serialized[0]);
     $this->assertEquals($expectedTableRows, $tableAfter->getRows());
     $subsubtableAfter = new Piwik_DataTable();
     $subsubtableAfter->addRowsFromSerializedArray($serialized[$idsubsubtable]);
     $this->assertEquals($subsubtable->getRows(), $subsubtableAfter->getRows());
     $this->assertEquals($table, Piwik_DataTable_Manager::getInstance()->getTable($idtable));
     $this->assertEquals($subsubtable, Piwik_DataTable_Manager::getInstance()->getTable($idsubsubtable));
 }
 /**
  * General tests that tries to test the normal behaviour of DataTable
  * 
  * We create some tables, add rows, some of the rows link to sub tables
  * 
  * Then we serialize everything, and we check that the unserialize give the same object back
  */
 function test_general()
 {
     /*
      * create some fake tables to make sure that the serialized array of the first TABLE
      * does not take in consideration those tables
      */
     $useless1 = new Piwik_DataTable();
     $useless1->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(13)));
     /*
      * end fake tables
      */
     /*
      * MAIN TABLE
      */
     $table = new Piwik_DataTable();
     $subtable = new Piwik_DataTable();
     $idtable = $table->getId();
     $idsubtable = $subtable->getId();
     /*
      * create some fake tables to make sure that the serialized array of the first TABLE
      * does not take in consideration those tables
      * -> we check that the DataTable_Manager is not impacting DataTable 
      */
     $useless2 = new Piwik_DataTable();
     $useless1->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(8487)));
     $useless3 = new Piwik_DataTable();
     $useless3->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(8487)));
     /*
      * end fake tables
      */
     $row = array(Piwik_DataTable_Row::COLUMNS => array(0 => 1554, 1 => 42, 2 => 657, 3 => 155744), Piwik_DataTable_Row::METADATA => array('logo' => 'test.png'));
     $row = new Piwik_DataTable_Row($row);
     $table->addRow($row);
     $table->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(0 => 1554, 1 => 42), Piwik_DataTable_Row::METADATA => array('url' => 'piwik.org')));
     $table->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(0 => 787877888787), Piwik_DataTable_Row::METADATA => array('url' => 'OUPLA ADDED'), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $subtable));
     /*
      * SUB TABLE
      */
     $row = array(Piwik_DataTable_Row::COLUMNS => array(0 => 1554), Piwik_DataTable_Row::METADATA => array('searchengine' => 'google'));
     $subtable->addRowFromArray($row);
     $row = array(Piwik_DataTable_Row::COLUMNS => array(0 => 84894), Piwik_DataTable_Row::METADATA => array('searchengine' => 'yahoo'));
     $subtable->addRowFromArray($row);
     $row = array(Piwik_DataTable_Row::COLUMNS => array(0 => 4898978989), Piwik_DataTable_Row::METADATA => array('searchengine' => 'ask'));
     $subtable->addRowFromArray($row);
     /*
      * SUB SUB TABLE
      */
     $subsubtable = new Piwik_DataTable();
     $subsubtable->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(245), Piwik_DataTable_Row::METADATA => array('yes' => 'subsubmetadata1')));
     $subsubtable->addRowFromArray(array(Piwik_DataTable_Row::COLUMNS => array(13), Piwik_DataTable_Row::METADATA => array('yes' => 'subsubmetadata2')));
     $row = array(Piwik_DataTable_Row::COLUMNS => array(0 => 666666666666666), Piwik_DataTable_Row::METADATA => array('url' => 'NEW ROW ADDED'), Piwik_DataTable_Row::DATATABLE_ASSOCIATED => $subsubtable);
     $subtable->addRowFromArray($row);
     $idsubsubtable = $subsubtable->getId();
     $serialized = $table->getSerialized();
     $this->assertEqual(array_keys($serialized), array($idsubsubtable, $idsubtable, 0));
     $tableAfter = new Piwik_DataTable();
     $tableAfter->addRowsFromSerializedArray($serialized[0]);
     $this->assertEqual($table->getRows(), $tableAfter->getRows());
     $subsubtableAfter = new Piwik_DataTable();
     $subsubtableAfter->addRowsFromSerializedArray($serialized[$idsubsubtable]);
     $this->assertEqual($subsubtable->getRows(), $subsubtableAfter->getRows());
     $this->assertEqual($table, Piwik_DataTable_Manager::getInstance()->getTable($idtable));
     $this->assertEqual($subsubtable, Piwik_DataTable_Manager::getInstance()->getTable($idsubsubtable));
 }
Ejemplo n.º 28
0
 /**
  * Filters the given data table
  *
  * @param Piwik_DataTable  $table
  */
 public function filter($table)
 {
     foreach ($table->getRows() as $row) {
         $row->addMetadata($this->name, $this->value);
     }
 }
Ejemplo n.º 29
0
 /**
  * Converts the given data table to an array
  *
  * @param Piwik_DataTable  $table
  * @return array
  */
 protected function renderTable($table)
 {
     $array = array();
     foreach ($table->getRows() as $id => $row) {
         $newRow = array('columns' => $row->getColumns(), 'metadata' => $row->getMetadata(), 'idsubdatatable' => $row->getIdSubDataTable());
         if ($id == Piwik_DataTable::ID_SUMMARY_ROW) {
             $newRow['issummaryrow'] = true;
         }
         if ($this->isRenderSubtables() && $row->isSubtableLoaded()) {
             $subTable = $this->renderTable(Piwik_DataTable_Manager::getInstance()->getTable($row->getIdSubDataTable()));
             $newRow['subtable'] = $subTable;
             if ($this->hideIdSubDatatable === false && isset($newRow['metadata']['idsubdatatable_in_db'])) {
                 $newRow['columns']['idsubdatatable'] = $newRow['metadata']['idsubdatatable_in_db'];
             }
             unset($newRow['metadata']['idsubdatatable_in_db']);
         }
         if ($this->hideIdSubDatatable !== false) {
             unset($newRow['idsubdatatable']);
         }
         $array[] = $newRow;
     }
     return $array;
 }
Ejemplo n.º 30
0
 /**
  * Returns the report Visits by Browser family given the Browser report
  *
  * @param Piwik_DataTable $tableBrowser
  * @return Piwik_DataTable
  */
 protected function getTableBrowserByType(Piwik_DataTable $tableBrowser)
 {
     $nameToRow = array();
     foreach ($tableBrowser->getRows() as $row) {
         $browserLabel = $row->getColumn('label');
         $familyNameToUse = Piwik_getBrowserFamily($browserLabel);
         if (!isset($nameToRow[$familyNameToUse])) {
             $nameToRow[$familyNameToUse] = new Piwik_DataTable_Row();
             $nameToRow[$familyNameToUse]->addColumn('label', $familyNameToUse);
         }
         $nameToRow[$familyNameToUse]->sumRow($row);
     }
     $tableBrowserType = new Piwik_DataTable();
     $tableBrowserType->addRowsFromArray($nameToRow);
     return $tableBrowserType;
 }