Completed
Push — master ( 1deced...926e5e )
by Daniel
08:35
created

cfg_handler::get_edit_form()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 17
ccs 9
cts 9
cp 1
rs 9.4286
cc 2
eloc 8
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
use blitze\sitemaker\services\blocks\cfg_fields;
13
14
class cfg_handler extends cfg_fields
15
{
16
	/** @var \phpbb\request\request_interface */
17
	protected $request;
18
19
	/** @var \phpbb\template\template */
20
	protected $template;
21
22
	/** @var \phpbb\user */
23
	protected $user;
24
25
	/** @var \blitze\sitemaker\services\groups */
26
	protected $groups;
27
28
	/** @var string phpBB root path */
29
	protected $phpbb_root_path;
30
31
	/** @var string phpEx */
32
	protected $php_ext;
33
34
	/**
35
	 * Constructor
36
	 *
37
	 * @param \phpbb\request\request_interface		$request				Request object
38
	 * @param \phpbb\template\template				$template				Template object
39
	 * @param \phpbb\user							$user					User object
40
	 * @param \blitze\sitemaker\services\groups		$groups					Groups object
41
	 * @param string								$phpbb_root_path		phpBB root path
42
	 * @param string								$php_ext				phpEx
43
	 */
44 45
	public function __construct(\phpbb\request\request_interface $request, \phpbb\template\template $template, \phpbb\user $user, \blitze\sitemaker\services\groups $groups, $phpbb_root_path, $php_ext)
45
	{
46 45
		parent::__construct($user);
47
48 45
		$this->request = $request;
49 45
		$this->template = $template;
50 45
		$this->user = $user;
51 45
		$this->groups = $groups;
52 45
		$this->phpbb_root_path = $phpbb_root_path;
53 45
		$this->php_ext = $php_ext;
54 45
	}
55
56
	/**
57
	 * @param array $block_data
58
	 * @param array $default_settings
59
	 * @return template|string
60
	 */
61 7
	public function get_edit_form(array $block_data, array $default_settings)
62
	{
63 7
		global $module;
64
65 7
		if (!function_exists('build_cfg_template'))
66 7
		{
67
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext); // @codeCoverageIgnore
68 1
		}
69
70
		// We fake this class as it is needed by the build_cfg_template function
71 7
		$module = new \stdClass();
72 7
		$module->module = $this;
73
74 7
		$this->_generate_config_fields($block_data['settings'], $default_settings);
75
76 7
		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 6
		if (!function_exists('validate_config_vars'))
86 6
		{
87
			include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext); // @codeCoverageIgnore
88
		}
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 7
	private function _get_form(array $block_data)
112
	{
113 7
		$selected_groups = $this->_ensure_array($block_data['permission']);
114
115 7
		$this->template->assign_vars(array(
116 7
			'S_ACTIVE'		=> $block_data['status'],
117 7
			'S_TYPE'		=> $block_data['type'],
118 7
			'S_NO_WRAP'		=> $block_data['no_wrap'],
119 7
			'S_HIDE_TITLE'	=> $block_data['hide_title'],
120 7
			'S_BLOCK_CLASS'	=> trim($block_data['class']),
121 7
			'S_GROUP_OPS'	=> $this->groups->get_options('special', $selected_groups),
122 7
		));
123
124 7
		$this->template->set_filenames(array(
125 7
			'block_settings' => 'block_settings.html',
126 7
		));
127
128 7
		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 7
	private function _generate_config_fields(array &$db_settings, array $default_settings)
138
	{
139 7
		foreach ($default_settings as $field => $vars)
140
		{
141 6
			if ($this->_sets_legend($field, $vars) || !is_array($vars))
142 6
			{
143 6
				continue;
144
			}
145
146 6
			$db_settings[$field] = $this->_get_field_value($field, $vars['default'], $db_settings);
147 6
			$content = $this->_get_field_template($field, $db_settings, $vars);
148
149 6
			$this->template->assign_block_vars('options', array(
150 6
				'KEY'			=> $field,
151 6
				'TITLE'			=> $this->user->lang($vars['lang']),
152 6
				'S_EXPLAIN'		=> $vars['explain'],
153 6
				'TITLE_EXPLAIN'	=> $vars['lang_explain'],
154 6
				'CONTENT'		=> $content,
155 6
			));
156 6
			unset($default_settings[$field]);
157 7
		}
158 7
	}
159
160
	/**
161
	 * @param $field
162
	 * @param array $db_settings
163
	 * @param array $vars
164
	 * @return string
165
	 */
166 6
	private function _get_field_template($field, array &$db_settings, array &$vars)
167
	{
168 6
		$vars['lang_explain'] = $this->_explain_field($vars);
169 6
		$vars['append'] = $this->_append_field($vars);
170
171 6
		$type = explode(':', $vars['type']);
172 6
		$method = '_prep_' . $type[0] . '_field_for_display';
173
174 6
		if (is_callable(array($this, $method)))
175 6
		{
176 5
			$this->_set_params($field, $vars, $db_settings);
177 5
			$this->$method($vars, $type, $field, $db_settings);
178 5
		}
179
180 6
		return build_cfg_template($type, $field, $db_settings, $field, $vars);
181
	}
182
183
	/**
184
	 * @param string $field
185
	 * @param string|array $vars
186
	 * @return boolean
187
	 */
188 6
	private function _sets_legend($field, $vars)
189
	{
190 6
		if (strpos($field, 'legend') !== false)
191 6
		{
192 6
			$this->template->assign_block_vars('options', array(
193 6
				'S_LEGEND'	=> $field,
194 6
				'LEGEND'	=> $this->user->lang($vars)
195 6
			));
196
197 6
			return true;
198
		}
199
200 6
		return false;
201
	}
202
203
	/**
204
	 * @param array $vars
205
	 * @return mixed|string
206
	 */
207 6
	private function _explain_field(array $vars)
208
	{
209 6
		$l_explain = '';
210 6
		if (!empty($vars['explain']))
211 6
		{
212 2
			$l_explain = (isset($vars['lang_explain'])) ? $this->user->lang($vars['lang_explain']) : $this->user->lang($vars['lang'] . '_EXPLAIN');
213 2
		}
214
215 6
		return $l_explain;
216
	}
217
218
	/**
219
	 * @param array $vars
220
	 * @return mixed|string
221
	 */
222 6
	private function _append_field(array $vars)
223
	{
224 6
		$append = '';
225 6
		if (!empty($vars['append']))
226 6
		{
227 1
			$append = $this->user->lang($vars['append']);
228 1
		}
229
230 6
		return $append;
231
	}
232
233
	/**
234
	 * @param $field
235
	 * @param $vars
236
	 * @param $settings
237
	 */
238 5
	private function _set_params($field, &$vars, $settings)
239
	{
240 5
		if (!empty($vars['options']))
241 5
		{
242 3
			$vars['params'][] = $vars['options'];
243 3
			$vars['params'][] = $settings[$field];
244 3
		}
245 5
	}
246
247
	/**
248
	 * @param $field
249
	 * @param $default
250
	 * @param $db_settings
251
	 * @return mixed
252
	 */
253 6
	private function _get_field_value($field, $default, $db_settings)
254
	{
255 6
		return (!empty($db_settings[$field])) ? $db_settings[$field] : $default;
256
	}
257
258
	/**
259
	 * @param $vars
260
	 */
261 1
	private function _prep_select_field_for_display(&$vars)
262
	{
263 1
		$this->_add_lang_vars($vars['params'][0]);
264
265 1
		$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : 'build_select';
266 1
	}
267
268
	/**
269
	 * @param $vars
270
	 * @param $type
271
	 * @param $field
272
	 */
273 2
	private function _prep_checkbox_field_for_display(&$vars, &$type, $field)
274
	{
275 2
		$this->_add_lang_vars($vars['params'][0]);
276
277 2
		$vars['method'] = 'build_checkbox';
278 2
		$vars['params'][] = $field;
279 2
		$type[0] = 'custom';
280 2
	}
281
282
	/**
283
	 * @param $vars
284
	 * @param $type
285
	 * @param $field
286
	 */
287 1
	private function _prep_multi_select_field_for_display(&$vars, &$type, $field)
288
	{
289 1
		$this->_prep_checkbox_field_for_display($vars, $type, $field);
290
291 1
		$vars['method'] ='build_multi_select';
292 1
	}
293
294
	/**
295
	 * @param $vars
296
	 * @param $type
297
	 */
298 1
	private function _prep_hidden_field_for_display(&$vars, &$type)
299
	{
300 1
		$vars['method'] = 'build_hidden';
301 1
		$vars['explain'] = '';
302 1
		$vars['lang'] = '';
303 1
		$type[0] = 'custom';
304 1
	}
305
306
	/**
307
	 * @param $vars
308
	 * @param $type
309
	 */
310 1
	private function _prep_custom_field_for_display(&$vars, &$type)
311
	{
312 1
		$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : '';
313 1
		$type[0] = 'custom';
314 1
	}
315
316
	/**
317
	 * this looks bad but its the only way without modifying phpbb code
318
	 * this is for select items that do not need to be translated
319
	 * @param array $options
320
	 */
321 3
	private function _add_lang_vars(array $options)
322
	{
323 3
		foreach ($options as $title)
324
		{
325 3
			if (!isset($this->user->lang[$title]))
326 3
			{
327 3
				$this->user->lang[$title] = $title;
328 3
			}
329 3
		}
330 3
	}
331
332
	/**
333
	 * @param array $cfg_array
334
	 * @param array $df_settings
335
	 */
336 5
	private function _get_multi_select(array &$cfg_array, array $df_settings)
337
	{
338 5
		$multi_select = utf8_normalize_nfc($this->request->variable('config', array('' => array('' => '')), true));
339 5
		$multi_select = array_filter($multi_select);
340
341 5
		foreach ($multi_select as $field => $settings)
342
		{
343 1
			$cfg_array[$field] = (!empty($settings)) ? $settings : $df_settings[$field]['default'];
344 5
		}
345 5
	}
346
}
347