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