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\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          return $this->parent->getSharedComponent()->getProtection();
  57      }
  58  
  59      /**
  60       * Build style array from subcomponents.
  61       *
  62       * @param array $array
  63       *
  64       * @return array
  65       */
  66      public function getStyleArray($array)
  67      {
  68          return ['protection' => $array];
  69      }
  70  
  71      /**
  72       * Apply styles from array.
  73       *
  74       * <code>
  75       * $spreadsheet->getActiveSheet()->getStyle('B2')->getLocked()->applyFromArray(
  76       *     [
  77       *         'locked' => TRUE,
  78       *         'hidden' => FALSE
  79       *     ]
  80       * );
  81       * </code>
  82       *
  83       * @param array $pStyles Array containing style information
  84       *
  85       * @return $this
  86       */
  87      public function applyFromArray(array $pStyles)
  88      {
  89          if ($this->isSupervisor) {
  90              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
  91          } else {
  92              if (isset($pStyles['locked'])) {
  93                  $this->setLocked($pStyles['locked']);
  94              }
  95              if (isset($pStyles['hidden'])) {
  96                  $this->setHidden($pStyles['hidden']);
  97              }
  98          }
  99  
 100          return $this;
 101      }
 102  
 103      /**
 104       * Get locked.
 105       *
 106       * @return string
 107       */
 108      public function getLocked()
 109      {
 110          if ($this->isSupervisor) {
 111              return $this->getSharedComponent()->getLocked();
 112          }
 113  
 114          return $this->locked;
 115      }
 116  
 117      /**
 118       * Set locked.
 119       *
 120       * @param string $pValue see self::PROTECTION_*
 121       *
 122       * @return $this
 123       */
 124      public function setLocked($pValue)
 125      {
 126          if ($this->isSupervisor) {
 127              $styleArray = $this->getStyleArray(['locked' => $pValue]);
 128              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 129          } else {
 130              $this->locked = $pValue;
 131          }
 132  
 133          return $this;
 134      }
 135  
 136      /**
 137       * Get hidden.
 138       *
 139       * @return string
 140       */
 141      public function getHidden()
 142      {
 143          if ($this->isSupervisor) {
 144              return $this->getSharedComponent()->getHidden();
 145          }
 146  
 147          return $this->hidden;
 148      }
 149  
 150      /**
 151       * Set hidden.
 152       *
 153       * @param string $pValue see self::PROTECTION_*
 154       *
 155       * @return $this
 156       */
 157      public function setHidden($pValue)
 158      {
 159          if ($this->isSupervisor) {
 160              $styleArray = $this->getStyleArray(['hidden' => $pValue]);
 161              $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
 162          } else {
 163              $this->hidden = $pValue;
 164          }
 165  
 166          return $this;
 167      }
 168  
 169      /**
 170       * Get hash code.
 171       *
 172       * @return string Hash code
 173       */
 174      public function getHashCode()
 175      {
 176          if ($this->isSupervisor) {
 177              return $this->getSharedComponent()->getHashCode();
 178          }
 179  
 180          return md5(
 181              $this->locked .
 182              $this->hidden .
 183              __CLASS__
 184          );
 185      }
 186  
 187      protected function exportArray1(): array
 188      {
 189          $exportedArray = [];
 190          $this->exportArray2($exportedArray, 'locked', $this->getLocked());
 191          $this->exportArray2($exportedArray, 'hidden', $this->getHidden());
 192  
 193          return $exportedArray;
 194      }
 195  }