From f212864824030e5a35d6f82a2a880510720c20d3 Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@gmail.com>
Date: Fri, 11 Oct 2013 02:25:53 +0000
Subject: [PATCH] use visualizations hooks

---
 .../Visualizations/HtmlTable.php              | 121 ++++++++++--------
 .../Visualizations/HtmlTable/AllColumns.php   |  10 +-
 .../Visualizations/HtmlTable/Goals.php        |  10 +-
 .../Visualizations/JqplotGraph.php            |  25 +---
 .../Visualizations/JqplotGraph/Evolution.php  |   5 +-
 .../Visualizations/JqplotGraph/Pie.php        |  18 ---
 6 files changed, 81 insertions(+), 108 deletions(-)

diff --git a/plugins/CoreVisualizations/Visualizations/HtmlTable.php b/plugins/CoreVisualizations/Visualizations/HtmlTable.php
index 078dbba436..9ccfc751a4 100644
--- a/plugins/CoreVisualizations/Visualizations/HtmlTable.php
+++ b/plugins/CoreVisualizations/Visualizations/HtmlTable.php
@@ -20,6 +20,8 @@ use Piwik\Plugins\Goals\API as APIGoals;
 use Piwik\Site;
 use Piwik\View;
 use Piwik\ViewDataTable\Visualization;
+use Piwik\Visualization\Config as VizConfig;
+use Piwik\Visualization\Request;
 
 require_once PIWIK_INCLUDE_PATH . '/plugins/CoreVisualizations/Visualizations/HtmlTable/AllColumns.php';
 require_once PIWIK_INCLUDE_PATH . '/plugins/CoreVisualizations/Visualizations/HtmlTable/Goals.php';
@@ -149,25 +151,42 @@ class HtmlTable extends Visualization
         'highlight_summary_row',
     );
 
-    /**
-     * Init.
-     */
-    public function init()
+    public function configureVisualization(VizConfig $properties)
     {
-        $view = $this->viewDataTable;
-
         if (Common::getRequestVar('idSubtable', false)
-            && $view->visualization_properties->show_embedded_subtable
+            && $properties->visualization_properties->show_embedded_subtable
         ) {
-            $view->show_visualization_only = true;
+            $properties->show_visualization_only = true;
         }
 
-        if ($view->visualization_properties->show_extra_columns) {
-            $this->setShowExtraColumnsProperties($view);
+        if ($properties->visualization_properties->show_extra_columns) {
+            $properties->show_exclude_low_population = true;
+            $properties->datatable_css_class = 'dataTableVizAllColumns';
         }
 
-        if ($view->visualization_properties->show_goals_columns) {
-            $this->setShowGoalsColumnsProperties($view);
+        if ($properties->visualization_properties->show_goals_columns) {
+            $properties->datatable_css_class = 'dataTableVizGoals';
+            $properties->show_exclude_low_population = true;
+            $properties->show_goals = true;
+
+            $properties->translations += array(
+                'nb_conversions'    => Piwik::translate('Goals_ColumnConversions'),
+                'conversion_rate'   => Piwik::translate('General_ColumnConversionRate'),
+                'revenue'           => Piwik::translate('General_ColumnRevenue'),
+                'revenue_per_visit' => Piwik::translate('General_ColumnValuePerVisit'),
+            );
+            $properties->metrics_documentation['nb_visits'] = Piwik::translate('Goals_ColumnVisits');
+
+            if (Common::getRequestVar('documentationForGoalsPage', 0, 'int') == 1) { // TODO: should not use query parameter
+                $properties->documentation = Piwik::translate('Goals_ConversionByTypeReportDocumentation',
+                    array('<br />', '<br />', '<a href="http://piwik.org/docs/tracking-goals-web-analytics/" target="_blank">', '</a>'));
+            }
+
+            if (!$properties->visualization_properties->disable_subtable_when_show_goals) {
+                $properties->subtable_controller_action = null;
+            }
+
+            $this->setShowGoalsColumnsProperties();
         }
     }
 
@@ -200,59 +219,55 @@ class HtmlTable extends Visualization
         return $defaults;
     }
 
-    private function setShowExtraColumnsProperties($view)
+    /**
+     * @param DataTable|DataTable\Map $dataTable
+     * @param \Piwik\Visualization\Config $properties
+     * @param \Piwik\Visualization\Request $request
+     */
+    public function beforeGenericFiltersAreAppliedToLoadedDataTable($dataTable, VizConfig $properties, Request $request)
     {
-        $view->filters[] = array('AddColumnsProcessedMetrics', array(), $priority = true);
+        if ($properties->visualization_properties->show_extra_columns) {
+            $dataTable->filter(function ($dataTable) use ($properties) {
+                $columnsToDisplay = array('label', 'nb_visits');
 
-        $view->filters[] = function ($dataTable, $view) {
-            $columnsToDisplay = array('label', 'nb_visits');
+                if (in_array('nb_uniq_visitors', $dataTable->getColumns())) {
+                    $columnsToDisplay[] = 'nb_uniq_visitors';
+                }
 
-            if (in_array('nb_uniq_visitors', $dataTable->getColumns())) {
-                $columnsToDisplay[] = 'nb_uniq_visitors';
-            }
+                $columnsToDisplay = array_merge(
+                    $columnsToDisplay, array('nb_actions', 'nb_actions_per_visit', 'avg_time_on_site', 'bounce_rate')
+                );
 
-            $columnsToDisplay = array_merge(
-                $columnsToDisplay, array('nb_actions', 'nb_actions_per_visit', 'avg_time_on_site', 'bounce_rate')
-            );
+                // only display conversion rate for the plugins that do not provide "per goal" metrics
+                // otherwise, conversion rate is meaningless as a whole (since we don't process 'cross goals' conversions)
+                if (!$properties->show_goals) {
+                    $columnsToDisplay[] = 'conversion_rate';
+                }
 
-            // only display conversion rate for the plugins that do not provide "per goal" metrics
-            // otherwise, conversion rate is meaningless as a whole (since we don't process 'cross goals' conversions)
-            if (!$view->show_goals) {
-                $columnsToDisplay[] = 'conversion_rate';
-            }
-
-            $view->columns_to_display = $columnsToDisplay;
-        };
-
-        $prettifyTime = array('\Piwik\MetricsFormatter', 'getPrettyTimeFromSeconds');
-        $view->filters[] = array('ColumnCallbackReplace', array('avg_time_on_site', $prettifyTime));
+                $properties->columns_to_display = $columnsToDisplay;
+            });
 
-        $view->show_exclude_low_population = true;
+            $prettifyTime = array('\Piwik\MetricsFormatter', 'getPrettyTimeFromSeconds');
 
-        $view->datatable_css_class = 'dataTableVizAllColumns';
+            $dataTable->filter('ColumnCallbackReplace', array('avg_time_on_site', $prettifyTime));
+        }
     }
 
-    private function setShowGoalsColumnsProperties($view)
+    /**
+     * @param DataTable|DataTable\Map $dataTable
+     * @param \Piwik\Visualization\Config $properties
+     * @param \Piwik\Visualization\Request $request
+     */
+    public function afterAllFilteresAreApplied($dataTable, VizConfig $properties, Request $request)
     {
-        $view->datatable_css_class = 'dataTableVizGoals';
-        $view->show_exclude_low_population = true;
-        $view->show_goals = true;
-        $view->translations += array(
-            'nb_conversions'    => Piwik::translate('Goals_ColumnConversions'),
-            'conversion_rate'   => Piwik::translate('General_ColumnConversionRate'),
-            'revenue'           => Piwik::translate('General_ColumnRevenue'),
-            'revenue_per_visit' => Piwik::translate('General_ColumnValuePerVisit'),
-        );
-        $view->metrics_documentation['nb_visits'] = Piwik::translate('Goals_ColumnVisits');
-
-        if (Common::getRequestVar('documentationForGoalsPage', 0, 'int') == 1) { // TODO: should not use query parameter
-            $view->documentation = Piwik::translate('Goals_ConversionByTypeReportDocumentation',
-                array('<br />', '<br />', '<a href="http://piwik.org/docs/tracking-goals-web-analytics/" target="_blank">', '</a>'));
+        if ($properties->visualization_properties->show_extra_columns) {
+            $dataTable->filter('AddColumnsProcessedMetrics');
         }
+    }
 
-        if (!$view->visualization_properties->disable_subtable_when_show_goals) {
-            $view->subtable_controller_action = null;
-        }
+    private function setShowGoalsColumnsProperties()
+    {
+        $view = $this->viewDataTable;
 
         // set view properties based on goal requested
         $idSite = Common::getRequestVar('idSite', null, 'int');
diff --git a/plugins/CoreVisualizations/Visualizations/HtmlTable/AllColumns.php b/plugins/CoreVisualizations/Visualizations/HtmlTable/AllColumns.php
index 1abcdf9394..1b7fc1236b 100644
--- a/plugins/CoreVisualizations/Visualizations/HtmlTable/AllColumns.php
+++ b/plugins/CoreVisualizations/Visualizations/HtmlTable/AllColumns.php
@@ -12,6 +12,7 @@
 namespace Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
 
 use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
+use Piwik\Visualization\Config;
 
 /**
  * DataTable Visualization that derives from HtmlTable and sets show_extra_columns to true.
@@ -20,13 +21,10 @@ class AllColumns extends HtmlTable
 {
     const ID = 'tableAllColumns';
 
-    /**
-     * Init.
-     */
-    public function init()
+    public function configureVisualization(Config $properties)
     {
-        $this->viewDataTable->visualization_properties->show_extra_columns = true;
+        $properties->visualization_properties->show_extra_columns = true;
 
-        parent::init();
+        parent::configureVisualization($properties);
     }
 }
\ No newline at end of file
diff --git a/plugins/CoreVisualizations/Visualizations/HtmlTable/Goals.php b/plugins/CoreVisualizations/Visualizations/HtmlTable/Goals.php
index 469e7b2bcf..d158b85c68 100644
--- a/plugins/CoreVisualizations/Visualizations/HtmlTable/Goals.php
+++ b/plugins/CoreVisualizations/Visualizations/HtmlTable/Goals.php
@@ -12,6 +12,7 @@
 namespace Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
 
 use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
+use Piwik\Visualization\Config;
 
 /**
  * DataTable Visualization that derives from HtmlTable and sets show_goals_columns to true.
@@ -20,13 +21,10 @@ class Goals extends HtmlTable
 {
     const ID = 'tableGoals';
 
-    /**
-     * Init
-     */
-    public function init()
+    public function configureVisualization(Config $properties)
     {
-        $this->viewDataTable->visualization_properties->show_goals_columns = true;
+        $properties->visualization_properties->show_goals_columns = true;
 
-        parent::init();
+        parent::configureVisualization($properties);
     }
 }
\ No newline at end of file
diff --git a/plugins/CoreVisualizations/Visualizations/JqplotGraph.php b/plugins/CoreVisualizations/Visualizations/JqplotGraph.php
index c2b01ade1b..289b2a07ef 100644
--- a/plugins/CoreVisualizations/Visualizations/JqplotGraph.php
+++ b/plugins/CoreVisualizations/Visualizations/JqplotGraph.php
@@ -15,6 +15,8 @@ use Piwik\DataTable;
 use Piwik\Plugins\CoreVisualizations\JqplotDataGenerator;
 use Piwik\View;
 use Piwik\ViewDataTable\Graph;
+use Piwik\Visualization\Config;
+use Piwik\Visualization\Request;
 
 /**
  * DataTable visualization that displays DataTable data in a JQPlot graph.
@@ -58,29 +60,6 @@ class JqplotGraph extends Graph
 
     public static $overridableProperties = array('x_axis_step_size');
 
-    /**
-     * Init.
-     */
-    public function init()
-    {
-        $view = $this->viewDataTable;
-
-        parent::init();
-
-        // do not sort if sorted column was initially "label" or eg. it would make "Visits by Server time" not pretty
-        if ($view->filter_sort_column != 'label') {
-            $columns = $view->columns_to_display;
-
-            $firstColumn = reset($columns);
-            if ($firstColumn == 'label') {
-                $firstColumn = next($columns);
-            }
-
-            $result['filter_sort_column'] = $firstColumn;
-            $result['filter_sort_order'] = 'desc';
-        }
-    }
-
     /**
      * Returns an array mapping property names with default values for this visualization.
      *
diff --git a/plugins/CoreVisualizations/Visualizations/JqplotGraph/Evolution.php b/plugins/CoreVisualizations/Visualizations/JqplotGraph/Evolution.php
index 6f13e82a09..a6e7e51ca1 100644
--- a/plugins/CoreVisualizations/Visualizations/JqplotGraph/Evolution.php
+++ b/plugins/CoreVisualizations/Visualizations/JqplotGraph/Evolution.php
@@ -45,7 +45,7 @@ class Evolution extends JqplotGraph
     {
         parent::init();
 
-        $this->calculateEvolutionDateRange($this->viewDataTable);
+        $this->calculateEvolutionDateRange();
     }
 
     public function beforeLoadDataTable(Request $request, Config $properties)
@@ -102,8 +102,9 @@ class Evolution extends JqplotGraph
      * Based on the period, date and evolution_{$period}_last_n query parameters,
      * calculates the date range this evolution chart will display data for.
      */
-    private function calculateEvolutionDateRange(&$view)
+    private function calculateEvolutionDateRange()
     {
+        $view = $this->viewDataTable;
         $period = Common::getRequestVar('period');
 
         $defaultLastN = self::getDefaultLastN($period);
diff --git a/plugins/CoreVisualizations/Visualizations/JqplotGraph/Pie.php b/plugins/CoreVisualizations/Visualizations/JqplotGraph/Pie.php
index e8751b67a0..d4c217c16c 100644
--- a/plugins/CoreVisualizations/Visualizations/JqplotGraph/Pie.php
+++ b/plugins/CoreVisualizations/Visualizations/JqplotGraph/Pie.php
@@ -23,23 +23,6 @@ class Pie extends JqplotGraph
 {
     const ID = 'graphPie';
 
-    public function init()
-    {
-        parent::init();
-
-        $view->visualization_properties->show_all_ticks = true;
-        $view->datatable_js_type = 'JqplotPieGraphDataTable';
-
-        // make sure only one non-label column is displayed
-        $view->after_data_loaded_functions[] = function ($dataTable) use ($view) {
-            $metricColumn = reset($view->columns_to_display);
-            if ($metricColumn == 'label') {
-                $metricColumn = next($view->columns_to_display);
-            }
-            $view->columns_to_display = array($metricColumn ? : 'nb_visits');
-        };
-    }
-
     public function configureVisualization(Config $properties)
     {
         parent::configureVisualization($properties);
@@ -61,7 +44,6 @@ class Pie extends JqplotGraph
         $properties->columns_to_display = array($metricColumn ? : 'nb_visits');
     }
 
-
     public static function getDefaultPropertyValues()
     {
         $result = parent::getDefaultPropertyValues();
-- 
GitLab