/**
  * 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);
 }
 /**
  * @param $data
  * @return DataTable\Simple
  */
 private function makeFromMetricsArray($data)
 {
     $table = new DataTable\Simple();
     if (!empty($data)) {
         $table->setAllTableMetadata(DataCollection::getDataRowMetadata($data));
         DataCollection::removeMetadataFromDataRow($data);
         $table->addRow(new Row(array(Row::COLUMNS => $data)));
     } else {
         // if we're querying numeric data, we couldn't find any, and we're only
         // looking for one metric, add a row w/ one column w/ value 0. this is to
         // ensure that the PHP renderer outputs 0 when only one column is queried.
         // w/o this code, an empty array would be created, and other parts of Piwik
         // would break.
         if (count($this->dataNames) == 1 && $this->dataType == 'numeric') {
             $name = reset($this->dataNames);
             $table->addRow(new Row(array(Row::COLUMNS => array($name => 0))));
         }
     }
     $result = $table;
     return $result;
 }
 /**
  * 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);
 }
 /**
  * Convert a dimension-less report to a multi-row two-column data table
  *
  * @static
  * @param  $reportMetadata array
  * @param  $report DataTable
  * @param  $reportColumns array
  * @return array DataTable $report & array $columns
  */
 protected static function processTableFormat($reportMetadata, $report, $reportColumns)
 {
     $finalReport = $report;
     if (empty($reportMetadata['dimension'])) {
         $simpleReportMetrics = $report->getFirstRow();
         if ($simpleReportMetrics) {
             $finalReport = new Simple();
             foreach ($simpleReportMetrics->getColumns() as $metricId => $metric) {
                 $newRow = new Row();
                 $newRow->addColumn("label", $reportColumns[$metricId]);
                 $newRow->addColumn("value", $metric);
                 $finalReport->addRow($newRow);
             }
         }
         $reportColumns = array('label' => Piwik::translate('General_Name'), 'value' => Piwik::translate('General_Value'));
     }
     return array($finalReport, $reportColumns);
 }
Beispiel #5
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);
 }