Passed
Push — forum_specific_blocks ( 6a1fa8 )
by Daniel
13:29
created

display::get_current_route()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\services\blocks;
12
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
class display
16
{
17
	/** @var \phpbb\auth\auth */
18
	protected $auth;
19
20
	/** @var \phpbb\config\config */
21
	protected $config;
22
23
	/** @var \phpbb\config\db_text */
24
	protected $config_text;
25
26
	/** @var ContainerInterface */
27
	protected $phpbb_container;
28
29
	/** @var \phpbb\request\request_interface */
30
	protected $request;
31
32
	/** @var \phpbb\template\template */
33
	protected $template;
34
35
	/** @var \phpbb\user */
36
	protected $user;
37
38
	const SHOW_ON_ALL_ROUTES = 0;
39
	const SHOW_ON_PARENT_ROUTE_ONLY = 1;
40
	const SHOW_ON_CHILD_ROUTE_ONLY = 2;
41
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param \phpbb\auth\auth							$auth					Auth object
46
	 * @param \phpbb\config\config						$config					Config object
47
	 * @param \phpbb\config\db_text						$config_text			Config text object
48
	 * @param ContainerInterface						$phpbb_container		Service container
49
	 * @param \phpbb\request\request_interface			$request				Request object
50
	 * @param \phpbb\template\template					$template				Template object
51
	 * @param \phpbb\user								$user					User object
52
	 */
53
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\config\db_text $config_text, ContainerInterface $phpbb_container, \phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\user $user)
54
	{
55
		$this->auth = $auth;
56
		$this->config = $config;
57
		$this->config_text = $config_text;
58
		$this->phpbb_container = $phpbb_container;
59
		$this->request = $request;
60
		$this->template = $template;
61
		$this->user = $user;
62
	}
63
64
	/**
65
	 * Show blocks
66
	 */
67
	public function show()
68
	{
69
		if ($this->page_can_have_blocks())
70
		{
71
			$edit_mode = $this->toggle_edit_mode();
72
			$style_id = $this->get_style_id();
73
			$current_route = $this->get_current_route();
74
75
			$this->show_sitemaker($current_route, $this->user->page['page_dir'], $style_id, $edit_mode);
76
		}
77
	}
78
79
	/**
80
	 * @return string
81
	 */
82
	protected function get_current_route()
83
	{
84
		return ltrim($this->user->page['page_dir'] . '/' . $this->user->page['page_name'], './') . ($this->user->page['forum'] ? '?f=' . $this->user->page['forum'] : '');
85
	}
86
87
	/**
88
	 * Get style id
89
	 * @return int
90
	 */
91
	public function get_style_id()
92
	{
93
		if ($this->request->is_set('style'))
94
		{
95
			return $this->request->variable('style', 0);
96
		}
97
		else
98
		{
99
			return (int) ((!$this->config['override_user_style']) ? $this->user->data['user_style'] : $this->config['default_style']);
100
		}
101
	}
102
103
	/**
104
	 * @return bool
105
	 */
106
	protected function page_can_have_blocks()
107
	{
108
		$offlimits = array('ucp.php', 'mcp.php', 'memberlist.php');
109
		return ($this->user->page['page_dir'] == 'adm' || in_array($this->user->page['page_name'], $offlimits)) ? false : true;
110
	}
111
112
	/**
113
	 * @return bool
114
	 */
115
	protected function toggle_edit_mode()
116
	{
117
		$edit_mode = $this->request->variable($this->config['cookie_name'] . '_sm_edit_mode', false, false, \phpbb\request\request_interface::COOKIE);
0 ignored issues
show
Bug introduced by
phpbb\request\request_interface::COOKIE of type integer is incompatible with the type phpbb\request\request_interface expected by parameter $super_global of phpbb\request\request_interface::variable(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

117
		$edit_mode = $this->request->variable($this->config['cookie_name'] . '_sm_edit_mode', false, false, /** @scrutinizer ignore-type */ \phpbb\request\request_interface::COOKIE);
Loading history...
118
119
		if ($this->request->is_set('edit_mode'))
120
		{
121
			$edit_mode = $this->request->variable('edit_mode', false);
122
			$this->user->set_cookie('sm_edit_mode', $edit_mode, 0);
123
		}
124
125
		return $edit_mode;
126
	}
127
128
	/**
129
	 * @param bool $is_sub_route
130
	 * @return array
131
	 */
132
	protected function get_display_modes($is_sub_route)
133
	{
134
		if ($is_sub_route === false)
135
		{
136
			$modes = array(
137
				self::SHOW_ON_ALL_ROUTES		=> true,
138
				self::SHOW_ON_PARENT_ROUTE_ONLY	=> true,
139
				self::SHOW_ON_CHILD_ROUTE_ONLY	=> false,
140
			);
141
		}
142
		else
143
		{
144
			$modes = array(
145
				self::SHOW_ON_ALL_ROUTES		=> true,
146
				self::SHOW_ON_PARENT_ROUTE_ONLY	=> false,
147
				self::SHOW_ON_CHILD_ROUTE_ONLY	=> true,
148
			);
149
		}
150
151
		return $modes;
152
	}
153
154
	/**
155
	 * @param bool  $edit_mode
156
	 * @param array $modes
157
	 * @return string
158
	 */
159
	protected function get_edit_mode_url(&$edit_mode, array &$modes)
160
	{
161
		$u_edit_mode = '';
162
		if ($this->auth->acl_get('a_sm_manage_blocks'))
163
		{
164
			if ($edit_mode)
165
			{
166
				$modes = array(
167
					self::SHOW_ON_ALL_ROUTES		=> true,
168
					self::SHOW_ON_PARENT_ROUTE_ONLY	=> true,
169
					self::SHOW_ON_CHILD_ROUTE_ONLY	=> true,
170
				);
171
			}
172
173
			$u_edit_mode = append_sid(generate_board_url() . '/' . ltrim(rtrim(build_url(array('edit_mode', 'sid', 'style')), '?'), './../'), 'edit_mode=' . (int) !$edit_mode);
174
		}
175
		else
176
		{
177
			$edit_mode = false;
178
		}
179
180
		return $u_edit_mode;
181
	}
182
183
	/**
184
	 * @param int $style_id
185
	 * @return string
186
	 */
187
	protected function get_layout($style_id)
188
	{
189
		$style_prefs = array_filter((array) json_decode((string) $this->config_text->get('sm_layout_prefs'), true));
190
191
		return (isset($style_prefs[$style_id])) ? basename($style_prefs[$style_id]['layout']) : 'portal';
192
	}
193
194
	/**
195
	 * @param int $style_id
196
	 * @return string
197
	 */
198
	protected function get_column_widths($style_id)
199
	{
200
		$column_widths = array_filter((array) json_decode($this->config['sitemaker_column_widths'], true));
201
202
		return (isset($column_widths[$style_id])) ? $column_widths[$style_id] : '';
203
	}
204
205
	/**
206
	 * @param int $style_id
207
	 * @param string $current_route
208
	 * @param string $page_dir
209
	 * @param int $style_id
210
	 * @param bool $edit_mode
211
	 */
212
	protected function show_sitemaker($current_route, $page_dir, $style_id, $edit_mode)
213
	{
214
		$blocks = $this->phpbb_container->get('blitze.sitemaker.blocks');
215
216
		$route_info = $blocks->get_route_info($current_route, $page_dir, $style_id, $edit_mode);
217
		$display_modes = $this->get_display_modes($route_info['is_sub_route']);
218
		$u_edit_mode = $this->get_edit_mode_url($edit_mode, $display_modes);
219
220
		$this->show_admin_bar($edit_mode, $route_info);
221
222
		if ($edit_mode || !$route_info['hide_blocks'])
223
		{
224
			$blocks->display($edit_mode, $route_info, $style_id, $display_modes);
225
		}
226
227
		$this->template->assign_vars(array(
228
			'S_SITEMAKER'		=> true,
229
			'S_LAYOUT'			=> $this->get_layout($style_id),
230
			'S_COLUMN_WIDTHS'	=> $this->get_column_widths($style_id),
231
			'U_EDIT_MODE'		=> $u_edit_mode,
232
		));
233
	}
234
235
	/**
236
	 * @param bool  $edit_mode
237
	 * @param array $route_info
238
	 */
239
	protected function show_admin_bar($edit_mode, array $route_info)
240
	{
241
		if ($edit_mode)
242
		{
243
			$this->phpbb_container->get('blitze.sitemaker.blocks.admin_bar')->show($route_info);
244
		}
245
	}
246
}
247