Completed
Push — develop ( fd19b8...660ae6 )
by Daniel
11:13
created

display::get_current_path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
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
		$this->min_depth = 0;
134 11
135
		arsort($paths);
136 11
137 11
		$curr_path = $this->get_current_path();
138
		foreach ($paths as $item_id => $test_url)
139 11
		{
140
			if (strpos($curr_path, $test_url) !== false)
141 9
			{
142 9
				$row = $data['items'][$item_id];
143
				$this->adjust_depth($row);
144 6
				$this->current_item = $row;
145 6
146 6
				return true;
147
			}
148 6
		}
149
150 10
		$this->current_item = $this->default_current_item();
151
		return false;
152 5
	}
153 5
154
	/**
155
	 * @return string
156
	 */
157
	protected function get_current_path()
158
	{
159 11
		$curr_page = '/' . ltrim($this->user->page['page_dir'] . '/' . $this->user->page['page_name'], './');
160
		$curr_parts = explode('&', $this->user->page['query_string']);
161 11
162
		sort($curr_parts);
163 11
164
		return $curr_page . '?' . join('&', $curr_parts);
165 11
	}
166
167 11
	/**
168
	 * return void
169
	 */
170
	protected function default_current_item()
171
	{
172
		$this->max_depth = ($this->expanded) ? $this->max_depth : 0;
173 5
		$this->min_depth = 0;
174
175 5
		return array(
176 5
			'item_id'	=> 0,
177
			'parent_id'	=> 0,
178
			'left_id'	=> 0,
179 5
			'right_id'	=> 0,
180 5
			'depth'		=> 0,
181 5
		);
182 5
	}
183 5
184 5
	/**
185
	 * @param array $data
186
	 * @return void
187
	 */
188
	protected function prepare_items(array &$data)
189
	{
190
		$leaf = array();
191 11
		$prev_depth = $this->min_depth;
192
		$this->parental_depth = array(0 => -1);
193 11
194 11
		foreach ($data as $item_id => $row)
195 11
		{
196
			// Skip branch
197 11
			if ($this->should_skip_branch($row, $leaf))
198
			{
199
				$this->adjust_right_id($leaf['item_id'], $data, $leaf);
200 11
				unset($data[$item_id]);
201 11
				continue;
202 4
			}
203 4
204 4
			$is_current_item = $this->is_current_item($row);
205
			$is_parent = $this->is_parent_of_current_item($row);
206
			$this_depth	= $this->parental_depth[$row['parent_id']] + 1;
207 11
			$leaf = $this->get_leaf_node($row, $is_current_item, $is_parent);
208 11
209 11
			$this->parental_depth[$row[$this->pk]] = $this_depth;
210 11
211
			if ($row['depth'] < $this->min_depth)
212 11
			{
213
				unset($data[$item_id]);
214 11
				continue;
215 11
			}
216 2
217 2
			$data[$item_id] = array_merge($data[$item_id], array(
218
				'prev_depth'	=> $prev_depth,
219
				'this_depth'	=> $this_depth,
220 11
				'is_current'	=> $is_current_item,
221 11
				'is_parent'		=> $is_parent,
222 11
				'full_url'		=> $this->get_full_url($row),
223 11
			));
224 11
225 11
			$prev_depth = $this_depth;
226 11
		}
227
		unset($this->parental_depth, $data);
228 11
	}
229 11
230 11
	/**
231 11
	 * @param array $row
232
	 * @param array $leaf
233
	 * @return bool
234
	 */
235
	protected function should_skip_branch(array $row, array $leaf)
236
	{
237
		return (sizeof($leaf) && $row['left_id'] < $leaf['right_id']);
238 11
	}
239
240 11
	/**
241
	 * @param array $row
242
	 * @return bool
243
	 */
244
	protected function is_current_item(array $row)
245
	{
246
		return ($row['item_id'] === $this->current_item['item_id']) ? true : false;
247 11
	}
248
249 11
	/**
250
	 * @param array $row
251
	 * @return bool
252
	 */
253
	protected function is_parent_of_current_item(array $row)
254
	{
255
		return ($row['left_id'] < $this->current_item['left_id'] && $row['right_id'] > $this->current_item['right_id']) ? true : false;
256 11
	}
257
258 11
	/**
259
	 * Does the branch end here?
260
	 *
261
	 * @param array $row
262
	 * @param bool $is_current_item
263
	 * @param bool $is_current_items_parent
264
	 * @return array
265
	 */
266
	protected function get_leaf_node(array $row, $is_current_item, $is_current_items_parent)
267
	{
268
		return ($this->must_not_expand($row, $is_current_items_parent) && !$is_current_item && $row['is_expandable']) ? $row : array();
269 11
	}
270
271 11
	/**
272
	 * @param array $row
273
	 * @param bool $is_current_items_parent
274
	 * @return bool
275
	 */
276
	protected function must_not_expand(array $row, $is_current_items_parent)
277
	{
278
		return ($row['depth'] === $this->max_depth || !$is_current_items_parent && !$this->expanded) ? true : false;
279 11
	}
280
281 11
	/**
282
	 * @param \phpbb\template\twig\twig $template
283
	 * @param string $handle
284
	 * @param int $repeat
285
	 * @return void
286
	 */
287
	protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat)
288
	{
289
		for ($i = 0; $i < $repeat; $i++)
290 11
		{
291
			$template->assign_block_vars($handle, array());
292 11
		}
293
	}
294 9
295 9
	/**
296 11
	 * @param int $items_depth
297
	 * return bool
298
	 * @return bool
299
	 */
300
	protected function needs_adjustment($items_depth)
301
	{
302
		return (!$this->expanded && $items_depth >= $this->max_depth) ? true : false;
303 6
	}
304
305 6
	/**
306
	 * @param array $row
307
	 * @return void
308
	 */
309
	protected function adjust_depth(array $row)
310
	{
311
		$depth = (int) $row['depth'];
312 6
		if ($this->needs_adjustment($depth))
313
		{
314 6
			$adjustment = ($this->count_descendants($row)) ? 1 : 0;
315 6
			$this->set_depth_limits($depth, $adjustment);
316 6
		}
317 2
	}
318 2
319 2
	/**
320 6
	 * @param int $item_id
321
	 * @param array $data
322
	 * @param array $leaf
323
	 * @return void
324
	 */
325
	protected function adjust_right_id($item_id, array &$data, array $leaf)
326
	{
327
		if (isset($data[$item_id]))
328 4
		{
329
			$data[$leaf['item_id']]['right_id'] -= 2;
330 4
		}
331 4
	}
332 4
333 4
	/**
334 4
	 * Append session id to local, non-directory paths
335
	 *
336
	 * @param array $row
337
	 * @return string
338
	 */
339
	protected function get_full_url(array $row)
340
	{
341
		$full_url = $row['full_url'];
342 11
		if ($row['is_navigable'])
343
		{
344 11
			$full_url = append_sid($row['full_url'], false, false);
345 11
		}
346 11
347 7
		return $full_url;
348 7
	}
349
350 11
	/**
351
	 * @param int $depth
352
	 * @param int $adjustment
353
	 */
354
	protected function set_depth_limits($depth, $adjustment)
355
	{
356
		$this->min_depth = ($this->max_depth && $depth >= $this->max_depth) ? $depth - $this->max_depth + $adjustment : 0;
357 2
		$this->max_depth = $depth + $adjustment;
358
	}
359
}
360