1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package sitemaker |
5
|
|
|
* @copyright (c) 2013 Daniel A. (blitze) |
6
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace blitze\sitemaker\services\menus; |
11
|
|
|
|
12
|
|
|
class display extends \blitze\sitemaker\services\tree\display |
13
|
|
|
{ |
14
|
|
|
/** @var \phpbb\template\template */ |
15
|
|
|
protected $template; |
16
|
|
|
|
17
|
|
|
/** @var \phpbb\user */ |
18
|
|
|
protected $user; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $php_ext; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
private $expanded = true; |
25
|
|
|
|
26
|
|
|
/** @var integer */ |
27
|
|
|
private $max_depth = 100; |
28
|
|
|
|
29
|
|
|
/** @var integer */ |
30
|
|
|
private $min_depth = 0; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
private $parental_depth; |
34
|
|
|
|
35
|
|
|
/** @var array */ |
36
|
|
|
private $current_item; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Construct |
40
|
|
|
* |
41
|
|
|
* @param \phpbb\db\driver\driver_interface $db Database connection |
42
|
|
|
* @param \phpbb\template\template $template Template object |
43
|
|
|
* @param \phpbb\user $user User Object |
44
|
|
|
* @param string $menu_items_table Menu Items table |
45
|
|
|
* @param string $pk Primary key |
46
|
|
|
* @param string $php_ext php file extension |
47
|
|
|
*/ |
48
|
21 |
|
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, $menu_items_table, $pk, $php_ext) |
49
|
|
|
{ |
50
|
21 |
|
parent::__construct($db, $menu_items_table, $pk); |
51
|
|
|
|
52
|
21 |
|
$this->template = $template; |
53
|
21 |
|
$this->user = $user; |
54
|
21 |
|
$this->php_ext = $php_ext; |
55
|
21 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $params |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
11 |
|
public function set_params(array $params) |
62
|
|
|
{ |
63
|
11 |
|
$this->expanded = (bool) ((isset($params['expanded'])) ? $params['expanded'] : true); |
64
|
11 |
|
$this->max_depth = (int) ((isset($params['max_depth'])) ? $params['max_depth'] : 100); |
65
|
11 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $data |
69
|
|
|
* @param \phpbb\template\twig\twig $template |
70
|
|
|
* @param string $handle |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
11 |
|
public function display_navlist(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree') |
74
|
|
|
{ |
75
|
11 |
|
$this->set_current_item($data); |
76
|
11 |
|
$this->prepare_items($data['items']); |
77
|
|
|
|
78
|
11 |
|
if (sizeof($data['items'])) |
79
|
11 |
|
{ |
80
|
11 |
|
$this_depth = 0; |
81
|
11 |
|
foreach ($data['items'] as $row) |
82
|
|
|
{ |
83
|
11 |
|
$prev_depth = $row['prev_depth']; |
84
|
11 |
|
$this_depth = $row['this_depth']; |
85
|
11 |
|
$row['num_kids'] = $this->count_descendants($row); |
86
|
|
|
|
87
|
11 |
|
$template->assign_block_vars($handle, array_change_key_case($row, CASE_UPPER)); |
88
|
11 |
|
$this->close_open_tags($template, $handle . '.close', abs($prev_depth - $this_depth)); |
89
|
11 |
|
} |
90
|
|
|
|
91
|
11 |
|
$this->close_open_tags($template, 'close_' . $handle, ($this_depth - $this->min_depth)); |
92
|
11 |
|
} |
93
|
11 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Comment this out for now. May revisit this later |
97
|
|
|
* |
98
|
|
|
* @param array $data |
99
|
|
|
* @return void |
100
|
|
|
* |
101
|
|
|
public function generate_breadcrumb(array $data) |
102
|
|
|
{ |
103
|
|
|
$this->find_parents($data, $this->current_item['parent_id']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $data |
108
|
|
|
* @param int $parent_id |
109
|
|
|
* @return void |
110
|
|
|
* |
111
|
|
|
protected function find_parents(array $data, $parent_id) |
112
|
|
|
{ |
113
|
|
|
if (isset($data[$parent_id]) && $data[$parent_id]['item_url'] !== 'index.php') |
114
|
|
|
{ |
115
|
|
|
$row = $data[$parent_id]; |
116
|
|
|
$this->template->alter_block_array('navlinks', array( |
117
|
|
|
'FORUM_NAME' => $row['item_title'], |
118
|
|
|
'U_VIEW_FORUM' => $row['full_url'], |
119
|
|
|
)); |
120
|
|
|
|
121
|
|
|
$this->find_parents($data, $row['parent_id']); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
*/ |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array $data |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
11 |
|
protected function set_current_item(array $data) |
131
|
|
|
{ |
132
|
11 |
|
$paths = (array) $data['paths']; |
133
|
|
|
|
134
|
11 |
|
arsort($paths); |
135
|
|
|
|
136
|
11 |
|
$this->min_depth = 0; |
137
|
11 |
|
$curr_path = $this->get_current_path(); |
138
|
|
|
|
139
|
11 |
|
foreach ($paths as $item_id => $test_url) |
140
|
|
|
{ |
141
|
9 |
|
if (strpos($curr_path, $test_url) !== false) |
142
|
9 |
|
{ |
143
|
|
|
|
144
|
6 |
|
$row = $data['items'][$item_id]; |
145
|
6 |
|
$this->adjust_depth($row); |
146
|
6 |
|
$this->current_item = $row; |
147
|
|
|
|
148
|
6 |
|
return true; |
149
|
|
|
} |
150
|
10 |
|
} |
151
|
|
|
|
152
|
5 |
|
$this->current_item = $this->default_current_item(); |
153
|
5 |
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
11 |
|
protected function get_current_path() |
160
|
|
|
{ |
161
|
11 |
|
$curr_page = '/' . ltrim($this->user->page['page_dir'] . '/' . $this->user->page['page_name'], './'); |
162
|
|
|
//$curr_page = str_replace('/index.' . $this->php_ext, '/', $curr_page); |
|
|
|
|
163
|
11 |
|
$curr_parts = explode('&', $this->user->page['query_string']); |
164
|
|
|
|
165
|
11 |
|
sort($curr_parts); |
166
|
|
|
|
167
|
11 |
|
return $curr_page . '?' . join('&', $curr_parts); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* return void |
172
|
|
|
*/ |
173
|
5 |
|
protected function default_current_item() |
174
|
|
|
{ |
175
|
5 |
|
$this->max_depth = ($this->expanded) ? $this->max_depth : 0; |
176
|
5 |
|
$this->min_depth = 0; |
177
|
|
|
|
178
|
|
|
return array( |
179
|
5 |
|
'item_id' => 0, |
180
|
5 |
|
'parent_id' => 0, |
181
|
5 |
|
'left_id' => 0, |
182
|
5 |
|
'right_id' => 0, |
183
|
5 |
|
'depth' => 0, |
184
|
5 |
|
); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param array $data |
189
|
|
|
* @return void |
190
|
|
|
*/ |
191
|
11 |
|
protected function prepare_items(array &$data) |
192
|
|
|
{ |
193
|
11 |
|
$leaf = array(); |
194
|
11 |
|
$prev_depth = $this->min_depth; |
195
|
11 |
|
$this->parental_depth = array(0 => -1); |
196
|
|
|
|
197
|
11 |
|
foreach ($data as $item_id => $row) |
198
|
|
|
{ |
199
|
|
|
// Skip branch |
200
|
11 |
|
if ($this->should_skip_branch($row, $leaf)) |
201
|
11 |
|
{ |
202
|
4 |
|
$this->adjust_right_id($leaf['item_id'], $data, $leaf); |
203
|
4 |
|
unset($data[$item_id]); |
204
|
4 |
|
continue; |
205
|
|
|
} |
206
|
|
|
|
207
|
11 |
|
$is_current_item = $this->is_current_item($row); |
208
|
11 |
|
$is_parent = $this->is_parent_of_current_item($row); |
209
|
11 |
|
$this_depth = $this->parental_depth[$row['parent_id']] + 1; |
210
|
11 |
|
$leaf = $this->get_leaf_node($row, $is_current_item, $is_parent); |
211
|
|
|
|
212
|
11 |
|
$this->parental_depth[$row[$this->pk]] = $this_depth; |
213
|
|
|
|
214
|
11 |
|
if ($row['depth'] < $this->min_depth) |
215
|
11 |
|
{ |
216
|
2 |
|
unset($data[$item_id]); |
217
|
2 |
|
continue; |
218
|
|
|
} |
219
|
|
|
|
220
|
11 |
|
$data[$item_id] = array_merge($data[$item_id], array( |
221
|
11 |
|
'prev_depth' => $prev_depth, |
222
|
11 |
|
'this_depth' => $this_depth, |
223
|
11 |
|
'is_current' => $is_current_item, |
224
|
11 |
|
'is_parent' => $is_parent, |
225
|
11 |
|
'full_url' => $this->get_full_url($row), |
226
|
11 |
|
)); |
227
|
|
|
|
228
|
11 |
|
$prev_depth = $this_depth; |
229
|
11 |
|
} |
230
|
11 |
|
unset($this->parental_depth, $data); |
231
|
11 |
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param array $row |
235
|
|
|
* @param array $leaf |
236
|
|
|
* @return bool |
237
|
|
|
*/ |
238
|
11 |
|
protected function should_skip_branch(array $row, array $leaf) |
239
|
|
|
{ |
240
|
11 |
|
return (sizeof($leaf) && $row['left_id'] < $leaf['right_id']); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param array $row |
245
|
|
|
* @return bool |
246
|
|
|
*/ |
247
|
11 |
|
protected function is_current_item(array $row) |
248
|
|
|
{ |
249
|
11 |
|
return ($row['item_id'] === $this->current_item['item_id']) ? true : false; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param array $row |
254
|
|
|
* @return bool |
255
|
|
|
*/ |
256
|
11 |
|
protected function is_parent_of_current_item(array $row) |
257
|
|
|
{ |
258
|
11 |
|
return ($row['left_id'] < $this->current_item['left_id'] && $row['right_id'] > $this->current_item['right_id']) ? true : false; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Does the branch end here? |
263
|
|
|
* |
264
|
|
|
* @param array $row |
265
|
|
|
* @param bool $is_current_item |
266
|
|
|
* @param bool $is_current_items_parent |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
11 |
|
protected function get_leaf_node(array $row, $is_current_item, $is_current_items_parent) |
270
|
|
|
{ |
271
|
11 |
|
return ($this->must_not_expand($row, $is_current_items_parent) && !$is_current_item && $row['is_expandable']) ? $row : array(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @param array $row |
276
|
|
|
* @param bool $is_current_items_parent |
277
|
|
|
* @return bool |
278
|
|
|
*/ |
279
|
11 |
|
protected function must_not_expand(array $row, $is_current_items_parent) |
280
|
|
|
{ |
281
|
11 |
|
return ($row['depth'] === $this->max_depth || !$is_current_items_parent && !$this->expanded) ? true : false; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param \phpbb\template\twig\twig $template |
286
|
|
|
* @param string $handle |
287
|
|
|
* @param int $repeat |
288
|
|
|
* @return void |
289
|
|
|
*/ |
290
|
11 |
|
protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat) |
291
|
|
|
{ |
292
|
11 |
|
for ($i = 0; $i < $repeat; $i++) |
293
|
|
|
{ |
294
|
9 |
|
$template->assign_block_vars($handle, array()); |
295
|
9 |
|
} |
296
|
11 |
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param int $items_depth |
300
|
|
|
* return bool |
301
|
|
|
* @return bool |
302
|
|
|
*/ |
303
|
6 |
|
protected function needs_adjustment($items_depth) |
304
|
|
|
{ |
305
|
6 |
|
return (!$this->expanded && $items_depth >= $this->max_depth) ? true : false; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param array $row |
310
|
|
|
* @return void |
311
|
|
|
*/ |
312
|
6 |
|
protected function adjust_depth(array $row) |
313
|
|
|
{ |
314
|
6 |
|
$depth = (int) $row['depth']; |
315
|
6 |
|
if ($this->needs_adjustment($depth)) |
316
|
6 |
|
{ |
317
|
2 |
|
$adjustment = ($this->count_descendants($row)) ? 1 : 0; |
318
|
2 |
|
$this->set_depth_limits($depth, $adjustment); |
319
|
2 |
|
} |
320
|
6 |
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param int $item_id |
324
|
|
|
* @param array $data |
325
|
|
|
* @param array $leaf |
326
|
|
|
* @return void |
327
|
|
|
*/ |
328
|
4 |
|
protected function adjust_right_id($item_id, array &$data, array $leaf) |
329
|
|
|
{ |
330
|
4 |
|
if (isset($data[$item_id])) |
331
|
4 |
|
{ |
332
|
4 |
|
$data[$leaf['item_id']]['right_id'] -= 2; |
333
|
4 |
|
} |
334
|
4 |
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Append session id to local, non-directory paths |
338
|
|
|
* |
339
|
|
|
* @param array $row |
340
|
|
|
* @return string |
341
|
|
|
*/ |
342
|
11 |
|
protected function get_full_url(array $row) |
343
|
|
|
{ |
344
|
11 |
|
$full_url = $row['full_url']; |
345
|
11 |
|
if ($row['is_navigable']) |
346
|
11 |
|
{ |
347
|
7 |
|
$full_url = append_sid($row['full_url'], false, false); |
348
|
7 |
|
} |
349
|
|
|
|
350
|
11 |
|
return $full_url; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param int $depth |
355
|
|
|
* @param int $adjustment |
356
|
|
|
*/ |
357
|
2 |
|
protected function set_depth_limits($depth, $adjustment) |
358
|
|
|
{ |
359
|
2 |
|
$this->min_depth = ($this->max_depth && $depth >= $this->max_depth) ? $depth - $this->max_depth + $adjustment : 0; |
360
|
2 |
|
$this->max_depth = $depth + $adjustment; |
361
|
2 |
|
} |
362
|
|
|
} |
363
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.