Completed
Push — master ( 4f5aad...a0199d )
by Daniel
09:18
created

cfg_handler   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 352
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 39
c 5
b 1
f 1
lcom 1
cbo 2
dl 0
loc 352
ccs 149
cts 149
cp 1
rs 8.2857

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A get_edit_form() 0 17 2
A get_submitted_settings() 0 23 3
A _get_form() 0 19 1
B _generate_config_fields() 0 22 4
A _get_field_template() 0 16 2
A _sets_legend() 0 14 2
A _explain_field() 0 10 3
A _append_field() 0 10 2
A _set_params() 0 8 2
A _get_field_value() 0 4 2
A _prep_select_field_for_display() 0 6 2
A _prep_checkbox_field_for_display() 0 8 1
A _prep_radio_field_for_display() 0 11 2
A _prep_multi_select_field_for_display() 0 6 1
A _prep_hidden_field_for_display() 0 7 1
A _prep_custom_field_for_display() 0 5 2
A _add_lang_vars() 0 10 3
A _get_multi_select() 0 10 3
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2015 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 blitze\sitemaker\services\blocks\cfg_fields;
13
14
class cfg_handler extends cfg_fields
15
{
16
	/** @var \phpbb\request\request_interface */
17
	protected $request;
18
19
	/** @var \phpbb\template\template */
20
	protected $template;
21
22
	/** @var \phpbb\user */
23
	protected $user;
24
25
	/** @var \blitze\sitemaker\services\groups */
26
	protected $groups;
27
28
	/** @var string phpBB root path */
29
	protected $phpbb_root_path;
30
31
	/** @var string phpEx */
32
	protected $php_ext;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param \phpbb\request\request_interface		$request				Request object
38
	 * @param \phpbb\template\template				$template				Template object
39
	 * @param \phpbb\user							$user					User object
40
	 * @param \blitze\sitemaker\services\groups		$groups					Groups object
41
	 * @param string								$phpbb_root_path		phpBB root path
42
	 * @param string								$php_ext				phpEx
43
	 */
44 49
	public function __construct(\phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\user $user, \blitze\sitemaker\services\groups $groups, $phpbb_root_path, $php_ext)
45
	{
46 49
		parent::__construct($user);
47
48 49
		$this->request = $request;
49 49
		$this->template = $template;
50 49
		$this->user = $user;
51 49
		$this->groups = $groups;
52 49
		$this->phpbb_root_path = $phpbb_root_path;
53 49
		$this->php_ext = $php_ext;
54 49
	}
55
56
	/**
57
	 * @param array $block_data
58
	 * @param array $default_settings
59
	 * @return template|string
60
	 */
61 8
	public function get_edit_form(array $block_data, array $default_settings)
62
	{
63 8
		global $module;
64
65 8
		if (!function_exists('build_cfg_template'))
66 8
		{
67
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext); // @codeCoverageIgnore
68 1
		}
69
70
		// We fake this class as it is needed by the build_cfg_template function
71 8
		$module = new \stdClass();
72 8
		$module->module = $this;
73
74 8
		$this->_generate_config_fields($block_data['settings'], $default_settings);
75
76 8
		return $this->_get_form($block_data);
77
	}
78
79
	/**
80
	 * @param array $default_settings
81
	 * @return array|void
82
	 */
83 6
	public function get_submitted_settings(array $default_settings)
84
	{
85
		// @codeCoverageIgnoreStart
86
		if (!function_exists('validate_config_vars'))
87
		{
88
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext);
89
		}
90
		// @codeCoverageIgnoreEnd
91
92 6
		$cfg_array = utf8_normalize_nfc($this->request->variable('config', array('' => ''), true));
93
94 6
		$errors = array();
95 6
		validate_config_vars($default_settings, $cfg_array, $errors);
96
97 6
		if (sizeof($errors))
98 6
		{
99 1
			return array('errors' => join("\n", $errors));
100
		}
101
102 5
		$this->_get_multi_select($cfg_array, $default_settings);
103
104 5
		return array_intersect_key($cfg_array, $default_settings);
105
	}
106
107
	/**
108
	 * Get the html form
109
	 *
110
	 * @param array $block_data
111
	 * @return string
112
	 */
113 8
	private function _get_form(array $block_data)
114
	{
115 8
		$selected_groups = $this->_ensure_array($block_data['permission']);
116
117 8
		$this->template->assign_vars(array(
118 8
			'S_ACTIVE'		=> $block_data['status'],
119 8
			'S_TYPE'		=> $block_data['type'],
120 8
			'S_NO_WRAP'		=> $block_data['no_wrap'],
121 8
			'S_HIDE_TITLE'	=> $block_data['hide_title'],
122 8
			'S_BLOCK_CLASS'	=> trim($block_data['class']),
123 8
			'S_GROUP_OPS'	=> $this->groups->get_options('special', $selected_groups),
124 8
		));
125
126 8
		$this->template->set_filenames(array(
127 8
			'block_settings' => 'block_settings.html',
128 8
		));
129
130 8
		return $this->template->assign_display('block_settings');
131
	}
132
133
	/**
134
	 * Generate block configuration fields
135
	 *
136
	 * @param array $db_settings
137
	 * @param array $default_settings
138
	 */
139 8
	private function _generate_config_fields(array &$db_settings, array $default_settings)
140
	{
141 8
		foreach ($default_settings as $field => $vars)
142
		{
143 7
			if ($this->_sets_legend($field, $vars) || !is_array($vars))
144 7
			{
145 7
				continue;
146
			}
147
148 7
			$db_settings[$field] = $this->_get_field_value($field, $vars['default'], $db_settings);
149 7
			$content = $this->_get_field_template($field, $db_settings, $vars);
150
151 7
			$this->template->assign_block_vars('options', array(
152 7
				'KEY'			=> $field,
153 7
				'TITLE'			=> $this->user->lang($vars['lang']),
154 7
				'S_EXPLAIN'		=> $vars['explain'],
155 7
				'TITLE_EXPLAIN'	=> $vars['lang_explain'],
156 7
				'CONTENT'		=> $content,
157 7
			));
158 7
			unset($default_settings[$field]);
159 8
		}
160 8
	}
161
162
	/**
163
	 * @param $field
164
	 * @param array $db_settings
165
	 * @param array $vars
166
	 * @return string
167
	 */
168 7
	private function _get_field_template($field, array &$db_settings, array &$vars)
169
	{
170 7
		$vars['lang_explain'] = $this->_explain_field($vars);
171 7
		$vars['append'] = $this->_append_field($vars);
172
173 7
		$type = explode(':', $vars['type']);
174 7
		$method = '_prep_' . $type[0] . '_field_for_display';
175
176 7
		if (is_callable(array($this, $method)))
177 7
		{
178 7
			$this->_set_params($field, $vars, $db_settings);
179 7
			$this->$method($vars, $type, $field, $db_settings);
180 7
		}
181
182 7
		return build_cfg_template($type, $field, $db_settings, $field, $vars);
183
	}
184
185
	/**
186
	 * @param string $field
187
	 * @param string|array $vars
188
	 * @return boolean
189
	 */
190 7
	private function _sets_legend($field, $vars)
191
	{
192 7
		if (strpos($field, 'legend') !== false)
193 7
		{
194 7
			$this->template->assign_block_vars('options', array(
195 7
				'S_LEGEND'	=> $field,
196 7
				'LEGEND'	=> $this->user->lang($vars)
197 7
			));
198
199 7
			return true;
200
		}
201
202 7
		return false;
203
	}
204
205
	/**
206
	 * @param array $vars
207
	 * @return mixed|string
208
	 */
209 7
	private function _explain_field(array $vars)
210
	{
211 7
		$l_explain = '';
212 7
		if (!empty($vars['explain']))
213 7
		{
214 2
			$l_explain = (isset($vars['lang_explain'])) ? $this->user->lang($vars['lang_explain']) : $this->user->lang($vars['lang'] . '_EXPLAIN');
215 2
		}
216
217 7
		return $l_explain;
218
	}
219
220
	/**
221
	 * @param array $vars
222
	 * @return mixed|string
223
	 */
224 7
	private function _append_field(array $vars)
225
	{
226 7
		$append = '';
227 7
		if (!empty($vars['append']))
228 7
		{
229 1
			$append = $this->user->lang($vars['append']);
230 1
		}
231
232 7
		return $append;
233
	}
234
235
	/**
236
	 * @param string $field
237
	 * @param array $vars
238
	 * @param array $settings
239
	 */
240 7
	private function _set_params($field, array &$vars, array $settings)
241
	{
242 7
		if (isset($vars['options']))
243 7
		{
244 4
			$vars['params'][] = $vars['options'];
245 4
			$vars['params'][] = $settings[$field];
246 4
		}
247 7
	}
248
249
	/**
250
	 * @param string $field
251
	 * @param mixed $default
252
	 * @param array $db_settings
253
	 * @return mixed
254
	 */
255 7
	private function _get_field_value($field, $default, array $db_settings)
256
	{
257 7
		return (!empty($db_settings[$field])) ? $db_settings[$field] : $default;
258
	}
259
260
	/**
261
	 * @param array $vars
262
	 */
263 1
	private function _prep_select_field_for_display(array &$vars)
264
	{
265 1
		$this->_add_lang_vars($vars['params'][0]);
266
267 1
		$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : 'build_select';
268 1
	}
269
270
	/**
271
	 * @param array $vars
272
	 * @param array $type
273
	 * @param string $field
274
	 */
275 2
	private function _prep_checkbox_field_for_display(array &$vars, array &$type, $field)
276
	{
277 2
		$this->_add_lang_vars($vars['params'][0]);
278
279 2
		$vars['method'] = 'build_checkbox';
280 2
		$vars['params'][] = $field;
281 2
		$type[0] = 'custom';
282 2
	}
283
284
	/**
285
	 * @param array $vars
286
	 * @param array $type
287
	 * @param string $field
288
	 */
289 2
	private function _prep_radio_field_for_display(array &$vars, array &$type, $field)
290
	{
291 2
		if (!isset($type[1]))
292 2
		{
293 1
			$this->_add_lang_vars($vars['params'][0]);
294
295 1
			$vars['method'] = 'build_radio';
296 1
			$vars['params'][] = $field;
297 1
			$type[0] = 'custom';
298 1
		}
299 2
	}
300
301
	/**
302
	 * @param array $vars
303
	 * @param array $type
304
	 * @param string $field
305
	 */
306 1
	private function _prep_multi_select_field_for_display(array &$vars, array &$type, $field)
307
	{
308 1
		$this->_prep_checkbox_field_for_display($vars, $type, $field);
309
310 1
		$vars['method'] ='build_multi_select';
311 1
	}
312
313
	/**
314
	 * @param array $vars
315
	 * @param array $type
316
	 */
317 1
	private function _prep_hidden_field_for_display(array &$vars, array &$type)
318
	{
319 1
		$vars['method'] = 'build_hidden';
320 1
		$vars['explain'] = '';
321 1
		$vars['lang'] = '';
322 1
		$type[0] = 'custom';
323 1
	}
324
325
	/**
326
	 * @param array $vars
327
	 * @param array $type
328
	 */
329 1
	private function _prep_custom_field_for_display(array &$vars, array &$type)
330
	{
331 1
		$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : '';
332 1
		$type[0] = 'custom';
333 1
	}
334
335
	/**
336
	 * this looks bad but its the only way without modifying phpbb code
337
	 * this is for select items that do not need to be translated
338
	 * @param array $options
339
	 */
340 4
	private function _add_lang_vars(array $options)
341
	{
342 4
		foreach ($options as $title)
343
		{
344 4
			if (!isset($this->user->lang[$title]))
345 4
			{
346 4
				$this->user->lang[$title] = $title;
347 4
			}
348 4
		}
349 4
	}
350
351
	/**
352
	 * @param array $cfg_array
353
	 * @param array $df_settings
354
	 */
355 5
	private function _get_multi_select(array &$cfg_array, array $df_settings)
356
	{
357 5
		$multi_select = utf8_normalize_nfc($this->request->variable('config', array('' => array('' => '')), true));
358 5
		$multi_select = array_filter($multi_select);
359
360 5
		foreach ($multi_select as $field => $settings)
361
		{
362 1
			$cfg_array[$field] = (!empty($settings)) ? $settings : $df_settings[$field]['default'];
363 5
		}
364 5
	}
365
}
366