Completed
Push — master ( edb932...4a8b55 )
by Daniel
08:30
created

display::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 5
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 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 7
	public function set_params(array $params)
53
	{
54 7
		$this->expanded = (bool) $params['expanded'];
55 7
		$this->max_depth = (int) $params['max_depth'];
56 7
	}
57
58
	/**
59
	 *
60
	 */
61 7
	public function display_navlist(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree')
62
	{
63 7
		$this->prepare_items($data);
64
65 7
		if (sizeof($data))
66 7
		{
67 7
			$this_depth = 0;
68 7
			foreach ($data as $row)
69
			{
70 7
				$prev_depth = $row['prev_depth'];
71 7
				$this_depth = $row['this_depth'];
72 7
				$row['num_kids'] = $this->count_descendants($row);
73
74 7
				$template->assign_block_vars($handle, array_change_key_case($row, CASE_UPPER));
75 7
				$this->close_open_tags($template, $handle . '.close', abs($prev_depth - $this_depth));
76 7
			}
77
78 7
			$this->close_open_tags($template, 'close_' . $handle, ($this_depth - $this->min_depth));
79 7
		}
80 7
	}
81
82 5
	public function generate_breadcrumb(array $data)
83
	{
84 5
		$this->find_parents($data, $this->current_item['parent_id']);
85 5
	}
86
87 7
	protected function prepare_items(array &$data)
88
	{
89 7
		$this->set_current_item($data);
90
91 7
		$prev_depth = $this->min_depth;
92 7
		$this->parental_depth = array(0 => -1);
93
94 7
		foreach ($data as $item_id => $row)
95
		{
96
			// Skip branch
97 7
			if (isset($leaf))
98 7
			{
99 6
				if ($row['left_id'] < $leaf['right_id'])
100 6
				{
101 4
					$this->adjust_right_id($leaf['item_id'], $data, $leaf);
102 4
					unset($data[$item_id]);
103 4
					continue;
104
				}
105 6
				unset($leaf);
106 6
			}
107
108 7
			$is_current_item = $this->is_current_item($row);
109 7
			$this_depth	= $this->parental_depth[$row['parent_id']] + 1;
110
111 7
			$this->set_parental_depth($row, $this_depth, $is_current_item);
112
113 7
			if ($row['depth'] == $this->max_depth)
114 7
			{
115 7
				$leaf = $row;
116 7
			}
117
118 7
			if ($row['depth'] < $this->min_depth)
119 7
			{
120 2
				unset($data[$item_id]);
121 2
				continue;
122
			}
123
124 7
			$data[$item_id] = array_merge($data[$item_id], array(
125 7
				'prev_depth'	=> $prev_depth,
126 7
				'this_depth'	=> $this_depth,
127 7
				'is_current'	=> $is_current_item,
128 7
				'full_url'		=> append_sid($row['full_url']),
129 7
			));
130
131 7
			$prev_depth = $this_depth;
132 7
		}
133 7
	}
134
135 7
	protected function set_current_item($data)
136
	{
137 7
		$curr_page = $this->user->page['page_name'];
138 7
		$curr_parts = explode('&', $this->user->page['query_string']);
139
140 7
		$data = array_values($data);
141 7
		for ($i = 0, $size = sizeof($data); $i < $size; $i++)
142
		{
143 7
			$row = $data[$i];
144 7
			if ($this->is_current_path($curr_page, $curr_parts, $row))
145 7
			{
146 6
				$this->adjust_depth($row);
147 6
				$this->current_item = $row;
148 6
				return true;
149
			}
150 4
		}
151
152 1
		$this->current_item = $this->default_current_item();
153 1
		return false;
154
	}
155
156 1
	protected function default_current_item()
157
	{
158 1
		$this->max_depth = ($this->expanded) ? $this->max_depth : 0;
159 1
		$this->min_depth = 0;
160
161
		return array(
162 1
			'item_id'	=> 0,
163 1
			'parent_id'	=> 0,
164 1
			'left_id'	=> 0,
165 1
			'right_id'	=> 0,
166 1
			'depth'		=> 0,
167 1
		);
168
	}
169
170 7
	protected function is_current_path($curr_page, array $curr_parts, array $row)
171
	{
172 7
		return ($curr_page == $row['url_path'] && (!sizeof($row['url_query']) || sizeof(array_intersect($row['url_query'], $curr_parts)))) ? true : false;
173
	}
174
175 7
	protected function is_current_item(array $row)
176
	{
177 7
		return ($row['item_id'] === $this->current_item['item_id']) ? true : false;
178
	}
179
180 7
	protected function set_parental_depth($row, $depth, $is_current_item)
181
	{
182 7
		if ($is_current_item || $this->expanded || !$row['item_url'] || ($row['left_id'] < $this->current_item['left_id'] && $row['right_id'] > $this->current_item['right_id']))
183 7
		{
184 6
			$this->parental_depth[$row[$this->pk]] = $depth;
185 6
		}
186 7
	}
187
188 7
	protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat)
189
	{
190 7
		for ($i = 0; $i < $repeat; $i++)
191
		{
192 5
			$template->assign_block_vars($handle, array());
193 5
		}
194 7
	}
195
196 6
	protected function needs_adjustment($items_depth)
197
	{
198 6
		return ($items_depth >= $this->max_depth || !$this->expanded) ? true : false;
199
	}
200
201 6
	protected function adjust_depth(array $row)
202
	{
203 6
		$depth = (int) $row['depth'];
204 6
		if ($this->needs_adjustment($depth))
205 6
		{
206 5
			$adjustment = ($this->count_descendants($row)) ? 1 : 0;
207 5
			$this->min_depth = ($depth) ? $depth - $this->max_depth + $adjustment : 0;
208 5
			$this->max_depth = $depth + $adjustment;
209 5
		}
210 6
	}
211
212 4
	protected function adjust_right_id($item_id, array &$data, array $leaf)
213
	{
214 4
		if (isset($data[$item_id]))
215 4
		{
216 4
			$data[$leaf['item_id']]['right_id'] -= 2;
217 4
		}
218 4
	}
219
220 5
	protected function find_parents(array $data, $parent_id)
221
	{
222 5
		if (isset($data[$parent_id]) && $data[$parent_id]['item_url'] !== 'index.php')
223 5
		{
224 2
			$row = $data[$parent_id];
225 2
			$this->template->alter_block_array('navlinks', array(
226 2
				'FORUM_NAME'	=> $row['item_title'],
227 2
				'U_VIEW_FORUM'	=> $row['full_url'],
228 2
			));
229
230 2
			$this->find_parents($data, $row['parent_id']);
231 2
		}
232 5
	}
233
}
234