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 DataTable $table * @return string */ protected function renderTable($table) { if (is_array($table)) // convert array to DataTable { $table = DataTable::makeFromSimpleArray($table); } if ($table instanceof DataTable\Map) { foreach ($table->getArray() as $date => $subtable) { if ($subtable->getRowsCount()) { $this->buildTableStructure($subtable, '_' . $table->getKeyName(), $date); } } } else // Simple { if ($table->getRowsCount()) { $this->buildTableStructure($table); } } $out = $this->renderDataTable(); return $out; } /** * Adds the given data table to the table structure array * * @param 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 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("
", $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 = "tableId ? "id=\"{$this->tableId}\" " : "") . "border=\"1\">\n\n\t\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\n"; } } $html .= "\t\n\n\n"; foreach ($this->tableStructure as $row) { $html .= "\t\n"; foreach ($this->allColumns as $name => $toDisplay) { if ($toDisplay !== false) { $value = "-"; if (isset($row[$name])) { if (is_array($row[$name])) { $value = "
" . self::formatValueXml(var_export($row[$name], true)) . "
"; } else { $value = self::formatValueXml($row[$name]); } } $html .= "\t\t\n"; } } $html .= "\t\n"; } $html .= "\n
$name
$value
\n"; return $html; } }