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