Completed
Push — master ( 926e5e...2f974b )
by Daniel
21:21
created

display::get_route()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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