Passed
Push — develop ( cdc274...c2fe6a )
by Daniel
03:41
created

startpage::get_extension_styles_path()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\event;
12
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
16
class startpage implements EventSubscriberInterface
17
{
18
	/** @var \phpbb\config\config */
19
	protected $config;
20
21
	/** @var ContainerInterface */
22
	protected $phpbb_container;
23
24
	/** @var \phpbb\request\request_interface */
25
	protected $request;
26
27
	/** @var \phpbb\template\template */
28
	protected $template;
29
30
	/** @var \phpbb\language\language */
31
	protected $translator;
32
33
	/** @var \phpbb\user */
34
	protected $user;
35
36
	/** @var string */
37
	protected $php_ext;
38
39
	/* @var bool */
40
	protected $is_startpage = false;
41
42
	/**
43
	 * Constructor
44
	 *
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
	 * @param string									$php_ext				php file extension
52 16
	 */
53
	public function __construct(\phpbb\config\config $config, ContainerInterface $phpbb_container, \phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\language\language $translator, \phpbb\user $user, $php_ext)
54 16
	{
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
		$this->php_ext = $php_ext;
62
	}
63
64
	/**
65
	 * @return array
66 1
	 */
67
	public static function getSubscribedEvents()
68
	{
69 1
		return array(
70 1
			'core.page_header'					=> 'add_forum_to_navbar',
71 1
			'core.display_forums_modify_sql'	=> 'set_startpage',
72 1
			'core.page_footer'					=> 'cleanup_breadcrumbs',
73
		);
74
	}
75
76
	/**
77
	 * If start page is set,
78
	 * - Add "Forum" to navbar
79
	 * - Add "Forum" to the breadcrump when viewing forum page (viewforum/viewtopic/posting)
80 7
	 */
81
	public function add_forum_to_navbar()
82 7
	{
83 7
		if ($this->config['sitemaker_startpage_controller'])
84 6
		{
85
			$u_viewforum = $this->phpbb_container->get('controller.helper')->route('blitze_sitemaker_forum');
86
87 6
			// show 'Forum' menu item in navbar
88 6
			$this->template->assign_vars(array(
89 6
				'SM_FORUM_ICON'		=> $this->config['sm_forum_icon'],
90 6
				'SM_SHOW_FORUM_NAV'	=> $this->config['sm_show_forum_nav'],
91 6
				'U_SM_VIEWFORUM'	=> $u_viewforum,
92
			));
93
94 6
			// Add "Forum" to breadcrumb menu when viewing forum pages (viewforum/viewtopic/posting)
95 6
			if ($this->request->is_set('f'))
96 3
			{
97 3
				$this->template->alter_block_array('navlinks', array(
98 3
					'FORUM_NAME'	=> $this->translator->lang('FORUM'),
99 3
					'U_VIEW_FORUM'	=> $u_viewforum,
100 3
				));
101 6
			}
102 7
		}
103
	}
104
105
	/**
106
	 * If we are on the index page and we have set a custom start page,
107
	 * we do not want breadcrumbs like Home > Articles on the index page.
108
	 * This removes everything else, leaving just 'Home'
109
	 * @return void
110 2
	 */
111
	public function cleanup_breadcrumbs()
112 2
	{
113 2
		if ($this->is_startpage)
114 1
		{
115 1
			$this->template->destroy_block_vars('navlinks');
116 2
		}
117
	}
118
119
	/**
120
	 * @return void
121 6
	 */
122
	public function set_startpage()
123 6
	{
124 6
		if ($this->user->page['page_name'] == 'index.' . $this->php_ext && !$this->is_startpage && ($controller_object = $this->get_startpage_controller()) !== false)
125 1
		{
126 1
			$this->is_startpage = true;
127
			$this->template->set_style(array($this->get_extension_styles_path(get_class($controller_object)), 'styles'));
128 1
129 1
			$arguments = explode('/', $this->config['sitemaker_startpage_params']);
130 1
			$method = $this->config['sitemaker_startpage_method'];
131
132 1
			/** @type \Symfony\Component\HttpFoundation\Response $response */
133
			$response = call_user_func_array(array($controller_object, $method), $arguments);
134
			$response->send();
135 1
136 1
			$this->exit_handler();
137
		}
138 1
	}
139 1
140 6
	/**
141
	 * @codeCoverageIgnore
142
	 */
143
	protected function exit_handler()
144
	{
145
		exit_handler();
146
	}
147
148
	/**
149
	 * @return object|false
150
	 */
151
	protected function get_startpage_controller()
152
	{
153 5
		$controller_service_name = $this->config['sitemaker_startpage_controller'];
154
		if ($this->phpbb_container->has($controller_service_name))
155 5
		{
156 5
			$controller_object = $this->phpbb_container->get($controller_service_name);
157 5
			$method = $this->config['sitemaker_startpage_method'];
158 2
159 2
			if (is_callable(array($controller_object, $method)))
160
			{
161 2
				return $controller_object;
162 2
			}
163 1
		}
164
165 1
		// we have a startpage controller but it does not exist or it is not callable so remove it
166
		if ($controller_service_name)
167
		{
168
			$this->config->set('sitemaker_startpage_controller', '');
169 4
			$this->config->set('sitemaker_startpage_method', '');
170 2
			$this->config->set('sitemaker_startpage_params', '');
171 2
		}
172 2
173 2
		return false;
174
	}
175 4
176
	/**
177
	 * @param string $controller
178
	 * @return string
179
	 */
180
	protected function get_extension_styles_path($controller)
181
	{
182
		$parts = explode('\\', $controller);
183
		return 'ext/' . $parts[0] . '/' . $parts[1] . '/styles';
184
	}
185
}
186