Skip to content

Commit

Permalink
Refs #4278 Removing transformMetadata()
Browse files Browse the repository at this point in the history
  • Loading branch information
mattab committed Nov 18, 2013
1 parent 8972c29 commit 9471ce3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 46 deletions.
112 changes: 67 additions & 45 deletions core/Archive/DataTableFactory.php
Expand Up @@ -152,7 +152,7 @@ public function make($index, $resultIndices)

$dataTable = $this->createDataTable($index, $keyMetadata = array());
} else {
$dataTable = $this->createDataTableMapFromIndex($index, $resultIndices);
$dataTable = $this->createDataTableMapFromIndex($index, $resultIndices, $keyMetadata = array());
}

$this->transformMetadata($dataTable);
Expand Down Expand Up @@ -287,45 +287,9 @@ private function createDataTable($data, $keyMetadata)
if ($this->dataType == 'blob') {
$result = $this->makeFromBlobRow($data);
} else {
$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) {
$name = reset($this->dataNames);
$table->addRow(new Row(array(Row::COLUMNS => array($name => 0))));
}
}

$result = $table;
}

if (!isset($keyMetadata[DataTableFactory::TABLE_METADATA_SITE_INDEX])) {
$keyMetadata[DataTableFactory::TABLE_METADATA_SITE_INDEX] = reset($this->sitesId);
}

if (!isset($keyMetadata[DataTableFactory::TABLE_METADATA_PERIOD_INDEX])) {
reset($this->periods);
$keyMetadata[DataTableFactory::TABLE_METADATA_PERIOD_INDEX] = key($this->periods);
$result = $this->makeFromMetricsArray($data);
}

// Note: $result can be a DataTable\Map
$result->filter(function ($table) use ($keyMetadata) {
foreach ($keyMetadata as $name => $value) {
$table->setMetadata($name, $value);
}
});

$this->setTableMetadata($keyMetadata, $result);
return $result;
}

Expand Down Expand Up @@ -384,11 +348,12 @@ private function setSubtables($dataTable, $blobRow, $treeLevel = 0)
*/
private function transformMetadata($table)
{
$periods = $this->periods;
$table->filter(function ($table) use ($periods) {
$table->setMetadata(DataTableFactory::TABLE_METADATA_SITE_INDEX, new Site($table->getMetadata(DataTableFactory::TABLE_METADATA_SITE_INDEX)));
$table->setMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX, $periods[$table->getMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX)]);
});
return;
// $periods = $this->periods;
// $table->filter(function ($table) use ($periods) {
// $table->setMetadata(DataTableFactory::TABLE_METADATA_SITE_INDEX, new Site($table->getMetadata(DataTableFactory::TABLE_METADATA_SITE_INDEX)));
// $table->setMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX, $periods[$table->getMetadata(DataTableFactory::TABLE_METADATA_PERIOD_INDEX)]);
// });
}

/**
Expand All @@ -405,4 +370,61 @@ private function prettifyIndexLabel($labelType, $label)
}
return $label;
}
}

/**
* @param $keyMetadata
* @param $result
*/
private function setTableMetadata($keyMetadata, $result)
{
if (!isset($keyMetadata[DataTableFactory::TABLE_METADATA_SITE_INDEX])) {
$idSite = reset($this->sitesId);
$keyMetadata[DataTableFactory::TABLE_METADATA_SITE_INDEX] = new Site($idSite);
}

if (!isset($keyMetadata[DataTableFactory::TABLE_METADATA_PERIOD_INDEX])) {
reset($this->periods);
$keyMetadata[DataTableFactory::TABLE_METADATA_PERIOD_INDEX] = key($this->periods);
}

// Note: $result can be a DataTable\Map
$result->filter(function ($table) use ($keyMetadata) {
foreach ($keyMetadata as $name => $value) {
$table->setMetadata($name, $value);
}
});
}

/**
* @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;
}
}

2 changes: 1 addition & 1 deletion plugins/CoreHome/templates/_headerMessage.twig
Expand Up @@ -30,7 +30,7 @@
{{ 'General_PiwikXIsAvailablePleaseNotifyPiwikAdmin'|translate("<a href='?module=Proxy&action=redirect&url=http://piwik.org/' target='_blank'>Piwik</a> <a href='?module=Proxy&action=redirect&url=http://piwik.org/changelog/' target='_blank'>{{ latest_version_available }}</a>")|raw }}
{% endif %}
{% elseif not isPiwikDemo %}
{{ 'General_PiwikIsACollaborativeProjectYo uCanContributeAndDonate'|translate(
{{ 'General_PiwikIsACollaborativeProjectYouCanContributeAndDonate'|translate(
"<a href='?module=Proxy&action=redirect&url=http://piwik.org' target='_blank'>",
piwik_version~"</a>",
"<br />",
Expand Down

0 comments on commit 9471ce3

Please sign in to comment.