Completed
Push — develop ( c767cb...92ad0e )
by Daniel
10:06
created

cfg_handler::prep_radio_field_for_display()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 3
crap 2
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
class cfg_handler extends cfg_fields
13
{
14
	/** @var \phpbb\request\request_interface */
15
	protected $request;
16
17
	/** @var \phpbb\template\template */
18
	protected $template;
19
20
	/** @var \phpbb\language\language */
21
	protected $translator;
22
23
	/** @var \blitze\sitemaker\services\groups */
24
	protected $groups;
25
26
	/** @var string phpBB root path */
27
	protected $phpbb_root_path;
28
29
	/** @var string phpEx */
30
	protected $php_ext;
31
32
	/**
33
	 * Constructor
34
	 *
35
	 * @param \phpbb\request\request_interface		$request				Request object
36
	 * @param \phpbb\template\template				$template				Template object
37
	 * @param \phpbb\language\language				$translator				Language object
38
	 * @param \blitze\sitemaker\services\groups		$groups					Groups object
39
	 * @param string								$phpbb_root_path		phpBB root path
40
	 * @param string								$php_ext				phpEx
41
	 */
42 53
	public function __construct(\phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\language\language $translator, \blitze\sitemaker\services\groups $groups, $phpbb_root_path, $php_ext)
43
	{
44 53
		parent::__construct($translator);
45
46 53
		$this->request = $request;
47 53
		$this->template = $template;
48 53
		$this->translator = $translator;
49 53
		$this->groups = $groups;
50 53
		$this->phpbb_root_path = $phpbb_root_path;
51 53
		$this->php_ext = $php_ext;
52 53
	}
53
54
	/**
55
	 * @param array $block_data
56
	 * @param array $default_settings
57
	 * @return string
58
	 */
59 8
	public function get_edit_form(array $block_data, array $default_settings)
60
	{
61 8
		global $module;
62
63
		// @codeCoverageIgnoreStart
64
		if (!function_exists('validate_config_vars'))
65
		{
66
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext);
67
		}
68
		// @codeCoverageIgnoreEnd
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_VIEW'		=> $block_data['view'],
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('all', $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->set_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->translator->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
	 * Get the field html
164
	 *
165
	 * @param string $field
166
	 * @param array $db_settings
167
	 * @param array $vars
168
	 * @return string
169
	 */
170 7
	private function get_field_template($field, array &$db_settings, array &$vars)
171
	{
172 7
		$vars['lang_explain'] = $this->explain_field($vars);
173 7
		$vars['append'] = $this->append_field($vars);
174
175 7
		$type = explode(':', $vars['type']);
176 7
		$method = 'prep_' . $type[0] . '_field_for_display';
177
178 7
		if (is_callable(array($this, $method)))
179 7
		{
180 7
			$this->set_params($field, $vars, $db_settings);
181 7
			$this->$method($vars, $type, $field, $db_settings);
182 7
		}
183
184 7
		return build_cfg_template($type, $field, $db_settings, $field, $vars);
185
	}
186
187
	/**
188
	 * Set field legend
189
	 *
190
	 * @param string $field
191
	 * @param string|array $vars
192
	 * @return boolean
193
	 */
194 7
	private function set_legend($field, $vars)
195
	{
196 7
		if (strpos($field, 'legend') !== false)
197 7
		{
198 7
			$this->template->assign_block_vars('options', array(
199 7
				'S_LEGEND'	=> $field,
200 7
				'LEGEND'	=> $this->translator->lang($vars)
201 7
			));
202
203 7
			return true;
204
		}
205
206 7
		return false;
207
	}
208
209
	/**
210
	 * Get field details
211
	 *
212
	 * @param array $vars
213
	 * @return mixed|string
214
	 */
215 7
	private function explain_field(array $vars)
216
	{
217 7
		$l_explain = '';
218 7
		if (!empty($vars['explain']))
219 7
		{
220 2
			$l_explain = (isset($vars['lang_explain'])) ? $this->translator->lang($vars['lang_explain']) : $this->translator->lang($vars['lang'] . '_EXPLAIN');
221 2
		}
222
223 7
		return $l_explain;
224
	}
225
226
	/**
227
	 * Add text after field
228
	 *
229
	 * @param array $vars
230
	 * @return mixed|string
231
	 */
232 7
	private function append_field(array $vars)
233
	{
234 7
		$append = '';
235 7
		if (!empty($vars['append']))
236 7
		{
237 1
			$append = $this->translator->lang($vars['append']);
238 1
		}
239
240 7
		return $append;
241
	}
242
243
	/**
244
	 * Set field parameters
245
	 *
246
	 * @param string $field
247
	 * @param array $vars
248
	 * @param array $settings
249
	 */
250 7
	private function set_params($field, array &$vars, array $settings)
251
	{
252 7
		if (isset($vars['options']))
253 7
		{
254 4
			$vars['params'][] = $vars['options'];
255 4
			$vars['params'][] = $settings[$field];
256 4
		}
257 7
	}
258
259
	/**
260
	 * Get field value
261
	 *
262
	 * @param string $field
263
	 * @param mixed $default
264
	 * @param array $db_settings
265
	 * @return mixed
266
	 */
267 7
	private function get_field_value($field, $default, array $db_settings)
268
	{
269 7
		return (!empty($db_settings[$field])) ? $db_settings[$field] : $default;
270
	}
271
272
	/**
273
	 * @param array $vars
274
	 * @param array $type
275
	 * @param string $field
276
	 */
277 1
	private function prep_select_field_for_display(array &$vars, array &$type, $field)
278
	{
279 1
		$vars['method'] = 'build_select';
280 1
		$vars['params'][] = $field;
281 1
		$type[0] = 'custom';
282 1
	}
283
284
	/**
285
	 * @param array $vars
286
	 * @param array $type
287
	 * @param string $field
288
	 */
289 2
	private function prep_checkbox_field_for_display(array &$vars, array &$type, $field)
290
	{
291 2
		$vars['method'] = 'build_checkbox';
292 2
		$vars['params'][] = $field;
293 2
		$type[0] = 'custom';
294 2
	}
295
296
	/**
297
	 * @param array $vars
298
	 * @param array $type
299
	 * @param string $field
300
	 */
301 2
	private function prep_radio_field_for_display(array &$vars, array &$type, $field)
302
	{
303 2
		if (!isset($type[1]))
304 2
		{
305 1
			$vars['method'] = 'build_radio';
306 1
			$vars['params'][] = $field;
307 1
			$type[0] = 'custom';
308 1
		}
309 2
	}
310
311
	/**
312
	 * @param array $vars
313
	 * @param array $type
314
	 * @param string $field
315
	 */
316 2
	private function prep_multi_select_field_for_display(array &$vars, array &$type, $field)
317
	{
318 1
		$this->prep_checkbox_field_for_display($vars, $type, $field);
319
320 2
		$vars['method'] ='build_multi_select';
321 2
	}
322
323
	/**
324
	 * @param array $vars
325
	 * @param array $type
326
	 */
327 1
	private function prep_hidden_field_for_display(array &$vars, array &$type)
328
	{
329 1
		$vars['method'] = 'build_hidden';
330 1
		$vars['explain'] = '';
331 1
		$vars['lang'] = '';
332 1
		$type[0] = 'custom';
333 1
	}
334
335
	/**
336
	 * @param array $vars
337
	 * @param array $type
338
	 */
339 1
	private function prep_custom_field_for_display(array &$vars, array &$type)
340
	{
341 1
		$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : '';
342 1
		$type[0] = 'custom';
343 1
	}
344
345
	/**
346
	 * @param array $cfg_array
347
	 * @param array $df_settings
348
	 */
349 5
	private function get_multi_select(array &$cfg_array, array $df_settings)
350
	{
351 5
		$multi_select = utf8_normalize_nfc($this->request->variable('config', array('' => array('' => '')), true));
352 5
		$multi_select = array_filter($multi_select);
353
354 5
		foreach ($multi_select as $field => $settings)
355
		{
356 1
			$cfg_array[$field] = (!empty($settings)) ? $settings : $df_settings[$field]['default'];
357 5
		}
358 5
	}
359
}
360