cfg_handler::get_multi_select()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2015 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\services\blocks\config;
12
13
class cfg_handler
14
{
15
	/** @var \phpbb\request\request_interface */
16
	protected $request;
17
18
	/** @var \phpbb\template\template */
19
	protected $template;
20
21
	/** @var \phpbb\language\language */
22
	protected $translator;
23
24
	/** @var \blitze\sitemaker\services\blocks\config\cfg_factory */
25
	protected $cfg_fields_factory;
26
27
	/** @var \blitze\sitemaker\services\groups */
28
	protected $groups;
29
30
	/** @var string phpBB root path */
31
	protected $phpbb_root_path;
32
33
	/** @var string phpEx */
34
	protected $php_ext;
35
36
	/**
37
	 * Constructor
38
	 *
39
	 * @param \phpbb\request\request_interface						$request				Request object
40
	 * @param \phpbb\template\template								$template				Template object
41
	 * @param \phpbb\language\language								$translator				Language object
42
	 * @param \blitze\sitemaker\services\blocks\config\cfg_factory	$cfg_fields_factory		Block config fields factory
43
	 * @param \blitze\sitemaker\services\groups						$groups					Groups object
44
	 * @param string												$phpbb_root_path		phpBB root path
45
	 * @param string												$php_ext				phpEx
46
	 */
47
	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)
48
	{
49
		$this->request = $request;
50
		$this->template = $template;
51
		$this->translator = $translator;
52
		$this->cfg_fields_factory = $cfg_fields_factory;
53
		$this->groups = $groups;
54
		$this->phpbb_root_path = $phpbb_root_path;
55
		$this->php_ext = $php_ext;
56
	}
57
58
	/**
59
	 * @param array $block_data
60
	 * @param array $default_settings
61
	 * @return mixed
62
	 */
63
	public function get_edit_form(array $block_data, array $default_settings)
64
	{
65
		// @codeCoverageIgnoreStart
66
		if (!function_exists('build_cfg_template'))
67
		{
68
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext);
69
		}
70
		// @codeCoverageIgnoreEnd
71
72
		$this->generate_config_fields($block_data['settings'], $default_settings);
73
74
		return $this->get_form($block_data);
75
	}
76
77
	/**
78
	 * @param array $default_settings
79
	 * @return array
80
	 */
81
	public function get_submitted_settings(array $default_settings)
82
	{
83
		$cfg_array = $this->request->variable('config', array('' => ''), true);
84
		$cfg_array = $this->decode_source_html($cfg_array);
85
		$errors = $this->validate_block_settings($default_settings, $cfg_array);
86
87
		if (sizeof($errors))
88
		{
89
			throw new \Exception(join("\n", $errors));
90
		}
91
92
		$this->get_multi_select($cfg_array, $default_settings);
93
94
		return array_intersect_key($cfg_array, $default_settings);
95
	}
96
97
	/**
98
	 * @param array $default_settings
99
	 * @param array $cfg_array
100
	 * @return array
101
	 */
102
	protected function validate_block_settings(array $default_settings, array $cfg_array)
103
	{
104
		// @codeCoverageIgnoreStart
105
		if (!function_exists('validate_config_vars'))
106
		{
107
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext);
108
		}
109
		// @codeCoverageIgnoreEnd
110
111
		$errors = array();
112
		validate_config_vars($default_settings, $cfg_array, $errors);
113
114
		return $errors;
115
	}
116
117
	/**
118
	 * As a workaround to prevent mod_security and similar from preventing us from posting html/script,
119
	 * we use encodeURI before submitting data via ajax (see develop/blocks/manager.js).
120
	 * This decodes the data before submitting to the database
121
	 *
122
	 * @param array $cfg_array
123
	 * @return array
124
	 */
125
	private function decode_source_html(array $cfg_array)
126
	{
127
		if (isset($cfg_array['source']))
128
		{
129
			$cfg_array['source'] = rawurldecode($cfg_array['source']);
130
		}
131
132
		return $cfg_array;
133
	}
134
135
	/**
136
	 * Get the html form
137
	 *
138
	 * @param array $block_data
139
	 * @return mixed
140
	 */
141
	private function get_form(array $block_data)
142
	{
143
		$this->template->assign_vars(array(
144
			'S_BLOCK_ID'	=> $block_data['bid'],
145
			'S_ACTIVE'		=> $block_data['status'],
146
			'S_TYPE'		=> $block_data['type'],
147
			'S_VIEW'		=> $block_data['view'],
148
			'S_HIDE_TITLE'	=> $block_data['hide_title'],
149
			'S_BLOCK_CLASS'	=> trim($block_data['class']),
150
			'S_PERMISSION'	=> $block_data['permission'],
151
			'S_GROUPS'		=> $this->groups->get_data('all'),
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
185
			$this->template->assign_block_vars('cfg_fields', array_merge(
186
				$this->get_field_template($field, $db_settings, $vars),
187
				array(
188
					'KEY'			=> $field,
189
					'TITLE'			=> $this->translator->lang($vars['lang']),
190
					'S_EXPLAIN'		=> $vars['explain'],
191
					'TITLE_EXPLAIN'	=> $vars['lang_explain'],
192
				)
193
			));
194
		}
195
	}
196
197
	/**
198
	 * Get the field html
199
	 *
200
	 * @param string $field
201
	 * @param array $db_settings
202
	 * @param array $vars
203
	 * @return array
204
	 */
205
	private function get_field_template($field, array &$db_settings, array &$vars)
206
	{
207
		global $module;
208
209
		$vars['lang_explain'] = $this->explain_field($vars);
210
		$append = $this->append_field($vars);
211
212
		/**
213
		 * as our own custom fields return an array while phpbb expects a string
214
		 * and appends to that string, we remove it here to prevent an error
215
		 */
216
		unset($vars['append']);
217
218
		$type = explode(':', $vars['type']);
219
		$object = $this->get_field_object($vars, $type, $db_settings, $field);
220
221
		$tpl_data = array(
222
			'append'	=> $append,
223
		);
224
225
		// We fake this class as it is needed by the build_cfg_template function
226
		$module = new \stdClass();
227
		$module->module = $object;
228
229
		$tpl = build_cfg_template($type, $field, $db_settings, $field, $vars);
230
231
		if (is_array($tpl) && $object instanceof \blitze\sitemaker\services\blocks\config\fields\cfg_field_interface)
232
		{
233
			$tpl_data['template'] = $object->get_template();
234
			$tpl_data['tpl_data'] = $tpl;
235
		}
236
		else
237
		{
238
			$tpl_data['content'] = $tpl;
239
		}
240
241
		return array_change_key_case($tpl_data, CASE_UPPER);
242
	}
243
244
	/**
245
	 * @param array $vars
246
	 * @param array $type
247
	 * @param array $db_settings
248
	 * @param string $field
249
	 * @return object
250
	 */
251
	private function get_field_object(array &$vars, array &$type, array &$db_settings, $field)
252
	{
253
		if (empty($vars['object']))
254
		{
255
			if (($object = $this->cfg_fields_factory->get($type[0])) !== false)
256
			{
257
				$this->set_params($field, $vars, $db_settings);
258
				$object->prep_field($vars, $type, $field, $db_settings);
259
			}
260
		}
261
		else
262
		{
263
			$object = $vars['object'];
264
			$this->set_params($field, $vars, $db_settings);
265
		}
266
267
		return $object;
268
	}
269
270
	/**
271
	 * Set field legend
272
	 *
273
	 * @param string $field
274
	 * @param string|array $vars
275
	 * @return boolean
276
	 */
277
	private function is_input_field($field, $vars)
278
	{
279
		return ($this->set_legend($field, $vars) || !is_array($vars)) ? false : true;
280
	}
281
282
	/**
283
	 * Set field legend
284
	 *
285
	 * @param string $field
286
	 * @param string|array $vars
287
	 * @return boolean
288
	 */
289
	private function set_legend($field, $vars)
290
	{
291
		if (strpos($field, 'legend') !== false)
292
		{
293
			$this->template->assign_block_vars('cfg_fields', array(
294
				'S_LEGEND'	=> $field,
295
				'LEGEND'	=> $this->translator->lang($vars)
296
			));
297
298
			return true;
299
		}
300
301
		return false;
302
	}
303
304
	/**
305
	 * Get field details
306
	 *
307
	 * @param array $vars
308
	 * @return mixed|string
309
	 */
310
	private function explain_field(array $vars)
311
	{
312
		$l_explain = '';
313
		if (!empty($vars['explain']))
314
		{
315
			$l_explain = (!empty($vars['lang_explain'])) ? $this->translator->lang($vars['lang_explain']) : $this->translator->lang($vars['lang'] . '_EXPLAIN');
316
		}
317
318
		return $l_explain;
319
	}
320
321
	/**
322
	 * Add text after field
323
	 *
324
	 * @param array $vars
325
	 * @return mixed|string
326
	 */
327
	private function append_field(array $vars)
328
	{
329
		$append = '';
330
		if (!empty($vars['append']))
331
		{
332
			$append = $this->translator->lang($vars['append']);
333
		}
334
335
		return $append;
336
	}
337
338
	/**
339
	 * Set field parameters
340
	 *
341
	 * @param string $field
342
	 * @param array $vars
343
	 * @param array $settings
344
	 */
345
	private function set_params($field, array &$vars, array $settings)
346
	{
347
		if (isset($vars['options']))
348
		{
349
			$vars['params'][] = $vars['options'];
350
			$vars['params'][] = $settings[$field];
351
		}
352
	}
353
354
	/**
355
	 * Get field value
356
	 *
357
	 * @param string $field
358
	 * @param mixed $default
359
	 * @param array $db_settings
360
	 * @return mixed
361
	 */
362
	private function get_field_value($field, $default, array $db_settings)
363
	{
364
		return (isset($db_settings[$field])) ? $db_settings[$field] : $default;
365
	}
366
367
	/**
368
	 * @param array $cfg_array
369
	 * @param array $df_settings
370
	 */
371
	private function get_multi_select(array &$cfg_array, array $df_settings)
372
	{
373
		$multi_select = $this->request->variable('config', array('' => array(0 => '')), true);
374
		$multi_select = array_filter($multi_select);
375
376
		foreach ($multi_select as $field => $settings)
377
		{
378
			$cfg_array[$field] = (!empty($settings)) ? $settings : $df_settings[$field]['default'];
379
		}
380
	}
381
}
382