Completed
Push — develop ( 49e718...1c36a4 )
by Daniel
10:08
created

admin_bar::show()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 20
cts 20
cp 1
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 1
crap 1
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 \phpbb\controller\helper */
20
	protected $controller_helper;
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\filemanager\setup */
35
	protected $filemanager;
36
37
	/** @var \blitze\sitemaker\services\icon_picker */
38
	protected $icons;
39
40
	/** @var \blitze\sitemaker\services\util */
41
	protected $util;
42
43
	/** @var string phpEx */
44
	protected $php_ext;
45
46
	/**
47
	 * Constructor
48
	 *
49
	 * @param \phpbb\config\config							$config					Config object
50
	 * @param \phpbb\controller\helper						$controller_helper		Controller Helper object
51
	 * @param ContainerInterface							$phpbb_container		Service container
52
	 * @param \phpbb\event\dispatcher_interface				$phpbb_dispatcher		Event dispatcher object
53
	 * @param \phpbb\template\template						$template				Template object
54
	 * @param \phpbb\language\language						$translator				Language object
55
	 * @param \phpbb\user									$user					User object
56
	 * @param \blitze\sitemaker\services\filemanager\setup	$filemanager			Filemanager object
57
	 * @param \blitze\sitemaker\services\icon_picker		$icons					Sitemaker icon picker object
58
	 * @param \blitze\sitemaker\services\util				$util					Sitemaker util object
59
	 * @param string										$php_ext				phpEx
60
	 */
61 12
	public function __construct(\phpbb\config\config $config, \phpbb\controller\helper $controller_helper, ContainerInterface $phpbb_container, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \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)
62
	{
63 12
		$this->config = $config;
64 12
		$this->controller_helper = $controller_helper;
65 12
		$this->phpbb_container = $phpbb_container;
66 12
		$this->phpbb_dispatcher = $phpbb_dispatcher;
0 ignored issues
show
Bug introduced by
The property phpbb_dispatcher does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
67 12
		$this->template = $template;
68 12
		$this->translator = $translator;
69 12
		$this->user = $user;
70 12
		$this->filemanager = $filemanager;
71 12
		$this->icons = $icons;
72 12
		$this->util = $util;
73 12
		$this->php_ext = $php_ext;
74 12
	}
75
76
	/**
77
	 * Show admin bar
78
	 *
79
	 * @param array $route_info
80
	 */
81 2
	public function show(array $route_info)
82
	{
83 2
		$this->translator->add_lang('block_manager', 'blitze/sitemaker');
84
85 2
		$this->phpbb_container->get('blitze.sitemaker.auto_lang')->add('blocks_admin');
86
87 2
		$route = $route_info['route'];
88 2
		$style_id = $route_info['style'];
89
90 2
		$this->get_available_blocks();
91 2
		$this->get_startpage_options();
92 2
		$this->set_javascript_data($route, $style_id);
93 2
		$this->set_assets();
94
95 2
		$this->template->assign_vars(array(
96 2
			'S_EDIT_MODE'		=> true,
97 2
			'S_ROUTE_OPS'		=> $this->get_route_options($route),
98 2
			'S_HIDE_BLOCKS'		=> $route_info['hide_blocks'],
99 2
			'S_POSITION_OPS'	=> $this->get_excluded_position_options($route_info['ex_positions']),
100 2
			'S_EX_POSITIONS'	=> join(', ', $route_info['ex_positions']),
101 2
			'S_STYLE_OPTIONS'	=> style_select($style_id, true),
102 2
			'S_STARTPAGE'		=> $this->startpage_is_set(),
103
104 2
			'ICON_PICKER'		=> $this->icons->picker(),
105 2
		));
106 2
	}
107
108
	/**
109
	 * Set data used in javascript
110
	 * @param string $route
111
	 * @param int $style_id
112
	 */
113 4
	public function set_javascript_data($route, $style_id)
114
	{
115 4
		$board_url = generate_board_url();
116 4
		$is_default_route = $u_default_route = false;
117 4
		if ($this->config['sitemaker_default_layout'])
118 4
		{
119 1
			$is_default_route = ($this->config['sitemaker_default_layout'] === $route) ? true : false;
120 1
			$u_default_route .= $board_url . '/' . $this->config['sitemaker_default_layout'];
121 1
			$u_default_route = reapply_sid($u_default_route);
122 1
		}
123
124 4
		$this->template->assign_vars(array(
125 4
			'S_IS_DEFAULT'		=> $is_default_route,
126
127 4
			'BLOCK_ACTIONS'		=> $this->get_block_actions(),
128 4
			'FILEMANAGER'		=> $this->filemanager->is_enabled(),
129 4
			'FILEMANAGER_AKEY'	=> $this->filemanager->get_access_key(),
130 4
			'PAGE_URL'			=> build_url(array('style')),
131
132 4
			'UA_BOARD_URL'		=> $board_url,
133 4
			'UA_ROUTE'			=> $route,
134 4
			'UA_STYLE_ID'		=> $style_id,
135 4
			'UA_SCRIPT_PATH'	=> $this->user->page['root_script_path'],
136 4
			'UA_MODREWRITE'		=> $this->config['enable_mod_rewrite'],
137 4
			'UA_WEB_ROOT_PATH'	=> $this->util->get_web_path(),
138 4
			'UA_UPLOAD_URL'		=> $this->controller_helper->route('blitze_sitemaker_image_upload'),
139
140 4
			'U_VIEW_DEFAULT'	=> $u_default_route,
141 4
		));
142 4
	}
143
144
	/**
145
	 * @return array
146
	 */
147 4
	protected function get_block_actions()
148
	{
149
		$list = array(
150 4
			'add_block',
151 4
			'copy_route',
152 4
			'edit_block',
153 4
			'handle_custom_action',
154 4
			'save_block',
155 4
			'save_blocks',
156 4
			'set_default_route',
157 4
			'set_route_prefs',
158 4
			'set_startpage',
159 4
			'update_block',
160 4
		);
161
162 4
		$actions = array();
163 4
		foreach ($list as $action)
164
		{
165 4
			$actions[$action] = $this->controller_helper->route('blitze_sitemaker_blocks_admin', array('action' => $action));
166 4
		}
167
168 4
		return $actions;
169
	}
170
171
	/**
172
	 * Get all available sitemaker blocks
173
	 */
174 3
	public function get_available_blocks()
175
	{
176 3
		$blocks = $this->phpbb_container->get('blitze.sitemaker.blocks.factory')->get_all_blocks();
177
178 3
		foreach ($blocks as $service => $name)
179
		{
180 3
			$this->template->assign_block_vars('block', array(
181 3
				'NAME'		=> $name,
182 3
				'SERVICE'	=> $service)
183 3
			);
184 3
		}
185 3
	}
186
187
	/**
188
	 * Provide options to set/unset current page as landing page
189
	 */
190 5
	public function get_startpage_options()
191
	{
192 5
		$symfony_request = $this->phpbb_container->get('symfony_request');
193 5
		$controller = $symfony_request->attributes->get('_controller');
194
195
		if ($controller)
196 5
		{
197 2
			list($controller_service, $controller_method) = explode(':', $controller);
198 2
			$controller_params	= $symfony_request->attributes->get('_route_params');
199 2
			$controller_object	= $this->phpbb_container->get($controller_service);
200 2
			$controller_class	= get_class($controller_object);
201
202 2
			$r = new \ReflectionMethod($controller_class, $controller_method);
203 2
			$class_params = $r->getParameters();
204
205 2
			list($namespace, $extension) = explode('\\', $controller_class);
206 2
			$controller_arguments = $this->get_arguments($controller_params, $class_params);
207
208 2
			$this->template->assign_vars(array(
209 2
				'CONTROLLER_NAME'	=> $controller_service,
210 2
				'CONTROLLER_METHOD'	=> $controller_method,
211 2
				'CONTROLLER_PARAMS'	=> $controller_arguments,
212 2
				'S_IS_STARTPAGE'	=> $this->is_startpage($controller_service, $controller_arguments),
213 2
				'UA_EXTENSION'		=> $namespace . '/' . $extension,
214 2
			));
215 2
		}
216 5
	}
217
218
	/**
219
	 * Add js/css
220
	 */
221 2
	public function set_assets()
222
	{
223
		$assets = array(
224
			'js'	=> array(
225 2
				'@blitze_sitemaker/vendor/jquery-ui/jquery-ui.min.js',
226 2
				'@blitze_sitemaker/vendor/tinymce/tinymce.min.js',
227 2
				'@blitze_sitemaker/vendor/jqueryui-touch-punch/jquery.ui.touch-punch.min.js',
228 2
				'@blitze_sitemaker/vendor/twig.js/index.js',
229 2
				1000 =>  '@blitze_sitemaker/assets/blocks/manager.min.js',
230 2
			),
231
			'css'   => array(
232 2
				'@blitze_sitemaker/vendor/jquery-ui/themes/smoothness/jquery-ui.min.css',
233 2
				'@blitze_sitemaker/assets/blocks/manager.min.css',
234
			)
235 2
		);
236
237
		/**
238
		 * Event to set assets for available blocks
239
		 * Use array_merge_recursive to add new assets
240
		 *
241
		 * @event blitze.sitemaker.admin_bar.set_assets
242
		 * @var	array	assets		Array of assets to include of form array([js] => array(), [css] => array())
243
		 */
244 2
		$vars = array('assets');
245 2
		extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.admin_bar.set_assets', compact($vars)));
0 ignored issues
show
Bug introduced by
$this->phpbb_dispatcher-...ssets', compact($vars)) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
246
247 2
		$this->util->add_assets($assets);
248 2
	}
249
250
	/**
251
	 * Get routes with blocks
252
	 *
253
	 * @param string $current_route
254
	 * @return string
255
	 */
256 4
	public function get_route_options($current_route)
257
	{
258 4
		$routes_ary = $this->get_routes();
259
260 4
		$options = '<option value="">' . $this->translator->lang('SELECT') . '</option>';
261 4
		foreach ($routes_ary as $route)
262
		{
263 4
			$selected = ($route == $current_route) ? ' selected="selected"' : '';
264 4
			$options .= '<option value="' . $route . '"' . $selected . '>' . $route . '</option>';
265 4
		}
266
267 4
		return $options;
268
	}
269
270
	/**
271
	 * Get excluded position options
272
	 *
273
	 * @param array $ex_positions
274
	 * @return string
275
	 */
276 4
	public function get_excluded_position_options(array $ex_positions)
277
	{
278 4
		$options = '';
279 4
		foreach ($ex_positions as $position)
280
		{
281 2
			$options .= '<option value="' . $position . '" selected="selected">' . $position . '</option>';
282 4
		}
283
284 4
		return $options;
285
	}
286
287
	/**
288
	 * @return array
289
	 */
290 4
	protected function get_routes()
291
	{
292 4
		$factory = $this->phpbb_container->get('blitze.sitemaker.mapper.factory');
293 4
		$collection = $factory->create('routes')->find();
294
295 4
		$routes_ary = array();
296 4
		foreach ($collection as $entity)
297
		{
298
			/** @var \blitze\sitemaker\model\entity\route $entity */
299 4
			$route_name = $entity->get_route();
300 4
			$routes_ary[$route_name] = $route_name;
301 4
		}
302
303 4
		return $routes_ary;
304
	}
305
306
	/**
307
	 * @param array $controller_params
308
	 * @param array $class_params
309
	 * @return string
310
	 */
311 2
	protected function get_arguments(array $controller_params, array $class_params)
312
	{
313 2
		$arguments = array();
314 2
		foreach ($class_params as $param)
315
		{
316 2
			$name = $param->getName();
317 2
			$arguments[$name] = ($param->isOptional()) ? $param->getDefaultValue() : $controller_params[$name];
318 2
		}
319
320 2
		return join('/', $arguments);
321
	}
322
323
	/**
324
	 * @param string $controller_service
325
	 * @param string $controller_arguments
326
	 * @return bool
327
	 */
328 2
	protected function is_startpage($controller_service, $controller_arguments)
329
	{
330 2
		return ($this->config['sitemaker_startpage_controller'] == $controller_service && $this->config['sitemaker_startpage_params'] == $controller_arguments) ? true : false;
331
	}
332
333
	/**
334
	 * @return bool
335
	 */
336 2
	protected function startpage_is_set()
337
	{
338 2
		return ($this->config['sitemaker_startpage_controller']) ? true : false;
339
	}
340
}
341