Completed
Push — master ( db8e66...00afbe )
by Daniel
09:03
created

display::get_edit_mode_url()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

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