PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.7
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.7
5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / DataTable / Renderer / Html.php
matomo / app / core / DataTable / Renderer Last commit date
Console.php 2 years ago Csv.php 2 years ago Html.php 2 years ago Json.php 2 years ago Rss.php 2 years ago Tsv.php 2 years ago Xml.php 2 years ago
Html.php
177 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 *
9 */
10 namespace Piwik\DataTable\Renderer;
11
12 use Exception;
13 use Piwik\DataTable;
14 use Piwik\DataTable\DataTableInterface;
15 use Piwik\DataTable\Renderer;
16 /**
17 * Simple HTML output
18 * Does not work with recursive DataTable (i.e., when a row can be associated with a subDataTable).
19 *
20 */
21 class Html extends Renderer
22 {
23 protected $tableId;
24 protected $allColumns;
25 protected $tableStructure;
26 protected $i;
27 /**
28 * Sets the table id
29 *
30 * @param string $id
31 */
32 public function setTableId($id)
33 {
34 $this->tableId = str_replace('.', '_', $id);
35 }
36 /**
37 * Computes the dataTable output and returns the string/binary
38 *
39 * @return string
40 */
41 public function render()
42 {
43 $this->tableStructure = array();
44 $this->allColumns = array();
45 $this->i = 0;
46 return $this->renderTable($this->table);
47 }
48 /**
49 * Computes the output for the given data table
50 *
51 * @param DataTableInterface $table
52 * @return string
53 */
54 protected function renderTable($table)
55 {
56 if (is_array($table)) {
57 // convert array to DataTable
58 $table = DataTable::makeFromSimpleArray($table);
59 }
60 if ($table instanceof DataTable\Map) {
61 foreach ($table->getDataTables() as $date => $subtable) {
62 if ($subtable->getRowsCount()) {
63 $this->buildTableStructure($subtable, '_' . $table->getKeyName(), $date);
64 }
65 }
66 } else {
67 // Simple
68 if ($table->getRowsCount()) {
69 $this->buildTableStructure($table);
70 }
71 }
72 $out = $this->renderDataTable();
73 return $out;
74 }
75 /**
76 * Adds the given data table to the table structure array
77 *
78 * @param DataTable $table
79 * @param null|string $columnToAdd
80 * @param null|string $valueToAdd
81 * @throws Exception
82 */
83 protected function buildTableStructure($table, $columnToAdd = null, $valueToAdd = null)
84 {
85 $i = $this->i;
86 $someMetadata = false;
87 $someIdSubTable = false;
88 /*
89 * table = array
90 * ROW1 = col1 | col2 | col3 | metadata | idSubTable
91 * ROW2 = col1 | col2 (no value but appears) | col3 | metadata | idSubTable
92 */
93 if (!$table instanceof DataTable) {
94 throw new Exception("HTML Renderer does not work with this combination of parameters");
95 }
96 foreach ($table->getRows() as $row) {
97 if (isset($columnToAdd) && isset($valueToAdd)) {
98 $this->allColumns[$columnToAdd] = true;
99 $this->tableStructure[$i][$columnToAdd] = $valueToAdd;
100 }
101 foreach ($row->getColumns() as $column => $value) {
102 $this->allColumns[$column] = true;
103 $this->tableStructure[$i][$column] = $value;
104 }
105 $metadata = array();
106 foreach ($row->getMetadata() as $name => $value) {
107 if (is_string($value)) {
108 $value = "'{$value}'";
109 } else {
110 if (is_array($value)) {
111 $value = var_export($value, true);
112 } else {
113 if ($value instanceof DataTable\DataTableInterface) {
114 $value = $this->renderTable($value);
115 }
116 }
117 }
118 $metadata[] = "'{$name}' => {$value}";
119 }
120 if (count($metadata) != 0) {
121 $someMetadata = true;
122 $metadata = implode("<br />", $metadata);
123 $this->tableStructure[$i]['_metadata'] = $metadata;
124 }
125 $idSubtable = $row->getIdSubDataTable();
126 if (!is_null($idSubtable)) {
127 $someIdSubTable = true;
128 $this->tableStructure[$i]['_idSubtable'] = $idSubtable;
129 }
130 $i++;
131 }
132 $this->i = $i;
133 $this->allColumns['_metadata'] = $someMetadata;
134 $this->allColumns['_idSubtable'] = $someIdSubTable;
135 }
136 /**
137 * Computes the output for the table structure array
138 *
139 * @return string
140 */
141 protected function renderDataTable()
142 {
143 $html = "<table " . ($this->tableId ? "id=\"{$this->tableId}\" " : "") . "border=\"1\">\n<thead>\n\t<tr>\n";
144 foreach ($this->allColumns as $name => $toDisplay) {
145 if ($toDisplay !== false) {
146 if ($name === 0) {
147 $name = 'value';
148 }
149 if ($this->translateColumnNames) {
150 $name = $this->translateColumnName($name);
151 }
152 $html .= "\t\t<th>{$name}</th>\n";
153 }
154 }
155 $html .= "\t</tr>\n</thead>\n<tbody>\n";
156 foreach ($this->tableStructure as $row) {
157 $html .= "\t<tr>\n";
158 foreach ($this->allColumns as $name => $toDisplay) {
159 if ($toDisplay !== false) {
160 $value = "-";
161 if (isset($row[$name])) {
162 if (is_array($row[$name])) {
163 $value = "<pre>" . self::formatValueXml(var_export($row[$name], true)) . "</pre>";
164 } else {
165 $value = self::formatValueXml($row[$name]);
166 }
167 }
168 $html .= "\t\t<td>{$value}</td>\n";
169 }
170 }
171 $html .= "\t</tr>\n";
172 }
173 $html .= "</tbody>\n</table>\n";
174 return $html;
175 }
176 }
177