diff --git a/lib/classes/dataformat/spout_base.php b/lib/classes/dataformat/spout_base.php index 6e622ea8beb..6b0c10b34b1 100644 --- a/lib/classes/dataformat/spout_base.php +++ b/lib/classes/dataformat/spout_base.php @@ -44,6 +44,9 @@ abstract class spout_base extends \core\dataformat\base { /** @var $sheettitle */ protected $sheettitle; + /** @var $renamecurrentsheet */ + protected $renamecurrentsheet = false; + /** * Output file headers to initialise the download of the file. */ @@ -54,10 +57,9 @@ abstract class spout_base extends \core\dataformat\base { } $filename = $this->filename . $this->get_extension(); $this->writer->openToBrowser($filename); - if ($this->sheettitle && $this->writer instanceof \Box\Spout\Writer\AbstractMultiSheetsWriter) { - $sheet = $this->writer->getCurrentSheet(); - $sheet->setName($this->sheettitle); - } + + // By default one sheet is always created, but we want to rename it when we call start_sheet(). + $this->renamecurrentsheet = true; } /** @@ -68,9 +70,6 @@ abstract class spout_base extends \core\dataformat\base { * @param string $title */ public function set_sheettitle($title) { - if (!$title) { - return; - } $this->sheettitle = $title; } @@ -80,6 +79,15 @@ abstract class spout_base extends \core\dataformat\base { * @param array $columns */ public function start_sheet($columns) { + if ($this->sheettitle && $this->writer instanceof \Box\Spout\Writer\AbstractMultiSheetsWriter) { + if ($this->renamecurrentsheet) { + $sheet = $this->writer->getCurrentSheet(); + $this->renamecurrentsheet = false; + } else { + $sheet = $this->writer->addNewSheetAndMakeItCurrent(); + } + $sheet->setName($this->sheettitle); + } $this->writer->addRow(array_values((array)$columns)); } diff --git a/lib/tablelib.php b/lib/tablelib.php index 4dd84f8de56..701267d4dfc 100644 --- a/lib/tablelib.php +++ b/lib/tablelib.php @@ -125,6 +125,12 @@ class flexible_table { */ private $prefs = array(); + /** @var $sheettitle */ + protected $sheettitle; + + /** @var $filename */ + protected $filename; + /** * Constructor * @param string $uniqueid all tables have to have a unique id, this is used @@ -180,7 +186,7 @@ class flexible_table { } else if (is_null($this->exportclass) && !empty($this->download)) { $this->exportclass = new table_dataformat_export_format($this, $this->download); if (!$this->exportclass->document_started()) { - $this->exportclass->start_document($this->filename); + $this->exportclass->start_document($this->filename, $this->sheettitle); } } return $this->exportclass; @@ -1741,11 +1747,14 @@ class table_dataformat_export_format extends table_default_export_format_parent * Start document * * @param string $filename + * @param string $sheettitle */ - public function start_document($filename) { - $this->filename = $filename; + public function start_document($filename, $sheettitle) { $this->documentstarted = true; $this->dataformat->set_filename($filename); + $this->dataformat->send_http_headers(); + $this->dataformat->set_sheettitle($sheettitle); + $this->dataformat->start_output(); } /** @@ -1755,7 +1764,6 @@ class table_dataformat_export_format extends table_default_export_format_parent */ public function start_table($sheettitle) { $this->dataformat->set_sheettitle($sheettitle); - $this->dataformat->send_http_headers(); } /** @@ -1765,7 +1773,13 @@ class table_dataformat_export_format extends table_default_export_format_parent */ public function output_headers($headers) { $this->columns = $headers; - $this->dataformat->write_header($headers); + if (method_exists($this->dataformat, 'write_header')) { + error_log('The function write_header() does not support multiple tables. In order to support multiple tables you ' . + 'must implement start_output() and start_sheet() and remove write_header() in your dataformat.'); + $this->dataformat->write_header($headers); + } else { + $this->dataformat->start_sheet($headers); + } } /** @@ -1782,15 +1796,21 @@ class table_dataformat_export_format extends table_default_export_format_parent * Finish export */ public function finish_table() { - $this->dataformat->write_footer($this->columns); + if (method_exists($this->dataformat, 'write_footer')) { + error_log('The function write_footer() does not support multiple tables. In order to support multiple tables you ' . + 'must implement close_sheet() and close_output() and remove write_footer() in your dataformat.'); + $this->dataformat->write_footer($this->columns); + } else { + $this->dataformat->close_sheet($this->columns); + } } /** * Finish download */ public function finish_document() { - exit; + $this->dataformat->close_output(); + exit(); } - }