示例#1
0
 /**
  * @param Simple $table
  */
 protected function filterSimple(Simple $table)
 {
     foreach ($table->getRows() as $row) {
         $columns = array_keys($row->getColumns());
         foreach ($columns as $column) {
             $newName = $this->getRenamedColumn($column);
             if ($newName) {
                 $row->renameColumn($column, $newName);
             }
         }
     }
 }
示例#2
0
 /**
  * Converts the output of the given simple data table
  *
  * @param DataTable|Simple $table
  * @param array $allColumns
  * @return string
  */
 protected function renderDataTable($table, &$allColumns = array())
 {
     if ($table instanceof Simple) {
         $row = $table->getFirstRow();
         if ($row !== false) {
             $columnNameToValue = $row->getColumns();
             if (count($columnNameToValue) == 1) {
                 // simple tables should only have one column, the value
                 $allColumns['value'] = true;
                 $value = array_values($columnNameToValue);
                 $str = 'value' . $this->lineEnd . $this->formatValue($value[0]);
                 return $str;
             }
         }
     }
     $csv = array();
     foreach ($table->getRows() as $row) {
         $csvRow = $this->flattenColumnArray($row->getColumns());
         if ($this->exportMetadata) {
             $metadata = $row->getMetadata();
             foreach ($metadata as $name => $value) {
                 if ($name == 'idsubdatatable_in_db') {
                     continue;
                 }
                 //if a metadata and a column have the same name make sure they dont overwrite
                 if ($this->translateColumnNames) {
                     $name = Piwik::translate('General_Metadata') . ': ' . $name;
                 } else {
                     $name = 'metadata_' . $name;
                 }
                 $csvRow[$name] = $value;
             }
         }
         foreach ($csvRow as $name => $value) {
             $allColumns[$name] = true;
         }
         if ($this->exportIdSubtable) {
             $idsubdatatable = $row->getIdSubDataTable();
             if ($idsubdatatable !== false && $this->hideIdSubDatatable === false) {
                 $csvRow['idsubdatatable'] = $idsubdatatable;
             }
         }
         $csv[] = $csvRow;
     }
     // now we make sure that all the rows in the CSV array have all the columns
     foreach ($csv as &$row) {
         foreach ($allColumns as $columnName => $true) {
             if (!isset($row[$columnName])) {
                 $row[$columnName] = '';
             }
         }
     }
     $str = '';
     // specific case, we have only one column and this column wasn't named properly (indexed by a number)
     // we don't print anything in the CSV file => an empty line
     if (sizeof($allColumns) == 1 && reset($allColumns) && !is_string(key($allColumns))) {
         $str .= '';
     } else {
         // render row names
         $str .= $this->getHeaderLine(array_keys($allColumns)) . $this->lineEnd;
     }
     // we render the CSV
     foreach ($csv as $theRow) {
         $rowStr = '';
         foreach ($allColumns as $columnName => $true) {
             $rowStr .= $this->formatValue($theRow[$columnName]) . $this->separator;
         }
         // remove the last separator
         $rowStr = substr_replace($rowStr, "", -strlen($this->separator));
         $str .= $rowStr . $this->lineEnd;
     }
     $str = substr($str, 0, -strlen($this->lineEnd));
     return $str;
 }
 /**
  * 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);
 }
 /**
  * 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);
 }
示例#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);
 }