Completed
Push — master ( db8e66...00afbe )
by Daniel
09:03
created

display::get_leaf_node()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 2
nc 6
nop 2
crap 4
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 = false;
25
26
	/** @var integer */
27
	private $max_depth = 0;
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 10
	public function set_params(array $params)
62
	{
63 10
		$this->expanded = (bool) $params['expanded'];
64 10
		$this->max_depth = (int) $params['max_depth'];
65 10
	}
66
67
	/**
68
	 * @param array $data
69
	 * @param \phpbb\template\twig\twig $template
70
	 * @param string $handle
71
	 * @return void
72
	 */
73 10
	public function display_navlist(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree')
74
	{
75 10
		$this->prepare_items($data);
76
77 10
		if (sizeof($data))
78 10
		{
79 10
			$this_depth = 0;
80 10
			foreach ($data as $row)
81
			{
82 10
				$prev_depth = $row['prev_depth'];
83 10
				$this_depth = $row['this_depth'];
84 10
				$row['num_kids'] = $this->count_descendants($row);
85
86 10
				$template->assign_block_vars($handle, array_change_key_case($row, CASE_UPPER));
87 10
				$this->close_open_tags($template, $handle . '.close', abs($prev_depth - $this_depth));
88 10
			}
89
90 10
			$this->close_open_tags($template, 'close_' . $handle, ($this_depth - $this->min_depth));
91 10
		}
92 10
	}
93
94
	/**
95
	 * @param array $data
96
	 * @return void
97
	 */
98 8
	public function generate_breadcrumb(array $data)
99
	{
100 8
		$this->find_parents($data, $this->current_item['parent_id']);
101 8
	}
102
103
	/**
104
	 * @param array $data
105
	 * @return void
106
	 */
107 10
	protected function prepare_items(array &$data)
108
	{
109 10
		$this->set_current_item($data);
110
111 10
		$leaf = array();
112 10
		$prev_depth = $this->min_depth;
113 10
		$this->parental_depth = array(0 => -1);
114
115 10
		foreach ($data as $item_id => $row)
116
		{
117
			// Skip branch
118 10
			if (sizeof($leaf))
119 10
			{
120 5
				if ($row['left_id'] < $leaf['right_id'])
121 5
				{
122 4
					$this->adjust_right_id($leaf['item_id'], $data, $leaf);
123 4
					unset($data[$item_id]);
124 4
					continue;
125
				}
126 5
			}
127
128 10
			$is_current_item = $this->is_current_item($row);
129 10
			$this_depth	= $this->parental_depth[$row['parent_id']] + 1;
130 10
			$leaf = $this->get_leaf_node($row, $is_current_item);
131
132 10
			$this->parental_depth[$row[$this->pk]] = $this_depth;
133
134 10
			if ($row['depth'] < $this->min_depth)
135 10
			{
136 2
				unset($data[$item_id]);
137 2
				continue;
138
			}
139
140 10
			$data[$item_id] = array_merge($data[$item_id], array(
141 10
				'prev_depth'	=> $prev_depth,
142 10
				'this_depth'	=> $this_depth,
143 10
				'is_current'	=> $is_current_item,
144 10
				'full_url'		=> $this->get_full_url($row),
145 10
			));
146
147 10
			$prev_depth = $this_depth;
148 10
		}
149 10
		unset($this->parental_depth, $data);
150 10
	}
151
152
	/**
153
	 * @param array $data
154
	 * @return bool
155
	 */
156 10
	protected function set_current_item(array $data)
157
	{
158 10
		$curr_page = '/' . ltrim($this->user->page['page_dir'] . '/' . $this->user->page['page_name'], './');
159 10
		$curr_page = str_replace('/index.' . $this->php_ext, '/', $curr_page);
160 10
		$curr_parts = explode('&', $this->user->page['query_string']);
161
162 10
		$data = array_values($data);
163 10
		for ($i = 0, $size = sizeof($data); $i < $size; $i++)
164
		{
165 10
			$row = $data[$i];
166 10
			if ($this->is_current_path($curr_page, $curr_parts, $row))
167 10
			{
168 6
				$this->adjust_depth($row);
169 6
				$this->current_item = $row;
170 6
				return true;
171
			}
172 7
		}
173
174 4
		$this->current_item = $this->default_current_item();
175 4
		return false;
176
	}
177
178
	/**
179
	 * return void
180
	 */
181 4
	protected function default_current_item()
182
	{
183 4
		$this->max_depth = ($this->expanded) ? $this->max_depth : 0;
184 4
		$this->min_depth = 0;
185
186
		return array(
187 4
			'item_id'	=> 0,
188 4
			'parent_id'	=> 0,
189 4
			'left_id'	=> 0,
190 4
			'right_id'	=> 0,
191 4
			'depth'		=> 0,
192 4
		);
193
	}
194
195
	/**
196
	 * @param string $curr_page
197
	 * @param array $curr_parts
198
	 * @param array $row
199
	 * @return bool
200
	 */
201 10
	protected function is_current_path($curr_page, array $curr_parts, array $row)
202
	{
203 10
		return ($curr_page === $row['url_path'] && (!sizeof($row['url_query']) || sizeof(array_intersect($row['url_query'], $curr_parts)))) ? true : false;
204
	}
205
206
	/**
207
	 * @param array $row
208
	 * @return bool
209
	 */
210 10
	protected function is_current_item(array $row)
211
	{
212 10
		return ($row['item_id'] === $this->current_item['item_id']) ? true : false;
213
	}
214
215
	/**
216
	 * @param array $row
217
	 * @return bool
218
	 */
219 9
	protected function is_child_of_current_item(array $row)
220
	{
221 9
		return ($row['left_id'] < $this->current_item['left_id'] && $row['right_id'] > $this->current_item['right_id']) ? true : false;
222
	}
223
224
	/**
225
	 * Does the branch end here?
226
	 *
227
	 * @param array $row
228
	 * @param bool $is_current_item
229
	 * @return array
230
	 */
231 10
	protected function get_leaf_node(array $row, $is_current_item)
232
	{
233 10
		return ($this->must_not_expand($row) && !$is_current_item && $row['is_expandable']) ? $row : array();
234
	}
235
236
	/**
237
	 * @param array $row
238
	 * @return bool
239
	 */
240 10
	protected function must_not_expand(array $row)
241
	{
242 10
		return ($row['depth'] === $this->max_depth || !$this->is_child_of_current_item($row) && !$this->expanded) ? true : false;
243
	}
244
245
	/**
246
	 * @param \phpbb\template\twig\twig $template
247
	 * @param string $handle
248
	 * @param int $repeat
249
	 * @return void
250
	 */
251 10
	protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat)
252
	{
253 10
		for ($i = 0; $i < $repeat; $i++)
254
		{
255 8
			$template->assign_block_vars($handle, array());
256 8
		}
257 10
	}
258
259
	/**
260
	 * @param int $items_depth
261
	 * return bool
262
	 * @return bool
263
	 */
264 6
	protected function needs_adjustment($items_depth)
265
	{
266 6
		return ($items_depth >= $this->max_depth) ? true : false;
267
	}
268
269
	/**
270
	 * @param array $row
271
	 * @return void
272
	 */
273 6
	protected function adjust_depth(array $row)
274
	{
275 6
		$depth = (int) $row['depth'];
276 6
		if ($this->needs_adjustment($depth))
277 6
		{
278 3
			$adjustment = ($this->count_descendants($row)) ? 1 : 0;
279 3
			$this->set_depth_limits($depth, $adjustment);
280 3
		}
281 6
	}
282
283
	/**
284
	 * @param int $item_id
285
	 * @param array $data
286
	 * @param array $leaf
287
	 * @return void
288
	 */
289 4
	protected function adjust_right_id($item_id, array &$data, array $leaf)
290
	{
291 4
		if (isset($data[$item_id]))
292 4
		{
293 4
			$data[$leaf['item_id']]['right_id'] -= 2;
294 4
		}
295 4
	}
296
297
	/**
298
	 * @param array $data
299
	 * @param int $parent_id
300
	 * @return void
301
	 */
302 8
	protected function find_parents(array $data, $parent_id)
303
	{
304 8
		if (isset($data[$parent_id]) && $data[$parent_id]['item_url'] !== 'index.php')
305 8
		{
306 2
			$row = $data[$parent_id];
307 2
			$this->template->alter_block_array('navlinks', array(
308 2
				'FORUM_NAME'	=> $row['item_title'],
309 2
				'U_VIEW_FORUM'	=> $row['full_url'],
310 2
			));
311
312 2
			$this->find_parents($data, $row['parent_id']);
313 2
		}
314 8
	}
315
316
	/**
317
	 * Append session id to local, non-directory paths
318
	 *
319
	 * @param array $row
320
	 * @return string
321
	 */
322 10
	protected function get_full_url(array $row)
323
	{
324 10
		$full_url = $row['full_url'];
325 10
		if ($row['is_navigable'])
326 10
		{
327 6
			$full_url = append_sid($row['full_url'], false, false);
328 6
		}
329
330 10
		return $full_url;
331
	}
332
333
	/**
334
	 * @param int $depth
335
	 * @param int $adjustment
336
	 */
337 3
	protected function set_depth_limits($depth, $adjustment)
338
	{
339 3
		$this->min_depth = ($this->max_depth && $depth >= $this->max_depth) ? $depth - $this->max_depth + $adjustment : 0;
340 3
		$this->max_depth = $depth + $adjustment;
341 3
	}
342
}
343