Skip to content

Commit

Permalink
refs #3994 coding style, removed unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Oct 2, 2013
1 parent f97b134 commit 2294b38
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 70 deletions.
37 changes: 18 additions & 19 deletions plugins/ExampleUI/API.php
Expand Up @@ -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];
Expand All @@ -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;
}
}
51 changes: 27 additions & 24 deletions plugins/ExampleUI/Controller.php
Expand Up @@ -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',
Expand All @@ -40,6 +41,7 @@ public function dataTables()
'max_graph_elements',
24
);

echo $view->render();
}

Expand All @@ -53,31 +55,37 @@ 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();
}

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();
}

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();
}

Expand All @@ -98,23 +106,27 @@ 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();
}

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();
}

Expand All @@ -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();
}

Expand All @@ -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);
}
}
38 changes: 12 additions & 26 deletions plugins/ExampleUI/ExampleUI.php
Expand Up @@ -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
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion 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+",
Expand Down

0 comments on commit 2294b38

Please sign in to comment.