Passed
Push — release-3.2.0 ( 24bcfb...4d8cca )
by Daniel
04:25
created

cfg_handler::set_legend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
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\config;
11
12
class cfg_handler
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\blocks\config\cfg_factory */
24
	protected $cfg_fields_factory;
25
26
	/** @var \blitze\sitemaker\services\groups */
27
	protected $groups;
28
29
	/** @var string phpBB root path */
30
	protected $phpbb_root_path;
31
32
	/** @var string phpEx */
33
	protected $php_ext;
34
35
	/**
36
	 * Constructor
37
	 *
38
	 * @param \phpbb\request\request_interface						$request				Request object
39
	 * @param \phpbb\template\template								$template				Template object
40
	 * @param \phpbb\language\language								$translator				Language object
41
	 * @param \blitze\sitemaker\services\blocks\config\cfg_factory	$cfg_fields_factory		Block config fields factory
42
	 * @param \blitze\sitemaker\services\groups						$groups					Groups object
43
	 * @param string												$phpbb_root_path		phpBB root path
44
	 * @param string												$php_ext				phpEx
45
	 */
46
	public function __construct(\phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\language\language $translator, \blitze\sitemaker\services\blocks\config\cfg_factory $cfg_fields_factory, \blitze\sitemaker\services\groups $groups, $phpbb_root_path, $php_ext)
47
	{
48
		$this->request = $request;
49
		$this->template = $template;
50
		$this->translator = $translator;
51
		$this->cfg_fields_factory = $cfg_fields_factory;
52
		$this->groups = $groups;
53
		$this->phpbb_root_path = $phpbb_root_path;
54
		$this->php_ext = $php_ext;
55
	}
56
57
	/**
58
	 * @param array $block_data
59
	 * @param array $default_settings
60
	 * @return mixed
61
	 */
62
	public function get_edit_form(array $block_data, array $default_settings)
63
	{
64
		// @codeCoverageIgnoreStart
65
		if (!function_exists('build_cfg_template'))
66
		{
67
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext);
68
		}
69
		// @codeCoverageIgnoreEnd
70
71
		$this->generate_config_fields($block_data['settings'], $default_settings);
72
73
		return $this->get_form($block_data);
74
	}
75
76
	/**
77
	 * @param array $default_settings
78
	 * @return array
79
	 */
80
	public function get_submitted_settings(array $default_settings)
81
	{
82
		$cfg_array = $this->request->variable('config', array('' => ''), true);
83
		$cfg_array = $this->decode_source_html($cfg_array);
84
		$errors = $this->validate_block_settings($default_settings, $cfg_array);
85
86
		if (sizeof($errors))
87
		{
88
			throw new \Exception(join("\n", $errors));
89
		}
90
91
		$this->get_multi_select($cfg_array, $default_settings);
92
93
		return array_intersect_key($cfg_array, $default_settings);
94
	}
95
96
	/**
97
	 * @param array $default_settings
98
	 * @param array $cfg_array
99
	 * @return array
100
	 */
101
	protected function validate_block_settings(array $default_settings, array $cfg_array)
102
	{
103
		// @codeCoverageIgnoreStart
104
		if (!function_exists('validate_config_vars'))
105
		{
106
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext);
107
		}
108
		// @codeCoverageIgnoreEnd
109
110
		$errors = array();
111
		validate_config_vars($default_settings, $cfg_array, $errors);
112
113
		return $errors;
114
	}
115
116
	/**
117
	 * As a workaround to prevent mod_security and similar from preventing us from posting html/script,
118
	 * we use encodeURI before submitting data via ajax (see develop/blocks/manager.js).
119
	 * This decodes the data before submitting to the database
120
	 *
121
	 * @param array $cfg_array
122
	 * @return array
123
	 */
124
	private function decode_source_html(array $cfg_array)
125
	{
126
		if (isset($cfg_array['source']))
127
		{
128
			$cfg_array['source'] = rawurldecode($cfg_array['source']);
129
		}
130
131
		return $cfg_array;
132
	}
133
134
	/**
135
	 * Get the html form
136
	 *
137
	 * @param array $block_data
138
	 * @return mixed
139
	 */
140
	private function get_form(array $block_data)
141
	{
142
		$selected_groups = cfg_utils::ensure_array($block_data['permission']);
143
144
		$this->template->assign_vars(array(
145
			'S_BLOCK_ID'	=> $block_data['bid'],
146
			'S_ACTIVE'		=> $block_data['status'],
147
			'S_TYPE'		=> $block_data['type'],
148
			'S_VIEW'		=> $block_data['view'],
149
			'S_HIDE_TITLE'	=> $block_data['hide_title'],
150
			'S_BLOCK_CLASS'	=> trim($block_data['class']),
151
			'S_GROUP_OPS'	=> $this->groups->get_options('all', $selected_groups),
152
		));
153
154
		$this->template->set_filenames(array(
155
			'block_settings' => 'block_settings.html',
156
		));
157
158
		return $this->template->assign_display('block_settings');
159
	}
160
161
	/**
162
	 * Generate block configuration fields
163
	 *
164
	 * @param array $db_settings
165
	 * @param array $default_settings
166
	 */
167
	private function generate_config_fields(array &$db_settings, array $default_settings)
168
	{
169
		foreach ($default_settings as $field => $vars)
170
		{
171
			if (!$this->is_input_field($field, $vars))
172
			{
173
				continue;
174
			}
175
176
			// set some defaults for optional props
177
			$vars += array(
178
				'explain'		=> false,
179
				'lang_explain'	=> '',
180
				'lang'			=> '', // optional for hidden field type
181
			);
182
183
			$db_settings[$field] = $this->get_field_value($field, $vars['default'], $db_settings);
184
			$content = $this->get_field_template($field, $db_settings, $vars);
185
186
			$this->template->assign_block_vars('options', array(
187
				'KEY'			=> $field,
188
				'TITLE'			=> $this->translator->lang($vars['lang']),
189
				'S_EXPLAIN'		=> $vars['explain'],
190
				'TITLE_EXPLAIN'	=> $vars['lang_explain'],
191
				'CONTENT'		=> $content,
192
			));
193
		}
194
	}
195
196
	/**
197
	 * Get the field html
198
	 *
199
	 * @param string $field
200
	 * @param array $db_settings
201
	 * @param array $vars
202
	 * @return string
203
	 */
204
	private function get_field_template($field, array &$db_settings, array &$vars)
205
	{
206
		global $module;
207
208
		$vars['lang_explain'] = $this->explain_field($vars);
209
		$vars['append'] = $this->append_field($vars);
210
211
		$type = explode(':', $vars['type']);
212
213
		// We fake this class as it is needed by the build_cfg_template function
214
		$module = new \stdClass();
215
		$module->module = $this->get_field_object($vars, $type, $db_settings, $field);
216
217
		return build_cfg_template($type, $field, $db_settings, $field, $vars);
218
	}
219
220
	/**
221
	 * @param array $vars
222
	 * @param array $type
223
	 * @param array $db_settings
224
	 * @param string $field
225
	 * @return object
226
	 */
227
	private function get_field_object(array &$vars, array &$type, array &$db_settings, $field)
228
	{
229
		if (empty($vars['object']))
230
		{
231
			if (($object = $this->cfg_fields_factory->get($type[0])) !== false)
232
			{
233
				$this->set_params($field, $vars, $db_settings);
234
				$object->prep_field($vars, $type, $field, $db_settings);
235
			}
236
		}
237
		else
238
		{
239
			$object = $vars['object'];
240
			$this->set_params($field, $vars, $db_settings);
241
		}
242
243
		return $object;
244
	}
245
246
	/**
247
	 * Set field legend
248
	 *
249
	 * @param string $field
250
	 * @param string|array $vars
251
	 * @return boolean
252
	 */
253
	private function is_input_field($field, $vars)
254
	{
255
		return ($this->set_legend($field, $vars) || !is_array($vars)) ? false : true;
256
	}
257
258
	/**
259
	 * Set field legend
260
	 *
261
	 * @param string $field
262
	 * @param string|array $vars
263
	 * @return boolean
264
	 */
265
	private function set_legend($field, $vars)
266
	{
267
		if (strpos($field, 'legend') !== false)
268
		{
269
			$this->template->assign_block_vars('options', array(
270
				'S_LEGEND'	=> $field,
271
				'LEGEND'	=> $this->translator->lang($vars)
272
			));
273
274
			return true;
275
		}
276
277
		return false;
278
	}
279
280
	/**
281
	 * Get field details
282
	 *
283
	 * @param array $vars
284
	 * @return mixed|string
285
	 */
286
	private function explain_field(array $vars)
287
	{
288
		$l_explain = '';
289
		if (!empty($vars['explain']))
290
		{
291
			$l_explain = (!empty($vars['lang_explain'])) ? $this->translator->lang($vars['lang_explain']) : $this->translator->lang($vars['lang'] . '_EXPLAIN');
292
		}
293
294
		return $l_explain;
295
	}
296
297
	/**
298
	 * Add text after field
299
	 *
300
	 * @param array $vars
301
	 * @return mixed|string
302
	 */
303
	private function append_field(array $vars)
304
	{
305
		$append = '';
306
		if (!empty($vars['append']))
307
		{
308
			$append = $this->translator->lang($vars['append']);
309
		}
310
311
		return $append;
312
	}
313
314
	/**
315
	 * Set field parameters
316
	 *
317
	 * @param string $field
318
	 * @param array $vars
319
	 * @param array $settings
320
	 */
321
	private function set_params($field, array &$vars, array $settings)
322
	{
323
		if (isset($vars['options']))
324
		{
325
			$vars['params'][] = $vars['options'];
326
			$vars['params'][] = $settings[$field];
327
		}
328
	}
329
330
	/**
331
	 * Get field value
332
	 *
333
	 * @param string $field
334
	 * @param mixed $default
335
	 * @param array $db_settings
336
	 * @return mixed
337
	 */
338
	private function get_field_value($field, $default, array $db_settings)
339
	{
340
		return (isset($db_settings[$field])) ? $db_settings[$field] : $default;
341
	}
342
343
	/**
344
	 * @param array $cfg_array
345
	 * @param array $df_settings
346
	 */
347
	private function get_multi_select(array &$cfg_array, array $df_settings)
348
	{
349
		$multi_select = $this->request->variable('config', array('' => array(0 => '')), true);
350
		$multi_select = array_filter($multi_select);
351
352
		foreach ($multi_select as $field => $settings)
353
		{
354
			$cfg_array[$field] = (!empty($settings)) ? $settings : $df_settings[$field]['default'];
355
		}
356
	}
357
}
358