Completed
Push — master ( 4a8b55...d86878 )
by Daniel
11:13
created

cfg_handler   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 350
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 99.35%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 39
c 4
b 1
f 1
lcom 1
cbo 2
dl 0
loc 350
ccs 152
cts 153
cp 0.9935
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 21 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 _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 _set_params() 0 8 2
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 48
	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 48
		parent::__construct($user);
47
48 48
		$this->request = $request;
49 48
		$this->template = $template;
50 48
		$this->user = $user;
51 48
		$this->groups = $groups;
52 48
		$this->phpbb_root_path = $phpbb_root_path;
53 48
		$this->php_ext = $php_ext;
54 48
	}
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 6
		if (!function_exists('validate_config_vars'))
86 6
		{
87
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext); // @codeCoverageIgnore
88
		}
89
90 6
		$cfg_array = utf8_normalize_nfc($this->request->variable('config', array('' => ''), true));
91
92 6
		$errors = array();
93 6
		validate_config_vars($default_settings, $cfg_array, $errors);
94
95 6
		if (sizeof($errors))
96 6
		{
97 1
			return array('errors' => join("\n", $errors));
98
		}
99
100 5
		$this->_get_multi_select($cfg_array, $default_settings);
101
102 5
		return array_intersect_key($cfg_array, $default_settings);
103
	}
104
105
	/**
106
	 * Get the html form
107
	 *
108
	 * @param array $block_data
109
	 * @return string
110
	 */
111 8
	private function _get_form(array $block_data)
112
	{
113 8
		$selected_groups = $this->_ensure_array($block_data['permission']);
114
115 8
		$this->template->assign_vars(array(
116 8
			'S_ACTIVE'		=> $block_data['status'],
117 8
			'S_TYPE'		=> $block_data['type'],
118 8
			'S_NO_WRAP'		=> $block_data['no_wrap'],
119 8
			'S_HIDE_TITLE'	=> $block_data['hide_title'],
120 8
			'S_BLOCK_CLASS'	=> trim($block_data['class']),
121 8
			'S_GROUP_OPS'	=> $this->groups->get_options('special', $selected_groups),
122 8
		));
123
124 8
		$this->template->set_filenames(array(
125 8
			'block_settings' => 'block_settings.html',
126 8
		));
127
128 8
		return $this->template->assign_display('block_settings');
129
	}
130
131
	/**
132
	 * Generate block configuration fields
133
	 *
134
	 * @param array $db_settings
135
	 * @param array $default_settings
136
	 */
137 8
	private function _generate_config_fields(array &$db_settings, array $default_settings)
138
	{
139 8
		foreach ($default_settings as $field => $vars)
140
		{
141 7
			if ($this->_sets_legend($field, $vars) || !is_array($vars))
142 7
			{
143 7
				continue;
144
			}
145
146 7
			$db_settings[$field] = $this->_get_field_value($field, $vars['default'], $db_settings);
147 7
			$content = $this->_get_field_template($field, $db_settings, $vars);
148
149 7
			$this->template->assign_block_vars('options', array(
150 7
				'KEY'			=> $field,
151 7
				'TITLE'			=> $this->user->lang($vars['lang']),
152 7
				'S_EXPLAIN'		=> $vars['explain'],
153 7
				'TITLE_EXPLAIN'	=> $vars['lang_explain'],
154 7
				'CONTENT'		=> $content,
155 7
			));
156 7
			unset($default_settings[$field]);
157 8
		}
158 8
	}
159
160
	/**
161
	 * @param $field
162
	 * @param array $db_settings
163
	 * @param array $vars
164
	 * @return string
165
	 */
166 7
	private function _get_field_template($field, array &$db_settings, array &$vars)
167
	{
168 7
		$vars['lang_explain'] = $this->_explain_field($vars);
169 7
		$vars['append'] = $this->_append_field($vars);
170
171 7
		$type = explode(':', $vars['type']);
172 7
		$method = '_prep_' . $type[0] . '_field_for_display';
173
174 7
		if (is_callable(array($this, $method)))
175 7
		{
176 7
			$this->_set_params($field, $vars, $db_settings);
177 7
			$this->$method($vars, $type, $field, $db_settings);
178 7
		}
179
180 7
		return build_cfg_template($type, $field, $db_settings, $field, $vars);
181
	}
182
183
	/**
184
	 * @param string $field
185
	 * @param string|array $vars
186
	 * @return boolean
187
	 */
188 7
	private function _sets_legend($field, $vars)
189
	{
190 7
		if (strpos($field, 'legend') !== false)
191 7
		{
192 7
			$this->template->assign_block_vars('options', array(
193 7
				'S_LEGEND'	=> $field,
194 7
				'LEGEND'	=> $this->user->lang($vars)
195 7
			));
196
197 7
			return true;
198
		}
199
200 7
		return false;
201
	}
202
203
	/**
204
	 * @param array $vars
205
	 * @return mixed|string
206
	 */
207 7
	private function _explain_field(array $vars)
208
	{
209 7
		$l_explain = '';
210 7
		if (!empty($vars['explain']))
211 7
		{
212 2
			$l_explain = (isset($vars['lang_explain'])) ? $this->user->lang($vars['lang_explain']) : $this->user->lang($vars['lang'] . '_EXPLAIN');
213 2
		}
214
215 7
		return $l_explain;
216
	}
217
218
	/**
219
	 * @param array $vars
220
	 * @return mixed|string
221
	 */
222 7
	private function _append_field(array $vars)
223
	{
224 7
		$append = '';
225 7
		if (!empty($vars['append']))
226 7
		{
227 1
			$append = $this->user->lang($vars['append']);
228 1
		}
229
230 7
		return $append;
231
	}
232
233
	/**
234
	 * @param string $field
235
	 * @param array $vars
236
	 * @param array $settings
237
	 */
238 7
	private function _set_params($field, array &$vars, array $settings)
239
	{
240 7
		if (isset($vars['options']))
241 7
		{
242 4
			$vars['params'][] = $vars['options'];
243 4
			$vars['params'][] = $settings[$field];
244 4
		}
245 7
	}
246
247
	/**
248
	 * @param string $field
249
	 * @param mixed $default
250
	 * @param array $db_settings
251
	 * @return mixed
252
	 */
253 7
	private function _get_field_value($field, $default, array $db_settings)
254
	{
255 7
		return (!empty($db_settings[$field])) ? $db_settings[$field] : $default;
256
	}
257
258
	/**
259
	 * @param array $vars
260
	 */
261 1
	private function _prep_select_field_for_display(array &$vars)
262
	{
263 1
		$this->_add_lang_vars($vars['params'][0]);
264
265 1
		$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : 'build_select';
266 1
	}
267
268
	/**
269
	 * @param array $vars
270
	 * @param array $type
271
	 * @param string $field
272
	 */
273 2
	private function _prep_checkbox_field_for_display(array &$vars, array &$type, $field)
274
	{
275 2
		$this->_add_lang_vars($vars['params'][0]);
276
277 2
		$vars['method'] = 'build_checkbox';
278 2
		$vars['params'][] = $field;
279 2
		$type[0] = 'custom';
280 2
	}
281
282
	/**
283
	 * @param array $vars
284
	 * @param array $type
285
	 * @param string $field
286
	 */
287 2
	private function _prep_radio_field_for_display(array &$vars, array &$type, $field)
288
	{
289 2
		if (!isset($type[1]))
290 2
		{
291 1
			$this->_add_lang_vars($vars['params'][0]);
292
293 1
			$vars['method'] = 'build_radio';
294 1
			$vars['params'][] = $field;
295 1
			$type[0] = 'custom';
296 1
		}
297 2
	}
298
299
	/**
300
	 * @param array $vars
301
	 * @param array $type
302
	 * @param string $field
303
	 */
304 1
	private function _prep_multi_select_field_for_display(array &$vars, array &$type, $field)
305
	{
306 1
		$this->_prep_checkbox_field_for_display($vars, $type, $field);
307
308 1
		$vars['method'] ='build_multi_select';
309 1
	}
310
311
	/**
312
	 * @param array $vars
313
	 * @param array $type
314
	 */
315 1
	private function _prep_hidden_field_for_display(array &$vars, array &$type)
316 1
	{
317 1
		$vars['method'] = 'build_hidden';
318 1
		$vars['explain'] = '';
319 1
		$vars['lang'] = '';
320 1
		$type[0] = 'custom';
321 1
	}
322
323
	/**
324
	 * @param array $vars
325
	 * @param array $type
326
	 */
327 1
	private function _prep_custom_field_for_display(array &$vars, array &$type)
328
	{
329 1
		$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : '';
330 1
		$type[0] = 'custom';
331 1
	}
332
333
	/**
334
	 * this looks bad but its the only way without modifying phpbb code
335
	 * this is for select items that do not need to be translated
336
	 * @param array $options
337
	 */
338 4
	private function _add_lang_vars(array $options)
339
	{
340 4
		foreach ($options as $title)
341
		{
342 4
			if (!isset($this->user->lang[$title]))
343 4
			{
344 4
				$this->user->lang[$title] = $title;
345 4
			}
346 4
		}
347 4
	}
348
349
	/**
350
	 * @param array $cfg_array
351
	 * @param array $df_settings
352
	 */
353 5
	private function _get_multi_select(array &$cfg_array, array $df_settings)
354
	{
355 5
		$multi_select = utf8_normalize_nfc($this->request->variable('config', array('' => array('' => '')), true));
356 5
		$multi_select = array_filter($multi_select);
357
358 5
		foreach ($multi_select as $field => $settings)
359
		{
360 1
			$cfg_array[$field] = (!empty($settings)) ? $settings : $df_settings[$field]['default'];
361 5
		}
362 5
	}
363
}
364