Skip to content

Commit

Permalink
Refs #4040, #4041, removed ViewDataTable::init & moved default proper…
Browse files Browse the repository at this point in the history
…ty values for core properties to Properties class.
  • Loading branch information
Benaka Moorthi committed Aug 5, 2013
1 parent 9e19f24 commit be35dc4
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 157 deletions.
5 changes: 3 additions & 2 deletions core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ protected function renderView(ViewDataTable $view, $fetch = false)
*/
protected function getLastUnitGraph($currentModuleName, $currentControllerAction, $apiMethod)
{
$view = ViewDataTable::factory('graphEvolution');
$view->init($currentModuleName, $currentControllerAction, $apiMethod);
$view = ViewDataTable::factory(
'graphEvolution', $apiMethod, $currentModuleName . '.' . $currentControllerAction, $forceDefault = true);
$view->show_goals = false;
return $view;
}

Expand Down
209 changes: 64 additions & 145 deletions core/ViewDataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,67 +124,55 @@ class ViewDataTable
/**
* Default constructor.
*/
public function __construct($visualizationClass = null)
public function __construct($currentControllerAction,
$apiMethodToRequestDataTable,
$viewProperties = array(),
$visualizationId = null)
{
$visualizationClass = $visualizationId ? DataTableVisualization::getClassFromId($visualizationId) : null;
$this->visualizationClass = $visualizationClass;

$this->viewProperties['visualization_properties'] = new VisualizationPropertiesProxy($visualizationClass);
$this->viewProperties['datatable_template'] = '@CoreHome/_dataTable';
$this->viewProperties['show_goals'] = false;
$this->viewProperties['show_ecommerce'] = false;
$this->viewProperties['show_search'] = true;
$this->viewProperties['show_table'] = true;
$this->viewProperties['show_table_all_columns'] = true;
$this->viewProperties['show_all_views_icons'] = true;
$this->viewProperties['show_active_view_icon'] = true;
$this->viewProperties['show_bar_chart'] = true;
$this->viewProperties['show_pie_chart'] = true;
$this->viewProperties['show_tag_cloud'] = true;
$this->viewProperties['show_export_as_image_icon'] = false;
$this->viewProperties['show_export_as_rss_feed'] = true;
$this->viewProperties['show_exclude_low_population'] = true;
$this->viewProperties['show_offset_information'] = true;
$this->viewProperties['show_pagination_control'] = true;
$this->viewProperties['show_limit_control'] = false;
$this->viewProperties['show_footer'] = true;
$this->viewProperties['show_related_reports'] = true;
$this->viewProperties['exportLimit'] = Config::getInstance()->General['API_datatable_default_limit'];
$this->viewProperties['highlight_summary_row'] = false;
$this->viewProperties['metadata'] = array();
$this->viewProperties['related_reports'] = array();
$this->viewProperties['title'] = 'unknown';
$this->viewProperties['tooltip_metadata_name'] = false;
$this->viewProperties['enable_sort'] = true;
$this->viewProperties['disable_generic_filters'] = false;
$this->viewProperties['disable_queued_filters'] = false;
$this->viewProperties['keep_summary_row'] = false;
$this->viewProperties['filter_excludelowpop'] = false;
$this->viewProperties['filter_excludelowpop_value'] = false;
$this->viewProperties['filter_pattern'] = false;
$this->viewProperties['filter_column'] = false;
$this->viewProperties['filter_limit'] = false;
$this->viewProperties['filter_sort_column'] = false;
$this->viewProperties['filter_sort_order'] = false;
$this->viewProperties['custom_parameters'] = array();
$this->viewProperties['translations'] = array_merge(
Metrics::getDefaultMetrics(),
Metrics::getDefaultProcessedMetrics()
);
$this->viewProperties['request_parameters_to_modify'] = array();
$this->viewProperties['documentation'] = false;
$this->viewProperties['subtable_controller_action'] = false;
$this->viewProperties['datatable_css_class'] = false;
$this->viewProperties['translations'] = array();
$this->viewProperties['filters'] = array();
$this->viewProperties['hide_annotations_view'] = true;
$this->viewProperties['columns_to_display'] = array();
$this->viewProperties['related_reports'] = array();

$this->setDefaultProperties();

list($currentControllerName, $currentControllerAction) = explode('.', $currentControllerAction);
$this->currentControllerName = $currentControllerName;
$this->currentControllerAction = $currentControllerAction;

foreach ($viewProperties as $name => $value) {
$this->setViewProperty($name, $value);
}

$queryParams = Url::getArrayFromCurrentQueryString();
foreach ($this->getClientSideProperties() as $name) {
if (isset($queryParams[$name])) {
$this->setViewProperty($name, $queryParams[$name]);
}
}

$this->idSubtable = Common::getRequestVar('idSubtable', false, 'int');
$this->viewProperties['show_footer_icons'] = ($this->idSubtable == false);
$this->viewProperties['apiMethodToRequestDataTable'] = $apiMethodToRequestDataTable;

$this->viewProperties['report_id'] = $currentControllerName . '.' . $currentControllerAction;
$this->viewProperties['self_url'] = $this->getBaseReportUrl($currentControllerName, $currentControllerAction);

$columns = Common::getRequestVar('columns', false);
if ($columns !== false) {
$this->viewProperties['columns_to_display'] = Piwik::getArrayFromApiParameter($columns);
array_unshift($this->viewProperties['columns_to_display'], 'label');
// the exclude low population threshold value is sometimes obtained by requesting data.
// to avoid issuing unecessary requests when display properties are determined by metadata,
// we allow it to be a closure.
if (isset($this->viewProperties['filter_excludelowpop_value'])
&& $this->viewProperties['filter_excludelowpop_value'] instanceof \Closure
) {
$function = $this->viewProperties['filter_excludelowpop_value'];
$this->viewProperties['filter_excludelowpop_value'] = $function();
}

$this->setDefaultPropertiesForVisualization();
$this->loadDocumentation();
}

/**
Expand Down Expand Up @@ -274,41 +262,25 @@ public function getViewDataTableId()
* @param string|bool $controllerAction
* @return ViewDataTable
*/
static public function factory($defaultType = null, $apiAction = false, $controllerAction = false)
static public function factory($defaultType = null, $apiAction = false, $controllerAction = false, $forceDefault = false)
{
if ($apiAction !== false) {
$defaultProperties = self::getDefaultPropertiesForReport($apiAction);
if (isset($defaultProperties['default_view_type'])) {
$defaultType = $defaultProperties['default_view_type'];
}

if ($controllerAction === false) {
$controllerAction = $apiAction;
}
if ($controllerAction === false) {
$controllerAction = $apiAction;
}

if ($defaultType === null) {
$defaultType = 'table';
$defaultProperties = self::getDefaultPropertiesForReport($apiAction);
if (!empty($defaultProperties['default_view_type'])
&& !$forceDefault
) {
$defaultType = $defaultProperties['default_view_type'];
}

$type = Common::getRequestVar('viewDataTable', $defaultType, 'string');
$type = Common::getRequestVar('viewDataTable', $defaultType ?: 'table', 'string');

if ($type == 'sparkline') {
$result = new ViewDataTable\Sparkline();
$result = new ViewDataTable\Sparkline($controllerAction, $apiAction, $defaultProperties);
} else {
$klass = DataTableVisualization::getClassFromId($type);
$result = new ViewDataTable($klass);
}

if ($apiAction !== false) {
list($plugin, $controllerAction) = explode('.', $controllerAction);

$subtableAction = $controllerAction;
if (isset($defaultProperties['subtable_action'])) {
$subtableAction = $defaultProperties['subtable_action'];
}

$result->init($plugin, $controllerAction, $apiAction, $subtableAction, $defaultProperties);
$result = new ViewDataTable($controllerAction, $apiAction, $defaultProperties, $type);
}

return $result;
Expand Down Expand Up @@ -377,66 +349,6 @@ public function getClientSideParameters()
return $result;
}

/**
* Inits the object given the $currentControllerName, $currentControllerAction of
* the calling controller action, eg. 'Referers' 'getLongListOfKeywords'.
* The initialization also requires the $apiMethodToRequestDataTable of the API method
* to call in order to get the DataTable, eg. 'Referers.getKeywords'.
* The optional $controllerActionCalledWhenRequestSubTable defines the method name of the API to call when there is a idSubtable.
* This value would be used by the javascript code building the GET request to the API.
*
* Example:
* For the keywords listing, a click on the row loads the subTable of the Search Engines for this row.
* In this case $controllerActionCalledWhenRequestSubTable = 'getSearchEnginesFromKeywordId'.
* The GET request will hit 'Referers.getSearchEnginesFromKeywordId'.
*
* @param string $currentControllerName eg. 'Referers'
* @param string $currentControllerAction eg. 'getKeywords'
* @param string $apiMethodToRequestDataTable eg. 'Referers.getKeywords'
* @param string $controllerActionCalledWhenRequestSubTable eg. 'getSearchEnginesFromKeywordId'
* @param array $defaultProperties
*/
public function init($currentControllerName,
$currentControllerAction,
$apiMethodToRequestDataTable,
$controllerActionCalledWhenRequestSubTable = null,
$defaultProperties = array())
{
$this->currentControllerName = $currentControllerName;
$this->currentControllerAction = $currentControllerAction;
$this->viewProperties['subtable_controller_action'] = $controllerActionCalledWhenRequestSubTable;
$this->idSubtable = Common::getRequestVar('idSubtable', false, 'int');

foreach ($defaultProperties as $name => $value) {
$this->setViewProperty($name, $value);
}

$queryParams = Url::getArrayFromCurrentQueryString();
foreach ($this->getClientSideProperties() as $name) {
if (isset($queryParams[$name])) {
$this->setViewProperty($name, $queryParams[$name]);
}
}

$this->viewProperties['show_footer_icons'] = ($this->idSubtable == false);
$this->viewProperties['apiMethodToRequestDataTable'] = $apiMethodToRequestDataTable;

$this->viewProperties['report_id'] = $currentControllerName . '.' . $currentControllerAction;
$this->viewProperties['self_url'] = $this->getBaseReportUrl($currentControllerName, $currentControllerAction);

// the exclude low population threshold value is sometimes obtained by requesting data.
// to avoid issuing unecessary requests when display properties are determined by metadata,
// we allow it to be a closure.
if (isset($this->viewProperties['filter_excludelowpop_value'])
&& $this->viewProperties['filter_excludelowpop_value'] instanceof \Closure
) {
$function = $this->viewProperties['filter_excludelowpop_value'];
$this->viewProperties['filter_excludelowpop_value'] = $function();
}

$this->loadDocumentation();
}

/**
* Returns the View_Interface.
* You can then call render() on this object.
Expand Down Expand Up @@ -1215,17 +1127,24 @@ public function getDefaultDataTableCssClass()
return 'dataTableViz' . Piwik::getUnnamespacedClassName($this->visualizationClass);
}

private function setDefaultPropertiesForVisualization()
private function setViewProperties($values)
{
foreach ($values as $name => $value) {
$this->setViewProperty($name, $value);
}
}

private function setDefaultProperties()
{
// set core default properties
$this->setViewProperties(Properties::getDefaultPropertyValues());

// set visualization default properties
if ($this->visualizationClass === null) {
return;
}

$visualizationClass = $this->visualizationClass;
$defaultProperties = $visualizationClass::getDefaultPropertyValues();

foreach ($defaultProperties as $name => $value) {
$this->setViewProperty($name, $value);
}
$this->setViewProperties($visualizationClass::getDefaultPropertyValues());
}
}
75 changes: 75 additions & 0 deletions core/ViewDataTable/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

use Exception;
use ReflectionClass;
use Piwik\Piwik;
use Piwik\Config;
use Piwik\Metrics;
use Piwik\Common;

/**
* Contains the list of all core DataTable display properties for use with ViewDataTable.
Expand Down Expand Up @@ -376,6 +380,11 @@ class Properties
*/
const HIDE_ANNOTATIONS_VIEW = 'hide_annotations_view';

/**
* The filter_limit query parameter value to use in export links.
*/
const EXPORT_LIMIT = 'export_limit';

/**
* Returns the set of all valid ViewDataTable properties. The result is an array with property
* names as keys. Values of the array are undefined.
Expand Down Expand Up @@ -462,6 +471,72 @@ public static function checkValidVisualizationProperty($visualizationClass, $nam
}
}

/**
* Returns the default values for each core view property.
*
* @return array
*/
public static function getDefaultPropertyValues()
{
$result = array(
'datatable_template' => '@CoreHome/_dataTable',
'show_goals' => false,
'show_ecommerce' => false,
'show_search' => true,
'show_table' => true,
'show_table_all_columns' => true,
'show_all_views_icons' => true,
'show_active_view_icon' => true,
'show_bar_chart' => true,
'show_pie_chart' => true,
'show_tag_cloud' => true,
'show_export_as_image_icon' => false,
'show_export_as_rss_feed' => true,
'show_exclude_low_population' => true,
'show_offset_information' => true,
'show_pagination_control' => true,
'show_limit_control' => false,
'show_footer' => true,
'show_related_reports' => true,
'export_limit' => Config::getInstance()->General['API_datatable_default_limit'],
'highlight_summary_row' => false,
'related_reports' => array(),
'title' => 'unknown',
'tooltip_metadata_name' => false,
'enable_sort' => true,
'disable_generic_filters' => false,
'disable_queued_filters' => false,
'keep_summary_row' => false,
'filter_excludelowpop' => false,
'filter_excludelowpop_value' => false,
'filter_pattern' => false,
'filter_column' => false,
'filter_limit' => false,
'filter_sort_column' => false,
'filter_sort_order' => false,
'custom_parameters' => array(),
'translations' => array_merge(
Metrics::getDefaultMetrics(),
Metrics::getDefaultProcessedMetrics()
),
'request_parameters_to_modify' => array(),
'documentation' => false,
'subtable_controller_action' => false,
'datatable_css_class' => false,
'filters' => array(),
'hide_annotations_view' => true,
'columns_to_display' => array(),
);

$columns = Common::getRequestVar('columns', false);
if ($columns !== false) {
$result['columns_to_display'] = Piwik::getArrayFromApiParameter($columns);
array_unshift($result['columns_to_display'], 'label');
}

return $result;
}

private static function getFlippedClassConstantMap($klass)
{
$klass = new ReflectionClass($klass);
Expand Down
5 changes: 3 additions & 2 deletions plugins/CoreHome/DataTableRowAction/RowEvolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,15 @@ protected function extractEvolutionReport($report)
public function getRowEvolutionGraph($graphType = false, $metrics = false)
{
// set up the view data table
$view = ViewDataTable::factory(
$graphType ?: $this->graphType, $this->apiMethod, $controllerAction = 'CoreHome.getRowEvolutionGraph');
$view = ViewDataTable::factory($graphType ?: $this->graphType, $this->apiMethod,
$controllerAction = 'CoreHome.getRowEvolutionGraph', $forceDefault = true);
$view->setDataTable($this->dataTable);

if (!empty($this->graphMetrics)) { // In row Evolution popover, this is empty
$view->columns_to_display = array_keys($metrics ?: $this->graphMetrics);
}

$view->show_goals = false;
$view->show_all_views_icons = false;
$view->show_active_view_icon = false;
$view->show_related_reports = false;
Expand Down
Loading

0 comments on commit be35dc4

Please sign in to comment.