Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
   4  
   5  use PhpOffice\PhpSpreadsheet\Style\Conditional;
   6  use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
   7  
   8  class ConditionalStyles
   9  {
  10      private $worksheet;
  11  
  12      private $worksheetXml;
  13  
  14      private $dxfs;
  15  
  16      public function __construct(Worksheet $workSheet, \SimpleXMLElement $worksheetXml, array $dxfs = [])
  17      {
  18          $this->worksheet = $workSheet;
  19          $this->worksheetXml = $worksheetXml;
  20          $this->dxfs = $dxfs;
  21      }
  22  
  23      public function load()
  24      {
  25          $this->setConditionalStyles(
  26              $this->worksheet,
  27              $this->readConditionalStyles($this->worksheetXml)
  28          );
  29      }
  30  
  31      private function readConditionalStyles($xmlSheet)
  32      {
  33          $conditionals = [];
  34          foreach ($xmlSheet->conditionalFormatting as $conditional) {
  35              foreach ($conditional->cfRule as $cfRule) {
  36                  if (((string) $cfRule['type'] == Conditional::CONDITION_NONE
  37                      || (string) $cfRule['type'] == Conditional::CONDITION_CELLIS
  38                      || (string) $cfRule['type'] == Conditional::CONDITION_CONTAINSTEXT
  39                      || (string) $cfRule['type'] == Conditional::CONDITION_EXPRESSION)
  40                      && isset($this->dxfs[(int) ($cfRule['dxfId'])])) {
  41                      $conditionals[(string) $conditional['sqref']][(int) ($cfRule['priority'])] = $cfRule;
  42                  }
  43              }
  44          }
  45  
  46          return $conditionals;
  47      }
  48  
  49      private function setConditionalStyles(Worksheet $worksheet, array $conditionals)
  50      {
  51          foreach ($conditionals as $ref => $cfRules) {
  52              ksort($cfRules);
  53              $conditionalStyles = $this->readStyleRules($cfRules);
  54  
  55              // Extract all cell references in $ref
  56              $cellBlocks = explode(' ', str_replace('$', '', strtoupper($ref)));
  57              foreach ($cellBlocks as $cellBlock) {
  58                  $worksheet->getStyle($cellBlock)->setConditionalStyles($conditionalStyles);
  59              }
  60          }
  61      }
  62  
  63      private function readStyleRules($cfRules)
  64      {
  65          $conditionalStyles = [];
  66          foreach ($cfRules as $cfRule) {
  67              $objConditional = new Conditional();
  68              $objConditional->setConditionType((string) $cfRule['type']);
  69              $objConditional->setOperatorType((string) $cfRule['operator']);
  70  
  71              if ((string) $cfRule['text'] != '') {
  72                  $objConditional->setText((string) $cfRule['text']);
  73              }
  74  
  75              if (isset($cfRule['stopIfTrue']) && (int) $cfRule['stopIfTrue'] === 1) {
  76                  $objConditional->setStopIfTrue(true);
  77              }
  78  
  79              if (count($cfRule->formula) > 1) {
  80                  foreach ($cfRule->formula as $formula) {
  81                      $objConditional->addCondition((string) $formula);
  82                  }
  83              } else {
  84                  $objConditional->addCondition((string) $cfRule->formula);
  85              }
  86              $objConditional->setStyle(clone $this->dxfs[(int) ($cfRule['dxfId'])]);
  87              $conditionalStyles[] = $objConditional;
  88          }
  89  
  90          return $conditionalStyles;
  91      }
  92  }