Completed
Push — develop ( f85365...cc1e8e )
by Daniel
15:30 queued 09:18
created

cfg_handler::validate_block_settings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
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 54
	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 54
		parent::__construct($translator);
45
46 54
		$this->request = $request;
47 54
		$this->template = $template;
48 54
		$this->translator = $translator;
49 54
		$this->groups = $groups;
50 54
		$this->phpbb_root_path = $phpbb_root_path;
51 54
		$this->php_ext = $php_ext;
52 54
	}
53
54
	/**
55
	 * @param array $block_data
56
	 * @param array $default_settings
57
	 * @return string
58
	 */
59 9
	public function get_edit_form(array $block_data, array $default_settings)
60
	{
61
		// @codeCoverageIgnoreStart
62
		if (!function_exists('build_cfg_template'))
63
		{
64
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext);
65
		}
66
		// @codeCoverageIgnoreEnd
67
68 9
		$this->generate_config_fields($block_data['settings'], $default_settings);
69
70 9
		return $this->get_form($block_data);
71
	}
72
73
	/**
74
	 * @param array $default_settings
75
	 * @return array|void
76
	 */
77 6
	public function get_submitted_settings(array $default_settings)
78
	{
79 6
		$cfg_array = utf8_normalize_nfc($this->request->variable('config', array('' => ''), true));
80 6
		$cfg_array = $this->decode_source_html($cfg_array);
81 6
		$errors = $this->validate_block_settings($default_settings, $cfg_array);
82
83 6
		if (sizeof($errors))
84 6
		{
85 1
			return array('errors' => join("\n", $errors));
86
		}
87
88 5
		$this->get_multi_select($cfg_array, $default_settings);
89
90 5
		return array_intersect_key($cfg_array, $default_settings);
91
	}
92
93
	/**
94
	 * @param array $default_settings
95
	 * @param array $cfg_array
96
	 * @return array
97
	 */
98 6
	protected function validate_block_settings(array $default_settings, array $cfg_array)
99
	{
100
		// @codeCoverageIgnoreStart
101
		if (!function_exists('validate_config_vars'))
102
		{
103
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext);
104
		}
105
		// @codeCoverageIgnoreEnd
106
107 6
		$errors = array();
108 6
		validate_config_vars($default_settings, $cfg_array, $errors);
109
110 6
		return $errors;
111
	}
112
113
	/**
114
	 * As a workaround to prevent mod_security and similar from preventing us
115
	 * from posting html/script, we use encodeURI before submitting data.
116
	 * This decodes the data before submitting to the database
117
	 *
118
	 * @param array $cfg_array
119
	 * @return array
120
	 */
121 6
	private function decode_source_html(array $cfg_array)
122
	{
123 6
		if (isset($cfg_array['source']))
124 6
		{
125
			$cfg_array['source'] = urldecode($cfg_array['source']);
126
		}
127
128 6
		return $cfg_array;
129
	}
130
131
	/**
132
	 * Get the html form
133
	 *
134
	 * @param array $block_data
135
	 * @return string
136
	 */
137 9
	private function get_form(array $block_data)
138
	{
139 9
		$selected_groups = $this->ensure_array($block_data['permission']);
140
141 9
		$this->template->assign_vars(array(
142 9
			'S_ACTIVE'		=> $block_data['status'],
143 9
			'S_TYPE'		=> $block_data['type'],
144 9
			'S_VIEW'		=> $block_data['view'],
145 9
			'S_HIDE_TITLE'	=> $block_data['hide_title'],
146 9
			'S_BLOCK_CLASS'	=> trim($block_data['class']),
147 9
			'S_GROUP_OPS'	=> $this->groups->get_options('all', $selected_groups),
148 9
		));
149
150 9
		$this->template->set_filenames(array(
151 9
			'block_settings' => 'block_settings.html',
152 9
		));
153
154 9
		return $this->template->assign_display('block_settings');
155
	}
156
157
	/**
158
	 * Generate block configuration fields
159
	 *
160
	 * @param array $db_settings
161
	 * @param array $default_settings
162
	 */
163 9
	private function generate_config_fields(array &$db_settings, array $default_settings)
164
	{
165 9
		foreach ($default_settings as $field => $vars)
166
		{
167 8
			if ($this->set_legend($field, $vars) || !is_array($vars))
168 8
			{
169 8
				continue;
170
			}
171
172 8
			$db_settings[$field] = $this->get_field_value($field, $vars['default'], $db_settings);
173 8
			$content = $this->get_field_template($field, $db_settings, $vars);
174
175 8
			$this->template->assign_block_vars('options', array(
176 8
				'KEY'			=> $field,
177 8
				'TITLE'			=> $this->translator->lang($vars['lang']),
178 8
				'S_EXPLAIN'		=> $vars['explain'],
179 8
				'TITLE_EXPLAIN'	=> $vars['lang_explain'],
180 8
				'CONTENT'		=> $content,
181 8
			));
182 8
			unset($default_settings[$field]);
183 9
		}
184 9
	}
185
186
	/**
187
	 * Get the field html
188
	 *
189
	 * @param string $field
190
	 * @param array $db_settings
191
	 * @param array $vars
192
	 * @return string
193
	 */
194 8
	private function get_field_template($field, array &$db_settings, array &$vars)
195
	{
196 8
		global $module;
197
198 8
		$vars['lang_explain'] = $this->explain_field($vars);
199 8
		$vars['append'] = $this->append_field($vars);
200
201 8
		$type = explode(':', $vars['type']);
202
203 8
		if (empty($vars['object']))
204 8
		{
205 7
			$object = $this;
206 7
			$method = 'prep_' . $type[0] . '_field_for_display';
207
208 7
			if (is_callable(array($this, $method)))
209 7
			{
210 7
				$this->set_params($field, $vars, $db_settings);
211 7
				$this->$method($vars, $type, $field, $db_settings);
212 7
			}
213 7
		}
214
		else
215
		{
216 1
			$object = $vars['object'];
217 1
			$this->set_params($field, $vars, $db_settings);
218
		}
219
220
		// We fake this class as it is needed by the build_cfg_template function
221 8
		$module = new \stdClass();
222 8
		$module->module = $object;
223
224 8
		return build_cfg_template($type, $field, $db_settings, $field, $vars);
225
	}
226
227
	/**
228
	 * Set field legend
229
	 *
230
	 * @param string $field
231
	 * @param string|array $vars
232
	 * @return boolean
233
	 */
234 8
	private function set_legend($field, $vars)
235
	{
236 8
		if (strpos($field, 'legend') !== false)
237 8
		{
238 8
			$this->template->assign_block_vars('options', array(
239 8
				'S_LEGEND'	=> $field,
240 8
				'LEGEND'	=> $this->translator->lang($vars)
241 8
			));
242
243 8
			return true;
244
		}
245
246 8
		return false;
247
	}
248
249
	/**
250
	 * Get field details
251
	 *
252
	 * @param array $vars
253
	 * @return mixed|string
254
	 */
255 8
	private function explain_field(array $vars)
256
	{
257 8
		$l_explain = '';
258 8
		if (!empty($vars['explain']))
259 8
		{
260 2
			$l_explain = (isset($vars['lang_explain'])) ? $this->translator->lang($vars['lang_explain']) : $this->translator->lang($vars['lang'] . '_EXPLAIN');
261 2
		}
262
263 8
		return $l_explain;
264
	}
265
266
	/**
267
	 * Add text after field
268
	 *
269
	 * @param array $vars
270
	 * @return mixed|string
271
	 */
272 8
	private function append_field(array $vars)
273
	{
274 8
		$append = '';
275 8
		if (!empty($vars['append']))
276 8
		{
277 1
			$append = $this->translator->lang($vars['append']);
278 1
		}
279
280 8
		return $append;
281
	}
282
283
	/**
284
	 * Set field parameters
285
	 *
286
	 * @param string $field
287
	 * @param array $vars
288
	 * @param array $settings
289
	 */
290 8
	private function set_params($field, array &$vars, array $settings)
291
	{
292 8
		if (isset($vars['options']))
293 8
		{
294 5
			$vars['params'][] = $vars['options'];
295 5
			$vars['params'][] = $settings[$field];
296 5
		}
297 8
	}
298
299
	/**
300
	 * Get field value
301
	 *
302
	 * @param string $field
303
	 * @param mixed $default
304
	 * @param array $db_settings
305
	 * @return mixed
306
	 */
307 8
	private function get_field_value($field, $default, array $db_settings)
308
	{
309 8
		return (isset($db_settings[$field])) ? $db_settings[$field] : $default;
310
	}
311
312
	/**
313
	 * @param array $vars
314
	 * @param array $type
315
	 * @param string $field
316
	 */
317 2
	private function prep_select_field_for_display(array &$vars, array &$type, $field)
318
	{
319 1
		$vars['method'] = 'build_select';
320 2
		$vars['params'][] = $field;
321 2
		$type[0] = 'custom';
322 1
	}
323
324
	/**
325
	 * @param array $vars
326
	 * @param array $type
327
	 * @param string $field
328
	 */
329 2
	private function prep_checkbox_field_for_display(array &$vars, array &$type, $field)
330
	{
331 2
		$vars['method'] = 'build_checkbox';
332 2
		$vars['params'][] = $field;
333 2
		$type[0] = 'custom';
334 2
	}
335
336
	/**
337
	 * @param array $vars
338
	 * @param array $type
339
	 * @param string $field
340
	 */
341 2
	private function prep_radio_field_for_display(array &$vars, array &$type, $field)
342
	{
343 2
		if (!isset($type[1]))
344 2
		{
345 1
			$vars['method'] = 'build_radio';
346 1
			$vars['params'][] = $field;
347 1
			$type[0] = 'custom';
348 1
		}
349 2
	}
350
351
	/**
352
	 * @param array $vars
353
	 * @param array $type
354
	 * @param string $field
355
	 */
356 1
	private function prep_multi_select_field_for_display(array &$vars, array &$type, $field)
357
	{
358 1
		$this->prep_checkbox_field_for_display($vars, $type, $field);
359
360 1
		$vars['method'] ='build_multi_select';
361 1
	}
362
363
	/**
364
	 * @param array $vars
365
	 * @param array $type
366
	 */
367 1
	private function prep_hidden_field_for_display(array &$vars, array &$type)
368
	{
369 1
		$vars['method'] = 'build_hidden';
370 1
		$vars['explain'] = '';
371 1
		$vars['lang'] = '';
372 1
		$type[0] = 'custom';
373 1
	}
374
375
	/**
376
	 * @param array $vars
377
	 * @param array $type
378
	 */
379 1
	private function prep_custom_field_for_display(array &$vars, array &$type)
380
	{
381 1
		$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : '';
382 1
		$type[0] = 'custom';
383 1
	}
384
385
	/**
386
	 * @param array $cfg_array
387
	 * @param array $df_settings
388
	 */
389 5
	private function get_multi_select(array &$cfg_array, array $df_settings)
390
	{
391 5
		$multi_select = $this->request->variable('config', array('' => array(0 => '')), true);
392 5
		$multi_select = array_filter($multi_select);
393
394 5
		foreach ($multi_select as $field => $settings)
395
		{
396 1
			$cfg_array[$field] = (!empty($settings)) ? $settings : $df_settings[$field]['default'];
397 5
		}
398 5
	}
399
}
400