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

RemoveUnusedGoalRevenueColumns.php « Filter « DataTable « Goals « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 32bbff91c47dc4d7bb369ecfd7076247b9a4fdf1 (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
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik\Plugins\Goals\DataTable\Filter;

use Piwik\DataTable\BaseFilter;
use Piwik\Plugin\Metric;
use Piwik\DataTable;

class RemoveUnusedGoalRevenueColumns extends BaseFilter
{

    /**
     * @param DataTable $table
     */
    public function filter($table)
    {
        $goals = $this->getGoalsInTable($table);

        if (count($goals) === 0) {
            return;
        }

        $columnNames = [
                'revenue',
                'revenue_entry',
                'revenue_per_entry',
                'revenue_per_visit',
                'revenue_attrib',
            ];

        // Build array of columns to check
        $columnsToCheck = [];
        foreach ($goals as $goalId) {
            foreach ($columnNames as $columnName) {
                $columnsToCheck['goal_'.$goalId.'_'.$columnName] = true;
            }
        }

        // Check if there are any values in each column
        foreach ($table->getRowsWithoutSummaryRow() as $row) {

            $didChecks = false;
            foreach ($columnsToCheck as $colName => $shouldRemove) {
                if ($shouldRemove) {
                    $didChecks = true;
                }
                if (isset($row[$colName]) && $row[$colName] > 0) {
                    $columnsToCheck[$colName] = false;
                }
            }
            if (!$didChecks) {
                break 1;
            }

        }

        $columnsToRemove = [];
        foreach ($columnsToCheck as $c => $shouldRemove) {
            if ($shouldRemove) {
                $columnsToRemove[] = $c;
            }
        }

        // We can't remove the columns from here, it needs to be done in the visualisation, so set a metadata value
        // on the datatable to indicate the columns to be removed, the visualisation can then check for this and
        // adjust the view config
        $table->setMetadata('excluded_goal_columns', json_encode($columnsToRemove, true));

    }

    /**
     * Get the ids of all goals used in the table
     *
     * @param DataTable $table
     *
     * @return array
     */
    private function getGoalsInTable(DataTable &$table)
    {
        $result = array();
        foreach ($table->getRows() as $row) {
            $goals = $row->getMetadata('goals');
            if (!$goals) {
                continue;
            }

            foreach ($goals as $goalId) {
                $result[] = $goalId;
            }
        }
        return array_unique($result);
    }

}