/** * Returns an array containing the information of the generic Piwik_DataTable_Filter * to be applied automatically to the data resulting from the API calls. * * Order to apply the filters: * 1 - Filter that remove filtered rows * 2 - Filter that sort the remaining rows * 3 - Filter that keep only a subset of the results * 4 - Presentation filters * * @return array See the code for spec */ public static function getGenericFiltersInformation() { if (is_null(self::$genericFiltersInfo)) { self::$genericFiltersInfo = array('Pattern' => array('filter_column' => array('string', 'label'), 'filter_pattern' => array('string')), 'PatternRecursive' => array('filter_column_recursive' => array('string', 'label'), 'filter_pattern_recursive' => array('string')), 'ExcludeLowPopulation' => array('filter_excludelowpop' => array('string'), 'filter_excludelowpop_value' => array('float', '0')), 'AddColumnsProcessedMetrics' => array('filter_add_columns_when_show_all_columns' => array('integer')), 'AddColumnsProcessedMetricsGoal' => array('filter_update_columns_when_show_all_goals' => array('integer'), 'idGoal' => array('string', Piwik_DataTable_Filter_AddColumnsProcessedMetricsGoal::GOALS_OVERVIEW)), 'Sort' => array('filter_sort_column' => array('string'), 'filter_sort_order' => array('string', 'desc')), 'Truncate' => array('filter_truncate' => array('integer')), 'Limit' => array('filter_offset' => array('integer', '0'), 'filter_limit' => array('integer'), 'keep_summary_row' => array('integer', '0'))); } return self::$genericFiltersInfo; }
/** * This functions reads the customization values for the DataTable and returns an array (name,value) to be printed in Javascript. * This array defines things such as: * - name of the module & action to call to request data for this table * - optional filters information, eg. filter_limit and filter_offset * - etc. * * The values are loaded: * - from the generic filters that are applied by default @see Piwik_API_DataTableGenericFilter.php::getGenericFiltersInformation() * - from the values already available in the GET array * - from the values set using methods from this class (eg. setSearchPattern(), setLimit(), etc.) * * @return array eg. array('show_offset_information' => 0, 'show_... */ 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 ($_GET as $name => $value) { try { $requestValue = Piwik_Common::getRequestVar($name); } catch (Exception $e) { $requestValue = ''; } $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->variablesDefault array and set the variables not set yet foreach ($this->variablesDefault as $name => $value) { if (!isset($javascriptVariablesToSet[$name])) { $javascriptVariablesToSet[$name] = $value; } } if ($this->dataTable instanceof Piwik_DataTable) { // we override the filter_sort_column with the column used for sorting, // which can be different from the one specified (eg. if the column doesn't exist) $javascriptVariablesToSet['filter_sort_column'] = $this->dataTable->getSortedByColumnName(); // datatable can return "2" but we want to write "nb_visits" in the js if (isset(Piwik_Archive::$mappingFromIdToName[$javascriptVariablesToSet['filter_sort_column']])) { $javascriptVariablesToSet['filter_sort_column'] = Piwik_Archive::$mappingFromIdToName[$javascriptVariablesToSet['filter_sort_column']]; } } $javascriptVariablesToSet['module'] = $this->currentControllerName; $javascriptVariablesToSet['action'] = $this->currentControllerAction; $javascriptVariablesToSet['viewDataTable'] = $this->getViewDataTableId(); $javascriptVariablesToSet['controllerActionCalledWhenRequestSubTable'] = $this->controllerActionCalledWhenRequestSubTable; if ($this->dataTable) { $javascriptVariablesToSet['totalRows'] = $this->dataTable->getRowsCountBeforeLimitFilter(); } // we escape the values that will be displayed in the javascript footer of each datatable // to make sure there is malicious code injected (the value are already htmlspecialchar'ed as they // are loaded with Piwik_Common::getRequestVar() foreach ($javascriptVariablesToSet as &$value) { if (is_array($value)) { $value = array_map('addslashes', $value); } else { $value = addslashes($value); } } $deleteFromJavascriptVariables = array('filter_excludelowpop', 'filter_excludelowpop_value'); foreach ($deleteFromJavascriptVariables as $name) { if (isset($javascriptVariablesToSet[$name])) { unset($javascriptVariablesToSet[$name]); } } return $javascriptVariablesToSet; }
protected function handleDataTable($datatable) { // if the flag disable_generic_filters is defined we skip the generic filters if('false' == Piwik_Common::getRequestVar('disable_generic_filters', 'false', 'string', $this->request)) { $genericFilter = new Piwik_API_DataTableGenericFilter($this->request); $genericFilter->filter($datatable); } // we automatically safe decode all datatable labels (against xss) $datatable->queueFilter('SafeDecodeLabel'); // if the flag disable_queued_filters is defined we skip the filters that were queued if(Piwik_Common::getRequestVar('disable_queued_filters', 'false', 'string', $this->request) == 'false') { $datatable->applyQueuedFilters(); } return $this->getRenderedDataTable($datatable); }
/** * Returns URL for this report w/o any filter parameters. * * @param string $module * @param string $action * @param array $queryParams */ private function getBaseReportUrl($module, $action, $queryParams = array()) { $params = array_merge($queryParams, array('module' => $module, 'action' => $action)); // unset all filter query params so the related report will show up in its default state, // unless the filter param was in $queryParams $genericFiltersInfo = Piwik_API_DataTableGenericFilter::getGenericFiltersInformation(); foreach ($genericFiltersInfo as $filter) { foreach ($filter as $queryParamName => $queryParamInfo) { if (!isset($params[$queryParamName])) { $params[$queryParamName] = null; } } } // add the related report $url = Piwik_Url::getCurrentQueryStringWithParametersModified($params); return $url; }
/** * Handles the given data table * * @param Piwik_DataTable $datatable * @return string */ protected function handleDataTable($datatable) { // if requested, flatten nested tables if (Piwik_Common::getRequestVar('flat', '0', 'string', $this->request) == '1') { $flattener = new Piwik_API_DataTableManipulator_Flattener($this->apiModule, $this->apiMethod, $this->request); if (Piwik_Common::getRequestVar('include_aggregate_rows', '0', 'string', $this->request) == '1') { $flattener->includeAggregateRows(); } $datatable = $flattener->flatten($datatable); } // if the flag disable_generic_filters is defined we skip the generic filters if (0 == Piwik_Common::getRequestVar('disable_generic_filters', '0', 'string', $this->request)) { $genericFilter = new Piwik_API_DataTableGenericFilter($this->request); $genericFilter->filter($datatable); } // we automatically safe decode all datatable labels (against xss) $datatable->queueFilter('SafeDecodeLabel'); // if the flag disable_queued_filters is defined we skip the filters that were queued if (Piwik_Common::getRequestVar('disable_queued_filters', 'false', 'string', $this->request) == 'false') { $datatable->applyQueuedFilters(); } // use the ColumnDelete filter if hideColumns/showColumns is provided (must be done // after queued filters are run so processed metrics can be removed, too) $hideColumns = Piwik_Common::getRequestVar('hideColumns', '', 'string', $this->request); $showColumns = Piwik_Common::getRequestVar('showColumns', '', 'string', $this->request); if ($hideColumns !== '' || $showColumns !== '') { $datatable->filter('ColumnDelete', array($hideColumns, $showColumns)); } // apply label filter: only return a single row matching the label parameter $label = Piwik_Common::getRequestVar('label', '', 'string', $this->request); if ($label !== '') { $label = Piwik_Common::unsanitizeInputValue($label); $filter = new Piwik_API_DataTableManipulator_LabelFilter($this->apiModule, $this->apiMethod, $this->request); $datatable = $filter->filter($label, $datatable); } return $this->getRenderedDataTable($datatable); }
protected function handleDataTable($datatable) { // if the flag disable_generic_filters is defined we skip the generic filters if (0 == Piwik_Common::getRequestVar('disable_generic_filters', '0', 'string', $this->request)) { $genericFilter = new Piwik_API_DataTableGenericFilter($this->request); $genericFilter->filter($datatable); } // we automatically safe decode all datatable labels (against xss) $datatable->queueFilter('SafeDecodeLabel'); // if the flag disable_queued_filters is defined we skip the filters that were queued if (Piwik_Common::getRequestVar('disable_queued_filters', 'false', 'string', $this->request) == 'false') { $datatable->applyQueuedFilters(); } // apply label filter: only return a single row matching the label parameter $label = Piwik_Common::getRequestVar('label', '', 'string', $this->request); if ($label !== '') { // the label can be passed with html entities. // we remove them here and try with and without them in the label filter. $label = html_entity_decode($label); $filter = new Piwik_API_DataTableLabelFilter(); $datatable = $filter->filter($label, $datatable, $this->apiModule, $this->apiMethod, $this->request); } return $this->getRenderedDataTable($datatable); }