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