addRow() public method

If {@link setMaximumAllowedRows()} was called and the current row count is at the maximum, the new row will be summed to the summary row. If there is no summary row, this row is set as the summary row.
public addRow ( Row $row ) : Row
$row Piwik\DataTable\Row
return Piwik\DataTable\Row `$row` or the summary row if we're at the maximum number of rows.
 private function addRowWithMetadata($metadata)
 {
     $row = new Row(array(Row::COLUMNS => array('label' => 'val1')));
     foreach ($metadata as $name => $value) {
         $row->setMetadata($name, $value);
     }
     $this->table->addRow($row);
 }
 /**
  * Returns table used for the tests
  *
  * @return DataTable
  */
 protected function getDataTableCount5()
 {
     $table = new DataTable();
     $table->addRow($this->getRow0());
     $table->addRow($this->getRow1());
     $table->addRow($this->getRow2());
     $table->addRow($this->getRow3());
     $table->addRow($this->getRow4());
     return $table;
 }
 public function test_filter_shouldRemoveTheMetadataFromSubtables_IfOneIsSet()
 {
     $row = $this->addRowWithMetadata(array('test' => '5', 'other' => 'value2'));
     $table = new DataTable();
     $table->addRow($this->buildRowWithMetadata(array('other' => 'value3')));
     $table->addRow($this->buildRowWithMetadata(array('test' => '6')));
     $table->addRow($this->buildRowWithMetadata(array('test' => '7', 'other' => 'value4')));
     $row->setSubtable($table);
     $this->table->filter($this->filter, array('other'));
     $this->assertFalse($row->getMetadata('other'));
     $metadata = $table->getRowsMetadata('other');
     $this->assertSame(array(false, false, false), $metadata);
 }
Esempio n. 4
0
 /**
  * Helper function to display operation/cluster summary stats.
  *
  * @param $idSite
  * @param $period
  * @param $date
  * @param $settings
  *
  * @return \Piwik\DataTable
  */
 protected function processSummary($idSite, $period, $date, $settings, $addCountry = true)
 {
     $table = new DataTable();
     $segment = $settings['name'] . '==' . $settings['context'] . ';' . $settings['value'] . '=@' . $settings['id'];
     // Build an archive to retrieve the information from the records.
     $archive = Archive::build($idSite, $period, $date, $segment);
     /* @var \Piwik\DataTable $tdata */
     $data = $archive->getDataTableFromNumeric(array('nb_visits', 'Actions_nb_downloads'));
     // Add the downloads by type.
     $downloads = $this->attachDownloadByType($idSite, $period, $date, $segment);
     $data->getRowFromId(0)->addColumns($downloads);
     $table->addRow($data->getRowFromId(0));
     // Calculate the right label and load the content.
     if (empty($settings['label'])) {
         $base_url = 'https://www.humanitarianresponse.info/api/v1.0/';
         if ($content_raw = @file_get_contents($base_url . $settings['type'] . '/' . $settings['id'])) {
             $hrContent = json_decode($content_raw);
             $settings['label'] = $hrContent->data[0]->label;
         }
     }
     // Add the label to the row.
     $table->getRowFromId(0)->addColumn('label', $settings['label']);
     // Only add country stats on demand.
     if ($addCountry && !empty($hrContent)) {
         $dataByCountry = $this->attachStatsbyCountry($hrContent, $idSite, $period, $date, $segment);
         if ($row = $dataByCountry->getRowFromId(0)) {
             $table->addRow($row);
         }
     }
     return $table;
 }
 /**
  * Merge the columns of two data tables.
  * Manipulates the first table.
  *
  * @param DataTable|DataTable\Map $table1 The table to eventually filter.
  * @param DataTable|DataTable\Map $table2 Whether to delete rows with no visits or not.
  */
 public function mergeDataTables($table1, $table2)
 {
     // handle table arrays
     if ($table1 instanceof DataTable\Map && $table2 instanceof DataTable\Map) {
         $subTables2 = $table2->getDataTables();
         foreach ($table1->getDataTables() as $index => $subTable1) {
             if (!array_key_exists($index, $subTables2)) {
                 // occurs when archiving starts on dayN and continues into dayN+1, see https://github.com/piwik/piwik/issues/5168#issuecomment-50959925
                 continue;
             }
             $subTable2 = $subTables2[$index];
             $this->mergeDataTables($subTable1, $subTable2);
         }
         return;
     }
     $firstRow2 = $table2->getFirstRow();
     if (!$firstRow2 instanceof Row) {
         return;
     }
     $firstRow1 = $table1->getFirstRow();
     if (empty($firstRow1)) {
         $firstRow1 = $table1->addRow(new Row());
     }
     foreach ($firstRow2->getColumns() as $metric => $value) {
         $firstRow1->setColumn($metric, $value);
     }
 }
 private function bannerStats($bannerName, $params)
 {
     $contentPiece = false;
     if (strpos($bannerName, '_') !== false) {
         list($bannerName, $contentPiece) = explode('_', $bannerName);
     }
     $segment = 'contentName==' . $bannerName;
     $recordName = Dimensions::getRecordNameForAction('getContentPieces');
     $subTable = Archive::getDataTableFromArchive($recordName, $params['idSite'], $params['period'], $params['date'], $segment, true);
     //echo '<pre>';
     $bannerTable = new DataTable();
     if (!$contentPiece) {
         foreach ($subTable->getRows() as $row) {
             $ContentPieceId = Db::fetchOne("SELECT idaction FROM piwik_log_action WHERE TYPE = 14 and name = ?", array($row->getColumn('label')));
             $bannerRow = new Row(array(Row::COLUMNS => array('Label' => $row->getColumn('label'), 'Impressions' => $row->getColumn(41), 'Interactions' => $row->getColumn(42), 'Conversion rate' => $this->interactionRate($row->getColumn(41), $row->getColumn(42))), Row::DATATABLE_ASSOCIATED => implode('_', array($bannerName, $ContentPieceId))));
             $bannerTable->addRow($bannerRow);
         }
     } else {
         $orderColumn = str_replace(' ', '_', strtolower($params['filter_sort_column']));
         $orderOrder = in_array($params['filter_sort_order'], array('asc', 'desc')) ? $params['filter_sort_order'] : 'asc';
         $orderLimit = intval($params['filter_limit']);
         $where = '';
         /*
         TODO: filter_pattern is processed by piwik in some way. The results are good with this query, but piwik does some post-processing?
         if (isset($params['filter_pattern'])) {
              $where = 'and piwik_log_action.name like "%' .  $params['filter_pattern'] . '%"';
         }
         */
         $result = Db::fetchAll("\n                    SELECT \n                        trim(substring_index(piwik_log_action.name, '|', 1)) as referrer,\n                        trim(substring_index(piwik_log_action.name, '|', -1)) as target,\n                        sum(IF(idaction_content_interaction is null, 1, 0)) as impressions, \n                        sum(IF(idaction_content_interaction is null, 0, 1)) as interactions,\n                        ((100 / sum(IF(idaction_content_interaction is null, 1, 0))) * sum(IF(idaction_content_interaction is null, 0, 1))) as conversion_rate\n                    FROM piwik_log_link_visit_action \n                    left join piwik_log_action on piwik_log_action.idaction = idaction_content_target\n                    WHERE \n                        idaction_content_name in (SELECT idaction FROM piwik_log_action WHERE name = ?)\n                    and\n                        idaction_content_piece = ?\n                    \n                    {$where}\n\n                    group by piwik_log_action.name\n                    order by {$orderColumn} {$orderOrder}\n                    limit {$orderLimit}\n            ", array($bannerName, $contentPiece));
         foreach ($result as $row) {
             $bannerRow = new Row(array(Row::COLUMNS => array('Referrer' => $row['referrer'], 'Target' => $row['target'], 'Impressions' => $row['impressions'], 'Interactions' => $row['interactions'], 'Conversion rate' => round($row['conversion_rate']) . '%')));
             $bannerTable->addRow($bannerRow);
         }
     }
     return $bannerTable;
 }
Esempio n. 7
0
 /**
  * Get following pages of a url.
  * This is done on the logs - not the archives!
  *
  * Note: if you use this method via the regular API, the number of results will be limited.
  * Make sure, you set filter_limit=-1 in the request.
  */
 public function getFollowingPages($url, $idSite, $period, $date, $segment = false)
 {
     $this->authenticate($idSite);
     $url = PageUrl::excludeQueryParametersFromUrl($url, $idSite);
     // we don't unsanitize $url here. it will be done in the Transitions plugin.
     $resultDataTable = new DataTable();
     try {
         $limitBeforeGrouping = Config::getInstance()->General['overlay_following_pages_limit'];
         $transitionsReport = APITransitions::getInstance()->getTransitionsForAction($url, $type = 'url', $idSite, $period, $date, $segment, $limitBeforeGrouping, $part = 'followingActions', $returnNormalizedUrls = true);
     } catch (Exception $e) {
         return $resultDataTable;
     }
     $reports = array('followingPages', 'outlinks', 'downloads');
     foreach ($reports as $reportName) {
         if (!isset($transitionsReport[$reportName])) {
             continue;
         }
         foreach ($transitionsReport[$reportName]->getRows() as $row) {
             // don't touch the row at all for performance reasons
             $resultDataTable->addRow($row);
         }
     }
     return $resultDataTable;
 }
Esempio n. 8
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
  */
 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 = $this->createDataTable(array(array(13)));
     /*
      * end fake tables
      */
     /*
      * MAIN TABLE
      */
     $table = new DataTable();
     $subtable = new DataTable();
     $idtable = $table->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
      */
     $useless1->addRowFromArray(array(Row::COLUMNS => array(8487)));
     $useless3 = $this->createDataTable(array(array(8487)));
     /*
      * end fake tables
      */
     $row = array(Row::COLUMNS => array(0 => 1554, 1 => 42, 2 => 657, 3 => 155744), Row::METADATA => array('logo' => 'test.png'));
     $row = new Row($row);
     $table->addRow($row);
     $table->addRowFromArray(array(Row::COLUMNS => array(0 => 1554, 1 => 42), Row::METADATA => array('url' => 'piwik.org')));
     $table->addRowFromArray(array(Row::COLUMNS => array(0 => 787877888787), Row::METADATA => array('url' => 'OUPLA ADDED'), Row::DATATABLE_ASSOCIATED => $subtable));
     /*
      * SUB TABLE
      */
     $row = array(Row::COLUMNS => array(0 => 1554), Row::METADATA => array('searchengine' => 'google'));
     $subtable->addRowFromArray($row);
     $row = array(Row::COLUMNS => array(0 => 84894), Row::METADATA => array('searchengine' => 'yahoo'));
     $subtable->addRowFromArray($row);
     $row = array(Row::COLUMNS => array(0 => 4898978989), Row::METADATA => array('searchengine' => 'ask'));
     $subtable->addRowFromArray($row);
     /*
      * SUB SUB TABLE
      */
     $subsubtable = new DataTable();
     $subsubtable->addRowFromArray(array(Row::COLUMNS => array(245), Row::METADATA => array('yes' => 'subsubmetadata1')));
     $subsubtable->addRowFromArray(array(Row::COLUMNS => array(13), Row::METADATA => array('yes' => 'subsubmetadata2')));
     $row = array(Row::COLUMNS => array(0 => 666666666666666), Row::METADATA => array('url' => 'NEW ROW ADDED'), Row::DATATABLE_ASSOCIATED => $subsubtable);
     $subtable->addRowFromArray($row);
     $idsubsubtable = $subsubtable->getId();
     $serialized = $table->getSerialized();
     $this->assertEquals(array_keys($serialized), array(2, 1, 0));
     // subtableIds are now consecutive
     // 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();
     $i = 0;
     foreach ($table->getRows() as $currentRow) {
         $expectedTableRow = clone $currentRow;
         $currentRowAssociatedDatatableId = $currentRow->subtableId;
         if ($currentRowAssociatedDatatableId != null) {
             $expectedTableRow->setNonLoadedSubtableId(++$i);
             // subtableIds are consecutive
         }
         $expectedTableRows[] = $expectedTableRow;
     }
     $tableAfter = new DataTable();
     $tableAfter->addRowsFromSerializedArray($serialized[0]);
     $this->assertEquals($expectedTableRows, $tableAfter->getRows());
     $subsubtableAfter = new DataTable();
     $subsubtableAfter->addRowsFromSerializedArray($serialized[$consecutiveSubtableId = 2]);
     $this->assertEquals($subsubtable->getRows(), $subsubtableAfter->getRows());
     $this->assertEquals($subsubtable->getRows(), DataTable::fromSerializedArray($serialized[$consecutiveSubtableId = 2])->getRows());
     $this->assertTrue($subsubtable->getRowsCount() > 0);
     $this->assertEquals($table, Manager::getInstance()->getTable($idtable));
     $this->assertEquals($subsubtable, Manager::getInstance()->getTable($idsubsubtable));
 }
Esempio n. 9
0
 /**
  * This looks very similar to LabelFilter.php should it be refactored somehow? FIXME
  */
 protected function doFilterPageDatatableSearch($callBackParameters, $table, $searchTree)
 {
     // filter a data table array
     if ($table instanceof DataTable\Map) {
         foreach ($table->getDataTables() as $subTable) {
             $filteredSubTable = $this->doFilterPageDatatableSearch($callBackParameters, $subTable, $searchTree);
             if ($filteredSubTable->getRowsCount() > 0) {
                 // match found in a sub table, return and stop searching the others
                 return $filteredSubTable;
             }
         }
         // nothing found in all sub tables
         return new DataTable();
     }
     // filter regular data table
     if ($table instanceof DataTable) {
         // search for the first part of the tree search
         $search = array_shift($searchTree);
         $row = $table->getRowFromLabel($search);
         if ($row === false) {
             // not found
             $result = new DataTable();
             $result->setAllTableMetadata($table->getAllTableMetadata());
             return $result;
         }
         // end of tree search reached
         if (count($searchTree) == 0) {
             $result = new DataTable();
             $result->addRow($row);
             $result->setAllTableMetadata($table->getAllTableMetadata());
             return $result;
         }
         // match found on this level and more levels remaining: go deeper
         $idSubTable = $row->getIdSubDataTable();
         $callBackParameters[6] = $idSubTable;
         /**
          * @var \Piwik\Period $period
          */
         $period = $table->getMetadata('period');
         if (!empty($period)) {
             $callBackParameters[3] = $period->getDateStart() . ',' . $period->getDateEnd();
         }
         $table = call_user_func_array(array($this, 'getDataTableFromArchive'), $callBackParameters);
         return $this->doFilterPageDatatableSearch($callBackParameters, $table, $searchTree);
     }
     throw new Exception("For this API function, DataTable " . get_class($table) . " is not supported");
 }
Esempio n. 10
0
 protected function makeDataTablesFollowingActions($types, $data)
 {
     $this->totalTransitionsToFollowingActions = 0;
     $dataTables = array();
     foreach ($types as $type => $recordName) {
         $dataTable = new DataTable();
         if (isset($data[$type])) {
             foreach ($data[$type] as &$record) {
                 $actions = intval($record[Metrics::INDEX_NB_ACTIONS]);
                 $dataTable->addRow(new Row(array(Row::COLUMNS => array('label' => $this->getPageLabel($record, $type), Metrics::INDEX_NB_ACTIONS => $actions))));
                 $this->totalTransitionsToFollowingActions += $actions;
             }
         }
         $dataTables[$recordName] = $dataTable;
     }
     return $dataTables;
 }
Esempio n. 11
0
 /**
  * Utility function used by mergeChildren. Copies the rows from one table,
  * sets their 'label' columns to a value and adds them to another table.
  *
  * @param DataTable $toTable The table to copy rows to.
  * @param DataTable $fromTable The table to copy rows from.
  * @param string $label The value to set the 'label' column of every copied row.
  */
 private function copyRowsAndSetLabel($toTable, $fromTable, $label)
 {
     foreach ($fromTable->getRows() as $fromRow) {
         $oldColumns = $fromRow->getColumns();
         unset($oldColumns['label']);
         $columns = array_merge(array('label' => $label), $oldColumns);
         $row = new Row(array(Row::COLUMNS => $columns, Row::METADATA => $fromRow->getMetadata(), Row::DATATABLE_ASSOCIATED => $fromRow->getIdSubDataTable()));
         $toTable->addRow($row);
     }
 }
 /**
  * Enhance $simpleDataTable using metadata :
  *
  * - remove metrics based on $reportMetadata['metrics']
  * - add 0 valued metrics if $simpleDataTable doesn't provide all $reportMetadata['metrics']
  * - format metric values to a 'human readable' format
  * - extract row metadata to a separate Simple $rowsMetadata
  *
  * @param int $idSite enables monetary value formatting based on site currency
  * @param Simple $simpleDataTable
  * @param array $metadataColumns
  * @param boolean $hasDimension
  * @param bool $returnRawMetrics If set to true, the original metrics will be returned
  * @param bool|null $formatMetrics
  * @return array DataTable $enhancedDataTable filtered metrics with human readable format & Simple $rowsMetadata
  */
 private function handleSimpleDataTable($idSite, $simpleDataTable, $metadataColumns, $hasDimension, $returnRawMetrics = false, $formatMetrics = null)
 {
     // new DataTable to store metadata
     $rowsMetadata = new DataTable();
     // new DataTable to store 'human readable' values
     if ($hasDimension) {
         $enhancedDataTable = new DataTable();
     } else {
         $enhancedDataTable = new Simple();
     }
     $formatter = new Formatter();
     foreach ($simpleDataTable->getRows() as $row) {
         $rowMetrics = $row->getColumns();
         // add missing metrics
         foreach ($metadataColumns as $id => $name) {
             if (!isset($rowMetrics[$id])) {
                 $row->setColumn($id, 0);
                 $rowMetrics[$id] = 0;
             }
         }
         $enhancedRow = new Row();
         $enhancedDataTable->addRow($enhancedRow);
         foreach ($rowMetrics as $columnName => $columnValue) {
             // filter metrics according to metadata definition
             if (isset($metadataColumns[$columnName])) {
                 // generate 'human readable' metric values
                 // if we handle MultiSites.getAll we do not always have the same idSite but different ones for
                 // each site, see https://github.com/piwik/piwik/issues/5006
                 $idSiteForRow = $idSite;
                 $idSiteMetadata = $row->getMetadata('idsite');
                 if ($idSiteMetadata && is_numeric($idSiteMetadata)) {
                     $idSiteForRow = (int) $idSiteMetadata;
                 }
                 // format metrics manually here to maintain API.getProcessedReport BC if format_metrics query parameter is
                 // not supplied. TODO: should be removed for 3.0. should only rely on format_metrics query parameter.
                 if ($formatMetrics === null || $formatMetrics == 'bc') {
                     $prettyValue = self::getPrettyValue($formatter, $idSiteForRow, $columnName, $columnValue, $htmlAllowed = false);
                 } else {
                     $prettyValue = $columnValue;
                 }
                 $enhancedRow->addColumn($columnName, $prettyValue);
             } else {
                 if ($returnRawMetrics) {
                     if (!isset($columnValue)) {
                         $columnValue = 0;
                     }
                     $enhancedRow->addColumn($columnName, $columnValue);
                 }
             }
         }
         // If report has a dimension, extract metadata into a distinct DataTable
         if ($hasDimension) {
             $rowMetadata = $row->getMetadata();
             $idSubDataTable = $row->getIdSubDataTable();
             // Create a row metadata only if there are metadata to insert
             if (count($rowMetadata) > 0 || !is_null($idSubDataTable)) {
                 $metadataRow = new Row();
                 $rowsMetadata->addRow($metadataRow);
                 foreach ($rowMetadata as $metadataKey => $metadataValue) {
                     $metadataRow->addColumn($metadataKey, $metadataValue);
                 }
                 if (!is_null($idSubDataTable)) {
                     $metadataRow->addColumn('idsubdatatable', $idSubDataTable);
                 }
             }
         }
     }
     return array($enhancedDataTable, $rowsMetadata);
 }
Esempio n. 13
0
 /**
  * Another example method that returns a data table.
  * @param int    $idSite
  * @param string $period
  * @param string $date
  * @param bool|string $segment
  * @return DataTable
  */
 public function getSummaryStats($idSite, $period, $date, $segment = false)
 {
     set_time_limit(600);
     $table = new DataTable();
     $operations_url = 'http://www.humanitarianresponse.info/api/v1.0/operations/?fields=id';
     if ($operations_raw = @file_get_contents($operations_url)) {
         $operations_json = json_decode($operations_raw);
         foreach ($operations_json->data as $op_id) {
             $temp = $this->getSpaceSummary($idSite, $period, $date, $op_id->id, 'operation');
             $table->addRow($temp->getRowFromId(0));
         }
     }
     $spaces_url = 'http://www.humanitarianresponse.info/api/v1.0/spaces/?fields=id';
     if ($spaces_raw = @file_get_contents($spaces_url)) {
         $spaces_json = json_decode($spaces_raw);
         foreach ($spaces_json->data as $space_id) {
             $temp = $this->getSpaceSummary($idSite, $period, $date, $space_id->id, 'space');
             $table->addRow($temp->getRowFromId(0));
         }
     }
     return $table;
 }
Esempio n. 14
0
 /**
  * Returns a custom data table.
  * This data table will be converted to all available formats
  * when requested in the API request.
  *
  * @return DataTable
  */
 public function getCompetitionDatatable()
 {
     $dataTable = new DataTable();
     $row1 = new Row();
     $row1->setColumns(array('name' => 'piwik', 'license' => 'GPL'));
     // Rows Metadata is useful to store non stats data for example (logos, urls, etc.)
     // When printed out, they are simply merged with columns
     $row1->setMetadata('logo', 'logo.png');
     $dataTable->addRow($row1);
     $dataTable->addRowFromSimpleArray(array('name' => 'google analytics', 'license' => 'commercial'));
     return $dataTable;
 }
Esempio n. 15
0
 /**
  * Returns a new DataTable in which the rows of this table are replaced with the aggregatated rows of all its subtables.
  *
  * @param string|bool $labelColumn If supplied the label of the parent row will be added to
  *                                 a new column in each subtable row.
  *
  *                                 If set to, `'label'` each subtable row's label will be prepended
  *                                 w/ the parent row's label. So `'child_label'` becomes
  *                                 `'parent_label - child_label'`.
  * @param bool $useMetadataColumn If true and if `$labelColumn` is supplied, the parent row's
  *                                label will be added as metadata and not a new column.
  * @return \Piwik\DataTable
  */
 public function mergeSubtables($labelColumn = false, $useMetadataColumn = false)
 {
     $result = new DataTable();
     $result->setAllTableMetadata($this->getAllTableMetadata());
     foreach ($this->getRowsWithoutSummaryRow() as $row) {
         $subtable = $row->getSubtable();
         if ($subtable !== false) {
             $parentLabel = $row->getColumn('label');
             // add a copy of each subtable row to the new datatable
             foreach ($subtable->getRows() as $id => $subRow) {
                 $copy = clone $subRow;
                 // if the summary row, add it to the existing summary row (or add a new one)
                 if ($id == self::ID_SUMMARY_ROW) {
                     $existing = $result->getRowFromId(self::ID_SUMMARY_ROW);
                     if ($existing === false) {
                         $result->addSummaryRow($copy);
                     } else {
                         $existing->sumRow($copy, $copyMeta = true, $this->getMetadata(self::COLUMN_AGGREGATION_OPS_METADATA_NAME));
                     }
                 } else {
                     if ($labelColumn !== false) {
                         // if we're modifying the subtable's rows' label column, then we make
                         // sure to prepend the existing label w/ the parent row's label. otherwise
                         // we're just adding the parent row's label as a new column/metadata.
                         $newLabel = $parentLabel;
                         if ($labelColumn == 'label') {
                             $newLabel .= ' - ' . $copy->getColumn('label');
                         }
                         // modify the child row's label or add new column/metadata
                         if ($useMetadataColumn) {
                             $copy->setMetadata($labelColumn, $newLabel);
                         } else {
                             $copy->setColumn($labelColumn, $newLabel);
                         }
                     }
                     $result->addRow($copy);
                 }
             }
         }
     }
     return $result;
 }
Esempio n. 16
0
 /**
  * Enhance $simpleDataTable using metadata :
  *
  * - remove metrics based on $reportMetadata['metrics']
  * - add 0 valued metrics if $simpleDataTable doesn't provide all $reportMetadata['metrics']
  * - format metric values to a 'human readable' format
  * - extract row metadata to a separate Simple $rowsMetadata
  *
  * @param int $idSite enables monetary value formatting based on site currency
  * @param Simple $simpleDataTable
  * @param array $metadataColumns
  * @param boolean $hasDimension
  * @param bool $returnRawMetrics If set to true, the original metrics will be returned
  *
  * @return array DataTable $enhancedDataTable filtered metrics with human readable format & Simple $rowsMetadata
  */
 private function handleSimpleDataTable($idSite, $simpleDataTable, $metadataColumns, $hasDimension, $returnRawMetrics = false)
 {
     // new DataTable to store metadata
     $rowsMetadata = new DataTable();
     // new DataTable to store 'human readable' values
     if ($hasDimension) {
         $enhancedDataTable = new DataTable();
     } else {
         $enhancedDataTable = new Simple();
     }
     foreach ($simpleDataTable->getRows() as $row) {
         $rowMetrics = $row->getColumns();
         // add missing metrics
         foreach ($metadataColumns as $id => $name) {
             if (!isset($rowMetrics[$id])) {
                 $row->setColumn($id, 0);
                 $rowMetrics[$id] = 0;
             }
         }
         $enhancedRow = new Row();
         $enhancedDataTable->addRow($enhancedRow);
         foreach ($rowMetrics as $columnName => $columnValue) {
             // filter metrics according to metadata definition
             if (isset($metadataColumns[$columnName])) {
                 // generate 'human readable' metric values
                 // if we handle MultiSites.getAll we do not always have the same idSite but different ones for
                 // each site, see http://dev.piwik.org/trac/ticket/5006
                 $idSiteForRow = $idSite;
                 if ($row->getMetadata('idsite') && is_numeric($row->getMetadata('idsite'))) {
                     $idSiteForRow = (int) $row->getMetadata('idsite');
                 }
                 $prettyValue = MetricsFormatter::getPrettyValue($idSiteForRow, $columnName, $columnValue, $htmlAllowed = false);
                 $enhancedRow->addColumn($columnName, $prettyValue);
             } elseif ($returnRawMetrics) {
                 if (!isset($columnValue)) {
                     $columnValue = 0;
                 }
                 $enhancedRow->addColumn($columnName, $columnValue);
             }
         }
         // If report has a dimension, extract metadata into a distinct DataTable
         if ($hasDimension) {
             $rowMetadata = $row->getMetadata();
             $idSubDataTable = $row->getIdSubDataTable();
             // Create a row metadata only if there are metadata to insert
             if (count($rowMetadata) > 0 || !is_null($idSubDataTable)) {
                 $metadataRow = new Row();
                 $rowsMetadata->addRow($metadataRow);
                 foreach ($rowMetadata as $metadataKey => $metadataValue) {
                     $metadataRow->addColumn($metadataKey, $metadataValue);
                 }
                 if (!is_null($idSubDataTable)) {
                     $metadataRow->addColumn('idsubdatatable', $idSubDataTable);
                 }
             }
         }
     }
     return array($enhancedDataTable, $rowsMetadata);
 }
 /**
  * Enhance $simpleDataTable using metadata :
  *
  * - remove metrics based on $reportMetadata['metrics']
  * - add 0 valued metrics if $simpleDataTable doesn't provide all $reportMetadata['metrics']
  * - format metric values to a 'human readable' format
  * - extract row metadata to a separate Simple $rowsMetadata
  *
  * @param int $idSite enables monetary value formatting based on site currency
  * @param Simple $simpleDataTable
  * @param array $metadataColumns
  * @param boolean $hasDimension
  * @param bool $returnRawMetrics If set to true, the original metrics will be returned
  *
  * @return array DataTable $enhancedDataTable filtered metrics with human readable format & Simple $rowsMetadata
  */
 private function handleSimpleDataTable($idSite, $simpleDataTable, $metadataColumns, $hasDimension, $returnRawMetrics = false)
 {
     // new DataTable to store metadata
     $rowsMetadata = new DataTable();
     // new DataTable to store 'human readable' values
     if ($hasDimension) {
         $enhancedDataTable = new DataTable();
     } else {
         $enhancedDataTable = new Simple();
     }
     // add missing metrics
     foreach ($simpleDataTable->getRows() as $row) {
         $rowMetrics = $row->getColumns();
         foreach ($metadataColumns as $id => $name) {
             if (!isset($rowMetrics[$id])) {
                 $row->addColumn($id, 0);
             }
         }
     }
     foreach ($simpleDataTable->getRows() as $row) {
         $enhancedRow = new Row();
         $enhancedDataTable->addRow($enhancedRow);
         $rowMetrics = $row->getColumns();
         foreach ($rowMetrics as $columnName => $columnValue) {
             // filter metrics according to metadata definition
             if (isset($metadataColumns[$columnName])) {
                 // generate 'human readable' metric values
                 $prettyValue = MetricsFormatter::getPrettyValue($idSite, $columnName, $columnValue, $htmlAllowed = false);
                 $enhancedRow->addColumn($columnName, $prettyValue);
             } elseif ($returnRawMetrics) {
                 $enhancedRow->addColumn($columnName, $columnValue);
             }
         }
         // If report has a dimension, extract metadata into a distinct DataTable
         if ($hasDimension) {
             $rowMetadata = $row->getMetadata();
             $idSubDataTable = $row->getIdSubDataTable();
             // Create a row metadata only if there are metadata to insert
             if (count($rowMetadata) > 0 || !is_null($idSubDataTable)) {
                 $metadataRow = new Row();
                 $rowsMetadata->addRow($metadataRow);
                 foreach ($rowMetadata as $metadataKey => $metadataValue) {
                     $metadataRow->addColumn($metadataKey, $metadataValue);
                 }
                 if (!is_null($idSubDataTable)) {
                     $metadataRow->addColumn('idsubdatatable', $idSubDataTable);
                 }
             }
         }
     }
     return array($enhancedDataTable, $rowsMetadata);
 }
Esempio n. 18
0
 /**
  * @group Core
  */
 public function testGetSerializedCallsCleanPostSerialize()
 {
     $mockedDataTableRow = $this->getMock('\\Piwik\\DataTable\\Row', array('cleanPostSerialize'));
     $mockedDataTableRow->expects($this->once())->method('cleanPostSerialize');
     $dataTableBeingSerialized = new DataTable();
     $dataTableBeingSerialized->addRow($mockedDataTableRow);
     $dataTableBeingSerialized->getSerialized();
 }
Esempio n. 19
0
 /**
  * @param array $result
  * @return DataTable
  */
 private function resultToDatatable($result)
 {
     $retval = new DataTable();
     foreach ($result as $row) {
         if (isset($row["id"])) {
             $id = $row["id"];
             unset($row["id"]);
         }
         $row = new Row(array(Row::COLUMNS => $row, Row::DATATABLE_ASSOCIATED => isset($id) ? $id : null));
         $retval->addRow($row);
     }
     return $retval;
 }
 private function addRow($columns)
 {
     $this->table->addRow($this->buildRow($columns));
 }
Esempio n. 21
0
 private function makeMergedWithSiteIndex($index, $useSimpleDataTable, $isNumeric)
 {
     if ($useSimpleDataTable) {
         $table = new DataTable\Simple();
     } else {
         $table = new DataTable();
     }
     $table->setAllTableMetadata(array(DataTableFactory::TABLE_METADATA_PERIOD_INDEX => reset($this->periods)));
     foreach ($index as $idsite => $row) {
         if (!empty($row)) {
             $table->addRow(new Row(array(Row::COLUMNS => $row, Row::METADATA => array('idsite' => $idsite))));
         } elseif ($isNumeric) {
             $table->addRow(new Row(array(Row::COLUMNS => $this->defaultRow, Row::METADATA => array('idsite' => $idsite))));
         }
     }
     return $table;
 }
Esempio n. 22
0
 private function generateDataTableWithManySubtables($numSubtables)
 {
     $dataTable = new DataTable();
     for ($i = 1; $i <= $numSubtables; $i++) {
         $row = new Row(array(Row::COLUMNS => array('label' => 'Label Test ' . $i, 'nb_visits' => $i)));
         $subtable = DataTable::makeFromSimpleArray(array(array('label' => 'subtable' . $i, 'nb_visits' => $i)));
         $row->setSubtable($subtable);
         $dataTable->addRow($row);
     }
     return $dataTable->getSerialized();
 }
 public function getSegmentTable()
 {
     ++$this->segmentTableCount;
     $table = new DataTable();
     for ($i = 0; $i != $this->segmentTableCount; ++$i) {
         $row = new Row(array(Row::COLUMNS => array('label' => 'col ' . $i, 'nb_visits' => ($i + 1) * 2, 'nb_actions' => ($i + 1) * 3)));
         $table->addRow($row);
     }
     return $table;
 }