Completed
Push — master ( 382d5e...699a8d )
by Daniel
10:12
created

admin_bar::get_startpage_options()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 34
ccs 27
cts 27
cp 1
rs 8.439
cc 5
eloc 22
nc 4
nop 0
crap 5
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 admin_bar
15
{
16
	/** @var \phpbb\config\config */
17
	protected $config;
18
19
	/** @var ContainerInterface */
20
	protected $phpbb_container;
21
22
	/** @var \phpbb\template\template */
23
	protected $template;
24
25
	/** @var \phpbb\user */
26
	protected $user;
27
28
	/** @var \blitze\sitemaker\services\icon_picker */
29
	protected $icons;
30
31
	/** @var \blitze\sitemaker\services\util */
32
	protected $util;
33
34
	/** @var string phpEx */
35
	protected $php_ext;
36
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param \phpbb\config\config						$config					Config object
41
	 * @param ContainerInterface						$phpbb_container		Service container
42
	 * @param \phpbb\template\template					$template				Template object
43
	 * @param \phpbb\user								$user					User object
44
	 * @param \blitze\sitemaker\services\icon_picker	$icons					Sitemaker icon picker object
45
	 * @param \blitze\sitemaker\services\util			$util					Sitemaker util object
46
	 * @param string									$php_ext				phpEx
47
	 */
48 11
	public function __construct(\phpbb\config\config $config, ContainerInterface $phpbb_container, \phpbb\template\template $template, \phpbb\user $user, \blitze\sitemaker\services\icon_picker $icons, \blitze\sitemaker\services\util $util, $php_ext)
49
	{
50 11
		$this->config = $config;
51 11
		$this->phpbb_container = $phpbb_container;
52 11
		$this->template = $template;
53 11
		$this->user = $user;
54 11
		$this->icons = $icons;
55 11
		$this->util = $util;
56 11
		$this->php_ext = $php_ext;
57 11
	}
58
59
	/**
60
	 * Show admin bar
61
	 */
62 3
	public function show($route_info)
63
	{
64 1
		$this->user->add_lang_ext('blitze/sitemaker', 'block_manager');
65
66 1
		$this->phpbb_container->get('blitze.sitemaker.auto_lang')->add('blocks_admin');
67
68 1
		$route = $route_info['route'];
69 1
		$style_id = $route_info['style'];
70
71 1
		$this->get_available_blocks();
72 1
		$this->get_startpage_options();
73 1
		$this->set_javascript_data($route, $style_id);
74 1
		$this->set_assets();
75
76 1
		$this->template->assign_vars(array(
77 1
			'S_EDIT_MODE'		=> true,
78 1
			'S_ROUTE_OPS'		=> $this->get_route_options($route),
79 1
			'S_HIDE_BLOCKS'		=> $route_info['hide_blocks'],
80 1
			'S_POSITION_OPS'	=> $this->get_excluded_position_options($route_info['ex_positions']),
81 1
			'S_EX_POSITIONS'	=> join(', ', $route_info['ex_positions']),
82 1
			'S_STYLE_OPTIONS'	=> style_select($style_id, true),
83 1
			'S_STARTPAGE'		=> $this->_startpage_is_set(),
84
85 1
			'ICON_PICKER'		=> $this->icons->picker(),
86 3
		));
87 1
	}
88
89
	/**
90
	 * Set data used in javascript
91
	 */
92 3
	public function set_javascript_data($route, $style_id)
93
	{
94 3
		$board_url = generate_board_url();
95 3
		$ajax_url = $board_url . ((!$this->config['enable_mod_rewrite']) ? '/app.' . $this->php_ext : '');
96
97 3
		$is_default_route = $u_default_route = false;
98 3
		if ($this->config['sitemaker_default_layout'])
99 3
		{
100 1
			$is_default_route = ($this->config['sitemaker_default_layout'] === $route) ? true : false;
101 1
			$u_default_route .= $board_url . '/' . $this->config['sitemaker_default_layout'];
102 1
			$u_default_route = reapply_sid($u_default_route);
103 1
		}
104
105 3
		$this->template->assign_vars(array(
106 3
			'S_IS_DEFAULT'		=> $is_default_route,
107
108 3
			'PAGE_URL'			=> build_url(array('style')),
109
110 3
			'UA_ROUTE'			=> $route,
111 3
			'UA_AJAX_URL'		=> $ajax_url,
112 3
			'UA_BOARD_URL'		=> $board_url,
113 3
			'UA_STYLE_ID'		=> $style_id,
114
115 3
			'U_VIEW_DEFAULT'	=> $u_default_route,
116 3
		));
117 3
	}
118
119
	/**
120
	 * Get all available sitemaker blocks
121
	 */
122 2
	public function get_available_blocks()
123
	{
124 2
		$blocks = $this->phpbb_container->get('blitze.sitemaker.blocks.factory')->get_all_blocks();
125
126 2
		foreach ($blocks as $service => $name)
127
		{
128 2
			$this->template->assign_block_vars('block', array(
129 2
				'NAME'		=> $name,
130 2
				'SERVICE'	=> $service)
131 2
			);
132 2
		}
133 2
	}
134
135 4
	public function get_startpage_options()
136
	{
137 4
		$symfony_request = $this->phpbb_container->get('symfony_request');
138 4
		$controller = $symfony_request->attributes->get('_controller');
139
140 4
		if ($controller && $controller !== 'blitze.sitemaker.forum.controller:handle')
141 4
		{
142 2
			list($controller_service, $controller_method) = explode(':', $controller);
143 2
			$controller_params	= $symfony_request->attributes->get('_route_params');
144 2
			$controller_object	= $this->phpbb_container->get($controller_service);
145 2
			$controller_class	= get_class($controller_object);
146
147 2
			$r = new \ReflectionMethod($controller_class, $controller_method);
148 2
			$params = $r->getParameters();
149
150 2
			$arguments = array();
151 2
			foreach ($params as $param)
152
			{
153 2
				$name = $param->getName();
154 2
				$arguments[$name] = ($param->isOptional()) ? $param->getDefaultValue() : $controller_params[$name];
155 2
			}
156
157 2
			list($namespace, $extension) = explode('\\', $controller_class);
158 2
			$controller_arguments = join('/', $arguments);
159
160 2
			$this->template->assign_vars(array(
161 2
				'CONTROLLER_NAME'	=> $controller_service,
162 2
				'CONTROLLER_METHOD'	=> $controller_method,
163 2
				'CONTROLLER_PARAMS'	=> $controller_arguments,
164 2
				'S_IS_STARTPAGE'	=> $this->_is_startpage($controller_service, $controller_arguments),
165 2
				'UA_EXTENSION'		=> $namespace . '/' . $extension,
166 2
			));
167 2
		}
168 4
	}
169
170 1
	public function set_assets()
171
	{
172 1
		$this->util->add_assets(array(
173
			'js'	=> array(
174 1
				'//ajax.googleapis.com/ajax/libs/jqueryui/' . JQUI_VERSION . '/jquery-ui.min.js',
175 1
				'//tinymce.cachefly.net/4.2/tinymce.min.js',
176 1
				'@blitze_sitemaker/vendor/jqueryui-touch-punch/jquery.ui.touch-punch.min.js',
177 1
				'@blitze_sitemaker/vendor/twig.js/twig.min.js',
178 1
				100 =>  '@blitze_sitemaker/assets/blocks/manager.min.js',
179 1
			),
180
			'css'   => array(
181 1
				'//ajax.googleapis.com/ajax/libs/jqueryui/' . JQUI_VERSION . '/themes/smoothness/jquery-ui.css',
182 1
				'@blitze_sitemaker/assets/blocks/manager.min.css',
183
			)
184 1
		));
185 1
	}
186
187
	/**
188
	 * Get routes with blocks
189
	 */
190 3
	public function get_route_options($current_route)
191
	{
192 3
		$routes_ary = $this->_get_routes();
193
194 3
		$options = '<option value="">' . $this->user->lang('SELECT') . '</option>';
195 3
		foreach ($routes_ary as $route)
196
		{
197 3
			$selected = ($route == $current_route) ? ' selected="selected"' : '';
198 3
			$options .= '<option value="' . $route . '"' . $selected . '>' . $route . '</option>';
199 3
		}
200
201 3
		return $options;
202
	}
203
204
	/**
205
	 * Get excluded position options
206
	 */
207 3
	public function get_excluded_position_options($ex_positions)
208
	{
209 3
		$options = '<option value=""' . ((!sizeof($ex_positions)) ? ' selected="selected"' : '') . '>' . $this->user->lang('NONE') . '</option>';
210 3
		foreach ($ex_positions as $position)
211
		{
212
			$options .= '<option value="' . $position . '" selected="selected">' . $position . '</option>';
213 3
		}
214
215 3
		return $options;
216
	}
217
218
	/**
219
	 * @return array
220
	 */
221 3
	protected function _get_routes()
222
	{
223 3
		$factory = $this->phpbb_container->get('blitze.sitemaker.mapper.factory');
224 3
		$collection = $factory->create('blocks', 'routes')->find();
225
226 3
		$routes_ary = array();
227 3
		foreach ($collection as $entity)
228
		{
229 3
			$route_name = $entity->get_route();
230 3
			$routes_ary[$route_name] = $route_name;
231 3
		}
232
233 3
		return $routes_ary;
234
	}
235
236
	/**
237
	 * @param string $controller_service
238
	 * @param string $controller_arguments
239
	 * @return bool
240
	 */
241 2
	protected function _is_startpage($controller_service, $controller_arguments)
242
	{
243 2
		return ($this->config['sitemaker_startpage_controller'] == $controller_service && $this->config['sitemaker_startpage_params'] == $controller_arguments) ? true : false;
244
	}
245
246
	/**
247
	 * @return bool
248
	 */
249 1
	protected function _startpage_is_set()
250
	{
251 1
		return ($this->config['sitemaker_startpage_controller']) ? true : false;
252
	}
253
}
254