Completed
Push — develop ( 733603...f85365 )
by Daniel
15:29 queued 09:35
created

display::is_child_of_current_item()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

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