Console.php
6 years ago
Csv.php
6 years ago
Html.php
6 years ago
Json.php
6 years ago
Php.php
6 years ago
Rss.php
6 years ago
Tsv.php
6 years ago
Xml.php
6 years ago
Html.php
196 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\DataTable\Renderer; |
| 10 | |
| 11 | use Exception; |
| 12 | use Piwik\DataTable; |
| 13 | use Piwik\DataTable\DataTableInterface; |
| 14 | use Piwik\DataTable\Renderer; |
| 15 | |
| 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 | /** |
| 29 | * Sets the table id |
| 30 | * |
| 31 | * @param string $id |
| 32 | */ |
| 33 | public function setTableId($id) |
| 34 | { |
| 35 | $this->tableId = str_replace('.', '_', $id); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Computes the dataTable output and returns the string/binary |
| 40 | * |
| 41 | * @return string |
| 42 | */ |
| 43 | public function render() |
| 44 | { |
| 45 | $this->tableStructure = array(); |
| 46 | $this->allColumns = array(); |
| 47 | $this->i = 0; |
| 48 | |
| 49 | return $this->renderTable($this->table); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Computes the output for the given data table |
| 54 | * |
| 55 | * @param DataTableInterface $table |
| 56 | * @return string |
| 57 | */ |
| 58 | protected function renderTable($table) |
| 59 | { |
| 60 | if (is_array($table)) { |
| 61 | // convert array to DataTable |
| 62 | |
| 63 | $table = DataTable::makeFromSimpleArray($table); |
| 64 | } |
| 65 | |
| 66 | if ($table instanceof DataTable\Map) { |
| 67 | foreach ($table->getDataTables() as $date => $subtable) { |
| 68 | if ($subtable->getRowsCount()) { |
| 69 | $this->buildTableStructure($subtable, '_' . $table->getKeyName(), $date); |
| 70 | } |
| 71 | } |
| 72 | } else { |
| 73 | // Simple |
| 74 | |
| 75 | if ($table->getRowsCount()) { |
| 76 | $this->buildTableStructure($table); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | $out = $this->renderDataTable(); |
| 81 | return $out; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Adds the given data table to the table structure array |
| 86 | * |
| 87 | * @param DataTable $table |
| 88 | * @param null|string $columnToAdd |
| 89 | * @param null|string $valueToAdd |
| 90 | * @throws Exception |
| 91 | */ |
| 92 | protected function buildTableStructure($table, $columnToAdd = null, $valueToAdd = null) |
| 93 | { |
| 94 | $i = $this->i; |
| 95 | $someMetadata = false; |
| 96 | $someIdSubTable = false; |
| 97 | |
| 98 | /* |
| 99 | * table = array |
| 100 | * ROW1 = col1 | col2 | col3 | metadata | idSubTable |
| 101 | * ROW2 = col1 | col2 (no value but appears) | col3 | metadata | idSubTable |
| 102 | */ |
| 103 | if (!($table instanceof DataTable)) { |
| 104 | throw new Exception("HTML Renderer does not work with this combination of parameters"); |
| 105 | } |
| 106 | foreach ($table->getRows() as $row) { |
| 107 | if (isset($columnToAdd) && isset($valueToAdd)) { |
| 108 | $this->allColumns[$columnToAdd] = true; |
| 109 | $this->tableStructure[$i][$columnToAdd] = $valueToAdd; |
| 110 | } |
| 111 | |
| 112 | foreach ($row->getColumns() as $column => $value) { |
| 113 | $this->allColumns[$column] = true; |
| 114 | $this->tableStructure[$i][$column] = $value; |
| 115 | } |
| 116 | |
| 117 | $metadata = array(); |
| 118 | foreach ($row->getMetadata() as $name => $value) { |
| 119 | if (is_string($value)) { |
| 120 | $value = "'$value'"; |
| 121 | } else if (is_array($value)) { |
| 122 | $value = var_export($value, true); |
| 123 | } else if ($value instanceof DataTable\DataTableInterface) { |
| 124 | $value = $this->renderTable($value); |
| 125 | } |
| 126 | $metadata[] = "'$name' => $value"; |
| 127 | } |
| 128 | |
| 129 | if (count($metadata) != 0) { |
| 130 | $someMetadata = true; |
| 131 | $metadata = implode("<br />", $metadata); |
| 132 | $this->tableStructure[$i]['_metadata'] = $metadata; |
| 133 | } |
| 134 | |
| 135 | $idSubtable = $row->getIdSubDataTable(); |
| 136 | if (!is_null($idSubtable)) { |
| 137 | $someIdSubTable = true; |
| 138 | $this->tableStructure[$i]['_idSubtable'] = $idSubtable; |
| 139 | } |
| 140 | |
| 141 | $i++; |
| 142 | } |
| 143 | $this->i = $i; |
| 144 | |
| 145 | $this->allColumns['_metadata'] = $someMetadata; |
| 146 | $this->allColumns['_idSubtable'] = $someIdSubTable; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Computes the output for the table structure array |
| 151 | * |
| 152 | * @return string |
| 153 | */ |
| 154 | protected function renderDataTable() |
| 155 | { |
| 156 | $html = "<table " . ($this->tableId ? "id=\"{$this->tableId}\" " : "") . "border=\"1\">\n<thead>\n\t<tr>\n"; |
| 157 | |
| 158 | foreach ($this->allColumns as $name => $toDisplay) { |
| 159 | if ($toDisplay !== false) { |
| 160 | if ($name === 0) { |
| 161 | $name = 'value'; |
| 162 | } |
| 163 | if ($this->translateColumnNames) { |
| 164 | $name = $this->translateColumnName($name); |
| 165 | } |
| 166 | $html .= "\t\t<th>$name</th>\n"; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | $html .= "\t</tr>\n</thead>\n<tbody>\n"; |
| 171 | |
| 172 | foreach ($this->tableStructure as $row) { |
| 173 | $html .= "\t<tr>\n"; |
| 174 | foreach ($this->allColumns as $name => $toDisplay) { |
| 175 | if ($toDisplay !== false) { |
| 176 | $value = "-"; |
| 177 | if (isset($row[$name])) { |
| 178 | if (is_array($row[$name])) { |
| 179 | $value = "<pre>" . self::formatValueXml(var_export($row[$name], true)) . "</pre>"; |
| 180 | } else { |
| 181 | $value = self::formatValueXml($row[$name]); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | $html .= "\t\t<td>$value</td>\n"; |
| 186 | } |
| 187 | } |
| 188 | $html .= "\t</tr>\n"; |
| 189 | } |
| 190 | |
| 191 | $html .= "</tbody>\n</table>\n"; |
| 192 | |
| 193 | return $html; |
| 194 | } |
| 195 | } |
| 196 |