Completed
Push — master ( a93112...75382e )
by Daniel
16:13
created

menu::_get_message()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4286
cc 3
eloc 5
nc 3
nop 2
crap 12
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\blocks;
11
12
/**
13
* Menu Block
14
* @package phpBB Sitemaker Menu
15
*/
16
class menu extends \blitze\sitemaker\services\blocks\driver\block
17
{
18
	/** @var \phpbb\cache\driver\driver_interface */
19
	protected $cache;
20
21
	/** @var \phpbb\config\config */
22
	protected $config;
23
24
	/** @var \phpbb\user */
25
	protected $user;
26
27
	/** @var \blitze\sitemaker\model\mapper_factory */
28
	protected $mapper_factory;
29
30
	/** @var \blitze\sitemaker\services\menus\display */
31
	protected $tree;
32
33
	/**
34
	 * Constructor
35
	 *
36
	 * @param \phpbb\cache\driver\driver_interface		$cache				Cache driver interface
37
	 * @param \phpbb\config\config						$config				Config object
38
	 * @param \phpbb\template\template					$user				User object
39
	 * @param \blitze\sitemaker\model\mapper_factory	$mapper_factory		Mapper factory object
40
	 * @param \blitze\sitemaker\services\menus\display	$tree				Menu tree display object
41
	 */
42
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\user $user, \blitze\sitemaker\model\mapper_factory $mapper_factory, \blitze\sitemaker\services\menus\display $tree)
43
	{
44
		$this->cache = $cache;
45
		$this->config = $config;
46
		$this->user = $user;
47
		$this->mapper_factory = $mapper_factory;
48
		$this->tree = $tree;
49
	}
50
51
	/**
52
	 * {@inheritdoc}
53
	 */
54
	public function get_config(array $settings)
55
	{
56
		$menu_options = $this->get_menu_options();
57
		$depth_options = $this->get_depth_options();
58
59
		return array(
60
			'legend1'       => $this->user->lang('SETTINGS'),
61
			'menu_id'		=> array('lang' => 'MENU', 'validate' => 'int', 'type' => 'select', 'options' => $menu_options, 'default' => 0, 'explain' => false),
62
			'expanded'		=> array('lang' => 'EXPANDED', 'validate' => 'bool', 'type' => 'checkbox', 'options' => array(1 => ''), 'default' => 0, 'explain' => false),
63
			'max_depth'		=> array('lang' => 'MAX_DEPTH', 'validate' => 'int', 'type' => 'select', 'options' => $depth_options, 'default' => 3, 'explain' => false),
64
		);
65
	}
66
67
	/**
68
	 * {@inheritdoc}
69
	 */
70
	public function display(array $db_data, $editing = false)
71
	{
72
		$title = $this->user->lang('MENU');
73
		$menu_id = $db_data['settings']['menu_id'];
74
75
		$data = $this->_get_menu($menu_id);
76
77
		if (!sizeof($data))
78
		{
79
			return array(
80
				'title'		=> $title,
81
				'content'	=> $this->_get_message($menu_id, $editing),
82
			);
83
		}
84
85
		$this->tree->set_params($db_data['settings']);
86
		$this->tree->display_list($data, $this->ptemplate, 'tree');
87
88
		return array(
89
			'title'     => $title,
90
			'content'   => $this->ptemplate->render_view('blitze/sitemaker', 'blocks/menu.html', 'menu_block'),
91
		);
92
	}
93
94
	/**
95
	 * @param int $menu_id
96
	 * @return array
97
	 */
98
	private function _get_menu($menu_id)
99
	{
100
		if (($data = $this->cache->get('sitemaker_menus')) === false)
101
		{
102
			$data = $this->_get_all_menus();
103
104
			$this->cache->put('sitemaker_menus', $data);
105
		}
106
107
		return (isset($data[$menu_id])) ? $data[$menu_id] : array();
108
	}
109
110
	/**
111
	 * @return array
112
	 */
113
	private function _get_all_menus()
114
	{
115
		$item_mapper = $this->mapper_factory->create('menus', 'items');
116
117
		$collection = $item_mapper->find(array(
118
			'item_status' => 1
119
		));
120
121
		$data = array();
122
		foreach ($collection as $entity)
123
		{
124
			if (!$entity->get_item_title())
125
			{
126
				continue;
127
			}
128
129
			$row = $entity->to_array();
130
			$this->_set_path_info($row);
131
132
			$data[$row['menu_id']][$row['item_id']] = $row;
133
		}
134
135
		return $data;
136
	}
137
138
	/**
139
	 * @param int $menu_id
140
	 * @param bool $editing
141
	 * @return string
142
	 */
143
	private function _get_message($menu_id, $editing)
144
	{
145
		$msg_key = '';
146
		if ($editing)
147
		{
148
			$msg_key = ($menu_id) ? 'MENU_NO_ITEMS' : 'SELECT_MENU';
149
		}
150
151
		return $this->user->lang($msg_key);
152
	}
153
154
	/**
155
	 * @param array $data
156
	 */
157
	private function _set_path_info(array &$data)
158
	{
159
		$url_info = parse_url($data['item_url']);
160
161
		$data['url_path']	= (isset($url_info['path'])) ? $url_info['path'] : '';
162
		$data['url_query']	= (isset($url_info['query'])) ? explode('&', $url_info['query']) : array();
163
	}
164
165
	/**
166
	 * @return array
167
	 */
168
	protected function get_menu_options()
169
	{
170
		$collection = $this->mapper_factory->create('menus', 'menus')->find();
171
172
		$options = array();
173
		foreach ($collection as $entity)
174
		{
175
			$options[$entity->get_menu_id()] = $entity->get_menu_name();
176
		}
177
178
		return $options;
179
	}
180
181
	/**
182
	 * @return array
183
	 */
184
	protected function get_depth_options()
185
	{
186
		$options = array();
187
		for ($i = 3; $i < 10; $i++)
188
		{
189
			$options[$i] = $i;
190
		}
191
192
		return $options;
193
	}
194
}
195