Completed
Push — develop ( cc7114...ef0a0d )
by Daniel
08:51
created

admin_bar::can_use_filemanager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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