Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Writer;
   4  
   5  abstract class BaseWriter implements IWriter
   6  {
   7      /**
   8       * Write charts that are defined in the workbook?
   9       * Identifies whether the Writer should write definitions for any charts that exist in the PhpSpreadsheet object;.
  10       *
  11       * @var bool
  12       */
  13      protected $includeCharts = false;
  14  
  15      /**
  16       * Pre-calculate formulas
  17       * Forces PhpSpreadsheet to recalculate all formulae in a workbook when saving, so that the pre-calculated values are
  18       * immediately available to MS Excel or other office spreadsheet viewer when opening the file.
  19       *
  20       * @var bool
  21       */
  22      protected $preCalculateFormulas = true;
  23  
  24      /**
  25       * Use disk caching where possible?
  26       *
  27       * @var bool
  28       */
  29      private $useDiskCaching = false;
  30  
  31      /**
  32       * Disk caching directory.
  33       *
  34       * @var string
  35       */
  36      private $diskCachingDirectory = './';
  37  
  38      /**
  39       * @var resource
  40       */
  41      protected $fileHandle;
  42  
  43      /**
  44       * @var bool
  45       */
  46      private $shouldCloseFile;
  47  
  48      public function getIncludeCharts()
  49      {
  50          return $this->includeCharts;
  51      }
  52  
  53      public function setIncludeCharts($pValue)
  54      {
  55          $this->includeCharts = (bool) $pValue;
  56  
  57          return $this;
  58      }
  59  
  60      public function getPreCalculateFormulas()
  61      {
  62          return $this->preCalculateFormulas;
  63      }
  64  
  65      public function setPreCalculateFormulas($pValue)
  66      {
  67          $this->preCalculateFormulas = (bool) $pValue;
  68  
  69          return $this;
  70      }
  71  
  72      public function getUseDiskCaching()
  73      {
  74          return $this->useDiskCaching;
  75      }
  76  
  77      public function setUseDiskCaching($pValue, $pDirectory = null)
  78      {
  79          $this->useDiskCaching = $pValue;
  80  
  81          if ($pDirectory !== null) {
  82              if (is_dir($pDirectory)) {
  83                  $this->diskCachingDirectory = $pDirectory;
  84              } else {
  85                  throw new Exception("Directory does not exist: $pDirectory");
  86              }
  87          }
  88  
  89          return $this;
  90      }
  91  
  92      public function getDiskCachingDirectory()
  93      {
  94          return $this->diskCachingDirectory;
  95      }
  96  
  97      /**
  98       * Open file handle.
  99       *
 100       * @param resource|string $filename
 101       */
 102      public function openFileHandle($filename): void
 103      {
 104          if (is_resource($filename)) {
 105              $this->fileHandle = $filename;
 106              $this->shouldCloseFile = false;
 107  
 108              return;
 109          }
 110  
 111          $fileHandle = $filename ? fopen($filename, 'wb+') : false;
 112          if ($fileHandle === false) {
 113              throw new Exception('Could not open file "' . $filename . '" for writing.');
 114          }
 115  
 116          $this->fileHandle = $fileHandle;
 117          $this->shouldCloseFile = true;
 118      }
 119  
 120      /**
 121       * Close file handle only if we opened it ourselves.
 122       */
 123      protected function maybeCloseFileHandle(): void
 124      {
 125          if ($this->shouldCloseFile) {
 126              if (!fclose($this->fileHandle)) {
 127                  throw new Exception('Could not close file after writing.');
 128              }
 129          }
 130      }
 131  }