ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ConditionalStyles.php
Go to the documentation of this file.
1 <?php
2 
4 
11 
13 {
14  private $worksheet;
15 
16  private $worksheetXml;
17 
18  private $dxfs;
19 
20  public function __construct(Worksheet $workSheet, SimpleXMLElement $worksheetXml, array $dxfs = [])
21  {
22  $this->worksheet = $workSheet;
23  $this->worksheetXml = $worksheetXml;
24  $this->dxfs = $dxfs;
25  }
26 
27  public function load(): void
28  {
29  $this->setConditionalStyles(
30  $this->worksheet,
31  $this->readConditionalStyles($this->worksheetXml),
32  $this->worksheetXml->extLst
33  );
34  }
35 
36  private function readConditionalStyles($xmlSheet)
37  {
38  $conditionals = [];
39  foreach ($xmlSheet->conditionalFormatting as $conditional) {
40  foreach ($conditional->cfRule as $cfRule) {
41  if (Conditional::isValidConditionType((string) $cfRule['type']) && isset($this->dxfs[(int) ($cfRule['dxfId'])])) {
42  $conditionals[(string) $conditional['sqref']][(int) ($cfRule['priority'])] = $cfRule;
43  } elseif ((string) $cfRule['type'] == Conditional::CONDITION_DATABAR) {
44  $conditionals[(string) $conditional['sqref']][(int) ($cfRule['priority'])] = $cfRule;
45  }
46  }
47  }
48 
49  return $conditionals;
50  }
51 
52  private function setConditionalStyles(Worksheet $worksheet, array $conditionals, $xmlExtLst): void
53  {
54  foreach ($conditionals as $ref => $cfRules) {
55  ksort($cfRules);
56  $conditionalStyles = $this->readStyleRules($cfRules, $xmlExtLst);
57 
58  // Extract all cell references in $ref
59  $cellBlocks = explode(' ', str_replace('$', '', strtoupper($ref)));
60  foreach ($cellBlocks as $cellBlock) {
61  $worksheet->getStyle($cellBlock)->setConditionalStyles($conditionalStyles);
62  }
63  }
64  }
65 
66  private function readStyleRules($cfRules, $extLst)
67  {
68  $conditionalFormattingRuleExtensions = ConditionalFormattingRuleExtension::parseExtLstXml($extLst);
69  $conditionalStyles = [];
70  foreach ($cfRules as $cfRule) {
71  $objConditional = new Conditional();
72  $objConditional->setConditionType((string) $cfRule['type']);
73  $objConditional->setOperatorType((string) $cfRule['operator']);
74 
75  if ((string) $cfRule['text'] != '') {
76  $objConditional->setText((string) $cfRule['text']);
77  }
78 
79  if (isset($cfRule['stopIfTrue']) && (int) $cfRule['stopIfTrue'] === 1) {
80  $objConditional->setStopIfTrue(true);
81  }
82 
83  if (count($cfRule->formula) > 1) {
84  foreach ($cfRule->formula as $formula) {
85  $objConditional->addCondition((string) $formula);
86  }
87  } else {
88  $objConditional->addCondition((string) $cfRule->formula);
89  }
90 
91  if (isset($cfRule->dataBar)) {
92  $objConditional->setDataBar(
93  $this->readDataBarOfConditionalRule($cfRule, $conditionalFormattingRuleExtensions)
94  );
95  } else {
96  $objConditional->setStyle(clone $this->dxfs[(int) ($cfRule['dxfId'])]);
97  }
98 
99  $conditionalStyles[] = $objConditional;
100  }
101 
102  return $conditionalStyles;
103  }
104 
105  private function readDataBarOfConditionalRule($cfRule, $conditionalFormattingRuleExtensions): ConditionalDataBar
106  {
107  $dataBar = new ConditionalDataBar();
108  //dataBar attribute
109  if (isset($cfRule->dataBar['showValue'])) {
110  $dataBar->setShowValue((bool) $cfRule->dataBar['showValue']);
111  }
112 
113  //dataBar children
114  //conditionalFormatValueObjects
115  $cfvoXml = $cfRule->dataBar->cfvo;
116  $cfvoIndex = 0;
117  foreach ((count($cfvoXml) > 1 ? $cfvoXml : [$cfvoXml]) as $cfvo) {
118  if ($cfvoIndex === 0) {
119  $dataBar->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $cfvo['type'], (string) $cfvo['val']));
120  }
121  if ($cfvoIndex === 1) {
122  $dataBar->setMaximumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $cfvo['type'], (string) $cfvo['val']));
123  }
124  ++$cfvoIndex;
125  }
126 
127  //color
128  if (isset($cfRule->dataBar->color)) {
129  $dataBar->setColor((string) $cfRule->dataBar->color['rgb']);
130  }
131  //extLst
132  $this->readDataBarExtLstOfConditionalRule($dataBar, $cfRule, $conditionalFormattingRuleExtensions);
133 
134  return $dataBar;
135  }
136 
137  private function readDataBarExtLstOfConditionalRule(ConditionalDataBar $dataBar, $cfRule, $conditionalFormattingRuleExtensions): void
138  {
139  if (isset($cfRule->extLst)) {
140  $ns = $cfRule->extLst->getNamespaces(true);
141  foreach ((count($cfRule->extLst) > 0 ? $cfRule->extLst->ext : [$cfRule->extLst->ext]) as $ext) {
142  $extId = (string) $ext->children($ns['x14'])->id;
143  if (isset($conditionalFormattingRuleExtensions[$extId]) && (string) $ext['uri'] === '{B025F937-C7B1-47D3-B67F-A62EFF666E3E}') {
144  $dataBar->setConditionalFormattingRuleExt($conditionalFormattingRuleExtensions[$extId]);
145  }
146  }
147  }
148  }
149 }
readDataBarOfConditionalRule($cfRule, $conditionalFormattingRuleExtensions)
getStyle($pCellCoordinate)
Get style for cell.
Definition: Worksheet.php:1420
__construct(Worksheet $workSheet, SimpleXMLElement $worksheetXml, array $dxfs=[])
setConditionalStyles(Worksheet $worksheet, array $conditionals, $xmlExtLst)
readDataBarExtLstOfConditionalRule(ConditionalDataBar $dataBar, $cfRule, $conditionalFormattingRuleExtensions)
setConditionalFormattingRuleExt(ConditionalFormattingRuleExtension $conditionalFormattingRuleExt)