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
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\DataTable\Renderer;
use Exception;
use Piwik\DataTable;
use Piwik\DataTable\DataTableInterface;
use Piwik\DataTable\Renderer;
/**
* Simple HTML output
* Does not work with recursive DataTable (i.e., when a row can be associated with a subDataTable).
*
*/
class Html extends Renderer
{
protected $tableId;
protected $allColumns;
protected $tableStructure;
protected $i;
/**
* Sets the table id
*
*/
public function setTableId(string $id): void
{
$this->tableId = str_replace('.', '_', $id);
}
/**
* Computes the dataTable output and returns the string/binary
*
*/
public function render(): string
{
$this->tableStructure = array();
$this->allColumns = array();
$this->i = 0;
return $this->renderTable($this->table);
}
/**
* Computes the output for the given data table
*
* @param DataTableInterface|array $table
*/
protected function renderTable($table): string
{
if (is_array($table)) {
// convert array to DataTable
$table = DataTable::makeFromSimpleArray($table);
}
if ($table instanceof DataTable\Map) {
foreach ($table->getDataTables() as $date => $subtable) {
if ($subtable->getRowsCount()) {
$this->buildTableStructure($subtable, '_' . $table->getKeyName(), $date);
}
}
} else {
// Simple
if ($table->getRowsCount()) {
$this->buildTableStructure($table);
}
}
return $this->renderDataTable();
}
/**
* Adds the given data table to the table structure array
*
* @param DataTable|DataTable\Map $table
* @param null|mixed $valueToAdd
* @throws Exception
*/
protected function buildTableStructure($table, ?string $columnToAdd = null, $valueToAdd = null): void
{
$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 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;
}
if (!$this->hideMetadata) {
$metadata = [];
foreach ($row->getMetadata() as $name => $value) {
if (is_string($value)) {
$value = "'$value'";
} elseif (is_array($value)) {
$value = var_export($value, true);
} elseif ($value instanceof DataTable\DataTableInterface) {
$value = $this->renderTable($value);
}
$metadata[] = "'$name' => $value";
}
if (count($metadata) != 0) {
$someMetadata = true;
$metadata = implode("<br />", $metadata);
$this->tableStructure[$i]['_metadata'] = $metadata;
}
if (!$this->hideIdSubDatatable) {
$idSubtable = $row->getIdSubDataTable();
if (!is_null($idSubtable)) {
$someIdSubTable = true;
$this->tableStructure[$i]['_idSubtable'] = $idSubtable;
}
}
}
$i++;
}
$this->i = $i;
if (!$this->hideMetadata) {
$this->allColumns['_metadata'] = $someMetadata;
if (!$this->hideIdSubDatatable) {
$this->allColumns['_idSubtable'] = $someIdSubTable;
}
}
}
/**
* Computes the output for the table structure array
*
*/
protected function renderDataTable(): string
{
$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;
}
}
|