forked from elkarte/Elkarte
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Positioning.class.php
437 lines (377 loc) · 9.58 KB
/
Positioning.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
/**
* @name ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause
*
* @version 1.0 Beta
*
*/
if (!defined('ELK'))
die('No access...');
/**
* This class is an experiment for the job of handling positioning of items.
* It has implementation for few simple things like:
* - add
* - addBefore
* - addAfter
* - remove
* - removeAll
* it should be extended in order to actually use its functionality
*/
abstract class Positioning_Items
{
/**
* Used when adding: it holds the "relative" position of the item added
* (i.e. "before", "after", "end" or "begin")
*
* @var string
*/
protected $_position = null;
/**
* An (unique) id to identify the "item"
*
* @var string
*/
protected $_name = null;
/**
* Known positions the item can be added
*
* @var array
*/
protected $_known_positions = array();
/**
* The index of the item after or before which another one should be placed
*
* @var string
*/
protected $_relative = '';
/**
* Reset or not the position where items as added
*
* @var bool
*/
protected $_reset = false;
/**
* Holds all the items to add
*
* @var array
*/
protected $_items = null;
/**
* An array containing all the items added
*
* @var string
*/
protected $_all_general = array();
/**
* An array containing all the items that should go *after* another one
*
* @var string
*/
protected $_all_after = array();
/**
* An array containing all the items that should go *before* another one
*
* @var string
*/
protected $_all_before = array();
/**
* An array containing all the items that should go at the end of the list
*
* @var string
*/
protected $_all_end = array();
/**
* An array containing all the items that should go at the beginning
*
* @var string
*/
protected $_all_begin = array();
/**
* The highest priority assigned at a certain moment for $_all_general
*
* @var string
*/
protected $_general_highest_priority = 0;
/**
* The highest priority assigned at a certain moment for $_all_end
*
* @var string
*/
protected $_end_highest_priority = 10000;
/**
* The highest priority assigned at a certain moment for $_all_begin
* Highest priority at "begin" is sort of tricky, because the value is negative
*
* @var string
*/
protected $_begin_highest_priority = -10000;
/**
* Used in prepareContext to store the items in the order
* they will be rendered, and used in reverseItems to return
* the reverse order for the "below" loop
*
* @var array
*/
protected $_sorted_items = null;
/**
* All the child items of the object
*
* @var array pairs of strings: the index is the item, the value is an identifier of Positioning_Items, child of another Positioning_Items
*/
protected $_children = null;
/**
* Create and initialize an instance of the class
*
* @param string an identifier
*/
public function __construct($id = 'default')
{
if (!empty($id))
$this->_name = $id;
// Just in case, let's initialize all the relevant variables
$this->removeAll();
$this->_known_positions = array('after', 'before', 'end', 'begin');
return $this;
}
/**
* Add a new item to the pile
*
* @param string $key index of a item
* @param string $item the item (usually is an array, but it could be anything)
* @param int $priority an integer defining the priority of the item.
*/
abstract public function add($key, $item = null, $priority = null);
/**
* Do we want to add the item after another one?
*/
public function after($position)
{
$this->_position = 'after';
$this->_relative = $position;
return $this;
}
/**
* Or before?
*/
public function before($position)
{
$this->_position = 'before';
$this->_relative = $position;
return $this;
}
/**
* Maybe at the end?
*/
public function end()
{
$this->_position = 'end';
$this->_relative = '';
return $this;
}
/**
* Or at the beginning of the pile?
*/
public function begin()
{
$this->_position = 'begin';
$this->_relative = '';
return $this;
}
/**
* Add a bunch of items in one go
*
* @param string $item name of a item
* @param int $priority an integer defining the priority of the item.
*/
public function addBulk($items)
{
$this->_reset = false;
foreach ($items as $key => $item)
$this->add($key, $item);
$this->_reset = true;
$this->_position = null;
}
/**
* Or at the beginning of the pile?
*/
public function childOf($parent)
{
$this->_position = 'child';
$this->_relative = $parent;
return $this;
}
public function get($name)
{
// First the easy ones
if ($this->_name === $name)
return $this;
// If not, then let's have some fun
if (!empty($this->_children))
{
foreach ($this->_children as $key => $item)
{
$found = $item->get($name);
if ($found)
return $found;
}
}
return false;
}
/**
* Remove a item by name
*
* @param string $item the name of a item
*/
public function remove($key)
{
if (isset($this->_all_general[$key]))
unset($this->_all_general[$key]);
if (isset($this->_all_after[$key]))
unset($this->_all_after[$key]);
if (isset($this->_all_before[$key]))
unset($this->_all_before[$key]);
if (isset($this->_all_end[$key]))
unset($this->_all_end[$key]);
if (isset($this->_all_begin[$key]))
unset($this->_all_begin[$key]);
}
/**
* Remove all the items added up to the moment is run
*/
public function removeAll()
{
$this->_all_general = array();
$this->_all_after = array();
$this->_all_before = array();
$this->_all_end = array();
$this->_all_begin = array();
$this->_general_highest_priority = 0;
$this->_end_highest_priority = 10000;
$this->_begin_highest_priority = -10000;
}
/**
* Prepares the items so that they are usable by the template
* The function sorts the items according to the priority and saves the
* result in $_sorted_items
*
* @return array the sorted items
*/
public function prepareContext()
{
$this->_sorted_items = array();
// Sorting
asort($this->_all_begin);
asort($this->_all_general);
asort($this->_all_end);
// The easy ones: just merge
$all_items = array_merge($this->_all_begin, $this->_all_general, $this->_all_end);
// Now the funny part, let's start with some cleanup: collecting all the items we know and pruning those that cannot be placed somewhere
$all_known = array_merge(array_keys($all_items), array_keys($this->_all_after), array_keys($this->_all_before));
$all['before'] = array();
foreach ($this->_all_before as $key => $value)
if (in_array($value, $all_known))
$all['before'][$key] = $value;
$all['after'] = array();
foreach ($this->_all_after as $key => $value)
if (in_array($value, $all_known))
$all['after'][$key] = $value;
// This is terribly optimized, though it shouldn't loop over too many things (hopefully)
// It "iteratively" adds all the after/before items shifting priority
// of all the other items to ensure each one has a different value
while (!empty($all['after']) || !empty($all['before']))
{
foreach (array('after' => 1, 'before' => -1) as $where => $inc)
{
if (empty($all[$where]))
continue;
foreach ($all[$where] as $item => $reference)
{
if (isset($all_items[$reference]))
{
$priority_threshold = $all_items[$reference];
foreach ($all_items as $key => $val)
{
switch ($where)
{
case 'after':
if ($val <= $priority_threshold)
$all_items[$key] -= $inc;
break;
case 'before':
if ($val >= $priority_threshold)
$all_items[$key] -= $inc;
break;
}
}
unset($all[$where][$item]);
$all_items[$item] = $priority_threshold;
}
}
}
}
asort($all_items);
foreach ($all_items as $key => $priority)
{
$this->_sorted_items[$key] = $this->_items[$key];
if ($this->hasChildren($key))
$this->_sorted_items[$key]['children'] = $this->_children[$key];
}
return $this->_sorted_items;
}
/**
* Reverse the items order
*
* @return array the reverse ordered items
*/
public function reverseItems()
{
if ($this->_prevent_reversing)
return array();
if ($this->_sorted_items === null)
$this->prepareContext();
return array_reverse($this->_sorted_items);
}
/**
* Check if at least one item has been added
*
* @return bool true if at least one item has been added
* @todo at that moment _all_after and _all_before are not considered because they may not be "forced"
*/
public function hasItems()
{
return (!empty($this->_all_general) || !empty($this->_all_begin) || !empty($this->_all_end));
}
/**
* Check if the current item has any child
*
* @param string $key the identifier of the menu item
* @return bool true if at least one child is found and this child has items
*/
public function hasChildren($key)
{
if (!empty($this->_children) && !empty($this->_children[$key]))
{
return true;
$has_children = false;
$children = $this->_children[$key];
$instance = Positioning_Items::context($children);
// If there are children then go and check them (recursion)
if ($instance->hasChildren())
$has_children = $instance->hasChildren();
// No children, means it's the last one, so let's come back to the origin telling if there is at least one item
else
return $instance->hasItems();
// If a child is present we can stop here, otherwise we check the next in the pile
if ($has_children)
return true;
}
else
return false;
}
public function preventReverse()
{
$this->_prevent_reversing = true;
}
}