From 426ad3f04e5f7b6e0e9d56331b9502afa2037341 Mon Sep 17 00:00:00 2001
From: BeezyT <timo@ezdesign.de>
Date: Tue, 8 Nov 2011 14:13:00 +0000
Subject: [PATCH] refs #1454 API.get has no plugin prefix for metrics anymore

git-svn-id: http://dev.piwik.org/svn/trunk@5414 59fd770c-687e-43c8-a1e3-f5a4ff64c105
---
 core/Controller.php                           | 53 +++++--------------
 plugins/API/API.php                           | 40 ++++++++------
 plugins/CoreHome/templates/jqplot.js          |  6 +++
 plugins/VisitsSummary/Controller.php          | 41 +++++++++-----
 plugins/VisitsSummary/VisitsSummary.php       |  2 +-
 tests/integration/Main.test.php               |  3 +-
 ...rceOrderWithItems_API_get__API.get_day.xml |  8 +--
 ...ceOrderWithItems_API_get__API.get_week.xml |  8 +--
 8 files changed, 84 insertions(+), 77 deletions(-)

diff --git a/core/Controller.php b/core/Controller.php
index dbc7e84114..55f4f00f06 100644
--- a/core/Controller.php
+++ b/core/Controller.php
@@ -186,9 +186,9 @@ abstract class Piwik_Controller
 	 * 
 	 * @param string $currentModuleName
 	 * @param string $currentControllerAction
-	 * @param array $columnsToDisplay in a format like ["VisitsSummary.nb_visits","Actions.nb_uniq_pageviews"]
-	 * @param array $selectableColumns in the same format as the parameter above
-	 * @param string $reportDocumentation the documentation to set on the report
+	 * @param array $columnsToDisplay
+	 * @param array $selectableColumns
+	 * @param string $reportDocumentation
 	 * @return Piwik_ViewDataTable_GenerateGraphHTML_ChartEvolution
 	 */
 	protected function getLastUnitGraphAcrossPlugins($currentModuleName, $currentControllerAction,
@@ -203,53 +203,28 @@ abstract class Piwik_Controller
 		
 		$_GET['columns'] = implode(',', $columnsToDisplay);
 		
-		// split the requested columns between the plugins
-		$columnsPerPlugin = array();
-		foreach (array_merge($columnsToDisplay, $selectableColumns) as $column)
-		{
-			@list($plugin, $col) = explode('.', $column);
-			$columnsPerPlugin[$plugin][] = $col;
-		}
-		
-		// load meta data for the requested plugins
+		// load translations from meta data
 		$idSite = Piwik_Common::getRequestVar('idSite');
 		$period = Piwik_Common::getRequestVar('period');
 		$date = Piwik_Common::getRequestVar('date');
-		$meta = array();
-		foreach ($columnsPerPlugin as $plugin => $columns)
-		{
-			$meta[$plugin] = Piwik_API_API::getInstance()->getMetadata(
-					$idSite, $plugin, 'get', array(), false, $period, $date);
-			$meta[$plugin] = &$meta[$plugin][0];
-		}
+		$meta = Piwik_API_API::getInstance()->getReportMetadata($idSite, $period, $date);
 		
-		// handle wildcards like VisitsSummary.* in $selectableColumns
-		foreach ($selectableColumns as $i => $column)
+		$columns = array_merge($columnsToDisplay, $selectableColumns);
+		$translations = array();
+		foreach ($meta as $reportMeta)
 		{
-			if (substr($column, -2) == '.*')
+			if ($reportMeta['action'] == 'get' && !isset($reportMeta['parameters']))
 			{
-				$plugin = substr($column, 0, -2);
-				unset($selectableColumns[$i]);
-				$columnsPerPlugin[$plugin] = array();
-				foreach ($meta[$plugin]['metrics'] as $column => $translation)
+				foreach ($columns as $column)
 				{
-					$selectableColumns[] = $plugin.'.'.$column;
-					$columnsPerPlugin[$plugin][] = $column;
+					if (isset($reportMeta['metrics'][$column]))
+					{
+						$translations[$column] = $reportMeta['metrics'][$column];
+					}
 				}
 			}
 		}
 		
-		// get metrics translations from meta data
-		$translations = array();
-		foreach ($columnsPerPlugin as $plugin => $columns)
-		{
-			foreach ($columns as $column)
-			{
-				$translations[$plugin.'.'.$column] = isset($meta[$plugin]['metrics'][$column]) ? 
-						$meta[$plugin]['metrics'][$column] : $column;
-			}
-		}
-		
 		// initialize the graph and load the data
 		$view = $this->getLastUnitGraph($currentModuleName, $currentControllerAction, 'API.get');
 		$view->setColumnsToDisplay($columnsToDisplay);
diff --git a/plugins/API/API.php b/plugins/API/API.php
index 4877adb4b3..51a2e05d83 100644
--- a/plugins/API/API.php
+++ b/plugins/API/API.php
@@ -767,22 +767,35 @@ class Piwik_API_API
 	}
 	
 	
-	/**
-	 * Get a combined report of the *.get API methods.
-	 * $columns has a format like "VisitsSummary.nb_visits,Actions.nb_uniq_pageviews".
-	 * In the example above, the metric nb_visits from VisitsSummary.get and the metric
-	 * nb_uniq_pageviews from Actions.get will be returned.
-	 */
+	/** Get a combined report of the *.get API methods. */
 	public function get( $idSite, $period, $date, $segment = false, $columns = false)
 	{
-		// get the columns that are requested per plugin
 		$columns = Piwik::getArrayFromApiParameter($columns);
-		$columnsByPlugin = array();
-		$columnNameMap = array();
+		
+		// build columns map for faster checks later on
+		$columnsMap = array();
 		foreach ($columns as $column) {
-			list($plugin, $metric) = explode('.', $column);
-			$columnsByPlugin[$plugin][] = $metric;
-			$columnNameMap[$plugin][$metric] = $plugin.'.'.$metric;
+			$columnsMap[$column] = true;
+		}
+		
+		// find out which columns belong to which plugin
+		$columnsByPlugin = array();
+		$meta = Piwik_API_API::getInstance()->getReportMetadata($idSite, $period, $date);
+		foreach ($meta as $reportMeta)
+		{
+			// scan all *.get reports
+			if ($reportMeta['action'] == 'get' && !isset($reportMeta['parameters']))
+			{
+				$plugin = $reportMeta['module'];
+				foreach ($reportMeta['metrics'] as $column => $columnTranslation)
+				{
+					// a metric from this report has been requested
+					if (isset($columnsMap[$column]))
+					{
+						$columnsByPlugin[$plugin][] = $column;
+					}
+				}
+			}
 		}
 		
 		$mergedDataTable = false;
@@ -814,9 +827,6 @@ class Piwik_API_API
 				}
 			}
 			
-			// prefix columns with plugin name
-			$dataTable->filter('ReplaceColumnNames', array($columnNameMap[$plugin]));
-			
 			// merge reports
 			if ($mergedDataTable === false)
 			{
diff --git a/plugins/CoreHome/templates/jqplot.js b/plugins/CoreHome/templates/jqplot.js
index ccc49cebdc..83c36dacb7 100644
--- a/plugins/CoreHome/templates/jqplot.js
+++ b/plugins/CoreHome/templates/jqplot.js
@@ -847,6 +847,12 @@ JQPlot.prototype = {
 			return;
 		}
 		
+		if ($('#dashboard').size() > 0) {
+			// don't display picker in dashboard
+			// it would be cut off by overflow:hidden containers
+			return;
+		}
+		
 		// initialize dom element
 		picker.domElem = $(document.createElement('a'))
 			.addClass('jqplot-seriespicker')
diff --git a/plugins/VisitsSummary/Controller.php b/plugins/VisitsSummary/Controller.php
index 8465be2e19..08c41ad59f 100644
--- a/plugins/VisitsSummary/Controller.php
+++ b/plugins/VisitsSummary/Controller.php
@@ -20,8 +20,7 @@ class Piwik_VisitsSummary_Controller extends Piwik_Controller
 	{
 		$view = Piwik_View::factory('index');
 		$this->setPeriodVariablesView($view);
-		$view->graphEvolutionVisitsSummary = $this->getEvolutionGraph( true, 
-				array('VisitsSummary.nb_visits', 'VisitsSummary.nb_uniq_visitors') );
+		$view->graphEvolutionVisitsSummary = $this->getEvolutionGraph( true, array('nb_visits') );
 		$this->setSparklinesAndNumbers($view);
 		echo $view->render();
 	}
@@ -42,7 +41,7 @@ class Piwik_VisitsSummary_Controller extends Piwik_Controller
 			$columns = Piwik::getArrayFromApiParameter($columns);
 		}
 		
-		$doc = Piwik_Translate('VisitsSummary_VisitsSummaryDocumentation').'<br />'
+		$documentation = Piwik_Translate('VisitsSummary_VisitsSummaryDocumentation').'<br />'
 		     . Piwik_Translate('General_BrokenDownReportDocumentation').'<br /><br />'
 		     
 		     . '<b>'.Piwik_Translate('General_ColumnNbVisits').':</b> '
@@ -57,8 +56,26 @@ class Piwik_VisitsSummary_Controller extends Piwik_Controller
 		     . '<b>'.Piwik_Translate('General_ColumnActionsPerVisit').':</b> '
 		     . Piwik_Translate('General_ColumnActionsPerVisitDocumentation');
 		
+		$selectableColumns = array(
+			// columns from VisitsSummary.get
+			'nb_visits',
+			'nb_uniq_visitors',
+			'avg_time_on_site',
+			'bounce_rate',
+			'nb_actions_per_visit',
+			'max_actions',
+			'nb_visits_converted',
+			// columns from Actions.get
+			'nb_pageviews',
+			'nb_uniq_pageviews',
+			'nb_downloads',
+			'nb_uniq_downloads',
+			'nb_outlinks',
+			'nb_uniq_outlinks'
+		);
+		
 		$view = $this->getLastUnitGraphAcrossPlugins($this->pluginName, __FUNCTION__, $columns, 
-							$selectableColumns = array('VisitsSummary.*', 'Actions.*'), $doc);
+							$selectableColumns, $documentation);
 		
 		return $this->renderView($view, $fetch);
 	}
@@ -85,14 +102,14 @@ class Piwik_VisitsSummary_Controller extends Piwik_Controller
 	
 	protected function setSparklinesAndNumbers($view)
 	{
-		$view->urlSparklineNbVisits 		= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => $view->displayUniqueVisitors ? array('VisitsSummary.nb_visits', 'VisitsSummary.nb_uniq_visitors') : array('VisitsSummary.nb_visits')));
-		$view->urlSparklineNbPageviews 		= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('Actions.nb_pageviews', 'Actions.nb_uniq_pageviews')));
-		$view->urlSparklineNbDownloads 	    = $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('Actions.nb_downloads', 'Actions.nb_uniq_downloads')));
-		$view->urlSparklineNbOutlinks 		= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('Actions.nb_outlinks', 'Actions.nb_uniq_outlinks')));
-		$view->urlSparklineAvgVisitDuration = $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('VisitsSummary.avg_time_on_site')));
-		$view->urlSparklineMaxActions 		= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('VisitsSummary.max_actions')));
-		$view->urlSparklineActionsPerVisit 	= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('VisitsSummary.nb_actions_per_visit')));
-		$view->urlSparklineBounceRate 		= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('VisitsSummary.bounce_rate')));
+		$view->urlSparklineNbVisits 		= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => $view->displayUniqueVisitors ? array('nb_visits', 'nb_uniq_visitors') : array('nb_visits')));
+		$view->urlSparklineNbPageviews 		= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('nb_pageviews', 'nb_uniq_pageviews')));
+		$view->urlSparklineNbDownloads 	    = $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('nb_downloads', 'nb_uniq_downloads')));
+		$view->urlSparklineNbOutlinks 		= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('nb_outlinks', 'nb_uniq_outlinks')));
+		$view->urlSparklineAvgVisitDuration = $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('avg_time_on_site')));
+		$view->urlSparklineMaxActions 		= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('max_actions')));
+		$view->urlSparklineActionsPerVisit 	= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('nb_actions_per_visit')));
+		$view->urlSparklineBounceRate 		= $this->getUrlSparkline( 'getEvolutionGraph', array('columns' => array('bounce_rate')));
 		
 		$dataTableVisit = self::getVisitsSummary();
 		$dataRow = $dataTableVisit->getFirstRow();
diff --git a/plugins/VisitsSummary/VisitsSummary.php b/plugins/VisitsSummary/VisitsSummary.php
index 5a7b708a1d..e1ce17d21a 100644
--- a/plugins/VisitsSummary/VisitsSummary.php
+++ b/plugins/VisitsSummary/VisitsSummary.php
@@ -67,7 +67,7 @@ class Piwik_VisitsSummary extends Piwik_Plugin
 	
 	function addWidgets()
 	{
-		Piwik_AddWidget( 'VisitsSummary_VisitsSummary', 'VisitsSummary_WidgetLastVisits', 'VisitsSummary', 'getEvolutionGraph', array('columns' => array('VisitsSummary.nb_visits')));
+		Piwik_AddWidget( 'VisitsSummary_VisitsSummary', 'VisitsSummary_WidgetLastVisits', 'VisitsSummary', 'getEvolutionGraph', array('columns' => array('nb_visits')));		
 		Piwik_AddWidget( 'VisitsSummary_VisitsSummary', 'VisitsSummary_WidgetVisits', 'VisitsSummary', 'getSparklines');
 		Piwik_AddWidget( 'VisitsSummary_VisitsSummary', 'VisitsSummary_WidgetOverviewGraph', 'VisitsSummary', 'index');
 	}
diff --git a/tests/integration/Main.test.php b/tests/integration/Main.test.php
index 71e5935e48..3e7ddba98d 100644
--- a/tests/integration/Main.test.php
+++ b/tests/integration/Main.test.php
@@ -272,8 +272,7 @@ class Test_Piwik_Integration_Main extends Test_Integration
 				$periods = array('day', 'week'), $setDateLastN = false, $language = false, 
 				$segment = false, $visitorId = false, $abandonedCarts = false, $idGoal = false, 
 				$apiModule = false, $apiAction = false, $otherParams = array(
-					'columns' => 'Actions.nb_pageviews,VisitsSummary.nb_visits,'
-						.'VisitsSummary.avg_time_on_site,Goals.nb_visits_converted'));
+					'columns' => 'nb_pageviews,nb_visits,avg_time_on_site,nb_visits_converted'));
 		
         // Website2
 		$this->setApiToCall( array('Goals.get', 'Goals.getItemsSku', 'Goals.getItemsName', 'Goals.getItemsCategory'	) );
diff --git a/tests/integration/expected/test_ecommerceOrderWithItems_API_get__API.get_day.xml b/tests/integration/expected/test_ecommerceOrderWithItems_API_get__API.get_day.xml
index 3b3ea87298..95f6f614b0 100644
--- a/tests/integration/expected/test_ecommerceOrderWithItems_API_get__API.get_day.xml
+++ b/tests/integration/expected/test_ecommerceOrderWithItems_API_get__API.get_day.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8" ?>
 <result>
-	<Actions.nb_pageviews>13</Actions.nb_pageviews>
-	<VisitsSummary.nb_visits>3</VisitsSummary.nb_visits>
-	<VisitsSummary.avg_time_on_site>1801</VisitsSummary.avg_time_on_site>
-	<Goals.nb_visits_converted>2</Goals.nb_visits_converted>
+	<nb_visits>3</nb_visits>
+	<avg_time_on_site>1801</avg_time_on_site>
+	<nb_pageviews>13</nb_pageviews>
+	<nb_visits_converted>2</nb_visits_converted>
 </result>
\ No newline at end of file
diff --git a/tests/integration/expected/test_ecommerceOrderWithItems_API_get__API.get_week.xml b/tests/integration/expected/test_ecommerceOrderWithItems_API_get__API.get_week.xml
index fe3df7381e..75b96fb22b 100644
--- a/tests/integration/expected/test_ecommerceOrderWithItems_API_get__API.get_week.xml
+++ b/tests/integration/expected/test_ecommerceOrderWithItems_API_get__API.get_week.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8" ?>
 <result>
-	<Actions.nb_pageviews>16</Actions.nb_pageviews>
-	<VisitsSummary.nb_visits>5</VisitsSummary.nb_visits>
-	<VisitsSummary.avg_time_on_site>1369</VisitsSummary.avg_time_on_site>
-	<Goals.nb_visits_converted>4</Goals.nb_visits_converted>
+	<nb_visits>5</nb_visits>
+	<avg_time_on_site>1369</avg_time_on_site>
+	<nb_pageviews>16</nb_pageviews>
+	<nb_visits_converted>4</nb_visits_converted>
 </result>
\ No newline at end of file
-- 
GitLab