Completed
Push — develop ( 9e183f...0abb5c )
by Daniel
08:58
created

display::should_skip_branch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 2
crap 2
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 21
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, $menu_items_table, $pk)
41
	{
42 21
		parent::__construct($db, $menu_items_table, $pk);
43
44 21
		$this->user = $user;
45 21
	}
46
47
	/**
48
	 * @param array $params
49
	 * @return void
50
	 */
51 10
	public function set_params(array $params)
52
	{
53 10
		$this->expanded = (bool) ((isset($params['expanded'])) ? $params['expanded'] : true);
54 10
		$this->max_depth = (int) ((isset($params['max_depth'])) ? $params['max_depth'] : 100);
55 10
	}
56
57
	/**
58
	 * @param array $data
59
	 * @param \phpbb\template\twig\twig $template
60
	 * @param string $handle
61
	 * @return void
62
	 */
63 10
	public function display_navlist(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree')
64
	{
65 10
		$this->set_current_item($data);
66 10
		$this->prepare_items($data['items']);
67
68 10
		if (sizeof($data['items']))
69 10
		{
70 10
			$this_depth = 0;
71 10
			foreach ($data['items'] as $row)
72
			{
73 10
				$prev_depth = $row['prev_depth'];
74 10
				$this_depth = $row['this_depth'];
75 10
				$row['num_kids'] = $this->count_descendants($row);
76
77 10
				$template->assign_block_vars($handle, array_change_key_case($row, CASE_UPPER));
78 10
				$this->close_open_tags($template, $handle . '.close', abs($prev_depth - $this_depth));
79 10
			}
80
81 10
			$this->close_open_tags($template, 'close_' . $handle, ($this_depth - $this->min_depth));
82 10
		}
83 10
	}
84
85
	/**
86
	 * @param array $data
87
	 * @return bool
88
	 */
89 10
	protected function set_current_item(array $data)
90
	{
91 10
		$paths = (array) $data['paths'];
92 10
		$this->min_depth = 0;
93
94 10
		arsort($paths);
95
96 10
		$curr_path = $this->get_current_path();
97 10
		foreach ($paths as $item_id => $test_url)
98
		{
99 8
			if (strpos($curr_path, $test_url) !== false)
100 8
			{
101 6
				$row = $data['items'][$item_id];
102 6
				$this->adjust_depth($row);
103 6
				$this->current_item = $row;
104
105 6
				return true;
106
			}
107 9
		}
108
109 4
		$this->current_item = $this->default_current_item();
110 4
		return false;
111
	}
112
113
	/**
114
	 * @return string
115
	 */
116 10
	protected function get_current_path()
117
	{
118 10
		$curr_page = '/' . ltrim($this->user->page['page_dir'] . '/' . $this->user->page['page_name'], './');
119 10
		$curr_parts = explode('&', $this->user->page['query_string']);
120
121 10
		sort($curr_parts);
122
123 10
		return $curr_page . '?' . join('&', $curr_parts);
124
	}
125
126
	/**
127
	 * return void
128
	 */
129 4
	protected function default_current_item()
130
	{
131 4
		$this->max_depth = ($this->expanded) ? $this->max_depth : 0;
132 4
		$this->min_depth = 0;
133
134
		return array(
135 4
			$this->column_item_id	=> 0,
136 4
			$this->column_parent_id	=> 0,
137 4
			$this->column_left_id	=> 0,
138 4
			$this->column_right_id	=> 0,
139 4
			$this->column_depth		=> 0,
140 4
		);
141
	}
142
143
	/**
144
	 * @param array $data
145
	 * @return void
146
	 */
147 10
	protected function prepare_items(array &$data)
148
	{
149 10
		$leaf = array();
150 10
		$prev_depth = $this->min_depth;
151 10
		$this->parental_depth = array(0 => -1);
152
153 10
		foreach ($data as $item_id => $row)
154
		{
155
			// Skip branch
156 10
			if ($this->should_skip_branch($row, $leaf))
157 10
			{
158 4
				$this->adjust_right_id($leaf[$this->column_item_id], $data, $leaf);
159 4
				unset($data[$item_id]);
160 4
				continue;
161
			}
162
163 10
			$is_current_item = $this->is_current_item($row);
164 10
			$is_parent = $this->is_parent_of_current_item($row);
165 10
			$this_depth	= $this->parental_depth[$row[$this->column_parent_id]] + 1;
166 10
			$leaf = $this->get_leaf_node($row, $is_current_item, $is_parent);
167
168 10
			$this->parental_depth[$row[$this->pk]] = $this_depth;
169
170 10
			if ($row[$this->column_depth] < $this->min_depth)
171 10
			{
172 2
				unset($data[$item_id]);
173 2
				continue;
174
			}
175
176 10
			$data[$item_id] = array_merge($data[$item_id], array(
177 10
				'prev_depth'	=> $prev_depth,
178 10
				'this_depth'	=> $this_depth,
179 10
				'is_current'	=> $is_current_item,
180 10
				'is_parent'		=> $is_parent,
181 10
				'full_url'		=> $this->get_full_url($row),
182 10
			));
183
184 10
			$prev_depth = $this_depth;
185 10
		}
186 10
		unset($this->parental_depth, $data);
187 10
	}
188
189
	/**
190
	 * @param array $row
191
	 * @param array $leaf
192
	 * @return bool
193
	 */
194 10
	protected function should_skip_branch(array $row, array $leaf)
195
	{
196 10
		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 10
	protected function is_current_item(array $row)
204
	{
205 10
		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 10
	protected function is_parent_of_current_item(array $row)
213
	{
214 10
		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 10
	protected function get_leaf_node(array $row, $is_current_item, $is_current_items_parent)
226
	{
227 10
		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 10
	protected function must_not_expand(array $row, $is_current_items_parent)
236
	{
237 10
		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 10
	protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat)
247
	{
248 10
		for ($i = 0; $i < $repeat; $i++)
249
		{
250 8
			$template->assign_block_vars($handle, array());
251 8
		}
252 10
	}
253
254
	/**
255
	 * @param int $items_depth
256
	 * return bool
257
	 * @return bool
258
	 */
259 6
	protected function needs_adjustment($items_depth)
260
	{
261 6
		return (!$this->expanded && $items_depth >= $this->max_depth) ? true : false;
262
	}
263
264
	/**
265
	 * @param array $row
266
	 * @return void
267
	 */
268 6
	protected function adjust_depth(array $row)
269
	{
270 6
		$depth = (int) $row[$this->column_depth];
271 6
		if ($this->needs_adjustment($depth))
272 6
		{
273 2
			$adjustment = ($this->count_descendants($row)) ? 1 : 0;
274 2
			$this->set_depth_limits($depth, $adjustment);
275 2
		}
276 6
	}
277
278
	/**
279
	 * @param int $item_id
280
	 * @param array $data
281
	 * @param array $leaf
282
	 * @return void
283
	 */
284 4
	protected function adjust_right_id($item_id, array &$data, array $leaf)
285
	{
286 4
		if (isset($data[$item_id]))
287 4
		{
288 4
			$data[$leaf[$this->column_item_id]][$this->column_right_id] -= 2;
289 4
		}
290 4
	}
291
292
	/**
293
	 * Append session id to local, non-directory paths
294
	 *
295
	 * @param array $row
296
	 * @return string
297
	 */
298 10
	protected function get_full_url(array $row)
299
	{
300 10
		$full_url = $row['full_url'];
301 10
		if ($row['is_navigable'])
302 10
		{
303 6
			$full_url = append_sid($row['full_url'], false, false);
304 6
		}
305
306 10
		return $full_url;
307
	}
308
309
	/**
310
	 * @param int $depth
311
	 * @param int $adjustment
312
	 */
313 2
	protected function set_depth_limits($depth, $adjustment)
314
	{
315 2
		$this->min_depth = ($this->max_depth && $depth >= $this->max_depth) ? $depth - $this->max_depth + $adjustment : 0;
316 2
		$this->max_depth = $depth + $adjustment;
317 2
	}
318
}
319