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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Style;
   4  
   5  class Protection extends Supervisor
   6  {
   7      /** Protection styles */
   8      const PROTECTION_INHERIT = 'inherit';
   9      const PROTECTION_PROTECTED = 'protected';
  10      const PROTECTION_UNPROTECTED = 'unprotected';
  11  
  12      /**
  13       * Locked.
  14       *
  15       * @var string
  16       */
  17      protected $locked;
  18  
  19      /**
  20       * Hidden.
  21       *
  22       * @var string
  23       */
  24      protected $hidden;
  25  
  26      /**
  27       * Create a new Protection.
  28       *
  29       * @param bool $isSupervisor Flag indicating if this is a supervisor or not
  30       *                                    Leave this value at default unless you understand exactly what
  31       *                                        its ramifications are
  32       * @param bool $isConditional Flag indicating if this is a conditional style or not
  33       *                                    Leave this value at default unless you understand exactly what
  34       *                                        its ramifications are
  35       */
  36      public function __construct($isSupervisor = false, $isConditional = false)
  37      {
  38          // Supervisor?
  39          parent::__construct($isSupervisor);
  40  
  41          // Initialise values
  42          if (!$isConditional) {
  43              $this->locked = self::PROTECTION_INHERIT;
  44              $this->hidden = self::PROTECTION_INHERIT;
  45          }
  46      }
  47  
  48      /**
  49       * Get the shared style component for the currently active cell in currently active sheet.
  50       * Only used for style supervisor.
  51       *
  52       * @return Protection
  53       */
  54      public function getSharedComponent()
  55      {
  56          /** @var Style */
  57          $parent = $this->parent;
  58  
  59          return $parent->getSharedComponent()->getProtection();
  60      }
  61  
  62      /**
  63       * Build style array from subcomponents.
  64       *
  65       * @param array $array
  66       *
  67       * @return array
  68       */
  69      public function getStyleArray($array)
  70      {
  71          return ['protection' => $array];
  72      }
  73  
  74      /**
  75       * Apply styles from array.
  76       *
  77       * <code>
  78       * $spreadsheet->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray(
  79       *     [
  80       *         'locked' => TRUE,
  81       *         'hidden' => FALSE
  82       *     ]
  83       * );
  84       * </code>
  85       *
  86       * @param array $styleArray Array containing style information
  87       *
  88       * @return $this
  89       */
  90      public function applyFromArray(array $styleArray)
  91      {
  92          if ($this->isSupervisor) {
  93              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($styleArray));
  94          } else {
  95              if (isset($styleArray['locked'])) {
  96                  $this->setLocked($styleArray['locked']);
  97              }
  98              if (isset($styleArray['hidden'])) {
  99                  $this->setHidden($styleArray['hidden']);
 100              }
 101          }
 102  
 103          return $this;
 104      }
 105  
 106      /**
 107       * Get locked.
 108       *
 109       * @return string
 110       */
 111      public function getLocked()
 112      {
 113          if ($this->isSupervisor) {
 114              return $this->getSharedComponent()->getLocked();
 115          }
 116  
 117          return $this->locked;
 118      }
 119  
 120      /**
 121       * Set locked.
 122       *
 123       * @param string $lockType see self::PROTECTION_*
 124       *
 125       * @return $this
 126       */
 127      public function setLocked($lockType)
 128      {
 129          if ($this->isSupervisor) {
 130              $styleArray = $this->getStyleArray(['locked' => $lockType]);
 131              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 132          } else {
 133              $this->locked = $lockType;
 134          }
 135  
 136          return $this;
 137      }
 138  
 139      /**
 140       * Get hidden.
 141       *
 142       * @return string
 143       */
 144      public function getHidden()
 145      {
 146          if ($this->isSupervisor) {
 147              return $this->getSharedComponent()->getHidden();
 148          }
 149  
 150          return $this->hidden;
 151      }
 152  
 153      /**
 154       * Set hidden.
 155       *
 156       * @param string $hiddenType see self::PROTECTION_*
 157       *
 158       * @return $this
 159       */
 160      public function setHidden($hiddenType)
 161      {
 162          if ($this->isSupervisor) {
 163              $styleArray = $this->getStyleArray(['hidden' => $hiddenType]);
 164              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 165          } else {
 166              $this->hidden = $hiddenType;
 167          }
 168  
 169          return $this;
 170      }
 171  
 172      /**
 173       * Get hash code.
 174       *
 175       * @return string Hash code
 176       */
 177      public function getHashCode()
 178      {
 179          if ($this->isSupervisor) {
 180              return $this->getSharedComponent()->getHashCode();
 181          }
 182  
 183          return md5(
 184              $this->locked .
 185              $this->hidden .
 186              __CLASS__
 187          );
 188      }
 189  
 190      protected function exportArray1(): array
 191      {
 192          $exportedArray = [];
 193          $this->exportArray2($exportedArray, 'locked', $this->getLocked());
 194          $this->exportArray2($exportedArray, 'hidden', $this->getHidden());
 195  
 196          return $exportedArray;
 197      }
 198  }