From 2294b38e9342049322a7f858369cb78154255826 Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Wed, 2 Oct 2013 20:25:54 +0000 Subject: [PATCH] refs #3994 coding style, removed unused code --- plugins/ExampleUI/API.php | 37 +++++++++++------------ plugins/ExampleUI/Controller.php | 51 +++++++++++++++++--------------- plugins/ExampleUI/ExampleUI.php | 38 ++++++++---------------- plugins/ExampleUI/plugin.json | 2 +- 4 files changed, 58 insertions(+), 70 deletions(-) diff --git a/plugins/ExampleUI/API.php b/plugins/ExampleUI/API.php index d2128fa296c..968f393ec6a 100644 --- a/plugins/ExampleUI/API.php +++ b/plugins/ExampleUI/API.php @@ -23,52 +23,46 @@ */ class API { - static private $instance = null; + private static $instance = null; /** * @return \Piwik\Plugins\ExampleUI\API */ - static public function getInstance() + public static function getInstance() { if (self::$instance == null) { self::$instance = new self; } + return self::$instance; } public function getTemperaturesEvolution($date, $period) { + $temperatures = array(); $period = new Range($period, 'last30'); - $dateStart = $period->getDateStart()->toString('Y-m-d'); // eg. "2009-04-01" - $dateEnd = $period->getDateEnd()->toString('Y-m-d'); // eg. "2009-04-30" - - // here you could select from your custom table in the database, eg. - $query = "SELECT AVG(temperature) - FROM server_temperatures - WHERE date > ? - AND date < ? - GROUP BY date - ORDER BY date ASC"; - //$result = Db::fetchAll($query, array($dateStart, $dateEnd)); - // to keep things simple, we generate the data + foreach ($period->getSubperiods() as $subPeriod) { $server1 = mt_rand(50, 90); $server2 = mt_rand(40, 110); - $value = array('server1' => $server1, 'server2' => $server2); + $value = array('server1' => $server1, 'server2' => $server2); + $temperatures[$subPeriod->getLocalizedShortString()] = $value; } + return DataTable::makeFromIndexedArray($temperatures); } - // we generate an array of random server temperatures public function getTemperatures() { $xAxis = array( '0h', '1h', '2h', '3h', '4h', '5h', '6h', '7h', '8h', '9h', '10h', '11h', '12h', '13h', '14h', '15h', '16h', '17h', '18h', '19h', '20h', '21h', '22h', '23h', ); + $temperatureValues = array_slice(range(50, 90), 0, count($xAxis)); shuffle($temperatureValues); + $temperatures = array(); foreach ($xAxis as $i => $xAxisLabel) { $temperatures[$xAxisLabel] = $temperatureValues[$i]; @@ -89,17 +83,22 @@ public function getPlanetRatios() 'Uranus' => 4.007, 'Neptune' => 3.883, ); - // convert this array to a DataTable object + return DataTable::makeFromIndexedArray($planetRatios); } public function getPlanetRatiosWithLogos() { $planetsDataTable = $this->getPlanetRatios(); + foreach ($planetsDataTable->getRows() as $row) { - $row->addMetadata('logo', "plugins/ExampleUI/images/icons-planet/" . strtolower($row->getColumn('label') . ".png")); - $row->addMetadata('url', "http://en.wikipedia.org/wiki/" . $row->getColumn('label')); + $logo = sprintf('plugins/ExampleUI/images/icons-planet/%s.png', strtolower($row->getColumn('label'))); + $url = sprintf('http://en.wikipedia.org/wiki/%s', $row->getColumn('label')); + + $row->addMetadata('logo', $logo); + $row->addMetadata('url', $url); } + return $planetsDataTable; } } diff --git a/plugins/ExampleUI/Controller.php b/plugins/ExampleUI/Controller.php index 284266ed70a..4281240c5a7 100644 --- a/plugins/ExampleUI/Controller.php +++ b/plugins/ExampleUI/Controller.php @@ -22,14 +22,15 @@ class Controller extends \Piwik\Controller public function dataTables() { $view = ViewDataTable::factory('table', 'ExampleUI.getTemperatures', $controllerAction = 'ExampleUI.dataTables'); + $view->translations['value'] = "Temperature in °C"; $view->translations['label'] = "Hour of day"; - $view->filter_sort_column = 'label'; - $view->filter_sort_order = 'asc'; - $view->filter_limit = 24; + $view->filter_sort_column = 'label'; + $view->filter_sort_order = 'asc'; + $view->filter_limit = 24; + $view->y_axis_unit = '°C'; // useful if the user requests the bar graph $view->show_exclude_low_population = false; - $view->show_table_all_columns = false; - $view->y_axis_unit = '°C'; // useful if the user requests the bar graph + $view->show_table_all_columns = false; $view->visualization_properties->setForVisualization( 'Piwik\\Plugins\\CoreVisualizations\\Visualizations\\HtmlTable', 'disable_row_evolution', @@ -40,6 +41,7 @@ public function dataTables() 'max_graph_elements', 24 ); + echo $view->render(); } @@ -53,9 +55,11 @@ public function echoEvolutionGraph() { $view = ViewDataTable::factory( 'graphEvolution', 'ExampleUI.getTemperaturesEvolution', $controllerAction = 'ExampleUI.echoEvolutionGraph'); + + $view->y_axis_unit = '°C'; // useful if the user requests the bar graph $view->translations['server1'] = "Temperature server piwik.org"; $view->translations['server2'] = "Temperature server dev.piwik.org"; - $view->y_axis_unit = '°C'; // useful if the user requests the bar graph + echo $view->render(); } @@ -63,10 +67,12 @@ public function barGraph() { $view = ViewDataTable::factory( 'graphVerticalBar', 'ExampleUI.getTemperatures', $controllerAction = 'ExampleUI.barGraph'); - $view->translations['value'] = "Temperature"; + $view->y_axis_unit = '°C'; - $view->visualization_properties->max_graph_elements = 24; $view->show_footer = false; + $view->translations['value'] = "Temperature"; + $view->visualization_properties->max_graph_elements = 24; + echo $view->render(); } @@ -74,10 +80,12 @@ public function pieGraph() { $view = ViewDataTable::factory( 'graphPie', 'ExampleUI.getPlanetRatios', $controllerAction = 'ExampleUI.pieGraph'); - $view->columns_to_display = array('value'); + + $view->columns_to_display = array('value'); $view->translations['value'] = "times the diameter of Earth"; + $view->show_footer_icons = false; $view->visualization_properties->max_graph_elements = 10; - $view->show_footer_icons = false; + echo $view->render(); } @@ -98,9 +106,11 @@ public function echoSimpleTagClouds() { $view = ViewDataTable::factory( 'cloud', 'ExampleUI.getPlanetRatios', $controllerAction = 'ExampleUI.echoSimpleTagClouds'); - $view->columns_to_display = array('label', 'value'); + + $view->columns_to_display = array('label', 'value'); $view->translations['value'] = "times the diameter of Earth"; - $view->show_footer = false; + $view->show_footer = false; + echo $view->render(); } @@ -108,13 +118,15 @@ public function echoAdvancedTagClouds() { $view = ViewDataTable::factory( 'cloud', 'ExampleUI.getPlanetRatiosWithLogos', $controllerAction = 'ExampleUI.echoAdvancedTagClouds'); + $view->visualization_properties->setForVisualization( 'Piwik\\Plugins\\CoreVisualizations\\Visualizations\\Cloud', 'display_logo_instead_of_label', true ); - $view->columns_to_display = array('label', 'value'); + $view->columns_to_display = array('label', 'value'); $view->translations['value'] = "times the diameter of Earth"; + echo $view->render(); } @@ -123,6 +135,7 @@ public function sparklines() $view = new View('@ExampleUI/sparklines'); $view->urlSparkline1 = $this->getUrlSparkline('generateSparkline', array('server' => 'server1', 'rand' => mt_rand())); $view->urlSparkline2 = $this->getUrlSparkline('generateSparkline', array('server' => 'server2', 'rand' => mt_rand())); + echo $view->render(); } @@ -132,20 +145,10 @@ public function generateSparkline() 'sparkline', 'ExampleUI.getTemperaturesEvolution', $controllerAction = 'ExampleUI.generateSparkline'); $serverRequested = Common::getRequestVar('server', false); - if ($serverRequested !== false) { + if (false !== $serverRequested) { $view->columns_to_display = array($serverRequested); } echo $view->render(); } - - // Example use - private function echoDataTableSearchEnginesFiltered() - { - $view = $this->getLastUnitGraph($this->pluginName, __FUNCTION__, 'Referers.getSearchEngines'); - $view->columns_to_display = array('nb_visits'); - $view->filter_pattern = '^(Google|Yahoo!)$'; - $view->filter_column = 'label'; - return $this->renderView($view); - } } diff --git a/plugins/ExampleUI/ExampleUI.php b/plugins/ExampleUI/ExampleUI.php index 97cf9f2bc2c..2c4e1e9967e 100644 --- a/plugins/ExampleUI/ExampleUI.php +++ b/plugins/ExampleUI/ExampleUI.php @@ -9,20 +9,9 @@ * @package ExampleUI */ -/* -- prepare a page with all use cases -- test the actions datatable in this page? -- test datatable with search disabled -- test datatable with low population disabled -- without footer -- without all columns icon -+ update http://piwik.org/participate/user-interface -*/ namespace Piwik\Plugins\ExampleUI; - /** - * * @package ExampleUI */ class ExampleUI extends \Piwik\Plugin @@ -32,27 +21,24 @@ class ExampleUI extends \Piwik\Plugin */ public function getListHooksRegistered() { - $hooks = array( + return array( 'Menu.Reporting.addItems' => 'addMenus', ); - return $hooks; } function addMenus() { - $menus = array( - 'Data tables' => 'dataTables', - 'Evolution graph' => 'evolutionGraph', - 'Bar graph' => 'barGraph', - 'Pie graph' => 'pieGraph', - 'Tag clouds' => 'tagClouds', - 'Sparklines' => 'sparklines', - ); - Piwik_AddMenu('UI Framework', '', array('module' => 'ExampleUI', 'action' => 'dataTables'), true, 30); - $order = 1; - foreach ($menus as $subMenu => $action) { - Piwik_AddMenu('UI Framework', $subMenu, array('module' => 'ExampleUI', 'action' => $action), true, $order++); - } + + $this->addSubMenu('Data tables', 'dataTables', 1); + $this->addSubMenu('Bar graph', 'barGraph', 2); + $this->addSubMenu('Pie graph', 'pieGraph', 3); + $this->addSubMenu('Tag clouds', 'tagClouds', 4); + $this->addSubMenu('Sparklines', 'sparklines', 5); + } + + private function addSubMenu($subMenu, $action, $order) + { + Piwik_AddMenu('UI Framework', $subMenu, array('module' => 'ExampleUI', 'action' => $action), true, $order); } } diff --git a/plugins/ExampleUI/plugin.json b/plugins/ExampleUI/plugin.json index 66c4611c54f..0e66982e35c 100644 --- a/plugins/ExampleUI/plugin.json +++ b/plugins/ExampleUI/plugin.json @@ -1,7 +1,7 @@ { "name": "ExampleUI", "description": "Example Plugin: This plugin showcases the Piwik User Interface framework, how to easily display custom data tables, graphs, and more.", - "version": "1.0", + "version": "1.0.1", "keywords": ["example", "framework", "platform", "ui", "visualization"], "homepage": "http://piwik.org", "license": "GPL-3.0+",