PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/classes/dataformat/spout_base.php

https://bitbucket.org/moodle/moodle
PHP | 145 lines | 58 code | 19 blank | 68 comment | 8 complexity | bf4fc6d3c22e9c0b96b4e39a8da19845 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Common Spout class for dataformat.
  18. *
  19. * @package core
  20. * @subpackage dataformat
  21. * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. namespace core\dataformat;
  25. /**
  26. * Common Spout class for dataformat.
  27. *
  28. * @package core
  29. * @subpackage dataformat
  30. * @copyright 2016 Brendan Heywood (brendan@catalyst-au.net)
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. abstract class spout_base extends \core\dataformat\base {
  34. /** @var $spouttype */
  35. protected $spouttype = '';
  36. /** @var $writer */
  37. protected $writer;
  38. /** @var $sheettitle */
  39. protected $sheettitle;
  40. /** @var $renamecurrentsheet */
  41. protected $renamecurrentsheet = false;
  42. /**
  43. * Output file headers to initialise the download of the file.
  44. */
  45. public function send_http_headers() {
  46. $this->writer = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createWriter($this->spouttype);
  47. if (method_exists($this->writer, 'setTempFolder')) {
  48. $this->writer->setTempFolder(make_request_directory());
  49. }
  50. $filename = $this->filename . $this->get_extension();
  51. if (PHPUNIT_TEST) {
  52. $this->writer->openToFile('php://output');
  53. } else {
  54. $this->writer->openToBrowser($filename);
  55. }
  56. // By default one sheet is always created, but we want to rename it when we call start_sheet().
  57. $this->renamecurrentsheet = true;
  58. }
  59. /**
  60. * Set the dataformat to be output to current file
  61. */
  62. public function start_output_to_file(): void {
  63. $this->writer = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createWriter($this->spouttype);
  64. if (method_exists($this->writer, 'setTempFolder')) {
  65. $this->writer->setTempFolder(make_request_directory());
  66. }
  67. $this->writer->openToFile($this->filepath);
  68. // By default one sheet is always created, but we want to rename it when we call start_sheet().
  69. $this->renamecurrentsheet = true;
  70. $this->start_output();
  71. }
  72. /**
  73. * Set the title of the worksheet inside a spreadsheet
  74. *
  75. * For some formats this will be ignored.
  76. *
  77. * @param string $title
  78. */
  79. public function set_sheettitle($title) {
  80. $this->sheettitle = $title;
  81. }
  82. /**
  83. * Write the start of the sheet we will be adding data to.
  84. *
  85. * @param array $columns
  86. */
  87. public function start_sheet($columns) {
  88. if ($this->sheettitle && $this->writer instanceof \Box\Spout\Writer\WriterMultiSheetsAbstract) {
  89. if ($this->renamecurrentsheet) {
  90. $sheet = $this->writer->getCurrentSheet();
  91. $this->renamecurrentsheet = false;
  92. } else {
  93. $sheet = $this->writer->addNewSheetAndMakeItCurrent();
  94. }
  95. $sheet->setName($this->sheettitle);
  96. }
  97. $row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray((array)$columns);
  98. $this->writer->addRow($row);
  99. }
  100. /**
  101. * Write a single record
  102. *
  103. * @param array $record
  104. * @param int $rownum
  105. */
  106. public function write_record($record, $rownum) {
  107. $row = \Box\Spout\Writer\Common\Creator\WriterEntityFactory::createRowFromArray($this->format_record($record));
  108. $this->writer->addRow($row);
  109. }
  110. /**
  111. * Write the end of the file.
  112. */
  113. public function close_output() {
  114. $this->writer->close();
  115. $this->writer = null;
  116. }
  117. /**
  118. * Write data to disk
  119. *
  120. * @return bool
  121. */
  122. public function close_output_to_file(): bool {
  123. $this->close_output();
  124. return true;
  125. }
  126. }