Completed
Push — master ( efce96...e6a35d )
by Daniel
15:11
created

display::show()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.9713
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 0
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\user */
32
	protected $user;
33
34
	const SHOW_ON_ALL_ROUTES = 0;
35
	const SHOW_ON_PARENT_ROUTE_ONLY = 1;
36
	const SHOW_ON_CHILD_ROUTE_ONLY = 2;
37
38
	/**
39
	 * Constructor
40
	 *
41
	 * @param \phpbb\auth\auth							$auth					Auth object
42
	 * @param \phpbb\config\config						$config					Config object
43
	 * @param ContainerInterface						$phpbb_container		Service container
44
	 * @param \phpbb\request\request_interface			$request				Request object
45
	 * @param \phpbb\template\template					$template				Template object
46
	 * @param \phpbb\user								$user					User object
47
	 */
48
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, ContainerInterface $phpbb_container, \phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\user $user)
49
	{
50
		$this->auth = $auth;
51
		$this->config = $config;
52
		$this->phpbb_container = $phpbb_container;
53
		$this->request = $request;
54 6
		$this->template = $template;
55
		$this->user = $user;
56 6
	}
57 6
58 6
	/**
59 6
	 * Show blocks
60 6
	 */
61 6
	public function show()
62 6
	{
63
		$this->template->assign_var('L_INDEX', $this->user->lang('HOME'));
64
65
		if ($this->page_can_have_blocks())
66
		{
67 6
			$blocks = $this->phpbb_container->get('blitze.sitemaker.blocks');
68
69 6
			$edit_mode = $this->toggle_edit_mode();
70 6
			$style_id = $this->get_style_id();
71 6
			$route_info = $blocks->get_route_info($this->user->page['page_name'], $style_id, $edit_mode);
72
73 6
			$display_modes = $this->get_display_modes($route_info['is_sub_route']);
74
			$u_edit_mode = $this->get_edit_mode_url($edit_mode, $display_modes);
75 6
76 6
			$this->show_admin_bar($edit_mode, $route_info);
77 4
			$blocks->display($edit_mode, $route_info, $style_id, $display_modes);
78 4
79 4
			$this->template->assign_vars(array(
80
				'S_SITEMAKER'		=> true,
81 4
				'U_EDIT_MODE'		=> $u_edit_mode,
82
			));
83 4
		}
84 4
	}
85 4
86 4
	/**
87 4
	 * Get style id
88 6
	 * @return int
89
	 */
90
	public function get_style_id()
91
	{
92
		if ($this->request->is_set('style'))
93 4
		{
94
			return $this->request->variable('style', 0);
95 4
		}
96 4
		else
97 4
		{
98
			return (!$this->config['override_user_style']) ? $this->user->data['user_style'] : $this->config['default_style'];
99
		}
100
	}
101
102
	/**
103 4
	 * @return bool
104
	 */
105 4
	protected function page_can_have_blocks()
106 4
	{
107 1
		$offlimits = array('ucp.php', 'mcp.php', 'memberlist.php');
108
		return ($this->user->page['page_dir'] == 'adm' || in_array($this->user->page['page_name'], $offlimits)) ? false : true;
109
	}
110
111 3
	/**
112
	 * @return bool
113
	 */
114
	protected function toggle_edit_mode()
115
	{
116
		$edit_mode = $this->request->variable($this->config['cookie_name'] . '_sm_edit_mode', false, false, \phpbb\request\request_interface::COOKIE);
117
118 6
		if ($this->request->is_set('edit_mode'))
119
		{
120 6
			$edit_mode = $this->request->variable('edit_mode', false);
121 6
			$this->user->set_cookie('sm_edit_mode', $edit_mode, 0);
122
		}
123
124
		return $edit_mode;
125
	}
126
127
	/**
128 4
	 * @param bool $is_sub_route
129
	 * @return array
130
	 */
131 4
	protected function get_display_modes($is_sub_route)
132 2
	{
133 2
		if ($is_sub_route === false)
134 4
		{
135
			$modes = array(
136
				self::SHOW_ON_ALL_ROUTES		=> true,
137
				self::SHOW_ON_PARENT_ROUTE_ONLY	=> true,
138
				self::SHOW_ON_CHILD_ROUTE_ONLY	=> false,
139 4
			);
140
		}
141 4
		else
142
		{
143 4
			$modes = array(
144 4
				self::SHOW_ON_ALL_ROUTES		=> true,
145 1
				self::SHOW_ON_PARENT_ROUTE_ONLY	=> false,
146 1
				self::SHOW_ON_CHILD_ROUTE_ONLY	=> true,
147 1
			);
148
		}
149 4
150
		return $modes;
151
	}
152
153
	/**
154
	 * @param bool  $edit_mode
155 4
	 * @param array $modes
156
	 * @return string
157 4
	 */
158
	protected function get_edit_mode_url(&$edit_mode, array &$modes)
159 4
	{
160 4
		$u_edit_mode = '';
161
		if ($this->auth->acl_get('a_sm_manage_blocks'))
162 3
		{
163 3
			if ($edit_mode)
164 3
			{
165 3
				$modes = array(
166 3
					self::SHOW_ON_ALL_ROUTES		=> true,
167
					self::SHOW_ON_PARENT_ROUTE_ONLY	=> true,
168
					self::SHOW_ON_CHILD_ROUTE_ONLY	=> true,
169
				);
170 1
			}
171 1
172 1
			$u_edit_mode = append_sid(generate_board_url() . '/' . ltrim(rtrim(build_url(array('edit_mode', 'sid', 'style')), '?'), './../'), 'edit_mode=' . (int) !$edit_mode);
173 1
		}
174
		else
175
		{
176 4
			$edit_mode = false;
177
		}
178
179
		return $u_edit_mode;
180
	}
181
182
	/**
183
	 * @param bool  $edit_mode
184 4
	 * @param array $route_info
185
	 */
186 4
	protected function show_admin_bar($edit_mode, array $route_info)
187 4
	{
188 4
		if ($edit_mode)
189
		{
190 3
			$this->phpbb_container->get('blitze.sitemaker.blocks.admin_bar')->show($route_info);
191
		}
192 2
	}
193
}
194