Completed
Push — develop ( 733603...f85365 )
by Daniel
15:29 queued 09:35
created

cfg_handler::prep_custom_field_for_display()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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