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\user */ |
15
|
|
|
protected $user; |
16
|
|
|
|
17
|
|
|
/** @var bool */ |
18
|
|
|
private $expanded = true; |
19
|
|
|
|
20
|
|
|
/** @var integer */ |
21
|
|
|
private $max_depth = 100; |
22
|
|
|
|
23
|
|
|
/** @var integer */ |
24
|
|
|
private $min_depth = 0; |
25
|
|
|
|
26
|
|
|
/** @var array */ |
27
|
|
|
private $parental_depth; |
28
|
|
|
|
29
|
|
|
/** @var array */ |
30
|
|
|
private $current_item; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Construct |
34
|
|
|
* |
35
|
|
|
* @param \phpbb\db\driver\driver_interface $db Database connection |
36
|
|
|
* @param \phpbb\user $user User Object |
37
|
|
|
* @param string $menu_items_table Menu Items table |
38
|
|
|
* @param string $pk Primary key |
39
|
|
|
*/ |
40
|
2 |
|
public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, $menu_items_table, $pk) |
41
|
|
|
{ |
42
|
2 |
|
parent::__construct($db, $menu_items_table, $pk); |
43
|
|
|
|
44
|
2 |
|
$this->user = $user; |
45
|
2 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $params |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
2 |
|
public function set_params(array $params) |
52
|
|
|
{ |
53
|
2 |
|
$this->expanded = (bool) ((isset($params['expanded'])) ? $params['expanded'] : true); |
54
|
2 |
|
$this->max_depth = (int) ((isset($params['max_depth'])) ? $params['max_depth'] : 100); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $data |
59
|
|
|
* @param \phpbb\template\twig\twig $template |
60
|
|
|
* @param string $handle |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
2 |
|
public function display_navlist(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree') |
64
|
|
|
{ |
65
|
2 |
|
$this->set_current_item($data); |
66
|
2 |
|
$this->prepare_items($data['items']); |
67
|
|
|
|
68
|
2 |
|
if (sizeof($data['items'])) |
69
|
2 |
|
{ |
70
|
2 |
|
$this_depth = 0; |
71
|
2 |
|
foreach ($data['items'] as $row) |
72
|
|
|
{ |
73
|
2 |
|
$prev_depth = $row['prev_depth']; |
74
|
2 |
|
$this_depth = $row['this_depth']; |
75
|
2 |
|
$row['num_kids'] = $this->count_descendants($row); |
76
|
|
|
|
77
|
2 |
|
$template->assign_block_vars($handle, array_change_key_case($row, CASE_UPPER)); |
78
|
2 |
|
$this->close_open_tags($template, $handle . '.close', abs($prev_depth - $this_depth)); |
79
|
2 |
|
} |
80
|
|
|
|
81
|
2 |
|
$this->close_open_tags($template, 'close_' . $handle, ($this_depth - $this->min_depth)); |
82
|
2 |
|
} |
83
|
2 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param array $data |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
2 |
|
protected function set_current_item(array $data) |
90
|
|
|
{ |
91
|
2 |
|
$paths = (array) $data['paths']; |
92
|
2 |
|
$this->min_depth = 0; |
93
|
|
|
|
94
|
2 |
|
arsort($paths); |
95
|
|
|
|
96
|
2 |
|
$curr_path = $this->get_current_path(); |
97
|
2 |
|
foreach ($paths as $item_id => $test_url) |
98
|
|
|
{ |
99
|
2 |
|
if (strpos($curr_path, $test_url) !== false) |
100
|
2 |
|
{ |
101
|
2 |
|
$row = $data['items'][$item_id]; |
102
|
2 |
|
$this->adjust_depth($row); |
103
|
2 |
|
$this->current_item = $row; |
104
|
|
|
|
105
|
2 |
|
return true; |
106
|
|
|
} |
107
|
2 |
|
} |
108
|
|
|
|
109
|
|
|
$this->current_item = $this->default_current_item(); |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
2 |
|
protected function get_current_path() |
117
|
|
|
{ |
118
|
2 |
|
$curr_page = '/' . ltrim($this->user->page['page_dir'] . '/' . $this->user->page['page_name'], './'); |
119
|
2 |
|
$curr_parts = explode('&', $this->user->page['query_string']); |
120
|
|
|
|
121
|
2 |
|
sort($curr_parts); |
122
|
|
|
|
123
|
2 |
|
return $curr_page . '?' . join('&', $curr_parts); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* return void |
128
|
|
|
*/ |
129
|
|
|
protected function default_current_item() |
130
|
|
|
{ |
131
|
|
|
$this->max_depth = ($this->expanded) ? $this->max_depth : 0; |
132
|
|
|
$this->min_depth = 0; |
133
|
|
|
|
134
|
|
|
return array( |
135
|
|
|
$this->column_item_id => 0, |
136
|
|
|
$this->column_parent_id => 0, |
137
|
|
|
$this->column_left_id => 0, |
138
|
|
|
$this->column_right_id => 0, |
139
|
|
|
$this->column_depth => 0, |
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param array $data |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
2 |
|
protected function prepare_items(array &$data) |
148
|
|
|
{ |
149
|
2 |
|
$leaf = array(); |
150
|
2 |
|
$prev_depth = $this->min_depth; |
151
|
2 |
|
$this->parental_depth = array(0 => -1); |
152
|
|
|
|
153
|
2 |
|
foreach ($data as $item_id => $row) |
154
|
|
|
{ |
155
|
|
|
// Skip branch |
156
|
2 |
|
if ($this->should_skip_branch($row, $leaf)) |
157
|
2 |
|
{ |
158
|
|
|
$this->adjust_right_id($leaf[$this->column_item_id], $data, $leaf); |
159
|
|
|
unset($data[$item_id]); |
160
|
|
|
continue; |
161
|
|
|
} |
162
|
|
|
|
163
|
2 |
|
$is_current_item = $this->is_current_item($row); |
164
|
2 |
|
$is_parent = $this->is_parent_of_current_item($row); |
165
|
2 |
|
$this_depth = $this->parental_depth[$row[$this->column_parent_id]] + 1; |
166
|
2 |
|
$leaf = $this->get_leaf_node($row, $is_current_item, $is_parent); |
167
|
|
|
|
168
|
2 |
|
$this->parental_depth[$row[$this->pk]] = $this_depth; |
169
|
|
|
|
170
|
2 |
|
if ($row[$this->column_depth] < $this->min_depth) |
171
|
2 |
|
{ |
172
|
1 |
|
unset($data[$item_id]); |
173
|
1 |
|
continue; |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
$data[$item_id] = array_merge($data[$item_id], array( |
177
|
2 |
|
'prev_depth' => $prev_depth, |
178
|
2 |
|
'this_depth' => $this_depth, |
179
|
2 |
|
'is_current' => $is_current_item, |
180
|
2 |
|
'is_parent' => $is_parent, |
181
|
2 |
|
'full_url' => $this->get_full_url($row), |
182
|
2 |
|
)); |
183
|
|
|
|
184
|
2 |
|
$prev_depth = $this_depth; |
185
|
2 |
|
} |
186
|
2 |
|
unset($this->parental_depth, $data); |
187
|
2 |
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param array $row |
191
|
|
|
* @param array $leaf |
192
|
|
|
* @return bool |
193
|
|
|
*/ |
194
|
2 |
|
protected function should_skip_branch(array $row, array $leaf) |
195
|
|
|
{ |
196
|
2 |
|
return (sizeof($leaf) && $row[$this->column_left_id] < $leaf[$this->column_right_id]); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param array $row |
201
|
|
|
* @return bool |
202
|
|
|
*/ |
203
|
2 |
|
protected function is_current_item(array $row) |
204
|
|
|
{ |
205
|
2 |
|
return ($row[$this->column_item_id] === $this->current_item[$this->column_item_id]) ? true : false; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @param array $row |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
2 |
|
protected function is_parent_of_current_item(array $row) |
213
|
|
|
{ |
214
|
2 |
|
return ($row[$this->column_left_id] < $this->current_item[$this->column_left_id] && $row[$this->column_right_id] > $this->current_item[$this->column_right_id]) ? true : false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Does the branch end here? |
219
|
|
|
* |
220
|
|
|
* @param array $row |
221
|
|
|
* @param bool $is_current_item |
222
|
|
|
* @param bool $is_current_items_parent |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
2 |
|
protected function get_leaf_node(array $row, $is_current_item, $is_current_items_parent) |
226
|
|
|
{ |
227
|
2 |
|
return ($this->must_not_expand($row, $is_current_items_parent) && !$is_current_item && $row['is_expandable']) ? $row : array(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param array $row |
232
|
|
|
* @param bool $is_current_items_parent |
233
|
|
|
* @return bool |
234
|
|
|
*/ |
235
|
2 |
|
protected function must_not_expand(array $row, $is_current_items_parent) |
236
|
|
|
{ |
237
|
2 |
|
return ($row[$this->column_depth] === $this->max_depth || !$is_current_items_parent && !$this->expanded) ? true : false; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param \phpbb\template\twig\twig $template |
242
|
|
|
* @param string $handle |
243
|
|
|
* @param int $repeat |
244
|
|
|
* @return void |
245
|
|
|
*/ |
246
|
2 |
|
protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat) |
247
|
|
|
{ |
248
|
2 |
|
for ($i = 0; $i < $repeat; $i++) |
249
|
|
|
{ |
250
|
2 |
|
$template->assign_block_vars($handle, array()); |
251
|
2 |
|
} |
252
|
2 |
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param int $items_depth |
256
|
|
|
* return bool |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
2 |
|
protected function needs_adjustment($items_depth) |
260
|
|
|
{ |
261
|
2 |
|
return (!$this->expanded && $items_depth >= $this->max_depth) ? true : false; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param array $row |
266
|
|
|
* @return void |
267
|
|
|
*/ |
268
|
2 |
|
protected function adjust_depth(array $row) |
269
|
|
|
{ |
270
|
2 |
|
$depth = (int) $row[$this->column_depth]; |
271
|
2 |
|
if ($this->needs_adjustment($depth)) |
272
|
2 |
|
{ |
273
|
1 |
|
$adjustment = ($this->count_descendants($row)) ? 1 : 0; |
274
|
1 |
|
$this->set_depth_limits($depth, $adjustment); |
275
|
1 |
|
} |
276
|
2 |
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param int $item_id |
280
|
|
|
* @param array $data |
281
|
|
|
* @param array $leaf |
282
|
|
|
* @return void |
283
|
|
|
*/ |
284
|
|
|
protected function adjust_right_id($item_id, array &$data, array $leaf) |
285
|
|
|
{ |
286
|
|
|
if (isset($data[$item_id])) |
287
|
|
|
{ |
288
|
|
|
$data[$leaf[$this->column_item_id]][$this->column_right_id] -= 2; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Append session id to local, non-directory paths |
294
|
|
|
* |
295
|
|
|
* @param array $row |
296
|
|
|
* @return string |
297
|
|
|
*/ |
298
|
2 |
|
protected function get_full_url(array $row) |
299
|
|
|
{ |
300
|
2 |
|
$full_url = $row['full_url']; |
301
|
2 |
|
if ($row['is_navigable']) |
302
|
2 |
|
{ |
303
|
|
|
$full_url = append_sid($row['full_url'], false, false); |
304
|
|
|
} |
305
|
|
|
|
306
|
2 |
|
return $full_url; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param int $depth |
311
|
|
|
* @param int $adjustment |
312
|
|
|
*/ |
313
|
1 |
|
protected function set_depth_limits($depth, $adjustment) |
314
|
|
|
{ |
315
|
1 |
|
$this->min_depth = ($this->max_depth && $depth >= $this->max_depth) ? $depth - $this->max_depth + $adjustment : 0; |
316
|
1 |
|
$this->max_depth = $depth + $adjustment; |
317
|
1 |
|
} |
318
|
|
|
} |
319
|
|
|
|