Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ColumnDelete.php « Filter « DataTable « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 37f0b1bf97fa0eb3ecc6dfec1f418899a90b0c4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\DataTable\Filter;

use Piwik\DataTable;
use Piwik\DataTable\BaseFilter;

/**
 * Filter that will remove columns from a {@link DataTable} using either a blacklist,
 * whitelist or both.
 *
 * This filter is used to handle the **hideColumn** and **showColumn** query parameters.
 *
 * **Basic usage example**
 *
 *     $columnsToRemove = array('nb_hits', 'nb_pageviews');
 *     $dataTable->filter('ColumnDelete', array($columnsToRemove));
 *
 *     $columnsToKeep = array('nb_visits');
 *     $dataTable->filter('ColumnDelete', array(array(), $columnsToKeep));
 *
 * @api
 */
class ColumnDelete extends BaseFilter
{
    /**
     * The columns that should be removed from DataTable rows.
     *
     * @var array
     */
    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;

    /**
     * Hack: when specifying "showColumns", sometimes we'd like to also keep columns that "look" like a given column,
     * without manually specifying all these columns (which may not be possible if column names are generated dynamically)
     *
     * Column will be kept, if they match any name in the $columnsToKeep, or if they look like anyColumnToKeep__anythingHere
     */
    const APPEND_TO_COLUMN_NAME_TO_KEEP = '__';

    /**
     * Delete the column, only if the value was zero
     *
     * @var bool
     */
    private $deleteIfZeroOnly;

    /**
     * Constructor.
     *
     * @param DataTable $table The DataTable instance that will eventually be filtered.
     * @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.
     * @param bool $deleteIfZeroOnly If true, columns will be removed only if their value is 0.
     */
    public function __construct($table, $columnsToRemove, $columnsToKeep = array(), $deleteIfZeroOnly = false)
    {
        parent::__construct($table);

        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
        $this->deleteIfZeroOnly = $deleteIfZeroOnly;
    }

    /**
     * See {@link ColumnDelete}.
     *
     * @param DataTable $table
     * @return DataTable
     */
    public function filter($table)
    {
        // always do recursive filter
        $this->enableRecursive(true);
        $recurse = false; // only recurse if there are columns to remove/keep

        // remove columns specified in $this->columnsToRemove
        if (!empty($this->columnsToRemove)) {
            $this->removeColumnsFromTable($table);
            $recurse = true;
        }

        // remove columns not specified in $columnsToKeep
        if (!empty($this->columnsToKeep)) {
            foreach ($table as $index => $row) {
                $columnsToDelete = array();
                foreach ($row as $name => $value) {
                    $keep = false;

                    // @see self::APPEND_TO_COLUMN_NAME_TO_KEEP
                    foreach ($this->columnsToKeep as $nameKeep => $true) {
                        if (strpos($name, $nameKeep . self::APPEND_TO_COLUMN_NAME_TO_KEEP) === 0) {
                            $keep = true;
                        }
                    }

                    if (!$keep
                        && $name != 'label' // label cannot be removed via whitelisting
                        && !isset($this->columnsToKeep[$name])
                    ) {
                        // we cannot remove row directly to prevent notice "ArrayIterator::next(): Array was modified
                        // outside object and internal position is no longer valid in /var/www..."
                        $columnsToDelete[] = $name;
                    }
                }
                foreach ($columnsToDelete as $columnToDelete) {
                    unset($table[$index][$columnToDelete]);
                }
            }

            $recurse = true;
        }

        // recurse
        if ($recurse && !is_array($table)) {
            foreach ($table as $row) {
                $this->filterSubTable($row);
            }
        }

        return $table;
    }

    /**
     * @param $table
     * @return array
     */
    protected function removeColumnsFromTable(&$table)
    {
        if(!$this->isArrayAccess($table)) {
            return;
        }
        foreach ($table as $index => &$row) {
            if(!$this->isArrayAccess($row)) {
                continue;
            }
            foreach ($this->columnsToRemove as $column) {

                if (!array_key_exists($column, $row)) {
                    continue;
                }

                if ($this->deleteIfZeroOnly) {
                    $value = $row[$column];
                    if ($value === false || !empty($value)) {
                        continue;
                    }
                }

                unset($table[$index][$column]);
            }

            $this->removeColumnsFromTable($row);
        }
    }

    /**
     * @param $table
     * @return bool
     */
    protected function isArrayAccess(&$table)
    {
        return is_array($table) || $table instanceof \ArrayAccess;
    }
}