Completed
Push — develop ( 524863...c767cb )
by Daniel
10:30
created

cfg_handler::append_field()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
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 8
		if (!function_exists('build_cfg_template'))
64 8
		{
65
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext); // @codeCoverageIgnore
66
		}
67
68
		// We fake this class as it is needed by the build_cfg_template function
69 8
		$module = new \stdClass();
70 8
		$module->module = $this;
71
72 8
		$this->generate_config_fields($block_data['settings'], $default_settings);
73
74 8
		return $this->get_form($block_data);
75
	}
76
77
	/**
78
	 * @param array $default_settings
79
	 * @return array|void
80
	 */
81 6
	public function get_submitted_settings(array $default_settings)
82
	{
83
		// @codeCoverageIgnoreStart
84
		if (!function_exists('validate_config_vars'))
85
		{
86
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext);
87
		}
88
		// @codeCoverageIgnoreEnd
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_VIEW'		=> $block_data['view'],
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('all', $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->set_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->translator->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
	 * Get the field html
162
	 *
163
	 * @param string $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
	 * Set field legend
187
	 *
188
	 * @param string $field
189
	 * @param string|array $vars
190
	 * @return boolean
191
	 */
192 7
	private function set_legend($field, $vars)
193
	{
194 7
		if (strpos($field, 'legend') !== false)
195 7
		{
196 7
			$this->template->assign_block_vars('options', array(
197 7
				'S_LEGEND'	=> $field,
198 7
				'LEGEND'	=> $this->translator->lang($vars)
199 7
			));
200
201 7
			return true;
202
		}
203
204 7
		return false;
205
	}
206
207
	/**
208
	 * Get field details
209
	 *
210
	 * @param array $vars
211
	 * @return mixed|string
212
	 */
213 7
	private function explain_field(array $vars)
214
	{
215 7
		$l_explain = '';
216 7
		if (!empty($vars['explain']))
217 7
		{
218 2
			$l_explain = (isset($vars['lang_explain'])) ? $this->translator->lang($vars['lang_explain']) : $this->translator->lang($vars['lang'] . '_EXPLAIN');
219 2
		}
220
221 7
		return $l_explain;
222
	}
223
224
	/**
225
	 * Add text after field
226
	 *
227
	 * @param array $vars
228
	 * @return mixed|string
229
	 */
230 7
	private function append_field(array $vars)
231
	{
232 7
		$append = '';
233 7
		if (!empty($vars['append']))
234 7
		{
235 1
			$append = $this->translator->lang($vars['append']);
236 1
		}
237
238 7
		return $append;
239
	}
240
241
	/**
242
	 * Set field parameters
243
	 *
244
	 * @param string $field
245
	 * @param array $vars
246
	 * @param array $settings
247
	 */
248 7
	private function set_params($field, array &$vars, array $settings)
249
	{
250 7
		if (isset($vars['options']))
251 7
		{
252 4
			$vars['params'][] = $vars['options'];
253 4
			$vars['params'][] = $settings[$field];
254 4
		}
255 7
	}
256
257
	/**
258
	 * Get field value
259
	 *
260
	 * @param string $field
261
	 * @param mixed $default
262
	 * @param array $db_settings
263
	 * @return mixed
264
	 */
265 7
	private function get_field_value($field, $default, array $db_settings)
266
	{
267 7
		return (!empty($db_settings[$field])) ? $db_settings[$field] : $default;
268
	}
269
270
	/**
271
	 * @param array $vars
272
	 * @param array $type
273
	 * @param string $field
274
	 */
275 1
	private function prep_select_field_for_display(array &$vars, array &$type, $field)
276
	{
277 1
		$vars['method'] = 'build_select';
278 1
		$vars['params'][] = $field;
279 1
		$type[0] = 'custom';
280 1
	}
281
282
	/**
283
	 * @param array $vars
284
	 * @param array $type
285
	 * @param string $field
286
	 */
287 2
	private function prep_checkbox_field_for_display(array &$vars, array &$type, $field)
288
	{
289 2
		$vars['method'] = 'build_checkbox';
290 2
		$vars['params'][] = $field;
291 2
		$type[0] = 'custom';
292 2
	}
293
294
	/**
295
	 * @param array $vars
296
	 * @param array $type
297
	 * @param string $field
298
	 */
299 2
	private function prep_radio_field_for_display(array &$vars, array &$type, $field)
300
	{
301 2
		if (!isset($type[1]))
302 2
		{
303 1
			$vars['method'] = 'build_radio';
304 1
			$vars['params'][] = $field;
305 1
			$type[0] = 'custom';
306 1
		}
307 2
	}
308
309
	/**
310
	 * @param array $vars
311
	 * @param array $type
312
	 * @param string $field
313
	 */
314 1
	private function prep_multi_select_field_for_display(array &$vars, array &$type, $field)
315
	{
316 1
		$this->prep_checkbox_field_for_display($vars, $type, $field);
317
318 1
		$vars['method'] ='build_multi_select';
319 1
	}
320
321
	/**
322
	 * @param array $vars
323
	 * @param array $type
324
	 */
325 1
	private function prep_hidden_field_for_display(array &$vars, array &$type)
326
	{
327 1
		$vars['method'] = 'build_hidden';
328 1
		$vars['explain'] = '';
329 1
		$vars['lang'] = '';
330 1
		$type[0] = 'custom';
331 1
	}
332
333
	/**
334
	 * @param array $vars
335
	 * @param array $type
336
	 */
337 1
	private function prep_custom_field_for_display(array &$vars, array &$type)
338
	{
339 1
		$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : '';
340 1
		$type[0] = 'custom';
341 1
	}
342
343
	/**
344
	 * @param array $cfg_array
345
	 * @param array $df_settings
346
	 */
347 5
	private function get_multi_select(array &$cfg_array, array $df_settings)
348
	{
349 5
		$multi_select = utf8_normalize_nfc($this->request->variable('config', array('' => array('' => '')), true));
350 5
		$multi_select = array_filter($multi_select);
351
352 5
		foreach ($multi_select as $field => $settings)
353
		{
354 1
			$cfg_array[$field] = (!empty($settings)) ? $settings : $df_settings[$field]['default'];
355 5
		}
356 5
	}
357
}
358