Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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