Completed
Push — master ( 60a459...efce96 )
by Daniel
08:46
created

display::get_leaf_node()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 2
nc 10
nop 2
crap 6
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 bool */
21
	private $expanded = false;
22
23
	/** @var integer */
24
	private $max_depth = 0;
25
26
	/** @var integer */
27
	private $min_depth = 0;
28
29
	/** @var array */
30
	private $parental_depth;
31
32
	/** @var array */
33
	private $current_item;
34
35
	/**
36
	 * Construct
37
	 *
38
	 * @param \phpbb\db\driver\driver_interface		$db             	Database connection
39
	 * @param \phpbb\template\template				$template			Template object
40
	 * @param \phpbb\user							$user				User Object
41
	 * @param string								$menu_items_table	Menu Items table
42
	 * @param string								$pk					Primary key
43
	 */
44 18
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, $menu_items_table, $pk)
45
	{
46 18
		parent::__construct($db, $menu_items_table, $pk);
47
48 18
		$this->template = $template;
49 18
		$this->user = $user;
50 18
	}
51
52
	/**
53
	 * @param array $params
54
	 * @return void
55
	 */
56 7
	public function set_params(array $params)
57
	{
58 7
		$this->expanded = (bool) $params['expanded'];
59 7
		$this->max_depth = (int) $params['max_depth'];
60 7
	}
61
62
	/**
63
	 * @param array $data
64
	 * @param \phpbb\template\twig\twig $template
65
	 * @param string $handle
66
	 * @return void
67
	 */
68 7
	public function display_navlist(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree')
69
	{
70 7
		$this->prepare_items($data);
71
72 7
		if (sizeof($data))
73 7
		{
74 7
			$this_depth = 0;
75 7
			foreach ($data as $row)
76
			{
77 7
				$prev_depth = $row['prev_depth'];
78 7
				$this_depth = $row['this_depth'];
79 7
				$row['num_kids'] = $this->count_descendants($row);
80
81 7
				$template->assign_block_vars($handle, array_change_key_case($row, CASE_UPPER));
82 7
				$this->close_open_tags($template, $handle . '.close', abs($prev_depth - $this_depth));
83 7
			}
84
85 7
			$this->close_open_tags($template, 'close_' . $handle, ($this_depth - $this->min_depth));
86 7
		}
87 7
	}
88
89
	/**
90
	 * @param array $data
91
	 * @return void
92
	 */
93 5
	public function generate_breadcrumb(array $data)
94
	{
95 5
		$this->find_parents($data, $this->current_item['parent_id']);
96 5
	}
97
98
	/**
99
	 * @param array $data
100
	 * @return void
101
	 */
102 7
	protected function prepare_items(array &$data)
103
	{
104 7
		$this->set_current_item($data);
105
106 7
		$leaf = array();
107 7
		$prev_depth = $this->min_depth;
108 7
		$this->parental_depth = array(0 => -1);
109
110 7
		foreach ($data as $item_id => $row)
111
		{
112
			// Skip branch
113 7
			if (sizeof($leaf))
114 7
			{
115 5
				if ($row['left_id'] < $leaf['right_id'])
116 5
				{
117 4
					$this->adjust_right_id($leaf['item_id'], $data, $leaf);
118 4
					unset($data[$item_id]);
119 4
					continue;
120
				}
121 5
				$leaf = array();
0 ignored issues
show
Unused Code introduced by
$leaf is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
122 5
			}
123
124 7
			$is_current_item = $this->is_current_item($row);
125 7
			$this_depth	= $this->parental_depth[$row['parent_id']] + 1;
126 7
			$leaf = $this->get_leaf_node($row, $is_current_item);
127
128 7
			$this->parental_depth[$row[$this->pk]] = $this_depth;
129
130 7
			if ($row['depth'] < $this->min_depth)
131 7
			{
132 2
				unset($data[$item_id]);
133 2
				continue;
134
			}
135
136 7
			$data[$item_id] = array_merge($data[$item_id], array(
137 7
				'prev_depth'	=> $prev_depth,
138 7
				'this_depth'	=> $this_depth,
139 7
				'is_current'	=> $is_current_item,
140 7
				'full_url'		=> $this->get_full_url($row),
141 7
			));
142
143 7
			$prev_depth = $this_depth;
144 7
		}
145 7
		unset($this->parental_depth, $data);
146 7
	}
147
148
	/**
149
	 * @param array $data
150
	 * @return bool
151
	 */
152 7
	protected function set_current_item(array $data)
153
	{
154 7
		$curr_page = $this->user->page['page_name'];
155 7
		$curr_parts = explode('&', $this->user->page['query_string']);
156
157 7
		$data = array_values($data);
158 7
		for ($i = 0, $size = sizeof($data); $i < $size; $i++)
159
		{
160 7
			$row = $data[$i];
161 7
			if ($this->is_current_path($curr_page, $curr_parts, $row))
162 7
			{
163 6
				$this->adjust_depth($row);
164 6
				$this->current_item = $row;
165 6
				return true;
166
			}
167 4
		}
168
169 1
		$this->current_item = $this->default_current_item();
170 1
		return false;
171
	}
172
173
	/**
174
	 * return void
175
	 */
176 1
	protected function default_current_item()
177
	{
178 1
		$this->max_depth = ($this->expanded) ? $this->max_depth : 0;
179 1
		$this->min_depth = 0;
180
181
		return array(
182 1
			'item_id'	=> 0,
183 1
			'parent_id'	=> 0,
184 1
			'left_id'	=> 0,
185 1
			'right_id'	=> 0,
186 1
			'depth'		=> 0,
187 1
		);
188
	}
189
190
	/**
191
	 * @param string $curr_page
192
	 * @param array $curr_parts
193
	 * @param array $row
194
	 * @return bool
195
	 */
196 7
	protected function is_current_path($curr_page, array $curr_parts, array $row)
197
	{
198 7
		return ($curr_page === ltrim($row['url_path'], './') && (!sizeof($row['url_query']) || sizeof(array_intersect($row['url_query'], $curr_parts)))) ? true : false;
199
	}
200
201
	/**
202
	 * @param array $row
203
	 * @return bool
204
	 */
205 7
	protected function is_current_item(array $row)
206
	{
207 7
		return ($row['item_id'] === $this->current_item['item_id']) ? true : false;
208
	}
209
210
	/**
211
	 * @param array $row
212
	 * @return bool
213
	 */
214 6
	protected function is_child_of_current_item(array $row)
215
	{
216 6
		return ($row['left_id'] < $this->current_item['left_id'] && $row['right_id'] > $this->current_item['right_id']) ? true : false;
217
	}
218
219
	/**
220
	 * Does the branch end here?
221
	 *
222
	 * @param array $row
223
	 * @param bool $is_current_item
224
	 * @return array
225
	 */
226 7
	protected function get_leaf_node(array $row, $is_current_item)
227
	{
228 7
		return (($row['depth'] === $this->max_depth || !$this->is_child_of_current_item($row) && !$this->expanded) && !$is_current_item && $row['is_expandable']) ? $row : array();
229
	}
230
231
	/**
232
	 * @param \phpbb\template\twig\twig $template
233
	 * @param string $handle
234
	 * @param int $repeat
235
	 * @return void
236
	 */
237 7
	protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat)
238
	{
239 7
		for ($i = 0; $i < $repeat; $i++)
240
		{
241 5
			$template->assign_block_vars($handle, array());
242 5
		}
243 7
	}
244
245
	/**
246
	 * @param int $items_depth
247
	 * return bool
248
	 * @return bool
249
	 */
250 6
	protected function needs_adjustment($items_depth)
251
	{
252 6
		return ($items_depth >= $this->max_depth) ? true : false;
253
	}
254
255
	/**
256
	 * @param array $row
257
	 * @return void
258
	 */
259 6
	protected function adjust_depth(array $row)
260
	{
261 6
		$depth = (int) $row['depth'];
262 6
		if ($this->needs_adjustment($depth))
263 6
		{
264 3
			$adjustment = ($this->count_descendants($row)) ? 1 : 0;
265 3
			$this->min_depth = ($this->max_depth && $depth >= $this->max_depth) ? $depth - $this->max_depth + $adjustment : 0;
266 3
			$this->max_depth = $depth + $adjustment;
267 3
		}
268 6
	}
269
270
	/**
271
	 * @param int $item_id
272
	 * @param array $data
273
	 * @param array $leaf
274
	 * @return void
275
	 */
276 4
	protected function adjust_right_id($item_id, array &$data, array $leaf)
277
	{
278 4
		if (isset($data[$item_id]))
279 4
		{
280 4
			$data[$leaf['item_id']]['right_id'] -= 2;
281 4
		}
282 4
	}
283
284
	/**
285
	 * @param array $data
286
	 * @param int $parent_id
287
	 * @return void
288
	 */
289 5
	protected function find_parents(array $data, $parent_id)
290
	{
291 5
		if (isset($data[$parent_id]) && $data[$parent_id]['item_url'] !== 'index.php')
292 5
		{
293 2
			$row = $data[$parent_id];
294 2
			$this->template->alter_block_array('navlinks', array(
295 2
				'FORUM_NAME'	=> $row['item_title'],
296 2
				'U_VIEW_FORUM'	=> $row['full_url'],
297 2
			));
298
299 2
			$this->find_parents($data, $row['parent_id']);
300 2
		}
301 5
	}
302
303
	/**
304
	 * Append session id to local, non-directory paths
305
	 *
306
	 * @param array $row
307
	 * @return string
308
	 */
309 7
	protected function get_full_url(array $row)
310
	{
311 7
		$full_url = $row['full_url'];
312 7
		if ($row['is_navigable'])
313 7
		{
314 5
			$full_url = append_sid($row['full_url'], false, false);
315 5
		}
316
317 7
		return $full_url;
318
	}
319
}
320