[ticket/17151] Make settings forms use macros

PHPBB3-17151
This commit is contained in:
rxu 2023-09-19 00:32:24 +07:00
parent c6fd14352e
commit 72770937f2
No known key found for this signature in database
GPG key ID: 955F0567380E586A
7 changed files with 187 additions and 77 deletions

View file

@ -851,7 +851,7 @@ class acp_board
/* @var $auth_providers \phpbb\auth\provider_collection */
$auth_providers = $phpbb_container->get('auth.provider_collection');
$auth_plugins = array();
$auth_plugins = [];
foreach ($auth_providers as $key => $value)
{
@ -864,14 +864,22 @@ class acp_board
sort($auth_plugins);
$auth_select = '';
$auth_select_options = [];
foreach ($auth_plugins as $method)
{
$selected = ($selected_method == $method) ? ' selected="selected"' : '';
$auth_select .= "<option value=\"$method\"$selected data-toggle-setting=\"#auth_{$method}_settings\">" . ucfirst($method) . '</option>';
$auth_select_options[] = [
'value' => $method,
'selected' => $selected_method == $method,
'label' => ucfirst($method),
'data' => [
'toggle-setting' => "#auth_{$method}_settings",
],
];
}
return $auth_select;
return [
'options' => $auth_select_options,
];
}
/**
@ -881,15 +889,21 @@ class acp_board
{
global $user;
$auth_methods = array('PLAIN', 'LOGIN', 'CRAM-MD5', 'DIGEST-MD5', 'POP-BEFORE-SMTP');
$s_smtp_auth_options = '';
$auth_methods = ['PLAIN', 'LOGIN', 'CRAM-MD5', 'DIGEST-MD5', 'POP-BEFORE-SMTP'];
$s_smtp_auth_options = [];
foreach ($auth_methods as $method)
{
$s_smtp_auth_options .= '<option value="' . $method . '"' . (($selected_method == $method) ? ' selected="selected"' : '') . '>' . $user->lang['SMTP_' . str_replace('-', '_', $method)] . '</option>';
$s_smtp_auth_options[] = [
'value' => $method,
'selected' => $selected_method == $method,
'label' => $user->lang('SMTP_' . str_replace('-', '_', $method)),
];
}
return $s_smtp_auth_options;
return [
'options' => $s_smtp_auth_options,
];
}
/**
@ -899,7 +913,22 @@ class acp_board
{
global $user;
return '<option value="1"' . (($value == 1) ? ' selected="selected"' : '') . '>' . $user->lang['DELETE_OLDEST_MESSAGES'] . '</option><option value="2"' . (($value == 2) ? ' selected="selected"' : '') . '>' . $user->lang['HOLD_NEW_MESSAGES_SHORT'] . '</option>';
$full_folder_select_options = [
0 => [
'value' => 1,
'selected' => $value == 1,
'label' => $user->lang('DELETE_OLDEST_MESSAGES'),
],
1 => [
'value' => 2,
'selected' => $value == 2,
'label' => $user->lang('HOLD_NEW_MESSAGES_SHORT'),
],
];
return [
'options' => $full_folder_select_options,
];
}
/**
@ -907,7 +936,7 @@ class acp_board
*/
function select_ip_check($value, $key = '')
{
$radio_ary = array(4 => 'ALL', 3 => 'CLASS_C', 2 => 'CLASS_B', 0 => 'NO_IP_VALIDATION');
$radio_ary = [4 => 'ALL', 3 => 'CLASS_C', 2 => 'CLASS_B', 0 => 'NO_IP_VALIDATION'];
return h_radio('config[ip_check]', $radio_ary, $value, $key);
}
@ -917,7 +946,7 @@ class acp_board
*/
function select_ref_check($value, $key = '')
{
$radio_ary = array(REFERER_VALIDATE_PATH => 'REF_PATH', REFERER_VALIDATE_HOST => 'REF_HOST', REFERER_VALIDATE_NONE => 'NO_REF_VALIDATION');
$radio_ary = [REFERER_VALIDATE_PATH => 'REF_PATH', REFERER_VALIDATE_HOST => 'REF_HOST', REFERER_VALIDATE_NONE => 'NO_REF_VALIDATION'];
return h_radio('config[referer_validation]', $radio_ary, $value, $key);
}
@ -929,23 +958,28 @@ class acp_board
{
global $user, $config;
$act_ary = array(
'ACC_DISABLE' => array(true, USER_ACTIVATION_DISABLE),
'ACC_NONE' => array(true, USER_ACTIVATION_NONE),
'ACC_USER' => array($config['email_enable'], USER_ACTIVATION_SELF),
'ACC_ADMIN' => array($config['email_enable'], USER_ACTIVATION_ADMIN),
);
$act_ary = [
'ACC_DISABLE' => [true, USER_ACTIVATION_DISABLE],
'ACC_NONE' => [true, USER_ACTIVATION_NONE],
'ACC_USER' => [$config['email_enable'], USER_ACTIVATION_SELF],
'ACC_ADMIN' => [$config['email_enable'], USER_ACTIVATION_ADMIN],
];
$act_options = '';
$act_options = [];
foreach ($act_ary as $key => $data)
{
list($available, $value) = $data;
$selected = ($selected_value == $value) ? ' selected="selected"' : '';
$class = (!$available) ? ' class="disabled-option"' : '';
$act_options .= '<option value="' . $value . '"' . $selected . $class . '>' . $user->lang($key) . '</option>';
$act_options[] = [
'value' => $value,
'selected' => $selected_value == $value,
'label' => $user->lang($key),
'disabled' => !$available,
];
}
return $act_options;
return [
'options' => $act_options,
];
}
/**
@ -955,7 +989,27 @@ class acp_board
{
global $user;
return '<input id="' . $key . '" type="number" min="1" max="999" name="config[min_name_chars]" value="' . $value . '" /> ' . $user->lang['MIN_CHARS'] . '&nbsp;&nbsp;<input type="number" min="8" max="180" name="config[max_name_chars]" value="' . $this->new_config['max_name_chars'] . '" /> ' . $user->lang['MAX_CHARS'];
return [
[
'tag' => 'input',
'id' => $key,
'type' => 'number',
'name' => 'config[min_name_chars]',
'min' => 1,
'max' => 999,
'value' => $value,
'append' => $user->lang('MIN_CHARS') . '&nbsp;&nbsp;',
],
[
'tag' => 'input',
'type' => 'number',
'name' => 'config[max_name_chars]',
'min' => 8,
'max' => 180,
'value' => $this->new_config['max_name_chars'],
'append' => $user->lang('MAX_CHARS'),
],
];
}
/**
@ -965,15 +1019,20 @@ class acp_board
{
global $user;
$user_char_ary = array('USERNAME_CHARS_ANY', 'USERNAME_ALPHA_ONLY', 'USERNAME_ALPHA_SPACERS', 'USERNAME_LETTER_NUM', 'USERNAME_LETTER_NUM_SPACERS', 'USERNAME_ASCII');
$user_char_options = '';
$user_char_ary = ['USERNAME_CHARS_ANY', 'USERNAME_ALPHA_ONLY', 'USERNAME_ALPHA_SPACERS', 'USERNAME_LETTER_NUM', 'USERNAME_LETTER_NUM_SPACERS', 'USERNAME_ASCII'];
$user_char_options = [];
foreach ($user_char_ary as $user_type)
{
$selected = ($selected_value == $user_type) ? ' selected="selected"' : '';
$user_char_options .= '<option value="' . $user_type . '"' . $selected . '>' . $user->lang[$user_type] . '</option>';
$user_char_options[] = [
'value' => $user_type,
'selected' => $selected_value == $user_type,
'label' => $user->lang($user_type),
];
}
return $user_char_options;
return [
'options' => $user_char_options,
];
}
/**
@ -983,7 +1042,16 @@ class acp_board
{
global $user;
return '<input id="' . $key . '" type="number" min="1" max="999" name="config[min_pass_chars]" value="' . $value . '" /> ' . $user->lang['MIN_CHARS'];
return [
[
'tag' => 'input',
'id' => $key,
'type' => 'number',
'name' => 'config[min_pass_chars]',
'value' => $value,
'append' => $user->lang('MIN_CHARS'),
],
];
}
/**
@ -994,14 +1062,20 @@ class acp_board
global $user;
$pass_type_ary = array('PASS_TYPE_ANY', 'PASS_TYPE_CASE', 'PASS_TYPE_ALPHA', 'PASS_TYPE_SYMBOL');
$pass_char_options = '';
$pass_char_options = [];
foreach ($pass_type_ary as $pass_type)
{
$selected = ($selected_value == $pass_type) ? ' selected="selected"' : '';
$pass_char_options .= '<option value="' . $pass_type . '"' . $selected . '>' . $user->lang[$pass_type] . '</option>';
$pass_char_options[] = [
'tag' => 'select',
'value' => $pass_type,
'selected' => $selected_value == $pass_type,
'label' => $user->lang[$pass_type],
];
}
return $pass_char_options;
return [
'options' => $pass_char_options,
];
}
/**
@ -1359,8 +1433,22 @@ class acp_board
{
global $user;
return '<input class="button2" type="submit" id="' . $key . '" name="' . $key . '" value="' . $user->lang('SEND_TEST_EMAIL') . '" />
<textarea id="' . $key . '_text" name="' . $key . '_text" placeholder="' . $user->lang('MESSAGE') . '"></textarea>';
return [
[
'tag' => 'input',
'type' => 'submit',
'name' => $key,
'id' => $key,
'class' => 'button2',
'value' => $user->lang('SEND_TEST_EMAIL'),
],
[
'tag' => 'textarea',
'name' => $key . '_text',
'id' => $key . '_text',
'placeholder' => $user->lang('MESSAGE'),
],
];
}
/**

View file

@ -291,7 +291,9 @@ function phpbb_language_select(\phpbb\db\driver\driver_interface $db, string $de
];
}
return $lang_options;
return [
'options' => $lang_options
];
}
/**
@ -319,14 +321,20 @@ function style_select($default = '', $all = false, array $styledata = [])
$db->sql_freeresult($result);
}
$style_options = '';
$style_options = [];
foreach ($styledata as $row)
{
$selected = ($row['style_id'] == $default) ? ' selected="selected"' : '';
$style_options .= '<option value="' . $row['style_id'] . '"' . $selected . '>' . $row['style_name'] . '</option>';
$style_options[] = [
'tag' => 'select',
'value' => $row['style_id'],
'selected' => $row['style_id'] == $default,
'label' => $row['style_name'],
];
}
return $style_options;
return [
'options' => $style_options,
];
}
/**

View file

@ -398,6 +398,32 @@ function phpbb_build_cfg_template(array $tpl_type, string $key, &$new_ary, $conf
$tpl['buttons'] = [$no_button, $yes_button];
}
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':
$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;
@ -484,38 +510,15 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
$return = call_user_func_array($call, $args);
if ($tpl_type[0] == 'select')
if (in_array($tpl_type[0], ['select', 'button']))
{
$size = (isset($tpl_type[1])) ? (int) $tpl_type[1] : 1;
if (is_string($return))
{
$data_toggle = (!empty($tpl_type[2])) ? ' data-togglable-settings="true"' : '';
$tpl = '<select id="' . $key . '" name="' . $name . '"' . (($size > 1) ? ' size="' . $size . '"' : '') . $data_toggle . '>' . $return . '</select>';
}
else
{
$tpl = [
'tag' => 'select',
'id' => $key,
'name' => $name,
'toggleable' => !empty($tpl_type[2]),
'options' => $return,
];
// Add size if it differs from default value of 1
if ($size != 1)
{
$tpl['size'] = $size;
}
}
$tpl_type = array_merge($tpl_type, $return);
$tpl = phpbb_build_cfg_template($tpl_type, $key, $new_ary, $config_key, $vars);
}
else
{
$tpl = $return;
}
break;
default:

View file

@ -179,7 +179,7 @@ class forms extends AbstractExtension
'CLASS' => (string) ($form_data['class'] ?? ''),
'ID' => (string) ($form_data['id'] ?? ''),
'DATA' => $form_data['data'] ?? [],
'NAME' => (string) $form_data['name'],
'NAME' => (string) ($form_data['name'] ?? ''),
'TOGGLEABLE' => (bool) ($form_data['toggleable'] ?? false),
'OPTIONS' => $form_data['options'] ?? [],
'GROUP_ONLY' => (bool) ($form_data['group_only'] ?? false),
@ -210,9 +210,10 @@ class forms extends AbstractExtension
'ID' => (string) $form_data['id'],
'DATA' => $form_data['data'] ?? [],
'NAME' => (string) $form_data['name'],
'ROWS' => (int) $form_data['rows'],
'COLS' => (int) $form_data['cols'],
'CONTENT' => (string) $form_data['content'],
'ROWS' => (int) ($form_data['rows'] ?? ''),
'COLS' => (int) ($form_data['cols'] ?? ''),
'CONTENT' => (string) ($form_data['content'] ?? ''),
'PLACEHOLDER' => (string) ($form_data['placeholder'] ?? ''),
]);
}
catch (\Twig\Error\Error $e)

View file

@ -21,11 +21,18 @@
label="{{ element.label }}">
{% endapply %}
{% for option in element.options %}
<option value="{{ option.value }}"{% if option.selected %} selected="selected"{% endif %}{% if option.disabled %} disabled="disabled" class="disabled-option"{% endif %}>{{ option.label }}</option>
<option
value="{{ option.value }}"
{% if option.selected %}selected="selected"{% endif %}
{% if option.disabled %} disabled="disabled" class="disabled-option"{% endif %}>{{ option.label }}</option>
{% endfor %}
</optgroup>
{% else %}
<option value="{{ element.value }}"{% if element.selected %} selected="selected"{% endif %}{% if element.disabled %} disabled="disabled" class="disabled-option"{% endif %}>{{ element.label }}</option>
<option
value="{{ element.value }}"
{% if element.selected %} selected="selected"{% endif %}
{% if element.disabled %} disabled="disabled" class="disabled-option"{% endif %}
{% if element.data %}{% for key, value in element.data %} data-{{ key }}="{{ value }}"{% endfor %}{% endif %}>{{ element.label }}</option>
{% endif %}
{% endfor %}
</select>

View file

@ -7,4 +7,5 @@
{% endfor %}
name="{{ NAME }}"
rows="{{ ROWS }}"
cols="{{ COLS }}">{% endapply %}{{ CONTENT }}</textarea>
cols="{{ COLS }}"
{% if PLACEHOLDER %}placeholder="{{ PLACEHOLDER }}"{% endif %}>{% endapply %}{{ CONTENT }}</textarea>

View file

@ -61,7 +61,9 @@
<!-- IF S_STYLE_OPTIONS and S_MORE_STYLES -->
<dl>
<dt><label for="user_style">{L_BOARD_STYLE}{L_COLON}</label></dt>
<dd><select name="user_style" id="user_style">{S_STYLE_OPTIONS}</select></dd>
<dd>
{{ FormsSelect(S_STYLE_OPTIONS) }}
</dd>
</dl>
<!-- ENDIF -->
<!-- INCLUDE timezone_option.html -->