Completed
Push — develop ( 524863...c767cb )
by Daniel
10:30
created

display::show_blocks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 3
crap 1
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
76 6
		if ($this->page_can_have_blocks())
77 6
		{
78 4
			$style_id = $this->get_style_id();
79 4
			$edit_mode = $this->toggle_edit_mode();
80 4
			$display_modes = $this->get_display_modes();
81 4
			$u_edit_mode = $this->get_edit_mode_url($edit_mode, $display_modes);
82
83 4
			$this->show_blocks($style_id, $edit_mode, $display_modes);
84
85 4
			$this->template->assign_vars(array(
86 4
				'S_SITEMAKER'		=> true,
87 4
				'S_LAYOUT'			=> $this->get_layout($style_id),
88 4
				'U_EDIT_MODE'		=> $u_edit_mode,
89 4
			));
90 4
		}
91 6
	}
92
93
	/**
94
	 * Set current route
95
	 */
96 4
	public function set_route()
97
	{
98 4
		$this->route = $this->user->page['page_name'];
99 4
		$this->is_subpage = ($this->user->page['query_string']) ? true : false;
100 4
	}
101
102
	/**
103
	 * Get style id
104
	 * @return int
105
	 */
106 4
	public function get_style_id()
107
	{
108 4
		if ($this->request->is_set('style'))
109 4
		{
110 1
			return $this->request->variable('style', 0);
111
		}
112
		else
113
		{
114 3
			return (!$this->config['override_user_style']) ? $this->user->data['user_style'] : $this->config['default_style'];
115
		}
116
	}
117
118
	/**
119
	 * @return bool
120
	 */
121 6
	protected function page_can_have_blocks()
122
	{
123 6
		$offlimits = array('ucp.php', 'mcp.php', 'memberlist.php');
124 6
		return ($this->user->page['page_dir'] == 'adm' || in_array($this->user->page['page_name'], $offlimits)) ? false : true;
125
	}
126
127
	/**
128
	 * @param bool  $edit_mode
129
	 * @param array $route_info
130
	 */
131 4
	protected function show_admin_bar($edit_mode, array $route_info)
132
	{
133
		if ($edit_mode)
134 4
		{
135 2
			$this->phpbb_container->get('blitze.sitemaker.blocks.admin_bar')->show($route_info);
136 2
		}
137 4
	}
138
139
	/**
140
	 * @return bool
141
	 */
142 4
	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 4
		{
148 1
			$edit_mode = $this->request->variable('edit_mode', false);
149 1
			$this->user->set_cookie('sm_edit_mode', $edit_mode, 0);
150 1
		}
151
152 4
		return $edit_mode;
153
	}
154
155
	/**
156
	 * @return array
157
	 */
158 4
	protected function get_display_modes()
159
	{
160 4
		$this->set_route();
161
162 4
		if ($this->is_subpage === false)
163 4
		{
164
			$modes = array(
165 3
				self::SHOW_BLOCK_BOTH		=> true,
166 3
				self::SHOW_BLOCK_LANDING	=> true,
167 3
				self::SHOW_BLOCK_SUBPAGE	=> false,
168 3
			);
169 3
		}
170
		else
171
		{
172
			$modes = array(
173 1
				self::SHOW_BLOCK_BOTH		=> true,
174 1
				self::SHOW_BLOCK_LANDING	=> false,
175 1
				self::SHOW_BLOCK_SUBPAGE	=> true,
176 1
			);
177
		}
178
179 4
		return $modes;
180
	}
181
182
	/**
183
	 * @param bool  $edit_mode
184
	 * @param array $modes
185
	 * @return string
186
	 */
187 4
	protected function get_edit_mode_url(&$edit_mode, array &$modes)
188
	{
189 4
		$u_edit_mode = '';
190 4
		if ($this->auth->acl_get('a_sm_manage_blocks'))
191 4
		{
192
			if ($edit_mode)
193 3
			{
194
				$modes = array(
195 2
					self::SHOW_BLOCK_BOTH		=> true,
196 2
					self::SHOW_BLOCK_LANDING	=> true,
197 2
					self::SHOW_BLOCK_SUBPAGE	=> true,
198 2
				);
199 2
			}
200
201 3
			$u_edit_mode = append_sid(generate_board_url() . '/' . ltrim(rtrim(build_url(array('edit_mode', 'style')), '?'), './../'), 'edit_mode=' . (int) !$edit_mode);
202 3
		}
203
		else
204
		{
205 1
			$edit_mode = false;
206
		}
207
208 4
		return $u_edit_mode;
209
	}
210
211
	/**
212
	 * @param int $style_id
213
	 * @return string
214
	 */
215 4
	protected function get_layout($style_id)
216
	{
217 4
		$config_text = $this->phpbb_container->get('config_text');
218 4
		$style_prefs = json_decode($config_text->get('sm_layout_prefs'), true);
219
220 4
		return (isset($style_prefs[$style_id])) ? basename($style_prefs[$style_id]['layout']) : 'portal';
221
	}
222
223
	/**
224
	 * @param int $style_id
225
	 * @param bool  $edit_mode
226
	 * @param array $display_modes
227
	 */
228 4
	protected function show_blocks($style_id, $edit_mode, array $display_modes)
229
	{
230 4
		$blocks = $this->phpbb_container->get('blitze.sitemaker.blocks');
231
232 4
		$route_info = $blocks->get_route_info($this->route, $style_id, $edit_mode);
233
234 4
		$this->show_admin_bar($edit_mode, $route_info);
235
236 4
		$blocks->display($edit_mode, $route_info, $style_id, $display_modes);
237 4
	}
238
}
239