PageRenderTime 33ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/DataTable/Filter/ColumnDelete.php

https://github.com/CodeYellowBV/piwik
PHP | 150 lines | 67 code | 17 blank | 66 comment | 15 complexity | 0bfb88e25f42770952265cf9c3177aa5 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  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\DataTable\Filter;
  10. use Piwik\DataTable;
  11. use Piwik\DataTable\BaseFilter;
  12. /**
  13. * Filter that will remove columns from a {@link DataTable} using either a blacklist,
  14. * whitelist or both.
  15. *
  16. * This filter is used to handle the **hideColumn** and **showColumn** query parameters.
  17. *
  18. * **Basic usage example**
  19. *
  20. * $columnsToRemove = array('nb_hits', 'nb_pageviews');
  21. * $dataTable->filter('ColumnDelete', array($columnsToRemove));
  22. *
  23. * $columnsToKeep = array('nb_visits');
  24. * $dataTable->filter('ColumnDelete', array(array(), $columnsToKeep));
  25. *
  26. * @api
  27. */
  28. class ColumnDelete extends BaseFilter
  29. {
  30. /**
  31. * The columns that should be removed from DataTable rows.
  32. *
  33. * @var array
  34. */
  35. private $columnsToRemove;
  36. /**
  37. * The columns that should be kept in DataTable rows. All other columns will be
  38. * removed. If a column is in $columnsToRemove and this variable, it will NOT be kept.
  39. *
  40. * @var array
  41. */
  42. private $columnsToKeep;
  43. /**
  44. * Hack: when specifying "showColumns", sometimes we'd like to also keep columns that "look" like a given column,
  45. * without manually specifying all these columns (which may not be possible if column names are generated dynamically)
  46. *
  47. * Column will be kept, if they match any name in the $columnsToKeep, or if they look like anyColumnToKeep__anythingHere
  48. */
  49. const APPEND_TO_COLUMN_NAME_TO_KEEP = '__';
  50. /**
  51. * Delete the column, only if the value was zero
  52. *
  53. * @var bool
  54. */
  55. private $deleteIfZeroOnly;
  56. /**
  57. * Constructor.
  58. *
  59. * @param DataTable $table The DataTable instance that will eventually be filtered.
  60. * @param array|string $columnsToRemove An array of column names or a comma-separated list of
  61. * column names. These columns will be removed.
  62. * @param array|string $columnsToKeep An array of column names that should be kept or a
  63. * comma-separated list of column names. Columns not in
  64. * this list will be removed.
  65. * @param bool $deleteIfZeroOnly If true, columns will be removed only if their value is 0.
  66. */
  67. public function __construct($table, $columnsToRemove, $columnsToKeep = array(), $deleteIfZeroOnly = false)
  68. {
  69. parent::__construct($table);
  70. if (is_string($columnsToRemove)) {
  71. $columnsToRemove = $columnsToRemove == '' ? array() : explode(',', $columnsToRemove);
  72. }
  73. if (is_string($columnsToKeep)) {
  74. $columnsToKeep = $columnsToKeep == '' ? array() : explode(',', $columnsToKeep);
  75. }
  76. $this->columnsToRemove = $columnsToRemove;
  77. $this->columnsToKeep = array_flip($columnsToKeep); // flip so we can use isset instead of in_array
  78. $this->deleteIfZeroOnly = $deleteIfZeroOnly;
  79. }
  80. /**
  81. * See {@link ColumnDelete}.
  82. *
  83. * @param DataTable $table
  84. */
  85. public function filter($table)
  86. {
  87. // always do recursive filter
  88. $this->enableRecursive(true);
  89. $recurse = false; // only recurse if there are columns to remove/keep
  90. // remove columns specified in $this->columnsToRemove
  91. if (!empty($this->columnsToRemove)) {
  92. foreach ($table->getRows() as $row) {
  93. foreach ($this->columnsToRemove as $column) {
  94. if ($this->deleteIfZeroOnly) {
  95. $value = $row->getColumn($column);
  96. if ($value === false || !empty($value)) {
  97. continue;
  98. }
  99. }
  100. $row->deleteColumn($column);
  101. }
  102. }
  103. $recurse = true;
  104. }
  105. // remove columns not specified in $columnsToKeep
  106. if (!empty($this->columnsToKeep)) {
  107. foreach ($table->getRows() as $row) {
  108. foreach ($row->getColumns() as $name => $value) {
  109. $keep = false;
  110. // @see self::APPEND_TO_COLUMN_NAME_TO_KEEP
  111. foreach ($this->columnsToKeep as $nameKeep => $true) {
  112. if (strpos($name, $nameKeep . self::APPEND_TO_COLUMN_NAME_TO_KEEP) === 0) {
  113. $keep = true;
  114. }
  115. }
  116. if (!$keep
  117. && $name != 'label' // label cannot be removed via whitelisting
  118. && !isset($this->columnsToKeep[$name])
  119. ) {
  120. $row->deleteColumn($name);
  121. }
  122. }
  123. }
  124. $recurse = true;
  125. }
  126. // recurse
  127. if ($recurse) {
  128. foreach ($table->getRows() as $row) {
  129. $this->filterSubTable($row);
  130. }
  131. }
  132. }
  133. }