Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataTableGenericFilter.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\API;
  10. use Exception;
  11. use Piwik\Common;
  12. use Piwik\DataTable\Filter\AddColumnsProcessedMetricsGoal;
  13. use Piwik\DataTable;
  14. class DataTableGenericFilter
  15. {
  16. /**
  17. * List of filter names not to run.
  18. *
  19. * @var string[]
  20. */
  21. private $disabledFilters = array();
  22. /**
  23. * Constructor
  24. *
  25. * @param $request
  26. */
  27. function __construct($request)
  28. {
  29. $this->request = $request;
  30. }
  31. /**
  32. * Filters the given data table
  33. *
  34. * @param DataTable $table
  35. */
  36. public function filter($table)
  37. {
  38. $this->applyGenericFilters($table);
  39. }
  40. /**
  41. * Makes sure a set of filters are not run.
  42. *
  43. * @param string[] $filterNames The name of each filter to disable.
  44. */
  45. public function disableFilters($filterNames)
  46. {
  47. $this->disabledFilters = array_unique(array_merge($this->disabledFilters, $filterNames));
  48. }
  49. /**
  50. * Returns an array containing the information of the generic Filter
  51. * to be applied automatically to the data resulting from the API calls.
  52. *
  53. * Order to apply the filters:
  54. * 1 - Filter that remove filtered rows
  55. * 2 - Filter that sort the remaining rows
  56. * 3 - Filter that keep only a subset of the results
  57. * 4 - Presentation filters
  58. *
  59. * @return array See the code for spec
  60. */
  61. public static function getGenericFiltersInformation()
  62. {
  63. return array(
  64. array('Pattern',
  65. array(
  66. 'filter_column' => array('string', 'label'),
  67. 'filter_pattern' => array('string')
  68. )),
  69. array('PatternRecursive',
  70. array(
  71. 'filter_column_recursive' => array('string', 'label'),
  72. 'filter_pattern_recursive' => array('string'),
  73. )),
  74. array('ExcludeLowPopulation',
  75. array(
  76. 'filter_excludelowpop' => array('string'),
  77. 'filter_excludelowpop_value' => array('float', '0'),
  78. )),
  79. array('AddColumnsProcessedMetrics',
  80. array(
  81. 'filter_add_columns_when_show_all_columns' => array('integer')
  82. )),
  83. array('AddColumnsProcessedMetricsGoal',
  84. array(
  85. 'filter_update_columns_when_show_all_goals' => array('integer'),
  86. 'idGoal' => array('string', AddColumnsProcessedMetricsGoal::GOALS_OVERVIEW),
  87. )),
  88. array('Sort',
  89. array(
  90. 'filter_sort_column' => array('string'),
  91. 'filter_sort_order' => array('string', 'desc'),
  92. )),
  93. array('Truncate',
  94. array(
  95. 'filter_truncate' => array('integer'),
  96. )),
  97. array('Limit',
  98. array(
  99. 'filter_offset' => array('integer', '0'),
  100. 'filter_limit' => array('integer'),
  101. 'keep_summary_row' => array('integer', '0'),
  102. )),
  103. );
  104. }
  105. /**
  106. * Apply generic filters to the DataTable object resulting from the API Call.
  107. * Disable this feature by setting the parameter disable_generic_filters to 1 in the API call request.
  108. *
  109. * @param DataTable $datatable
  110. * @return bool
  111. */
  112. protected function applyGenericFilters($datatable)
  113. {
  114. if ($datatable instanceof DataTable\Map) {
  115. $tables = $datatable->getDataTables();
  116. foreach ($tables as $table) {
  117. $this->applyGenericFilters($table);
  118. }
  119. return;
  120. }
  121. $genericFilters = self::getGenericFiltersInformation();
  122. $filterApplied = false;
  123. foreach ($genericFilters as $filterMeta) {
  124. $filterName = $filterMeta[0];
  125. $filterParams = $filterMeta[1];
  126. $filterParameters = array();
  127. $exceptionRaised = false;
  128. if (in_array($filterName, $this->disabledFilters)) {
  129. continue;
  130. }
  131. foreach ($filterParams as $name => $info) {
  132. // parameter type to cast to
  133. $type = $info[0];
  134. // default value if specified, when the parameter doesn't have a value
  135. $defaultValue = null;
  136. if (isset($info[1])) {
  137. $defaultValue = $info[1];
  138. }
  139. try {
  140. $value = Common::getRequestVar($name, $defaultValue, $type, $this->request);
  141. settype($value, $type);
  142. $filterParameters[] = $value;
  143. } catch (Exception $e) {
  144. $exceptionRaised = true;
  145. break;
  146. }
  147. }
  148. if (!$exceptionRaised) {
  149. $datatable->filter($filterName, $filterParameters);
  150. $filterApplied = true;
  151. }
  152. }
  153. return $filterApplied;
  154. }
  155. }