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

Html.php « Renderer « DataTable « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1d620f74cc73c58eb0fb6cc81c1f98c7d690b3d6 (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
<?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
 */

/**
 * Simple HTML output
 * Does not work with recursive DataTable (i.e., when a row can be associated with a subDataTable).
 *
 * @package Piwik
 * @subpackage Piwik_DataTable
 */
class Piwik_DataTable_Renderer_Html extends Piwik_DataTable_Renderer
{
    protected $tableId;
    protected $allColumns;
    protected $tableStructure;
    protected $i;

    /**
     * Sets the table id
     *
     * @param string $id
     */
    function setTableId($id)
    {
        $this->tableId = str_replace('.', '_', $id);
    }

    /**
     * Output HTTP Content-Type header
     */
    protected function renderHeader()
    {
        @header('Content-Type: text/html; charset=utf-8');
    }

    /**
     * Computes the dataTable output and returns the string/binary
     *
     * @return string
     */
    function render()
    {
        $this->renderHeader();
        $this->tableStructure = array();
        $this->allColumns = array();
        $this->i = 0;

        return $this->renderTable($this->table);
    }

    /**
     * Computes the exception output and returns the string/binary
     *
     * @return string
     */
    function renderException()
    {
        $this->renderHeader();
        $exceptionMessage = $this->getExceptionMessage();
        return nl2br($exceptionMessage);
    }

    /**
     * Computes the output for the given data table
     *
     * @param Piwik_DataTable $table
     * @return string
     */
    protected function renderTable($table)
    {
        if (is_array($table)) // convert array to DataTable
        {
            $table = Piwik_DataTable::makeFromSimpleArray($table);
        }

        if ($table instanceof Piwik_DataTable_Array) {
            foreach ($table->getArray() as $date => $subtable) {
                if ($subtable->getRowsCount()) {
                    $this->buildTableStructure($subtable, '_' . $table->getKeyName(), $date);
                }
            }
        } else // Piwik_DataTable_Simple
        {
            if ($table->getRowsCount()) {
                $this->buildTableStructure($table);
            }
        }

        $out = $this->renderDataTable();
        return $out;
    }

    /**
     * Adds the given data table to the table structure array
     *
     * @param Piwik_DataTable_Simple $table
     * @param null|string $columnToAdd
     * @param null|string $valueToAdd
     * @throws Exception
     */
    protected function buildTableStructure($table, $columnToAdd = null, $valueToAdd = null)
    {
        $i = $this->i;
        $someMetadata = false;
        $someIdSubTable = false;

        /*
         * table = array
         * ROW1 = col1 | col2 | col3 | metadata | idSubTable
         * ROW2 = col1 | col2 (no value but appears) | col3 | metadata | idSubTable
         */
        if (!($table instanceof Piwik_DataTable)) {
            throw new Exception("HTML Renderer does not work with this combination of parameters");
        }
        foreach ($table->getRows() as $row) {
            if (isset($columnToAdd) && isset($valueToAdd)) {
                $this->allColumns[$columnToAdd] = true;
                $this->tableStructure[$i][$columnToAdd] = $valueToAdd;
            }

            foreach ($row->getColumns() as $column => $value) {
                $this->allColumns[$column] = true;
                $this->tableStructure[$i][$column] = $value;
            }

            $metadata = array();
            foreach ($row->getMetadata() as $name => $value) {
                if (is_string($value)) $value = "'$value'";
                $metadata[] = "'$name' => $value";
            }

            if (count($metadata) != 0) {
                $someMetadata = true;
                $metadata = implode("<br />", $metadata);
                $this->tableStructure[$i]['_metadata'] = $metadata;
            }

            $idSubtable = $row->getIdSubDataTable();
            if (!is_null($idSubtable)) {
                $someIdSubTable = true;
                $this->tableStructure[$i]['_idSubtable'] = $idSubtable;
            }

            $i++;
        }
        $this->i = $i;

        $this->allColumns['_metadata'] = $someMetadata;
        $this->allColumns['_idSubtable'] = $someIdSubTable;
    }

    /**
     * Computes the output for the table structure array
     *
     * @return string
     */
    protected function renderDataTable()
    {
        $html = "<table " . ($this->tableId ? "id=\"{$this->tableId}\" " : "") . "border=\"1\">\n<thead>\n\t<tr>\n";

        foreach ($this->allColumns as $name => $toDisplay) {
            if ($toDisplay !== false) {
                if ($name === 0) {
                    $name = 'value';
                }
                if ($this->translateColumnNames) {
                    $name = $this->translateColumnName($name);
                }
                $html .= "\t\t<th>$name</th>\n";
            }
        }

        $html .= "\t</tr>\n</thead>\n<tbody>\n";

        foreach ($this->tableStructure as $row) {
            $html .= "\t<tr>\n";
            foreach ($this->allColumns as $name => $toDisplay) {
                if ($toDisplay !== false) {
                    $value = "-";
                    if (isset($row[$name])) {
                        if (is_array($row[$name])) {
                            $value = "<pre>" . self::formatValueXml(var_export($row[$name], true)) . "</pre>";
                        } else {
                            $value = self::formatValueXml($row[$name]);
                        }
                    }

                    $html .= "\t\t<td>$value</td>\n";
                }
            }
            $html .= "\t</tr>\n";
        }

        $html .= "</tbody>\n</table>\n";

        return $html;
    }
}