diff options
author | benakamoorthi <benaka.moorthi@gmail.com> | 2012-08-19 21:10:20 +0400 |
---|---|---|
committer | benakamoorthi <benaka.moorthi@gmail.com> | 2012-08-19 21:10:20 +0400 |
commit | 3f4bde1622e25dc754c1b5daafbfa1dd6214f5d5 (patch) | |
tree | eb4e5dc8048f2feede4445490e588bb58cb901ba /core/DataTable/Filter/ColumnDelete.php | |
parent | 65abaf1433d3872591f6efd702ea4c6be79b9efa (diff) |
Fixes #3300, added hideColumns/showColumns query parameters to customize API output & modified ColumnDelete filter to be more robust.
git-svn-id: http://dev.piwik.org/svn/trunk@6836 59fd770c-687e-43c8-a1e3-f5a4ff64c105
Diffstat (limited to 'core/DataTable/Filter/ColumnDelete.php')
-rw-r--r-- | core/DataTable/Filter/ColumnDelete.php | 105 |
1 files changed, 87 insertions, 18 deletions
diff --git a/core/DataTable/Filter/ColumnDelete.php b/core/DataTable/Filter/ColumnDelete.php index b83b82f508..1d10b7cdb7 100644 --- a/core/DataTable/Filter/ColumnDelete.php +++ b/core/DataTable/Filter/ColumnDelete.php @@ -11,7 +11,8 @@ */ /** - * Deletes a column from a data table + * Filter that will remove columns from a DataTable using either a blacklist, + * whitelist or both. * * @package Piwik * @subpackage Piwik_DataTable @@ -19,32 +20,100 @@ class Piwik_DataTable_Filter_ColumnDelete extends Piwik_DataTable_Filter { /** - * Column that should be removed - * - * @var string + * The columns that should be removed from DataTable rows. + * + * @var array */ - private $columnToDelete; - + private $columnsToRemove; + + /** + * The columns that should be kept in DataTable rows. All other columns will be + * removed. If a column is in $columnsToRemove and this variable, it will NOT be kept. + * + * @var array + */ + private $columnsToKeep; + /** - * Constructor - sets the column to be deleted - * - * @param Piwik_DataTable $table data table - * @param string $columnToDelete column to delete + * Constructor. + * + * @param Piwik_DataTable $table + * @param array|string $columnsToRemove An array of column names or a comma-separated list of + * column names. These columns will be removed. + * @param array|string $columnsToKeep An array of column names that should be kept or a + * comma-separated list of column names. Columns not in + * this list will be removed. */ - public function __construct( $table, $columnToDelete ) + public function __construct( $table, $columnsToRemove, $columnsToKeep = array() ) { parent::__construct($table); - $this->columnToDelete = $columnToDelete; + + if (is_string($columnsToRemove)) + { + $columnsToRemove = $columnsToRemove == '' ? array() : explode(',', $columnsToRemove); + } + + if (is_string($columnsToKeep)) + { + $columnsToKeep = $columnsToKeep == '' ? array() : explode(',', $columnsToKeep); + } + + $this->columnsToRemove = $columnsToRemove; + $this->columnsToKeep = array_flip($columnsToKeep); // flip so we can use isset instead of in_array + + // always do recursive filter + $this->enableRecursive(true); } - + /** - * Executes the filter and removes the specified column in the given data table - * - * @param Piwik_DataTable $table + * Filters the given DataTable. Removes columns that are not desired from + * each DataTable row. + * + * @param Piwik_DataTable $table */ public function filter($table) { - $table->deleteColumn($this->columnToDelete); + $recurse = false; // only recurse if there are columns to remove/keep + + // remove columns specified in $this->columnsToRemove + if (!empty($this->columnsToRemove)) + { + foreach ($table->getRows() as $row) + { + foreach ($this->columnsToRemove as $column) + { + $row->deleteColumn($column); + } + } + + $recurse = true; + } + + // remove columns not specified in $columnsToKeep + if (!empty($this->columnsToKeep)) + { + foreach ($table->getRows() as $row) + { + foreach ($row->getColumns() as $name => $value) + { + // label cannot be removed via whitelisting + if ($name != 'label' && !isset($this->columnsToKeep[$name])) + { + $row->deleteColumn($name); + } + } + } + + $recurse = true; + } + + // recurse + if ($recurse) + { + foreach ($table->getRows() as $row) + { + $this->filterSubTable($row); + } + } } - } |