1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Functions used to manage template layers |
5
|
|
|
* |
6
|
|
|
* @name ElkArte Forum |
7
|
|
|
* @copyright ElkArte Forum contributors |
8
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause |
9
|
|
|
* |
10
|
|
|
* @version 1.1 Release Candidate 1 |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class used to manage template layers |
16
|
|
|
* |
17
|
|
|
* An instance of the class can be retrieved with the static method getInstance |
18
|
|
|
*/ |
19
|
|
|
class Template_Layers extends Priority |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Layers not removed in case of errors |
23
|
|
|
*/ |
24
|
|
|
private $_error_safe_layers = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Are we handling an error? |
28
|
|
|
* Hopefully not, so default is false |
29
|
|
|
*/ |
30
|
|
|
private $_is_error = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The layers added when this is true will be used in the error screen |
34
|
|
|
*/ |
35
|
|
|
private static $_error_safe = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Instance of the class |
39
|
|
|
*/ |
40
|
|
|
private static $_instance = null; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Add a new layer to the pile |
44
|
|
|
* |
45
|
|
|
* @param string $layer name of a layer |
46
|
|
|
* @param int|null $priority an integer defining the priority of the layer. |
47
|
|
|
*/ |
48
|
8 |
|
public function add($layer, $priority = null) |
49
|
|
|
{ |
50
|
8 |
|
parent::add($layer, $priority); |
51
|
|
|
|
52
|
8 |
|
if (self::$_error_safe) |
53
|
8 |
|
$this->_error_safe_layers[] = $layer; |
54
|
8 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Add a layer to the pile before another existing layer |
58
|
|
|
* |
59
|
|
|
* @param string $layer the name of a layer |
60
|
|
|
* @param string $following the name of the layer before which $layer must be added |
61
|
|
|
*/ |
62
|
|
|
public function addBefore($layer, $following) |
63
|
|
|
{ |
64
|
|
|
parent::addBefore($layer, $following); |
65
|
|
|
|
66
|
|
|
if (self::$_error_safe) |
67
|
|
|
$this->_error_safe_layers[] = $layer; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add a layer to the pile after another existing layer |
72
|
|
|
* |
73
|
|
|
* @param string $layer the name of a layer |
74
|
|
|
* @param string $previous the name of the layer after which $layer must be added |
75
|
|
|
*/ |
76
|
|
|
public function addAfter($layer, $previous) |
77
|
|
|
{ |
78
|
|
|
parent::addAfter($layer, $previous); |
79
|
|
|
|
80
|
|
|
if (self::$_error_safe) |
81
|
|
|
$this->_error_safe_layers[] = $layer; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Add a layer at the end of the pile |
86
|
|
|
* |
87
|
|
|
* @param string $layer name of a layer |
88
|
|
|
* @param int|null $priority an integer defining the priority of the layer. |
89
|
|
|
*/ |
90
|
|
|
public function addEnd($layer, $priority = null) |
91
|
|
|
{ |
92
|
|
|
parent::addEnd($layer, $priority); |
93
|
|
|
|
94
|
|
|
if (self::$_error_safe) |
95
|
|
|
$this->_error_safe_layers[] = $layer; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Add a layer at the beginning of the pile |
100
|
|
|
* |
101
|
|
|
* @param string $layer name of a layer |
102
|
|
|
* @param int|null $priority an integer defining the priority of the layer. |
103
|
|
|
*/ |
104
|
7 |
|
public function addBegin($layer, $priority = null) |
105
|
|
|
{ |
106
|
7 |
|
parent::addBegin($layer, $priority); |
107
|
|
|
|
108
|
7 |
|
if (self::$_error_safe) |
109
|
7 |
|
$this->_error_safe_layers[] = $layer; |
110
|
7 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Prepares the layers so that they are usable by the template |
114
|
|
|
* The function sorts the layers according to the priority and saves the |
115
|
|
|
* result in $_sorted_entities |
116
|
|
|
* |
117
|
|
|
* @return array the sorted layers |
118
|
|
|
*/ |
119
|
|
|
public function prepareContext() |
120
|
|
|
{ |
121
|
|
|
$all_layers = $this->sort(); |
122
|
|
|
|
123
|
|
|
// If we are dealing with an error page (fatal_error) then we have to prune all the unwanted layers |
124
|
|
|
if ($this->_is_error) |
125
|
|
|
{ |
126
|
|
|
$dummy = $all_layers; |
127
|
|
|
$all_layers = array(); |
128
|
|
|
|
129
|
|
|
foreach ($dummy as $key => $val) |
130
|
|
|
{ |
131
|
|
|
if (in_array($key, $this->_error_safe_layers)) |
132
|
|
|
$all_layers[$key] = $val; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
asort($all_layers); |
137
|
|
|
$this->_sorted_entities = array_keys($all_layers); |
138
|
|
|
|
139
|
|
|
return $this->_sorted_entities; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Reverse the layers order |
144
|
|
|
* |
145
|
|
|
* @return array the reverse ordered layers |
146
|
|
|
*/ |
147
|
|
|
public function reverseLayers() |
148
|
|
|
{ |
149
|
|
|
if ($this->_sorted_entities === null) |
150
|
|
|
$this->prepareContext(); |
151
|
|
|
|
152
|
|
|
return array_reverse($this->_sorted_entities); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Check if at least one layer has been added |
157
|
|
|
* |
158
|
|
|
* @param boolean $base if true will not consider body and html layers in result |
159
|
|
|
* @return bool true if at least one layer has been added |
160
|
|
|
* @todo at that moment _all_after and _all_before are not considered because they may not be "forced" |
161
|
|
|
*/ |
162
|
|
|
public function hasLayers($base = false) |
163
|
|
|
{ |
164
|
|
|
if (!$base) |
165
|
|
|
return (!empty($this->_all_general) || !empty($this->_all_begin) || !empty($this->_all_end)); |
166
|
|
|
else |
167
|
|
|
return array_diff_key(array_merge($this->_all_general, $this->_all_begin, $this->_all_end), array('body' => 0, 'html' => 0)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Return the layers that have been loaded |
172
|
|
|
*/ |
173
|
|
|
public function getLayers() |
174
|
|
|
{ |
175
|
|
|
return array_keys(array_merge($this->_all_general, $this->_all_begin, $this->_all_end, $this->_all_after, $this->_all_before)); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Turns "error mode" on, so that only the allowed layers are displayed |
180
|
|
|
*/ |
181
|
|
|
public function isError() |
182
|
|
|
{ |
183
|
|
|
$this->_is_error = true; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Find and return Template_Layers instance if it exists, |
188
|
|
|
* or create a new instance if it didn't already exist. |
189
|
|
|
* |
190
|
|
|
* @param boolean $error_safe if error mode is on or off |
191
|
|
|
* @return Template_Layers instance of the class |
192
|
|
|
*/ |
193
|
13 |
|
public static function instance($error_safe = false) |
194
|
|
|
{ |
195
|
13 |
|
if (self::$_instance === null) |
196
|
13 |
|
self::$_instance = new Template_Layers(); |
197
|
|
|
|
198
|
13 |
|
self::$_error_safe = $error_safe; |
199
|
|
|
|
200
|
13 |
|
return self::$_instance; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Find and return Template_Layers instance if it exists, |
205
|
|
|
* or create a new instance if it didn't already exist. |
206
|
|
|
* |
207
|
|
|
* @deprecated since Elk 1.1 - use instance instead |
208
|
|
|
* |
209
|
|
|
* @param boolean $error_safe if error mode is on or off |
210
|
|
|
* @return Template_Layers instance of the class |
211
|
|
|
*/ |
212
|
|
|
public static function getInstance($error_safe = false) |
213
|
|
|
{ |
214
|
|
|
return self::instance($error_safe); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|