MDL-67547 dataformat: allow plugins to declare support for HTML.

This commit is contained in:
Paul Holden 2019-12-18 21:38:40 +00:00
parent a09eb2697f
commit 118a109499
6 changed files with 62 additions and 10 deletions

View file

@ -92,6 +92,15 @@ table {
echo \html_writer::end_tag('tr'); echo \html_writer::end_tag('tr');
} }
/**
* Method to define whether the dataformat supports export of HTML
*
* @return bool
*/
public function supports_html(): bool {
return true;
}
/** /**
* Write a single record * Write a single record
* *
@ -99,6 +108,8 @@ table {
* @param int $rownum * @param int $rownum
*/ */
public function write_record($record, $rownum) { public function write_record($record, $rownum) {
$record = $this->format_record($record);
echo \html_writer::start_tag('tr'); echo \html_writer::start_tag('tr');
foreach ($record as $cell) { foreach ($record as $cell) {
echo \html_writer::tag('td', $cell); echo \html_writer::tag('td', $cell);

View file

@ -80,7 +80,7 @@ class writer extends \core\dataformat\base {
echo ","; echo ",";
} }
echo json_encode($record, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); echo json_encode($this->format_record($record), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
$this->sheetdatadded = true; $this->sheetdatadded = true;
} }

View file

@ -93,14 +93,25 @@ class writer extends \core\dataformat\base {
$this->print_heading(); $this->print_heading();
} }
/**
* Method to define whether the dataformat supports export of HTML
*
* @return bool
*/
public function supports_html(): bool {
return true;
}
/**
* Write a single record
*
* @param array $record
* @param int $rownum
*/
public function write_record($record, $rownum) { public function write_record($record, $rownum) {
$rowheight = 0; $rowheight = 0;
// If $record is an object convert it to an array. $record = $this->format_record($record);
if (is_object($record)) {
$record = (array)$record;
}
foreach ($record as $cell) { foreach ($record as $cell) {
$rowheight = max($rowheight, $this->pdf->getStringHeight($this->colwidth, $cell, false, true, '', 1)); $rowheight = max($rowheight, $this->pdf->getStringHeight($this->colwidth, $cell, false, true, '', 1));
} }
@ -123,7 +134,7 @@ class writer extends \core\dataformat\base {
// Determine whether we're at the last element of the record. // Determine whether we're at the last element of the record.
$nextposition = ($lastkey === $key) ? 1 : 0; $nextposition = ($lastkey === $key) ? 1 : 0;
// Write the element. // Write the element.
$this->pdf->Multicell($this->colwidth, $rowheight, $cell, 1, 'L', false, $nextposition); $this->pdf->writeHTMLCell($this->colwidth, $rowheight, '', '', $cell, 1, $nextposition, false, true, 'L');
} }
} }

View file

@ -145,6 +145,27 @@ abstract class base {
// Override me if needed. // Override me if needed.
} }
/**
* Method to define whether the dataformat supports export of HTML
*
* @return bool
*/
public function supports_html(): bool {
return false;
}
/**
* Apply formatting to the cells of a given record
*
* @param array|\stdClass $record
* @return array
*/
protected function format_record($record): array {
$record = (array)$record;
return $record;
}
/** /**
* Write a single record * Write a single record
* *

View file

@ -116,11 +116,11 @@ abstract class spout_base extends \core\dataformat\base {
/** /**
* Write a single record * Write a single record
* *
* @param object $record * @param array $record
* @param int $rownum * @param int $rownum
*/ */
public function write_record($record, $rownum) { public function write_record($record, $rownum) {
$row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray((array)$record); $row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray($this->format_record($record));
$this->writer->addRow($row); $this->writer->addRow($row);
} }

View file

@ -2012,7 +2012,7 @@ class table_default_export_format_parent {
*/ */
class table_dataformat_export_format extends table_default_export_format_parent { class table_dataformat_export_format extends table_default_export_format_parent {
/** @var $dataformat */ /** @var \core\dataformat\base $dataformat */
protected $dataformat; protected $dataformat;
/** @var $rownum */ /** @var $rownum */
@ -2047,6 +2047,15 @@ class table_dataformat_export_format extends table_default_export_format_parent
\core\session\manager::write_close(); \core\session\manager::write_close();
} }
/**
* Whether the current dataformat supports export of HTML
*
* @return bool
*/
public function supports_html(): bool {
return $this->dataformat->supports_html();
}
/** /**
* Start document * Start document
* *