Completed
Push — develop ( 3c36de...4211b9 )
by Daniel
10:56
created

display::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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