Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
   4  
   5  use PhpOffice\PhpSpreadsheet\Spreadsheet;
   6  use SimpleXMLElement;
   7  
   8  class WorkbookView
   9  {
  10      /**
  11       * @var Spreadsheet
  12       */
  13      private $spreadsheet;
  14  
  15      public function __construct(Spreadsheet $spreadsheet)
  16      {
  17          $this->spreadsheet = $spreadsheet;
  18      }
  19  
  20      /**
  21       * @param mixed $mainNS
  22       */
  23      public function viewSettings(SimpleXMLElement $xmlWorkbook, $mainNS, array $mapSheetId, bool $readDataOnly): void
  24      {
  25          if ($this->spreadsheet->getSheetCount() == 0) {
  26              $this->spreadsheet->createSheet();
  27          }
  28          // Default active sheet index to the first loaded worksheet from the file
  29          $this->spreadsheet->setActiveSheetIndex(0);
  30  
  31          $workbookView = $xmlWorkbook->children($mainNS)->bookViews->workbookView;
  32          if ($readDataOnly !== true && !empty($workbookView)) {
  33              $workbookViewAttributes = self::testSimpleXml(self::getAttributes($workbookView));
  34              // active sheet index
  35              $activeTab = (int) $workbookViewAttributes->activeTab; // refers to old sheet index
  36              // keep active sheet index if sheet is still loaded, else first sheet is set as the active worksheet
  37              if (isset($mapSheetId[$activeTab]) && $mapSheetId[$activeTab] !== null) {
  38                  $this->spreadsheet->setActiveSheetIndex($mapSheetId[$activeTab]);
  39              }
  40  
  41              $this->horizontalScroll($workbookViewAttributes);
  42              $this->verticalScroll($workbookViewAttributes);
  43              $this->sheetTabs($workbookViewAttributes);
  44              $this->minimized($workbookViewAttributes);
  45              $this->autoFilterDateGrouping($workbookViewAttributes);
  46              $this->firstSheet($workbookViewAttributes);
  47              $this->visibility($workbookViewAttributes);
  48              $this->tabRatio($workbookViewAttributes);
  49          }
  50      }
  51  
  52      /**
  53       * @param mixed $value
  54       */
  55      public static function testSimpleXml($value): SimpleXMLElement
  56      {
  57          return ($value instanceof SimpleXMLElement)
  58              ? $value
  59              : new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root></root>');
  60      }
  61  
  62      public static function getAttributes(?SimpleXMLElement $value, string $ns = ''): SimpleXMLElement
  63      {
  64          return self::testSimpleXml($value === null ? $value : $value->attributes($ns));
  65      }
  66  
  67      /**
  68       * Convert an 'xsd:boolean' XML value to a PHP boolean value.
  69       * A valid 'xsd:boolean' XML value can be one of the following
  70       * four values: 'true', 'false', '1', '0'.  It is case sensitive.
  71       *
  72       * Note that just doing '(bool) $xsdBoolean' is not safe,
  73       * since '(bool) "false"' returns true.
  74       *
  75       * @see https://www.w3.org/TR/xmlschema11-2/#boolean
  76       *
  77       * @param string $xsdBoolean An XML string value of type 'xsd:boolean'
  78       *
  79       * @return bool  Boolean value
  80       */
  81      private function castXsdBooleanToBool(string $xsdBoolean): bool
  82      {
  83          if ($xsdBoolean === 'false') {
  84              return false;
  85          }
  86  
  87          return (bool) $xsdBoolean;
  88      }
  89  
  90      private function horizontalScroll(SimpleXMLElement $workbookViewAttributes): void
  91      {
  92          if (isset($workbookViewAttributes->showHorizontalScroll)) {
  93              $showHorizontalScroll = (string) $workbookViewAttributes->showHorizontalScroll;
  94              $this->spreadsheet->setShowHorizontalScroll($this->castXsdBooleanToBool($showHorizontalScroll));
  95          }
  96      }
  97  
  98      private function verticalScroll(SimpleXMLElement $workbookViewAttributes): void
  99      {
 100          if (isset($workbookViewAttributes->showVerticalScroll)) {
 101              $showVerticalScroll = (string) $workbookViewAttributes->showVerticalScroll;
 102              $this->spreadsheet->setShowVerticalScroll($this->castXsdBooleanToBool($showVerticalScroll));
 103          }
 104      }
 105  
 106      private function sheetTabs(SimpleXMLElement $workbookViewAttributes): void
 107      {
 108          if (isset($workbookViewAttributes->showSheetTabs)) {
 109              $showSheetTabs = (string) $workbookViewAttributes->showSheetTabs;
 110              $this->spreadsheet->setShowSheetTabs($this->castXsdBooleanToBool($showSheetTabs));
 111          }
 112      }
 113  
 114      private function minimized(SimpleXMLElement $workbookViewAttributes): void
 115      {
 116          if (isset($workbookViewAttributes->minimized)) {
 117              $minimized = (string) $workbookViewAttributes->minimized;
 118              $this->spreadsheet->setMinimized($this->castXsdBooleanToBool($minimized));
 119          }
 120      }
 121  
 122      private function autoFilterDateGrouping(SimpleXMLElement $workbookViewAttributes): void
 123      {
 124          if (isset($workbookViewAttributes->autoFilterDateGrouping)) {
 125              $autoFilterDateGrouping = (string) $workbookViewAttributes->autoFilterDateGrouping;
 126              $this->spreadsheet->setAutoFilterDateGrouping($this->castXsdBooleanToBool($autoFilterDateGrouping));
 127          }
 128      }
 129  
 130      private function firstSheet(SimpleXMLElement $workbookViewAttributes): void
 131      {
 132          if (isset($workbookViewAttributes->firstSheet)) {
 133              $firstSheet = (string) $workbookViewAttributes->firstSheet;
 134              $this->spreadsheet->setFirstSheetIndex((int) $firstSheet);
 135          }
 136      }
 137  
 138      private function visibility(SimpleXMLElement $workbookViewAttributes): void
 139      {
 140          if (isset($workbookViewAttributes->visibility)) {
 141              $visibility = (string) $workbookViewAttributes->visibility;
 142              $this->spreadsheet->setVisibility($visibility);
 143          }
 144      }
 145  
 146      private function tabRatio(SimpleXMLElement $workbookViewAttributes): void
 147      {
 148          if (isset($workbookViewAttributes->tabRatio)) {
 149              $tabRatio = (string) $workbookViewAttributes->tabRatio;
 150              $this->spreadsheet->setTabRatio((int) $tabRatio);
 151          }
 152      }
 153  }