Completed
Push — master ( db7709...24ebcc )
by Daniel
10:24
created

display::is_current_item()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 2
crap 2
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\user */
15
	protected $user;
16
17
	/** @var bool */
18
	private $expanded = false;
19
20
	/** @var integer */
21
	private $max_depth = 0;
22
23
	/** @var array */
24
	private $parental_depth;
25
26
	/**
27
	 * Construct
28
	 *
29
	 * @param \phpbb\db\driver\driver_interface		$db             	Database connection
30
	 * @param \phpbb\user							$user				User Object
31
	 * @param string								$menu_items_table	Menu Items table
32
	 * @param string								$pk					Primary key
33
	 */
34 8
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, $menu_items_table, $pk)
35
	{
36 8
		parent::__construct($db, $menu_items_table, $pk);
37
38 8
		$this->user = $user;
39 8
	}
40
41 3
	public function set_params(array $params)
42
	{
43 3
		$this->expanded = (bool) $params['expanded'];
44 3
		$this->max_depth = (int) $params['max_depth'];
45 3
	}
46
47
	/**
48
	 *
49
	 */
50 3
	public function display_list(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree')
51
	{
52 3
		$data = array_values($data);
53
54 3
		$current_page = $this->user->page['page_name'];
55 3
		$current_data = $this->get_current_item($data, $current_page);
56
57 3
		$prev_depth = 0;
58 3
		$this->parental_depth = array(0 => -1);
59
60 3
		for ($i = 0, $size = sizeof($data); $i < $size; $i++)
61
		{
62 3
			$row = $data[$i];
63
64 3
			if (!isset($this->parental_depth[$row['parent_id']]))
65 3
			{
66 1
				continue;
67
			}
68
69 3
			$is_current_item = $this->is_current_item($row, $current_data['item_id']);
70 3
			$this_depth	= $this->parental_depth[$row['parent_id']] + 1;
71
72 3
			$this->set_parental_depth($row, $this_depth, $current_data, $is_current_item);
73
74 3
			if ($this->max_depth && ($current_data['depth'] - $row['depth']) > $this->max_depth)
75 3
			{
76 1
				continue;
77
			}
78
79
			$tpl_data	= array(
80 3
				'S_PREV_DEPTH'	=> $prev_depth,
81 3
				'S_THIS_DEPTH'	=> $this_depth,
82 3
				'S_NUM_KIDS'	=> $this->count_descendants($row),
83 3
				'S_CURRENT'		=> $is_current_item,
84 3
			);
85
86 3
			$row['full_url'] = append_sid($row['full_url']);
87 3
			$template->assign_block_vars($handle, array_merge($tpl_data, array_change_key_case($row, CASE_UPPER)));
88
89 3
			$this->close_open_tags($template, $handle . '.close', abs($prev_depth - $this_depth));
90 3
			$prev_depth = $this_depth;
91 3
		}
92
93 3
		$this->close_open_tags($template, 'close_' . $handle, $prev_depth);
94 3
	}
95
96 3
	protected function get_current_item($data, $curr_page)
97
	{
98 3
		$curr_parts = explode('&', $this->user->page['query_string']);
99
100 3
		for ($i = 0, $size = sizeof($data); $i < $size; $i++)
101
		{
102 3
			$row = $data[$i];
103
104 3
			if ($curr_page == $row['url_path'] && (!sizeof($row['url_query']) || sizeof(array_intersect($row['url_query'], $curr_parts))))
105 3
			{
106 3
				$this->max_depth += ($this->count_descendants($row)) ? 1 : 0;
107
108 3
				return $row;
109
			}
110 2
		}
111
112
		return array(
113
			'item_id'	=> 0,
114
			'left_id'	=> 0,
115
			'right_id'	=> 0,
116
			'depth'		=> 0,
117
		);
118
	}
119
120 3
	protected function is_current_item($row, $current_item_id)
121
	{
122 3
		return ($row['item_id'] === $current_item_id) ? true : false;
123
	}
124
125 3
	protected function set_parental_depth($row, $depth, $current_data, $is_current_item)
126
	{
127 3
		if ($is_current_item || $this->expanded || !$row['item_url'] || ($row['left_id'] < $current_data['left_id'] && $row['right_id'] > $current_data['right_id']))
128 3
		{
129 3
			$this->parental_depth[$row[$this->pk]] = $depth;
130 3
		}
131 3
	}
132
133 3
	protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat)
134
	{
135 3
		for ($i = 0; $i < $repeat; $i++)
136
		{
137 3
			$template->assign_block_vars($handle, array());
138 3
		}
139 3
	}
140
}
141