Skip to content

Commit

Permalink
Refs #4041, allow all view/visualization properties to be set via que…
Browse files Browse the repository at this point in the history
…ry parameter, remove unecessary client side properties from ViewDataTable, move HtmlTable specific client side parameters to HtmlTable, remove code that looks for generic filter query parameters when setting client side parameters, make sure client side parameters are set if they're not false (ie, 0) instead of if not empty and add filter_offset as view property.
  • Loading branch information
Benaka Moorthi committed Aug 22, 2013
1 parent 8bb5f17 commit 9e56138
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 63 deletions.
76 changes: 19 additions & 57 deletions core/ViewDataTable.php
Expand Up @@ -139,17 +139,8 @@ public function __construct($currentControllerAction,
$this->viewProperties['subtable_controller_action'] = $currentControllerAction;

$this->setDefaultProperties();

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->setViewProperties($viewProperties);
$this->overrideViewPropertiesWithQueryParams();

$this->idSubtable = Common::getRequestVar('idSubtable', false, 'int');
$this->viewProperties['show_footer_icons'] = ($this->idSubtable == false);
Expand Down Expand Up @@ -274,25 +265,7 @@ static public function factory($defaultType = null, $apiAction = false, $control
*/
public function getClientSideProperties()
{
$result = array(
'show_search',
'show_table',
'show_table_all_columns',
'show_all_views_icons',
'show_active_view_icon',
'show_bar_chart',
'show_pie_chart',
'show_tag_cloud',
'show_export_as_image_icon',
'show_export_as_rss_feed',
'show_exclude_low_population',
'show_offset_information',
'show_pagination_control',
'show_footer',
'show_related_reports',
'keep_summary_row',
'subtable_controller_action',
);
$result = array();

if ($this->visualizationClass) {
$klass = $this->visualizationClass;
Expand All @@ -311,9 +284,6 @@ public function getClientSideProperties()
public function getClientSideParameters()
{
$result = array(
'enable_sort',
'disable_generic_filters',
'disable_queued_filters',
'filter_excludelowpop',
'filter_excludelowpop_value',
'filter_pattern',
Expand Down Expand Up @@ -733,25 +703,6 @@ protected function getJavascriptVariablesToSet()
// build javascript variables to set
$javascriptVariablesToSet = array();

$genericFilters = \Piwik\API\DataTableGenericFilter::getGenericFiltersInformation();
foreach ($genericFilters as $filter) {
foreach ($filter as $filterVariableName => $filterInfo) {
// if there is a default value for this filter variable we set it
// so that it is propagated to the javascript
if (isset($filterInfo[1])) {
$javascriptVariablesToSet[$filterVariableName] = $filterInfo[1];

// we set the default specified column and Order to sort by
// when this javascript variable is not set already
// for example during an AJAX call this variable will be set in the URL
// so this will not be executed (and the default sorted not be used as the sorted column might have changed in the meanwhile)
if (false !== ($defaultValue = $this->getDefault($filterVariableName))) {
$javascriptVariablesToSet[$filterVariableName] = $defaultValue;
}
}
}
}

foreach ($this->viewProperties['custom_parameters'] as $name => $value) {
$javascriptVariablesToSet[$name] = $value;
}
Expand All @@ -765,13 +716,11 @@ protected function getJavascriptVariablesToSet()
$javascriptVariablesToSet[$name] = $requestValue;
}

// at this point there are some filters values we may have not set,
// case of the filter without default values and parameters set directly in this class
// for example setExcludeLowPopulation
// we go through all the $this->viewProperties array and set the variables not set yet
foreach ($this->getClientSideParameters() as $name) {
if (!isset($javascriptVariablesToSet[$name])) {
if (!empty($this->viewProperties[$name])) {
if (isset($this->viewProperties[$name])
&& $this->viewProperties[$name] !== false
) {
$javascriptVariablesToSet[$name] = $this->convertForJson($this->viewProperties[$name]);
} else if (Properties::isValidVisualizationProperty($this->visualizationClass, $name)) {
$javascriptVariablesToSet[$name] =
Expand Down Expand Up @@ -1260,4 +1209,17 @@ private function convertForJson($value)
{
return is_bool($value) ? (int)$value : $value;
}

private function overrideViewPropertiesWithQueryParams()
{
// TODO: should mark properties that are overridable so not all properties can be overidden this way
$queryParams = Url::getArrayFromCurrentQueryString();
foreach ($queryParams as $name => $value) {
if (Properties::isCoreViewProperty($name)) {
$this->viewProperties[$name] = $value;
} else if (Properties::isValidVisualizationProperty($this->visualizationClass, $name)) {
$this->viewProperties['visualization_properties']->$name = $value;
}
}
}
}
12 changes: 11 additions & 1 deletion core/ViewDataTable/Properties.php
Expand Up @@ -66,9 +66,18 @@ class Properties

/**
* The number of items to truncate the data set to before rendering the DataTable view.
*
* @see self::OFFSET
*/
const LIMIT = 'filter_limit';

/**
* The number of items from the start of the data set that should be ignored.
*
* @see self::LIMIT
*/
const OFFSET = 'filter_offset';

/**
* Controls whether the 'Exclude Low Population' option (visible in the popup that displays after
* clicking the 'cog' icon) is shown.
Expand Down Expand Up @@ -528,8 +537,9 @@ public static function getDefaultPropertyValues()
'filter_pattern' => false,
'filter_column' => false,
'filter_limit' => false,
'filter_offset' => 0,
'filter_sort_column' => false,
'filter_sort_order' => false,
'filter_sort_order' => 'desc',
'custom_parameters' => array(),
'translations' => array_merge(
Metrics::getDefaultMetrics(),
Expand Down
6 changes: 4 additions & 2 deletions plugins/CoreHome/javascripts/dataTable.js
Expand Up @@ -350,7 +350,7 @@ dataTable.prototype =
return imageSortSrc;
}

if (self.param.enable_sort) {
if (self.props.enable_sort) {
$('.sortable', domElem).off('click.dataTableSort').on('click.dataTableSort',
function () {
$(this).off('click.dataTableSort');
Expand All @@ -364,7 +364,9 @@ dataTable.prototype =
var imageSortSrc = getSortImageSrc();
var imageSortWidth = 16;
var imageSortHeight = 16;
var ImageSortClass = self.param.filter_sort_order.charAt(0).toUpperCase() + self.param.filter_sort_order.substr(1);

var sortOrder = self.param.filter_sort_order;
var ImageSortClass = sortOrder.charAt(0).toUpperCase() + sortOrder.substr(1);

// we change the style of the column currently used as sort column
// adding an image and the class columnSorted to the TD
Expand Down
4 changes: 2 additions & 2 deletions plugins/CoreHome/templates/_dataTable.twig
Expand Up @@ -3,8 +3,8 @@
<div class="dataTable {{ visualizationCssClass }} {{ properties.datatable_css_class|default('') }} {% if isSubtable %}subDataTable{% endif %}"
data-table-type="{{ properties.datatable_js_type|default('dataTable') }}"
data-report="{{ properties.report_id }}"
data-props="{{ clientSidePropertiesToSet|json_encode }}"
data-params="{{ javascriptVariablesToSet|json_encode }}">
data-props="{% if clientSidePropertiesToSet is empty %}{}{% else %}{{ clientSidePropertiesToSet|json_encode }}{% endif %}"
data-params="{% if javascriptVariablesToSet is empty %}{}{% else %}{{ javascriptVariablesToSet|json_encode }}{% endif %}">
<div class="reportDocumentation">
{% if properties.documentation|default is not empty %}<p>{{ properties.documentation|raw }}</p>{% endif %}
{% if properties.metadata.archived_date is defined %}<span class='helpDate'>{{ properties.metadata.archived_date }}</span>{% endif %}
Expand Down
6 changes: 5 additions & 1 deletion plugins/CoreVisualizations/Visualizations/HtmlTable.php
Expand Up @@ -88,6 +88,7 @@ class HtmlTable extends DataTableVisualization
static public $clientSideParameters = array(
'search_recursive',
'filter_limit',
'filter_offset',
'filter_sort_column',
'filter_sort_order',
);
Expand All @@ -96,7 +97,10 @@ class HtmlTable extends DataTableVisualization
'show_extra_columns',
'show_goals_columns',
'disable_row_evolution',
'disable_row_actions'
'disable_row_actions',
'enable_sort',
'keep_summary_row',
'subtable_controller_action',
);

/**
Expand Down

0 comments on commit 9e56138

Please sign in to comment.