Completed
Push — develop ( 660ae6...f77be0 )
by Daniel
08:50
created

admin_bar::set_assets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
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 admin_bar
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\template\template */
26
	protected $template;
27
28
	/** @var \phpbb\language\language */
29
	protected $translator;
30
31
	/** @var \phpbb\user */
32
	protected $user;
33
34
	/** @var \blitze\sitemaker\services\icon_picker */
35
	protected $icons;
36
37
	/** @var \blitze\sitemaker\services\util */
38
	protected $util;
39
40
	/** @var string phpEx */
41
	protected $php_ext;
42
43
	/**
44
	 * Constructor
45
	 *
46
	 * @param \phpbb\auth\auth							$auth					Auth object
47
	 * @param \phpbb\config\config						$config					Config object
48
	 * @param ContainerInterface						$phpbb_container		Service container
49
	 * @param \phpbb\template\template					$template				Template object
50
	 * @param \phpbb\language\language					$translator				Language object
51
	 * @param \phpbb\user								$user					User object
52
	 * @param \blitze\sitemaker\services\icon_picker	$icons					Sitemaker icon picker object
53
	 * @param \blitze\sitemaker\services\util			$util					Sitemaker util object
54
	 * @param string									$php_ext				phpEx
55
	 */
56
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, ContainerInterface $phpbb_container, \phpbb\template\template $template, \phpbb\language\language $translator, \phpbb\user $user, \blitze\sitemaker\services\icon_picker $icons, \blitze\sitemaker\services\util $util, $php_ext)
57
	{
58
		$this->auth = $auth;
59
		$this->config = $config;
60
		$this->phpbb_container = $phpbb_container;
61
		$this->template = $template;
62
		$this->translator = $translator;
63
		$this->user = $user;
64
		$this->icons = $icons;
65
		$this->util = $util;
66
		$this->php_ext = $php_ext;
67
	}
68
69
	/**
70
	 * Show admin bar
71
	 *
72
	 * @param array $route_info
73
	 */
74
	public function show(array $route_info)
75
	{
76
		$this->translator->add_lang('block_manager', 'blitze/sitemaker');
77
78
		$this->phpbb_container->get('blitze.sitemaker.auto_lang')->add('blocks_admin');
79
80
		$route = $route_info['route'];
81
		$style_id = $route_info['style'];
82
83
		$this->get_available_blocks();
84
		$this->get_startpage_options();
85
		$this->set_javascript_data($route, $style_id);
86
		$this->set_assets();
87
88
		$this->template->assign_vars(array(
89
			'S_EDIT_MODE'		=> true,
90
			'S_ROUTE_OPS'		=> $this->get_route_options($route),
91
			'S_HIDE_BLOCKS'		=> $route_info['hide_blocks'],
92
			'S_POSITION_OPS'	=> $this->get_excluded_position_options($route_info['ex_positions']),
93
			'S_EX_POSITIONS'	=> join(', ', $route_info['ex_positions']),
94
			'S_STYLE_OPTIONS'	=> style_select($style_id, true),
95
			'S_STARTPAGE'		=> $this->startpage_is_set(),
96
97
			'ICON_PICKER'		=> $this->icons->picker(),
98
		));
99
	}
100
101
	/**
102
	 * Set data used in javascript
103
	 * @param string $route
104
	 * @param int $style_id
105
	 */
106
	public function set_javascript_data($route, $style_id)
107
	{
108
		$board_url = generate_board_url();
109
		$ajax_url = $board_url . ((!$this->config['enable_mod_rewrite']) ? '/app.' . $this->php_ext : '');
110
111
		$is_default_route = $u_default_route = false;
112
		if ($this->config['sitemaker_default_layout'])
113
		{
114
			$is_default_route = ($this->config['sitemaker_default_layout'] === $route) ? true : false;
115
			$u_default_route .= $board_url . '/' . $this->config['sitemaker_default_layout'];
116
			$u_default_route = reapply_sid($u_default_route);
117
		}
118
119
		$this->template->assign_vars(array(
120
			'S_IS_DEFAULT'		=> $is_default_route,
121
122
			'PAGE_URL'			=> build_url(array('style')),
123
124
			'UA_AJAX_URL'		=> $ajax_url,
125
			'UA_BOARD_URL'		=> $board_url,
126
			'UA_ROUTE'			=> $route,
127
			'UA_STYLE_ID'		=> $style_id,
128
			'UA_SCRIPT_PATH'	=> $this->user->page['root_script_path'],
129
			'UA_WEB_ROOT_PATH'	=> $this->util->get_web_path(),
130
			'UA_FILEMANAGER'	=> $this->can_use_filemanager(),
131
			'UA_RF_ACCESS_KEY'	=> sha1($this->user->data['user_form_salt'] . 'filemanager'),
132
133
			'U_VIEW_DEFAULT'	=> $u_default_route,
134
		));
135
	}
136
137
	/**
138
	 * Get all available sitemaker blocks
139
	 */
140
	public function get_available_blocks()
141
	{
142
		$blocks = $this->phpbb_container->get('blitze.sitemaker.blocks.factory')->get_all_blocks();
143
144
		foreach ($blocks as $service => $name)
145
		{
146
			$this->template->assign_block_vars('block', array(
147
				'NAME'		=> $name,
148
				'SERVICE'	=> $service)
149
			);
150
		}
151
	}
152
153
	/**
154
	 * Provide options to set/unset current page as landing page
155
	 */
156
	public function get_startpage_options()
157
	{
158
		$symfony_request = $this->phpbb_container->get('symfony_request');
159
		$controller = $symfony_request->attributes->get('_controller');
160
161
		if ($controller)
162
		{
163
			list($controller_service, $controller_method) = explode(':', $controller);
164
			$controller_params	= $symfony_request->attributes->get('_route_params');
165
			$controller_object	= $this->phpbb_container->get($controller_service);
166
			$controller_class	= get_class($controller_object);
167
168
			$r = new \ReflectionMethod($controller_class, $controller_method);
169
			$class_params = $r->getParameters();
170
171
			list($namespace, $extension) = explode('\\', $controller_class);
172
			$controller_arguments = $this->get_arguments($controller_params, $class_params);
173
174
			$this->template->assign_vars(array(
175
				'CONTROLLER_NAME'	=> $controller_service,
176
				'CONTROLLER_METHOD'	=> $controller_method,
177
				'CONTROLLER_PARAMS'	=> $controller_arguments,
178
				'S_IS_STARTPAGE'	=> $this->is_startpage($controller_service, $controller_arguments),
179
				'UA_EXTENSION'		=> $namespace . '/' . $extension,
180
			));
181
		}
182
	}
183
184
	/**
185
	 * Add js/css
186
	 */
187
	public function set_assets()
188
	{
189
		$this->util->add_assets(array(
190
			'js'	=> array(
191
				'@blitze_sitemaker/vendor/jquery-ui/jquery-ui.min.js',
192
				'@blitze_sitemaker/vendor/tinymce/tinymce.min.js',
193
				'@blitze_sitemaker/vendor/jqueryui-touch-punch/jquery.ui.touch-punch.min.js',
194
				'@blitze_sitemaker/vendor/twig.js/index.js',
195
				100 =>  '@blitze_sitemaker/assets/blocks/manager.min.js',
196
			),
197
			'css'   => array(
198
				'@blitze_sitemaker/vendor/jquery-ui/themes/smoothness/jquery-ui.min.css',
199
				'@blitze_sitemaker/assets/blocks/manager.min.css',
200
			)
201
		));
202
	}
203
204
	/**
205
	 * Get routes with blocks
206
	 *
207
	 * @param string $current_route
208
	 * @return string
209
	 */
210
	public function get_route_options($current_route)
211
	{
212
		$routes_ary = $this->get_routes();
213
214
		$options = '<option value="">' . $this->translator->lang('SELECT') . '</option>';
215
		foreach ($routes_ary as $route)
216
		{
217
			$selected = ($route == $current_route) ? ' selected="selected"' : '';
218
			$options .= '<option value="' . $route . '"' . $selected . '>' . $route . '</option>';
219
		}
220
221
		return $options;
222
	}
223
224
	/**
225
	 * Get excluded position options
226
	 *
227
	 * @param array $ex_positions
228
	 * @return string
229
	 */
230
	public function get_excluded_position_options(array $ex_positions)
231
	{
232
		$options = '';
233
		foreach ($ex_positions as $position)
234
		{
235
			$options .= '<option value="' . $position . '" selected="selected">' . $position . '</option>';
236
		}
237
238
		return $options;
239
	}
240
241
	/**
242
	 * @return array
243
	 */
244
	protected function get_routes()
245
	{
246
		$factory = $this->phpbb_container->get('blitze.sitemaker.mapper.factory');
247
		$collection = $factory->create('routes')->find();
248
249
		$routes_ary = array();
250
		foreach ($collection as $entity)
251
		{
252
			/** @var \blitze\sitemaker\model\entity\route $entity */
253
			$route_name = $entity->get_route();
254
			$routes_ary[$route_name] = $route_name;
255
		}
256
257
		return $routes_ary;
258
	}
259
260
	/**
261
	 * @param array $controller_params
262
	 * @param array $class_params
263
	 * @return string
264
	 */
265
	protected function get_arguments(array $controller_params, array $class_params)
266
	{
267
		$arguments = array();
268
		foreach ($class_params as $param)
269
		{
270
			$name = $param->getName();
271
			$arguments[$name] = ($param->isOptional()) ? $param->getDefaultValue() : $controller_params[$name];
272
		}
273
274
		return join('/', $arguments);
275
	}
276
277
	/**
278
	 * @param string $controller_service
279
	 * @param string $controller_arguments
280
	 * @return bool
281
	 */
282
	protected function is_startpage($controller_service, $controller_arguments)
283
	{
284
		return ($this->config['sitemaker_startpage_controller'] == $controller_service && $this->config['sitemaker_startpage_params'] == $controller_arguments) ? true : false;
285
	}
286
287
	/**
288
	 * @return bool
289
	 */
290
	protected function startpage_is_set()
291
	{
292
		return ($this->config['sitemaker_startpage_controller']) ? true : false;
293
	}
294
295
	/**
296
	 * @return bool
297
	 */
298
	protected function can_use_filemanager()
299
	{
300
		return $this->config['sm_filemanager'] && $this->auth->acl_get('u_sm_filemanager');
301
	}
302
}
303