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