Completed
Push — develop ( a3ab64...524863 )
by Daniel
13:34
created

display::get_layout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
ccs 3
cts 3
cp 1
cc 2
eloc 4
nc 2
nop 1
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\blocks;
11
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
class display
15
{
16
	/** @var \phpbb\auth\auth */
17
	protected $auth;
18
19
	/** @var \phpbb\config\config */
20
	protected $config;
21
22
	/** @var ContainerInterface */
23
	protected $phpbb_container;
24
25
	/** @var \phpbb\request\request_interface */
26
	protected $request;
27
28
	/** @var \phpbb\template\template */
29
	protected $template;
30
31
	/** @var \phpbb\language\language */
32
	protected $translator;
33
34
	/** @var \phpbb\user */
35
	protected $user;
36
37
	/** @var bool */
38
	private $is_subpage;
39
40
	/** @var string */
41
	public $route;
42
43
	const SHOW_BLOCK_BOTH = 0;
44
	const SHOW_BLOCK_LANDING = 1;
45
	const SHOW_BLOCK_SUBPAGE = 2;
46
47
	/**
48
	 * Constructor
49
	 *
50
	 * @param \phpbb\auth\auth							$auth					Auth object
51
	 * @param \phpbb\config\config						$config					Config object
52
	 * @param ContainerInterface						$phpbb_container		Service container
53
	 * @param \phpbb\request\request_interface			$request				Request object
54
	 * @param \phpbb\template\template					$template				Template object
55
	 * @param \phpbb\language\language					$translator				Language object
56
	 * @param \phpbb\user								$user					User object
57
	 */
58 6
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, ContainerInterface $phpbb_container, \phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\language\language $translator, \phpbb\user $user)
59
	{
60 6
		$this->auth = $auth;
61 6
		$this->config = $config;
62 6
		$this->phpbb_container = $phpbb_container;
63 6
		$this->request = $request;
64 6
		$this->template = $template;
65 6
		$this->translator = $translator;
66 6
		$this->user = $user;
67 6
	}
68
69
	/**
70
	 * Show blocks
71
	 */
72 6
	public function show()
73
	{
74 6
		$this->template->assign_var('L_INDEX', $this->translator->lang('HOME'));
75 6
76 6
		if ($this->page_can_have_blocks())
77
		{
78 6
			$style_id = $this->get_style_id();
79
			$edit_mode = $this->toggle_edit_mode();
80 6
			$display_modes = $this->get_display_modes();
81 6
			$u_edit_mode = $this->get_edit_mode_url($edit_mode, $display_modes);
82 4
83 4
			$this->show_blocks($style_id, $edit_mode, $display_modes);
84 4
85
			$this->template->assign_vars(array(
86 4
				'S_SITEMAKER'		=> true,
87
				'S_LAYOUT'			=> $this->get_layout($style_id),
88 4
				'U_EDIT_MODE'		=> $u_edit_mode,
89 4
			));
90 4
		}
91 4
	}
92 4
93 6
	/**
94
	 * Set current route
95
	 */
96
	public function set_route()
97
	{
98 4
		$this->route = $this->user->page['page_name'];
99
		$this->is_subpage = ($this->user->page['query_string']) ? true : false;
100 4
	}
101 4
102 4
	/**
103
	 * Get style id
104
	 * @return int
105
	 */
106
	public function get_style_id()
107
	{
108 4
		if ($this->request->is_set('style'))
109
		{
110 4
			return $this->request->variable('style', 0);
111 4
		}
112 1
		else
113
		{
114
			return (!$this->config['override_user_style']) ? $this->user->data['user_style'] : $this->config['default_style'];
115
		}
116 3
	}
117
118
	/**
119
	 * @return bool
120
	 */
121
	protected function page_can_have_blocks()
122
	{
123 6
		$offlimits = array('ucp.php', 'mcp.php', 'memberlist.php');
124
		return ($this->user->page['page_dir'] == 'adm' || in_array($this->user->page['page_name'], $offlimits)) ? false : true;
125 6
	}
126 6
127
	/**
128
	 * @param bool  $edit_mode
129
	 * @param array $route_info
130
	 */
131
	protected function show_admin_bar($edit_mode, array $route_info)
132
	{
133 4
		if ($edit_mode)
134
		{
135
			$this->phpbb_container->get('blitze.sitemaker.blocks.admin_bar')->show($route_info);
136 4
		}
137 2
	}
138 2
139 4
	/**
140
	 * @return bool
141
	 */
142
	protected function toggle_edit_mode()
143
	{
144 4
		$edit_mode = $this->request->variable($this->config['cookie_name'] . '_sm_edit_mode', false, false, \phpbb\request\request_interface::COOKIE);
145
146 4
		if ($this->request->is_set('edit_mode'))
147
		{
148 4
			$edit_mode = $this->request->variable('edit_mode', false);
149 4
			$this->user->set_cookie('sm_edit_mode', $edit_mode, 0);
150 1
		}
151 1
152 1
		return $edit_mode;
153
	}
154 4
155
	/**
156
	 * @return array
157
	 */
158
	protected function get_display_modes()
159
	{
160 4
		$this->set_route();
161
162 4
		if ($this->is_subpage === false)
163
		{
164 4
			$modes = array(
165 4
				self::SHOW_BLOCK_BOTH		=> true,
166
				self::SHOW_BLOCK_LANDING	=> true,
167 3
				self::SHOW_BLOCK_SUBPAGE	=> false,
168 3
			);
169 3
		}
170 3
		else
171 3
		{
172
			$modes = array(
173
				self::SHOW_BLOCK_BOTH		=> true,
174
				self::SHOW_BLOCK_LANDING	=> false,
175 1
				self::SHOW_BLOCK_SUBPAGE	=> true,
176 1
			);
177 1
		}
178 1
179
		return $modes;
180
	}
181 4
182
	/**
183
	 * @param bool  $edit_mode
184
	 * @param array $modes
185
	 * @return string
186
	 */
187
	protected function get_edit_mode_url(&$edit_mode, array &$modes)
188
	{
189 4
		$u_edit_mode = '';
190
		if ($this->auth->acl_get('a_sm_manage_blocks'))
191 4
		{
192 4
			if ($edit_mode)
193 4
			{
194
				$modes = array(
195 3
					self::SHOW_BLOCK_BOTH		=> true,
196
					self::SHOW_BLOCK_LANDING	=> true,
197 2
					self::SHOW_BLOCK_SUBPAGE	=> true,
198 2
				);
199 2
			}
200 2
201 2
			$u_edit_mode = append_sid(generate_board_url() . '/' . ltrim(rtrim(build_url(array('edit_mode', 'style')), '?'), './../'), 'edit_mode=' . (int) !$edit_mode);
202
		}
203 3
		else
204 3
		{
205
			$edit_mode = false;
206
		}
207 1
208
		return $u_edit_mode;
209
	}
210 4
211
	/**
212
	 * @param int $style_id
213
	 * @return string
214
	 */
215
	protected function get_layout($style_id)
216
	{
217 4
		$config_text = $this->phpbb_container->get('config_text');
218
		$style_prefs = json_decode($config_text->get('sm_layout_prefs'), true);
219 4
220
		return (isset($style_prefs[$style_id])) ? basename($style_prefs[$style_id]['layout']) : 'default';
221 4
	}
222 4
223
	/**
224 4
	 * @param int $style_id
225
	 * @param bool  $edit_mode
226 4
	 * @param array $display_modes
227 4
	 */
228
	protected function show_blocks($style_id, $edit_mode, array $display_modes)
229
	{
230
		$blocks = $this->phpbb_container->get('blitze.sitemaker.blocks');
231
232
		$route_info = $blocks->get_route_info($this->route, $style_id, $edit_mode);
233
234
		$this->show_admin_bar($edit_mode, $route_info);
235
236
		$blocks->display($edit_mode, $route_info, $style_id, $display_modes);
237
	}
238
}
239