/** * Converts the output of the given simple data table * * @param Piwik_DataTable_Simple $table * @param array $allColumns * @return string */ protected function renderDataTable($table, &$allColumns = array()) { if ($table instanceof Piwik_DataTable_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 = array(); $columns = $row->getColumns(); foreach ($columns as $name => $value) { //goals => array( 'idgoal=1' =>array(..), 'idgoal=2' => array(..)) if (is_array($value)) { foreach ($value as $key => $subValues) { if (is_array($subValues)) { foreach ($subValues as $subKey => $subValue) { if ($this->translateColumnNames) { $subName = $name != 'goals' ? $name . ' ' . $key : Piwik_Translate('Goals_GoalX', $key); $columnName = $this->translateColumnName($subKey) . ' (' . $subName . ')'; } else { // goals_idgoal=1 $columnName = $name . "_" . $key . "_" . $subKey; } $allColumns[$columnName] = true; $csvRow[$columnName] = $subValue; } } } } else { $allColumns[$name] = true; $csvRow[$name] = $value; } } 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; } $allColumns[$name] = true; $csvRow[$name] = $value; } } 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 Piwik_DataTable_Simple $rowsMetadata * * @param int $idSite enables monetary value formatting based on site currency * @param Piwik_DataTable_Simple $simpleDataTable * @param array $metadataColumns * @param boolean $hasDimension * @return array Piwik_DataTable $enhancedDataTable filtered metrics with human readable format & Piwik_DataTable_Simple $rowsMetadata */ private function handleSimpleDataTable($idSite, $simpleDataTable, $metadataColumns, $hasDimension) { // new DataTable to store metadata $rowsMetadata = new Piwik_DataTable(); // new DataTable to store 'human readable' values if ($hasDimension) { $enhancedDataTable = new Piwik_DataTable(); } else { $enhancedDataTable = new Piwik_DataTable_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 Piwik_DataTable_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 = Piwik::getPrettyValue($idSite, $columnName, $columnValue, false, false); $enhancedRow->addColumn($columnName, $prettyValue); } } // 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 Piwik_DataTable_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); }
/** * Adds the given data table to the table structure array * * @param Piwik_DataTable_Simple $table * @param null|string $columnToAdd * @param null|string $valueToAdd * @throws Exception */ protected function buildTableStructure($table, $columnToAdd = null, $valueToAdd = null) { $i = $this->i; $someMetadata = false; $someIdSubTable = false; /* * table = array * ROW1 = col1 | col2 | col3 | metadata | idSubTable * ROW2 = col1 | col2 (no value but appears) | col3 | metadata | idSubTable */ if (!$table instanceof Piwik_DataTable) { throw new Exception("HTML Renderer does not work with this combination of parameters"); } foreach ($table->getRows() as $row) { if (isset($columnToAdd) && isset($valueToAdd)) { $this->allColumns[$columnToAdd] = true; $this->tableStructure[$i][$columnToAdd] = $valueToAdd; } foreach ($row->getColumns() as $column => $value) { $this->allColumns[$column] = true; $this->tableStructure[$i][$column] = $value; } $metadata = array(); foreach ($row->getMetadata() as $name => $value) { if (is_string($value)) { $value = "'{$value}'"; } $metadata[] = "'{$name}' => {$value}"; } if (count($metadata) != 0) { $someMetadata = true; $metadata = implode("<br />", $metadata); $this->tableStructure[$i]['_metadata'] = $metadata; } $idSubtable = $row->getIdSubDataTable(); if (!is_null($idSubtable)) { $someIdSubTable = true; $this->tableStructure[$i]['_idSubtable'] = $idSubtable; } $i++; } $this->i = $i; $this->allColumns['_metadata'] = $someMetadata; $this->allColumns['_idSubtable'] = $someIdSubTable; }