作者:FluentDevelopmen
项目:piwi
/**
* 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);
}
}
作者:FluentDevelopmen
项目:piwi
private function createInstanceWithDataTableMaps()
{
$dataTable = new DataTable\Map();
$subDataTableMap1 = $this->createInstanceWithDataTables();
$dataTable->addTable($subDataTableMap1, 'subArray1');
$subDataTableMap2 = $this->createInstanceWithDataTables();
$dataTable->addTable($subDataTableMap2, 'subArray2');
return $dataTable;
}
作者:TensorWrenchOS
项目:piwi
/**
* Manipulates child DataTables of a DataTable\Map. See @manipulate for more info.
*
* @param DataTable\Map $dataTable
* @return DataTable\Map
*/
protected function manipulateDataTableMap($dataTable)
{
$result = $dataTable->getEmptyClone();
foreach ($dataTable->getDataTables() as $tableLabel => $childTable) {
$newTable = $this->manipulate($childTable);
$result->addTable($newTable, $tableLabel);
}
return $result;
}
作者:FluentDevelopmen
项目:piwi
/**
* Computes the output of the given array of data tables
*
* @param DataTable\Map $map data tables to render
* @param string $prefix prefix to output before table data
* @return string
*/
protected function renderDataTableMap(DataTable\Map $map, $prefix)
{
$output = "Set<hr />";
$prefix = $prefix . ' ';
foreach ($map->getDataTables() as $descTable => $table) {
$output .= $prefix . "<b>" . $descTable . "</b><br />";
$output .= $prefix . $this->renderTable($table, $prefix . ' ');
$output .= "<hr />";
}
return $output;
}
作者:brienomatt
项目:elmsl
private function getSeriesData($rowLabel, $columnName, DataTable\Map $dataTable)
{
$seriesData = array();
foreach ($dataTable->getDataTables() as $childTable) {
// get the row for this label (use the first if $rowLabel is false)
if ($rowLabel === false) {
$row = $childTable->getFirstRow();
} else {
$row = $childTable->getRowFromLabel($rowLabel);
}
// get series data point. defaults to 0 if no row or no column value.
if ($row === false) {
$seriesData[] = 0;
} else {
$seriesData[] = $row->getColumn($columnName) ?: 0;
}
}
return $seriesData;
}
作者:piwi
项目:piwi
/**
* @param DataTable\Map $dataTableMap
* @param string $columnToPlot
*
* @return array
* @throws \Exception
*/
protected function getValuesFromDataTableMap($dataTableMap, $columnToPlot)
{
$dataTableMap->applyQueuedFilters();
$values = array();
foreach ($dataTableMap->getDataTables() as $table) {
if ($table->getRowsCount() > 1) {
throw new Exception("Expecting only one row per DataTable");
}
$value = 0;
$onlyRow = $table->getFirstRow();
if (false !== $onlyRow) {
if (!empty($columnToPlot)) {
$value = $onlyRow->getColumn($columnToPlot);
} else {
$columns = $onlyRow->getColumns();
$value = current($columns);
}
}
$values[] = $value;
}
return $values;
}
作者:Abin
项目:piwi
public function test_renderDataTable_shouldRenderDataTableMaps()
{
$map = new DataTable\Map();
$dataTable = new DataTable();
$dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
$dataTable2 = new DataTable();
$dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
$map->addTable($dataTable, 'table1');
$map->addTable($dataTable2, 'table2');
$response = $this->builder->renderDataTable($map);
$expected = array('table1' => array(array('nb_visits' => 5, 'nb_random' => 10)), 'table2' => array(array('nb_visits' => 3, 'nb_random' => 6)));
$this->assertEquals($expected, $response);
}
作者:CaptainShar
项目:SSAD_Projec
/**
* Enhance a $dataTable using metadata :
*
* - remove metrics based on $reportMetadata['metrics']
* - add 0 valued metrics if $dataTable doesn't provide all $reportMetadata['metrics']
* - format metric values to a 'human readable' format
* - extract row metadata to a separate Simple|Set : $rowsMetadata
* - translate metric names to a separate array : $columns
*
* @param int $idSite enables monetary value formatting based on site currency
* @param \Piwik\DataTable\Map|\Piwik\DataTable\Simple $dataTable
* @param array $reportMetadata
* @param bool $showRawMetrics
* @param bool|null $formatMetrics
* @return array Simple|Set $newReport with human readable format & array $columns list of translated column names & Simple|Set $rowsMetadata
*/
private function handleTableReport($idSite, $dataTable, &$reportMetadata, $showRawMetrics = false, $formatMetrics = null)
{
$hasDimension = isset($reportMetadata['dimension']);
$columns = @$reportMetadata['metrics'] ?: array();
if ($hasDimension) {
$columns = array_merge(array('label' => $reportMetadata['dimension']), $columns);
}
if (isset($reportMetadata['processedMetrics']) && is_array($reportMetadata['processedMetrics'])) {
$processedMetricsAdded = Metrics::getDefaultProcessedMetrics();
foreach ($reportMetadata['processedMetrics'] as $processedMetricId => $processedMetricTranslation) {
// this processed metric can be displayed for this report
if ($processedMetricTranslation && $processedMetricId !== $processedMetricTranslation) {
$columns[$processedMetricId] = $processedMetricTranslation;
} elseif (isset($processedMetricsAdded[$processedMetricId])) {
// for instance in case 'nb_visits' => 'nb_visits' we will translate it
$columns[$processedMetricId] = $processedMetricsAdded[$processedMetricId];
}
}
}
// Display the global Goal metrics
if (isset($reportMetadata['metricsGoal'])) {
$metricsGoalDisplay = array('revenue');
// Add processed metrics to be displayed for this report
foreach ($metricsGoalDisplay as $goalMetricId) {
if (isset($reportMetadata['metricsGoal'][$goalMetricId])) {
$columns[$goalMetricId] = $reportMetadata['metricsGoal'][$goalMetricId];
}
}
}
$columns = $this->hideShowMetrics($columns);
$totals = array();
// $dataTable is an instance of Set when multiple periods requested
if ($dataTable instanceof DataTable\Map) {
// Need a new Set to store the 'human readable' values
$newReport = new DataTable\Map();
$newReport->setKeyName("prettyDate");
// Need a new Set to store report metadata
$rowsMetadata = new DataTable\Map();
$rowsMetadata->setKeyName("prettyDate");
// Process each Simple entry
foreach ($dataTable->getDataTables() as $simpleDataTable) {
$this->removeEmptyColumns($columns, $reportMetadata, $simpleDataTable);
list($enhancedSimpleDataTable, $rowMetadata) = $this->handleSimpleDataTable($idSite, $simpleDataTable, $columns, $hasDimension, $showRawMetrics, $formatMetrics);
$enhancedSimpleDataTable->setAllTableMetadata($simpleDataTable->getAllTableMetadata());
$period = $simpleDataTable->getMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX)->getLocalizedLongString();
$newReport->addTable($enhancedSimpleDataTable, $period);
$rowsMetadata->addTable($rowMetadata, $period);
$totals = $this->aggregateReportTotalValues($simpleDataTable, $totals);
}
} else {
$this->removeEmptyColumns($columns, $reportMetadata, $dataTable);
list($newReport, $rowsMetadata) = $this->handleSimpleDataTable($idSite, $dataTable, $columns, $hasDimension, $showRawMetrics, $formatMetrics);
$totals = $this->aggregateReportTotalValues($dataTable, $totals);
}
return array($newReport, $columns, $rowsMetadata, $totals);
}
作者:KiwiJuice
项目:handball-dacha
/**
* Creates a Set from an array index.
*
* @param array $index @see DataCollection
* @param array $resultIndices @see make
* @param array $keyMetadata The metadata to add to the table when it's created.
* @return DataTable\Map
*/
private function createDataTableMapFromIndex($index, $resultIndices, $keyMetadata = array())
{
$resultIndexLabel = reset($resultIndices);
$resultIndex = key($resultIndices);
array_shift($resultIndices);
$result = new DataTable\Map();
$result->setKeyName($resultIndexLabel);
foreach ($index as $label => $value) {
$keyMetadata[$resultIndex] = $label;
if (empty($resultIndices)) {
$newTable = $this->createDataTable($value, $keyMetadata);
} else {
$newTable = $this->createDataTableMapFromIndex($value, $resultIndices, $keyMetadata);
}
$result->addTable($newTable, $this->prettifyIndexLabel($resultIndex, $label));
}
return $result;
}
作者:FluentDevelopmen
项目:piwi
public function test_renderDataTable_shouldRenderDataTableMaps()
{
$map = new DataTable\Map();
$dataTable = new DataTable();
$dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
$dataTable2 = new DataTable();
$dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
$map->addTable($dataTable, 'table1');
$map->addTable($dataTable2, 'table2');
$response = $this->builder->renderDataTable($map);
$this->assertEquals('<table id="MultiSites_getAll" border="1">
<thead>
<tr>
<th>_defaultKeyName</th>
<th>nb_visits</th>
<th>nb_random</th>
</tr>
</thead>
<tbody>
<tr>
<td>table1</td>
<td>5</td>
<td>10</td>
</tr>
<tr>
<td>table2</td>
<td>3</td>
<td>6</td>
</tr>
</tbody>
</table>
', $response);
}
作者:a4tunad
项目:piwi
public function test_renderDataTable_shouldRenderDataTableMaps()
{
$map = new DataTable\Map();
$dataTable = new DataTable();
$dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
$dataTable2 = new DataTable();
$dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
$map->addTable($dataTable, 'table1');
$map->addTable($dataTable2, 'table2');
$response = $this->builder->renderDataTable($map);
$this->assertEquals('<?xml version="1.0" encoding="utf-8" ?>
<results>
<result defaultKeyName="table1">
<row>
<nb_visits>5</nb_visits>
<nb_random>10</nb_random>
</row>
</result>
<result defaultKeyName="table2">
<row>
<nb_visits>3</nb_visits>
<nb_random>6</nb_random>
</row>
</result>
</results>', $response);
}
作者:jos
项目:CGE-File-Sharin
/**
* Filter a DataTable instance. See @filter for more info.
*
* @param DataTable\Simple|DataTable\Map $dataTable
* @return mixed
*/
protected function manipulateDataTable($dataTable)
{
$result = $dataTable->getEmptyClone();
foreach ($this->labels as $labelIndex => $label) {
$row = null;
foreach ($this->getLabelVariations($label) as $labelVariation) {
$labelVariation = explode(self::SEPARATOR_RECURSIVE_LABEL, $labelVariation);
$row = $this->doFilterRecursiveDescend($labelVariation, $dataTable);
if ($row) {
if ($this->addLabelIndex) {
$row->setMetadata(self::FLAG_IS_ROW_EVOLUTION, $labelIndex);
}
$result->addRow($row);
break;
}
}
}
return $result;
}
作者:a4tunad
项目:piwi
/**
* Common filters for all Actions API
*
* @param DataTable|DataTable\Simple|DataTable\Map $dataTable
* @param bool $expanded
*/
protected function filterActionsDataTable($dataTable, $expanded = false)
{
// Must be applied before Sort in this case, since the DataTable can contain both int and strings indexes
// (in the transition period between pre 1.2 and post 1.2 datatable structure)
$dataTable->filter('ReplaceColumnNames');
$dataTable->filter('Sort', array('nb_visits', 'desc', $naturalSort = false, $expanded));
$dataTable->queueFilter('ReplaceSummaryRowLabel');
}
作者:KiwiJuice
项目:handball-dacha
/**
* @param DataTable|DataTable\Map $dataTable
* @param $fieldsToGet
* @param $sitesToProblablyAdd
*/
private function addMissingWebsites($dataTable, $fieldsToGet, $sitesToProblablyAdd)
{
$siteIdsInDataTable = array();
foreach ($dataTable->getRows() as $row) {
/** @var DataTable\Row $row */
$siteIdsInDataTable[] = $row->getColumn('label');
}
foreach ($sitesToProblablyAdd as $site) {
if (!in_array($site['idsite'], $siteIdsInDataTable)) {
$siteRow = array_combine($fieldsToGet, array_pad(array(), count($fieldsToGet), 0));
$siteRow['label'] = (int) $site['idsite'];
$dataTable->addRowFromSimpleArray($siteRow);
}
}
}
作者:FluentDevelopmen
项目:piwi
private function makeMergedTableWithPeriodAndSiteIndex($index, $resultIndices, $useSimpleDataTable, $isNumeric)
{
$map = new DataTable\Map();
$map->setKeyName($resultIndices[self::TABLE_METADATA_PERIOD_INDEX]);
// we save all tables of the map in this array to be able to add rows fast
$tables = array();
foreach ($this->periods as $range => $period) {
// as the resulting table is "merged", we do only set Period metedata and no metadata for site. Instead each
// row will have an idsite metadata entry.
$metadata = array(self::TABLE_METADATA_PERIOD_INDEX => $period);
if ($useSimpleDataTable) {
$table = new DataTable\Simple();
} else {
$table = new DataTable();
}
$table->setAllTableMetadata($metadata);
$map->addTable($table, $this->prettifyIndexLabel(self::TABLE_METADATA_PERIOD_INDEX, $range));
$tables[$range] = $table;
}
foreach ($index as $idsite => $table) {
$rowMeta = array('idsite' => $idsite);
foreach ($table as $range => $row) {
if (!empty($row)) {
$tables[$range]->addRow(new Row(array(Row::COLUMNS => $row, Row::METADATA => $rowMeta)));
} elseif ($isNumeric) {
$tables[$range]->addRow(new Row(array(Row::COLUMNS => $this->defaultRow, Row::METADATA => $rowMeta)));
}
}
}
return $map;
}
作者:brienomatt
项目:elmsl
/**
* Computes the output for the given data table array
*
* @param Map $table
* @param array $array
* @param string $prefixLines
* @return string
*/
protected function renderDataTableMap($table, $array, $prefixLines = "")
{
// CASE 1
//array
// 'day1' => string '14' (length=2)
// 'day2' => string '6' (length=1)
$firstTable = current($array);
if (!is_array($firstTable)) {
$xml = '';
$nameDescriptionAttribute = $table->getKeyName();
foreach ($array as $valueAttribute => $value) {
if (empty($value)) {
$xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\" />\n";
} elseif ($value instanceof Map) {
$out = $this->renderTable($value, true);
//TODO somehow this code is not tested, cover this case
$xml .= "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\">\n{$out}</result>\n";
} else {
$xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\">" . self::formatValueXml($value) . "</result>\n";
}
}
return $xml;
}
$subTables = $table->getDataTables();
$firstTable = current($subTables);
// CASE 2
//array
// 'day1' =>
// array
// 'nb_uniq_visitors' => string '18'
// 'nb_visits' => string '101'
// 'day2' =>
// array
// 'nb_uniq_visitors' => string '28'
// 'nb_visits' => string '11'
if ($firstTable instanceof Simple) {
$xml = '';
$nameDescriptionAttribute = $table->getKeyName();
foreach ($array as $valueAttribute => $dataTableSimple) {
if (count($dataTableSimple) == 0) {
$xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\" />\n";
} else {
if (is_array($dataTableSimple)) {
$dataTableSimple = "\n" . $this->renderDataTableSimple($dataTableSimple, $prefixLines . "\t") . $prefixLines . "\t";
}
$xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$valueAttribute}\">" . $dataTableSimple . "</result>\n";
}
}
return $xml;
}
// CASE 3
//array
// 'day1' =>
// array
// 0 =>
// array
// 'label' => string 'phpmyvisites'
// 'nb_uniq_visitors' => int 11
// 'nb_visits' => int 13
// 1 =>
// array
// 'label' => string 'phpmyvisits'
// 'nb_uniq_visitors' => int 2
// 'nb_visits' => int 2
// 'day2' =>
// array
// 0 =>
// array
// 'label' => string 'piwik'
// 'nb_uniq_visitors' => int 121
// 'nb_visits' => int 130
// 1 =>
// array
// 'label' => string 'piwik bis'
// 'nb_uniq_visitors' => int 20
// 'nb_visits' => int 120
if ($firstTable instanceof DataTable) {
$xml = '';
$nameDescriptionAttribute = $table->getKeyName();
foreach ($array as $keyName => $arrayForSingleDate) {
$dataTableOut = $this->renderDataTable($arrayForSingleDate, $prefixLines . "\t");
if (empty($dataTableOut)) {
$xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$keyName}\" />\n";
} else {
$xml .= $prefixLines . "\t<result {$nameDescriptionAttribute}=\"{$keyName}\">\n";
$xml .= $dataTableOut;
$xml .= $prefixLines . "\t</result>\n";
}
}
return $xml;
}
if ($firstTable instanceof Map) {
//.........这里部分代码省略.........
作者:diosmosi
项目:piwi
public function test_renderDataTable_shouldRenderDataTableMaps()
{
$map = new DataTable\Map();
$dataTable = new DataTable();
$dataTable->addRowFromSimpleArray(array('nb_visits' => 5, 'nb_random' => 10));
$dataTable2 = new DataTable();
$dataTable2->addRowFromSimpleArray(array('nb_visits' => 3, 'nb_random' => 6));
$map->addTable($dataTable, 'table1');
$map->addTable($dataTable2, 'table2');
$response = $this->builder->renderDataTable($map);
$this->assertEquals('defaultKeyName,nb_visits,nb_random
table1,5,10
table2,3,6', $response);
}
作者:cem
项目:piwi
public function test_hasDataTableUsers_shouldHandleADataTableMap()
{
$map = new DataTable\Map();
$map->addTable(new DataTable(), 'label1');
$map->addTable(new DataTable(), 'label2');
$map->addTable($this->getDataTableWithoutUsersColumn(), 'label3');
$this->assertNotDataTableHasUsers($map);
$map->addTable($this->getDataTableWithZeroUsers(), 'label4');
$map->addTable(new DataTable(), 'label5');
$this->assertNotDataTableHasUsers($map);
$map->addTable($this->getDataTableWithUsers(), 'label6');
$this->assertDataTableHasUsers($map);
}
作者:FluentDevelopmen
项目:piwi
private function generateOverviewReport($method, $idSite, $period, $date, $segment, array $defaultParams)
{
$tableManager = DataTable\Manager::getInstance();
/** @var DataTable[] $tables */
$tables = array();
foreach ($this->getOverviewReports() as $reportId => $reportParams) {
if (!empty($reportParams)) {
foreach ($defaultParams as $key => $defaultParam) {
if (!array_key_exists($key, $reportParams)) {
$reportParams[$key] = $defaultParam;
}
}
}
$firstTableId = $tableManager->getMostRecentTableId();
$table = $this->requestApiMethod($method, $idSite, $period, $date, $reportId, $segment, $reportParams);
$reportTableIds[] = $table->getId();
$tableManager->deleteTablesExceptIgnored($reportTableIds, $firstTableId);
$tables[] = $table;
}
$map = new DataTable\Map();
foreach ($tables as $table) {
$map->addTable($table, $table->getMetadata('reportName'));
}
return $map;
}
作者:jos
项目:CGE-File-Sharin
/**
* Computes the output of the given data table array
*
* @param DataTable\Map $table
* @param array $allColumns
* @return string
*/
protected function renderDataTableMap($table, &$allColumns = array())
{
$str = '';
foreach ($table->getDataTables() as $currentLinePrefix => $dataTable) {
$returned = explode("\n", $this->renderTable($dataTable, $allColumns));
// get rid of the columns names
$returned = array_slice($returned, 1);
// case empty datatable we dont print anything in the CSV export
// when in xml we would output <result date="2008-01-15" />
if (!empty($returned)) {
foreach ($returned as &$row) {
$row = $currentLinePrefix . $this->separator . $row;
}
$str .= "\n" . implode("\n", $returned);
}
}
// prepend table key to column list
$allColumns = array_merge(array($table->getKeyName() => true), $allColumns);
// add header to output string
$str = $this->getHeaderLine(array_keys($allColumns)) . $str;
return $str;
}