Passed
Push — develop ( f95e2c...24f786 )
by Daniel
26:40
created

display::get_column_widths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
ccs 1
cts 1
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 \phpbb\config\db_text */
23
	protected $config_text;
24
25
	/** @var ContainerInterface */
26
	protected $phpbb_container;
27
28
	/** @var \phpbb\request\request_interface */
29
	protected $request;
30
31
	/** @var \phpbb\template\template */
32
	protected $template;
33
34
	/** @var \phpbb\user */
35
	protected $user;
36
37
	const SHOW_ON_ALL_ROUTES = 0;
38
	const SHOW_ON_PARENT_ROUTE_ONLY = 1;
39
	const SHOW_ON_CHILD_ROUTE_ONLY = 2;
40
41
	/**
42
	 * Constructor
43
	 *
44
	 * @param \phpbb\auth\auth							$auth					Auth object
45
	 * @param \phpbb\config\config						$config					Config object
46
	 * @param \phpbb\config\db_text						$config_text			Config text object
47
	 * @param ContainerInterface						$phpbb_container		Service container
48
	 * @param \phpbb\request\request_interface			$request				Request object
49
	 * @param \phpbb\template\template					$template				Template object
50
	 * @param \phpbb\user								$user					User object
51
	 */
52 19
	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)
53
	{
54 19
		$this->auth = $auth;
55 19
		$this->config = $config;
56 19
		$this->config_text = $config_text;
57 19
		$this->phpbb_container = $phpbb_container;
58 19
		$this->request = $request;
59 19
		$this->template = $template;
60 19
		$this->user = $user;
61 19
	}
62
63
	/**
64
	 * Show blocks
65
	 */
66 19
	public function show()
67
	{
68 19
		if ($this->page_can_have_blocks())
69 19
		{
70 17
			$edit_mode = $this->toggle_edit_mode();
71 17
			$style_id = $this->get_style_id();
72 17
			$current_route = ltrim($this->user->page['page_dir'] . '/' . $this->user->page['page_name'], './');
73
74 17
			$this->show_sitemaker($current_route, $this->user->page['page_dir'], $style_id, $edit_mode);
75 17
		}
76 19
	}
77
78
	/**
79
	 * Get style id
80
	 * @return int
81
	 */
82 17
	public function get_style_id()
83
	{
84 17
		if ($this->request->is_set('style'))
85 17
		{
86 2
			return $this->request->variable('style', 0);
87
		}
88
		else
89
		{
90 15
			return (int) ((!$this->config['override_user_style']) ? $this->user->data['user_style'] : $this->config['default_style']);
91
		}
92
	}
93
94
	/**
95
	 * @return bool
96
	 */
97 19
	protected function page_can_have_blocks()
98
	{
99 19
		$offlimits = array('ucp.php', 'mcp.php', 'memberlist.php');
100 19
		return ($this->user->page['page_dir'] == 'adm' || in_array($this->user->page['page_name'], $offlimits)) ? false : true;
101
	}
102
103
	/**
104
	 * @return bool
105
	 */
106 17
	protected function toggle_edit_mode()
107
	{
108 17
		$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

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