[ticket/17151] Deduplicate code for building configuration options data

PHPBB3-17151
This commit is contained in:
rxu 2023-09-19 22:34:18 +07:00
parent 7b04e411b6
commit 7f365855ce
No known key found for this signature in database
GPG key ID: 955F0567380E586A

View file

@ -247,18 +247,19 @@ function h_radio($name, $input_ary, $input_default = false, $id = false, $key =
} }
/** /**
* HTML-less version of build_cfg_template * Build configuration data arrays or templates for configuration settings
* *
* @param array $tpl_type Template type * @param array $tpl_type Configuration setting type data
* @param string $key Config key * @param string $key Configuration option name
* @param $new_ary * @param array $new_ary Updated configuration data
* @param $config_key * @param string $config_key Configuration option name
* @param $vars * @param Array $vars Configuration setting data
* @return array *
* @return array|string
*/ */
function phpbb_build_cfg_template(array $tpl_type, string $key, &$new_ary, $config_key, $vars): array function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
{ {
global $language; global $language, $module, $phpbb_dispatcher;
$tpl = []; $tpl = [];
$name = 'config[' . $config_key . ']'; $name = 'config[' . $config_key . ']';
@ -399,78 +400,9 @@ function phpbb_build_cfg_template(array $tpl_type, string $key, &$new_ary, $conf
} }
break; break;
case 'select':
$tpl = [
'tag' => 'select',
'class' => $tpl_type['class'] ?? false,
'id' => $key,
'data' => $tpl_type['data'] ?? [],
'name' => $name,
'toggleable' => !empty($tpl_type[2]) || !empty($tpl_type['toggleable']),
'options' => $tpl_type['options'],
'group_only' => $tpl_type['group_only'] ?? false,
'size' => $tpl_type[1] ?? $tpl_type['size'] ?? 1,
'multiple' => $tpl_type['multiple'] ?? false,
];
break;
case 'button': case 'button':
$tpl = [
'tag' => 'input',
'class' => $tpl_type['options']['class'],
'id' => $key,
'type' => $tpl_type['options']['type'],
'name' => $tpl_type['options']['name'] ?? $name,
'value' => $tpl_type['options']['value'],
];
break;
}
return $tpl;
}
/**
* Build configuration template for acp configuration pages
*/
function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
{
global $module, $phpbb_dispatcher;
$tpl = '';
$name = 'config[' . $config_key . ']';
// Make sure there is no notice printed out for non-existent config options (we simply set them)
if (!isset($new_ary[$config_key]))
{
$new_ary[$config_key] = '';
}
switch ($tpl_type[0])
{
case 'password':
case 'text':
case 'url':
case 'email':
case 'tel':
case 'search':
case 'color':
case 'datetime':
case 'datetime-local':
case 'month':
case 'week':
case 'date':
case 'time':
case 'number':
case 'range':
case 'dimension':
case 'textarea':
case 'radio':
$tpl = phpbb_build_cfg_template($tpl_type, $key, $new_ary, $config_key, $vars);
break;
case 'select': case 'select':
case 'custom': case 'custom':
if (isset($vars['method'])) if (isset($vars['method']))
{ {
$call = array($module->module, $vars['method']); $call = array($module->module, $vars['method']);
@ -513,7 +445,33 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
if (in_array($tpl_type[0], ['select', 'button'])) if (in_array($tpl_type[0], ['select', 'button']))
{ {
$tpl_type = array_merge($tpl_type, $return); $tpl_type = array_merge($tpl_type, $return);
$tpl = phpbb_build_cfg_template($tpl_type, $key, $new_ary, $config_key, $vars);
if ($tpl_type[0] == 'select')
{
$tpl = [
'tag' => 'select',
'class' => $tpl_type['class'] ?? false,
'id' => $key,
'data' => $tpl_type['data'] ?? [],
'name' => $name,
'toggleable' => !empty($tpl_type[2]) || !empty($tpl_type['toggleable']),
'options' => $tpl_type['options'],
'group_only' => $tpl_type['group_only'] ?? false,
'size' => $tpl_type[1] ?? $tpl_type['size'] ?? 1,
'multiple' => $tpl_type['multiple'] ?? false,
];
}
else
{
$tpl = [
'tag' => 'input',
'class' => $tpl_type['options']['class'],
'id' => $key,
'type' => $tpl_type['options']['type'],
'name' => $tpl_type['options']['name'] ?? $name,
'value' => $tpl_type['options']['value'],
];
}
} }
else else
{ {
@ -550,7 +508,7 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
* @var array new Array with the config values we display * @var array new Array with the config values we display
* @var string name Should be used for the name attribute * @var string name Should be used for the name attribute
* @var array vars Array with the options for the config * @var array vars Array with the options for the config
* @var string tpl The resulting html code we display * @var array|string tpl The resulting html code we display
* @since 3.1.0-a1 * @since 3.1.0-a1
*/ */
$vars = array('tpl_type', 'key', 'new', 'name', 'vars', 'tpl'); $vars = array('tpl_type', 'key', 'new', 'name', 'vars', 'tpl');