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