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 string phpBB root path */ |
26
|
|
|
protected $phpbb_root_path; |
27
|
|
|
|
28
|
|
|
/** @var string phpEx */ |
29
|
|
|
protected $php_ext; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param \phpbb\request\request_interface $request Request object |
35
|
|
|
* @param \phpbb\template\template $template Template object |
36
|
|
|
* @param \phpbb\user $user User object |
37
|
|
|
* @param \blitze\sitemaker\services\groups $groups Groups object |
38
|
|
|
* @param string $phpbb_root_path phpBB root path |
39
|
|
|
* @param string $php_ext phpEx |
40
|
|
|
*/ |
41
|
44 |
|
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) |
42
|
|
|
{ |
43
|
44 |
|
parent::__construct($user); |
44
|
|
|
|
45
|
44 |
|
$this->request = $request; |
46
|
44 |
|
$this->template = $template; |
47
|
44 |
|
$this->user = $user; |
48
|
44 |
|
$this->groups = $groups; |
|
|
|
|
49
|
44 |
|
$this->phpbb_root_path = $phpbb_root_path; |
50
|
44 |
|
$this->php_ext = $php_ext; |
51
|
44 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $block_data |
55
|
|
|
* @param array $default_settings |
56
|
|
|
* @return template|string |
57
|
|
|
*/ |
58
|
7 |
|
public function get_edit_form(array $block_data, array $default_settings) |
59
|
|
|
{ |
60
|
7 |
|
global $module; |
61
|
|
|
|
62
|
7 |
|
if (!function_exists('build_cfg_template')) |
63
|
7 |
|
{ |
64
|
1 |
|
include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
// We fake this class as it is needed by the build_cfg_template function |
68
|
7 |
|
$module = new \stdClass(); |
69
|
7 |
|
$module->module = $this; |
70
|
|
|
|
71
|
7 |
|
$this->_generate_config_fields($block_data['settings'], $default_settings); |
72
|
7 |
|
$selected_groups = $this->_ensure_array($block_data['permission']); |
73
|
|
|
|
74
|
7 |
|
$this->template->assign_vars(array( |
75
|
7 |
|
'S_ACTIVE' => $block_data['status'], |
76
|
7 |
|
'S_TYPE' => $block_data['type'], |
77
|
7 |
|
'S_NO_WRAP' => $block_data['no_wrap'], |
78
|
7 |
|
'S_HIDE_TITLE' => $block_data['hide_title'], |
79
|
7 |
|
'S_BLOCK_CLASS' => trim($block_data['class']), |
80
|
7 |
|
'S_GROUP_OPS' => $this->groups->get_options('special', $selected_groups), |
81
|
7 |
|
)); |
82
|
|
|
|
83
|
7 |
|
$this->template->set_filenames(array( |
84
|
7 |
|
'block_settings' => 'block_settings.html', |
85
|
7 |
|
)); |
86
|
|
|
|
87
|
7 |
|
return $this->template->assign_display('block_settings'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $default_settings |
92
|
|
|
* @return array|void |
93
|
|
|
*/ |
94
|
6 |
|
public function get_submitted_settings(array $default_settings) |
95
|
|
|
{ |
96
|
6 |
|
if (!function_exists('validate_config_vars')) |
97
|
6 |
|
{ |
98
|
|
|
include($this->phpbb_root_path . 'includes/functions_acp.' . $this->php_ext); |
99
|
|
|
} |
100
|
|
|
|
101
|
6 |
|
$cfg_array = utf8_normalize_nfc($this->request->variable('config', array('' => ''), true)); |
102
|
|
|
|
103
|
6 |
|
$errors = array(); |
104
|
6 |
|
validate_config_vars($default_settings, $cfg_array, $errors); |
105
|
|
|
|
106
|
6 |
|
if (sizeof($errors)) |
107
|
6 |
|
{ |
108
|
1 |
|
return array('errors' => join("\n", $errors)); |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
$this->_get_multi_select($cfg_array, $default_settings); |
112
|
|
|
|
113
|
5 |
|
return array_intersect_key($cfg_array, $default_settings); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Generate block configuration fields |
118
|
|
|
* |
119
|
|
|
* @param array $db_settings |
120
|
|
|
* @param array $default_settings |
121
|
|
|
*/ |
122
|
7 |
|
private function _generate_config_fields(array &$db_settings, array $default_settings) |
123
|
|
|
{ |
124
|
7 |
|
foreach ($default_settings as $field => $vars) |
125
|
|
|
{ |
126
|
6 |
|
if ($this->_sets_legend($field, $vars) || !is_array($vars)) |
127
|
6 |
|
{ |
128
|
6 |
|
continue; |
129
|
|
|
} |
130
|
|
|
|
131
|
6 |
|
$db_settings[$field] = $this->_get_field_value($field, $vars['default'], $db_settings); |
132
|
6 |
|
$content = $this->_get_field_template($field, $db_settings, $vars); |
133
|
|
|
|
134
|
6 |
|
$this->template->assign_block_vars('options', array( |
135
|
6 |
|
'KEY' => $field, |
136
|
6 |
|
'TITLE' => $this->user->lang($vars['lang']), |
137
|
6 |
|
'S_EXPLAIN' => $vars['explain'], |
138
|
6 |
|
'TITLE_EXPLAIN' => $vars['lang_explain'], |
139
|
6 |
|
'CONTENT' => $content, |
140
|
6 |
|
)); |
141
|
6 |
|
unset($default_settings[$field]); |
142
|
7 |
|
} |
143
|
7 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param $field |
147
|
|
|
* @param array $db_settings |
148
|
|
|
* @param array $vars |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
6 |
|
private function _get_field_template($field, array &$db_settings, array &$vars) |
152
|
|
|
{ |
153
|
6 |
|
$vars['lang_explain'] = $this->_explain_field($vars); |
154
|
6 |
|
$vars['append'] = $this->_append_field($vars); |
155
|
|
|
|
156
|
6 |
|
$type = explode(':', $vars['type']); |
157
|
6 |
|
$method = '_prep_' . $type[0] . '_field_for_display'; |
158
|
|
|
|
159
|
6 |
|
if (is_callable(array($this, $method))) |
160
|
6 |
|
{ |
161
|
5 |
|
$this->_set_params($field, $vars, $db_settings); |
162
|
5 |
|
$this->$method($vars, $type, $field, $db_settings); |
163
|
5 |
|
} |
164
|
|
|
|
165
|
6 |
|
return build_cfg_template($type, $field, $db_settings, $field, $vars); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $field |
170
|
|
|
* @param string|array $vars |
171
|
|
|
* @return boolean |
172
|
|
|
*/ |
173
|
6 |
|
private function _sets_legend($field, $vars) |
174
|
|
|
{ |
175
|
6 |
|
if (strpos($field, 'legend') !== false) |
176
|
6 |
|
{ |
177
|
6 |
|
$this->template->assign_block_vars('options', array( |
178
|
6 |
|
'S_LEGEND' => $field, |
179
|
6 |
|
'LEGEND' => $this->user->lang($vars) |
180
|
6 |
|
)); |
181
|
|
|
|
182
|
6 |
|
return true; |
183
|
|
|
} |
184
|
|
|
|
185
|
6 |
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param array $vars |
190
|
|
|
* @return mixed|string |
191
|
|
|
*/ |
192
|
6 |
|
private function _explain_field(array $vars) |
193
|
|
|
{ |
194
|
6 |
|
$l_explain = ''; |
195
|
6 |
|
if (!empty($vars['explain'])) |
196
|
6 |
|
{ |
197
|
2 |
|
$l_explain = (isset($vars['lang_explain'])) ? $this->user->lang($vars['lang_explain']) : $this->user->lang($vars['lang'] . '_EXPLAIN'); |
198
|
2 |
|
} |
199
|
|
|
|
200
|
6 |
|
return $l_explain; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param array $vars |
205
|
|
|
* @return mixed|string |
206
|
|
|
*/ |
207
|
6 |
|
private function _append_field(array $vars) |
208
|
|
|
{ |
209
|
6 |
|
$append = ''; |
210
|
6 |
|
if (!empty($vars['append'])) |
211
|
6 |
|
{ |
212
|
1 |
|
$append = $this->user->lang($vars['append']); |
213
|
1 |
|
} |
214
|
|
|
|
215
|
6 |
|
return $append; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param $field |
220
|
|
|
* @param $vars |
221
|
|
|
* @param $settings |
222
|
|
|
*/ |
223
|
5 |
|
private function _set_params($field, &$vars, $settings) |
224
|
|
|
{ |
225
|
5 |
|
if (!empty($vars['options'])) |
226
|
5 |
|
{ |
227
|
3 |
|
$vars['params'][] = $vars['options']; |
228
|
3 |
|
$vars['params'][] = $settings[$field]; |
229
|
3 |
|
} |
230
|
5 |
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param $field |
234
|
|
|
* @param $default |
235
|
|
|
* @param $db_settings |
236
|
|
|
* @return mixed |
237
|
|
|
*/ |
238
|
6 |
|
private function _get_field_value($field, $default, $db_settings) |
239
|
|
|
{ |
240
|
6 |
|
return (!empty($db_settings[$field])) ? $db_settings[$field] : $default; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param $vars |
245
|
|
|
*/ |
246
|
1 |
|
private function _prep_select_field_for_display(&$vars) |
247
|
|
|
{ |
248
|
1 |
|
$this->_add_lang_vars($vars['params'][0]); |
249
|
|
|
|
250
|
1 |
|
$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : 'build_select'; |
251
|
1 |
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @param $vars |
255
|
|
|
* @param $type |
256
|
|
|
* @param $field |
257
|
|
|
*/ |
258
|
2 |
|
private function _prep_checkbox_field_for_display(&$vars, &$type, $field) |
259
|
|
|
{ |
260
|
2 |
|
$this->_add_lang_vars($vars['params'][0]); |
261
|
|
|
|
262
|
2 |
|
$vars['method'] = 'build_checkbox'; |
263
|
2 |
|
$vars['params'][] = $field; |
264
|
2 |
|
$type[0] = 'custom'; |
265
|
2 |
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param $vars |
269
|
|
|
* @param $type |
270
|
|
|
* @param $field |
271
|
|
|
*/ |
272
|
1 |
|
private function _prep_multi_select_field_for_display(&$vars, &$type, $field) |
273
|
|
|
{ |
274
|
1 |
|
$this->_prep_checkbox_field_for_display($vars, $type, $field); |
275
|
|
|
|
276
|
1 |
|
$vars['method'] ='build_multi_select'; |
277
|
1 |
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param $vars |
281
|
|
|
* @param $type |
282
|
|
|
*/ |
283
|
1 |
|
private function _prep_hidden_field_for_display(&$vars, &$type) |
284
|
|
|
{ |
285
|
1 |
|
$vars['method'] = 'build_hidden'; |
286
|
1 |
|
$vars['explain'] = ''; |
287
|
1 |
|
$vars['lang'] = ''; |
288
|
1 |
|
$type[0] = 'custom'; |
289
|
1 |
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param $vars |
293
|
|
|
* @param $type |
294
|
|
|
*/ |
295
|
1 |
|
private function _prep_custom_field_for_display(&$vars, &$type) |
296
|
|
|
{ |
297
|
1 |
|
$vars['function'] = (!empty($vars['function'])) ? $vars['function'] : ''; |
298
|
1 |
|
$type[0] = 'custom'; |
299
|
1 |
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* this looks bad but its the only way without modifying phpbb code |
303
|
|
|
* this is for select items that do not need to be translated |
304
|
|
|
* @param array $options |
305
|
|
|
*/ |
306
|
4 |
|
private function _add_lang_vars(array $options) |
307
|
|
|
{ |
308
|
3 |
|
foreach ($options as $title) |
309
|
|
|
{ |
310
|
3 |
|
if (!isset($this->user->lang[$title])) |
311
|
3 |
|
{ |
312
|
3 |
|
$this->user->lang[$title] = $title; |
313
|
3 |
|
} |
314
|
3 |
|
} |
315
|
4 |
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @param array $cfg_array |
319
|
|
|
* @param array $df_settings |
320
|
|
|
*/ |
321
|
5 |
|
private function _get_multi_select(array &$cfg_array, array $df_settings) |
322
|
|
|
{ |
323
|
5 |
|
$multi_select = utf8_normalize_nfc($this->request->variable('config', array('' => array('' => '')), true)); |
324
|
5 |
|
$multi_select = array_filter($multi_select); |
325
|
|
|
|
326
|
5 |
|
foreach ($multi_select as $field => $settings) |
327
|
|
|
{ |
328
|
1 |
|
$cfg_array[$field] = (!empty($settings)) ? $settings : $df_settings[$field]['default']; |
329
|
5 |
|
} |
330
|
5 |
|
} |
331
|
|
|
} |
332
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: