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