[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/classes/dataformat/ -> spout_base.php (source)

   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  /**
  18   * Common Spout class for dataformat.
  19   *
  20   * @package    core
  21   * @subpackage dataformat
  22   * @copyright  2016 Brendan Heywood (brendan@catalyst-au.net)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core\dataformat;
  27  
  28  /**
  29   * Common Spout class for dataformat.
  30   *
  31   * @package    core
  32   * @subpackage dataformat
  33   * @copyright  2016 Brendan Heywood (brendan@catalyst-au.net)
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  abstract class spout_base extends \core\dataformat\base {
  37  
  38      /** @var $spouttype */
  39      protected $spouttype = '';
  40  
  41      /** @var $writer */
  42      protected $writer;
  43  
  44      /** @var $sheettitle */
  45      protected $sheettitle;
  46  
  47      /**
  48       * Output file headers to initialise the download of the file.
  49       */
  50      public function send_http_headers() {
  51          $this->writer = \Box\Spout\Writer\WriterFactory::create($this->spouttype);
  52          $filename = $this->filename . $this->get_extension();
  53          $this->writer->openToBrowser($filename);
  54          if ($this->sheettitle && $this->writer instanceof \Box\Spout\Writer\AbstractMultiSheetsWriter) {
  55              $sheet = $this->writer->getCurrentSheet();
  56              $sheet->setName($this->sheettitle);
  57          }
  58      }
  59  
  60      /**
  61       * Set the title of the worksheet inside a spreadsheet
  62       *
  63       * For some formats this will be ignored.
  64       *
  65       * @param string $title
  66       */
  67      public function set_sheettitle($title) {
  68          if (!$title) {
  69              return;
  70          }
  71          $this->sheettitle = $title;
  72      }
  73  
  74      /**
  75       * Write the start of the format
  76       *
  77       * @param array $columns
  78       */
  79      public function write_header($columns) {
  80          $this->writer->addRow(array_values((array)$columns));
  81      }
  82  
  83      /**
  84       * Write a single record
  85       *
  86       * @param object $record
  87       * @param int $rownum
  88       */
  89      public function write_record($record, $rownum) {
  90          $this->writer->addRow(array_values((array)$record));
  91      }
  92  
  93      /**
  94       * Write the end of the format
  95       *
  96       * @param array $columns
  97       */
  98      public function write_footer($columns) {
  99          $this->writer->close();
 100          $this->writer = null;
 101      }
 102  
 103  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1