Completed
Push — develop ( f85365...cc1e8e )
by Daniel
15:30 queued 09:18
created

display::show_sitemaker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

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