Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Writer;
   4  
   5  use PhpOffice\PhpSpreadsheet\Spreadsheet;
   6  use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
   7  use PhpOffice\PhpSpreadsheet\Writer\Ods\Content;
   8  use PhpOffice\PhpSpreadsheet\Writer\Ods\Meta;
   9  use PhpOffice\PhpSpreadsheet\Writer\Ods\MetaInf;
  10  use PhpOffice\PhpSpreadsheet\Writer\Ods\Mimetype;
  11  use PhpOffice\PhpSpreadsheet\Writer\Ods\Settings;
  12  use PhpOffice\PhpSpreadsheet\Writer\Ods\Styles;
  13  use PhpOffice\PhpSpreadsheet\Writer\Ods\Thumbnails;
  14  use ZipStream\Exception\OverflowException;
  15  use ZipStream\Option\Archive;
  16  use ZipStream\ZipStream;
  17  
  18  class Ods extends BaseWriter
  19  {
  20      /**
  21       * Private PhpSpreadsheet.
  22       *
  23       * @var Spreadsheet
  24       */
  25      private $spreadSheet;
  26  
  27      /**
  28       * @var Content
  29       */
  30      private $writerPartContent;
  31  
  32      /**
  33       * @var Meta
  34       */
  35      private $writerPartMeta;
  36  
  37      /**
  38       * @var MetaInf
  39       */
  40      private $writerPartMetaInf;
  41  
  42      /**
  43       * @var Mimetype
  44       */
  45      private $writerPartMimetype;
  46  
  47      /**
  48       * @var Settings
  49       */
  50      private $writerPartSettings;
  51  
  52      /**
  53       * @var Styles
  54       */
  55      private $writerPartStyles;
  56  
  57      /**
  58       * @var Thumbnails
  59       */
  60      private $writerPartThumbnails;
  61  
  62      /**
  63       * Create a new Ods.
  64       */
  65      public function __construct(Spreadsheet $spreadsheet)
  66      {
  67          $this->setSpreadsheet($spreadsheet);
  68  
  69          $this->writerPartContent = new Content($this);
  70          $this->writerPartMeta = new Meta($this);
  71          $this->writerPartMetaInf = new MetaInf($this);
  72          $this->writerPartMimetype = new Mimetype($this);
  73          $this->writerPartSettings = new Settings($this);
  74          $this->writerPartStyles = new Styles($this);
  75          $this->writerPartThumbnails = new Thumbnails($this);
  76      }
  77  
  78      public function getWriterPartContent(): Content
  79      {
  80          return $this->writerPartContent;
  81      }
  82  
  83      public function getWriterPartMeta(): Meta
  84      {
  85          return $this->writerPartMeta;
  86      }
  87  
  88      public function getWriterPartMetaInf(): MetaInf
  89      {
  90          return $this->writerPartMetaInf;
  91      }
  92  
  93      public function getWriterPartMimetype(): Mimetype
  94      {
  95          return $this->writerPartMimetype;
  96      }
  97  
  98      public function getWriterPartSettings(): Settings
  99      {
 100          return $this->writerPartSettings;
 101      }
 102  
 103      public function getWriterPartStyles(): Styles
 104      {
 105          return $this->writerPartStyles;
 106      }
 107  
 108      public function getWriterPartThumbnails(): Thumbnails
 109      {
 110          return $this->writerPartThumbnails;
 111      }
 112  
 113      /**
 114       * Save PhpSpreadsheet to file.
 115       *
 116       * @param resource|string $filename
 117       */
 118      public function save($filename, int $flags = 0): void
 119      {
 120          $this->processFlags($flags);
 121  
 122          // garbage collect
 123          $this->spreadSheet->garbageCollect();
 124  
 125          $this->openFileHandle($filename);
 126  
 127          $zip = $this->createZip();
 128  
 129          $zip->addFile('META-INF/manifest.xml', $this->getWriterPartMetaInf()->write());
 130          $zip->addFile('Thumbnails/thumbnail.png', $this->getWriterPartthumbnails()->write());
 131          // Settings always need to be written before Content; Styles after Content
 132          $zip->addFile('settings.xml', $this->getWriterPartsettings()->write());
 133          $zip->addFile('content.xml', $this->getWriterPartcontent()->write());
 134          $zip->addFile('meta.xml', $this->getWriterPartmeta()->write());
 135          $zip->addFile('mimetype', $this->getWriterPartmimetype()->write());
 136          $zip->addFile('styles.xml', $this->getWriterPartstyles()->write());
 137  
 138          // Close file
 139          try {
 140              $zip->finish();
 141          } catch (OverflowException $e) {
 142              throw new WriterException('Could not close resource.');
 143          }
 144  
 145          $this->maybeCloseFileHandle();
 146      }
 147  
 148      /**
 149       * Create zip object.
 150       *
 151       * @return ZipStream
 152       */
 153      private function createZip()
 154      {
 155          // Try opening the ZIP file
 156          if (!is_resource($this->fileHandle)) {
 157              throw new WriterException('Could not open resource for writing.');
 158          }
 159  
 160          // Create new ZIP stream
 161          $options = new Archive();
 162          $options->setEnableZip64(false);
 163          $options->setOutputStream($this->fileHandle);
 164  
 165          return new ZipStream(null, $options);
 166      }
 167  
 168      /**
 169       * Get Spreadsheet object.
 170       *
 171       * @return Spreadsheet
 172       */
 173      public function getSpreadsheet()
 174      {
 175          return $this->spreadSheet;
 176      }
 177  
 178      /**
 179       * Set Spreadsheet object.
 180       *
 181       * @param Spreadsheet $spreadsheet PhpSpreadsheet object
 182       *
 183       * @return $this
 184       */
 185      public function setSpreadsheet(Spreadsheet $spreadsheet)
 186      {
 187          $this->spreadSheet = $spreadsheet;
 188  
 189          return $this;
 190      }
 191  }