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

Php.php « Renderer « DataTable « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6717746295c2a68169c5b95fcc74f1b2e7a5c371 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */
namespace Piwik\DataTable\Renderer;

use Exception;
use Piwik\DataTable\Manager;
use Piwik\DataTable\Simple;
use Piwik\DataTable\Renderer;
use Piwik\Piwik;
use Piwik\DataTable;

/**
 * Returns the equivalent PHP array for a given DataTable.
 * You can specify in the constructor if you want the serialized version.
 * Please note that by default it will produce a flat version of the array.
 * See the method flatRender() for details. @see flatRender();
 *
 * Works with recursive DataTable (when a row can be associated with a subDataTable).
 *
 * @package Piwik
 * @subpackage DataTable
 */
class Php extends Renderer
{
    protected $prettyDisplay = false;
    protected $serialize = true;

    /**
     * Enables/Disables serialize
     *
     * @param bool $bool
     */
    public function setSerialize($bool)
    {
        $this->serialize = (bool)$bool;
    }

    /**
     * Enables/Disables pretty display
     *
     * @param bool $bool
     */
    public function setPrettyDisplay($bool)
    {
        $this->prettyDisplay = (bool)$bool;
    }

    /**
     * Converts current data table to string
     *
     * @return string
     */
    public function __toString()
    {
        $data = $this->render();
        if (!is_string($data)) {
            $data = serialize($data);
        }
        return $data;
    }

    /**
     * Computes the dataTable output and returns the string/binary
     *
     * @param null|DataTable\Map|Simple $dataTable
     * @return string
     */
    public function render($dataTable = null)
    {
        $this->renderHeader();

        if (is_null($dataTable)) {
            $dataTable = $this->table;
        }
        $toReturn = $this->flatRender($dataTable);

        if ($this->prettyDisplay) {
            if (!is_array($toReturn)) {
                $toReturn = unserialize($toReturn);
            }
            $toReturn = "<pre>" . var_export($toReturn, true) . "</pre>";
        }
        return $toReturn;
    }

    /**
     * Computes the exception output and returns the string/binary
     *
     * @return string
     */
    public function renderException()
    {
        $this->renderHeader();

        $exceptionMessage = $this->getExceptionMessage();

        $return = array('result' => 'error', 'message' => $exceptionMessage);

        if ($this->serialize) {
            $return = serialize($return);
        }

        return $return;
    }

    /**
     * Produces a flat php array from the DataTable, putting "columns" and "metadata" on the same level.
     *
     * For example, when  a originalRender() would be
     *     array( 'columns' => array( 'col1_name' => value1, 'col2_name' => value2 ),
     *            'metadata' => array( 'metadata1_name' => value_metadata) )
     *
     * a flatRender() is
     *     array( 'col1_name' => value1,
     *            'col2_name' => value2,
     *            'metadata1_name' => value_metadata )
     *
     * @param null|DataTable\Map|Simple $dataTable
     * @return array  Php array representing the 'flat' version of the datatable
     */
    public function flatRender($dataTable = null)
    {
        if (is_null($dataTable)) {
            $dataTable = $this->table;
        }

        if (is_array($dataTable)) {
            $flatArray = $dataTable;
            if (self::shouldWrapArrayBeforeRendering($flatArray)) {
                $flatArray = array($flatArray);
            }
        } else if ($dataTable instanceof DataTable\Map) {
            $flatArray = array();
            foreach ($dataTable->getArray() as $keyName => $table) {
                $serializeSave = $this->serialize;
                $this->serialize = false;
                $flatArray[$keyName] = $this->flatRender($table);
                $this->serialize = $serializeSave;
            }
        } else if ($dataTable instanceof Simple) {
            $flatArray = $this->renderSimpleTable($dataTable);

            // if we return only one numeric value then we print out the result in a simple <result> tag
            // keep it simple!
            if (count($flatArray) == 1) {
                $flatArray = current($flatArray);
            }
        } // A normal DataTable needs to be handled specifically
        else {
            $array = $this->renderTable($dataTable);
            $flatArray = $this->flattenArray($array);
        }

        if ($this->serialize) {
            $flatArray = serialize($flatArray);
        }

        return $flatArray;
    }

    /**
     *
     * @param array $array
     * @return array
     */
    protected function flattenArray($array)
    {
        $flatArray = array();
        foreach ($array as $row) {
            $newRow = $row['columns'] + $row['metadata'];
            if (isset($row['idsubdatatable'])
                && $this->hideIdSubDatatable === false
            ) {
                $newRow += array('idsubdatatable' => $row['idsubdatatable']);
            }
            if (isset($row['subtable'])) {
                $newRow += array('subtable' => $this->flattenArray($row['subtable']));
            }
            $flatArray[] = $newRow;
        }
        return $flatArray;
    }

    /**
     * Converts the current data table to an array
     *
     * @return array
     * @throws Exception
     */
    public function originalRender()
    {
        Piwik::checkObjectTypeIs($this->table, array('Simple', 'DataTable'));

        if ($this->table instanceof Simple) {
            $array = $this->renderSimpleTable($this->table);
        } elseif ($this->table instanceof DataTable) {
            $array = $this->renderTable($this->table);
        }

        if ($this->serialize) {
            $array = serialize($array);
        }
        return $array;
    }

    /**
     * Converts the given data table to an array
     *
     * @param DataTable $table
     * @return array
     */
    protected function renderTable($table)
    {
        $array = array();

        foreach ($table->getRows() as $id => $row) {
            $newRow = array(
                'columns'        => $row->getColumns(),
                'metadata'       => $row->getMetadata(),
                'idsubdatatable' => $row->getIdSubDataTable(),
            );

            if ($id == DataTable::ID_SUMMARY_ROW) {
                $newRow['issummaryrow'] = true;
            }

            if ($this->isRenderSubtables()
                && $row->isSubtableLoaded()
            ) {
                $subTable = $this->renderTable(Manager::getInstance()->getTable($row->getIdSubDataTable()));
                $newRow['subtable'] = $subTable;
                if ($this->hideIdSubDatatable === false
                    && isset($newRow['metadata']['idsubdatatable_in_db'])
                ) {
                    $newRow['columns']['idsubdatatable'] = $newRow['metadata']['idsubdatatable_in_db'];
                }
                unset($newRow['metadata']['idsubdatatable_in_db']);
            }
            if ($this->hideIdSubDatatable !== false) {
                unset($newRow['idsubdatatable']);
            }

            $array[] = $newRow;
        }
        return $array;
    }

    /**
     * Converts the simple data table to an array
     *
     * @param Simple $table
     * @return array
     */
    protected function renderSimpleTable($table)
    {
        $array = array();

        $row = $table->getFirstRow();
        if ($row === false) {
            return $array;
        }
        foreach ($row->getColumns() as $columnName => $columnValue) {
            $array[$columnName] = $columnValue;
        }
        return $array;
    }
}