[ticket/17100] Move build_cfg_template parts to HTML-less function

PHPBB3-17100
This commit is contained in:
Marc Alexander 2022-04-18 11:31:23 +02:00
parent f64eb7dd04
commit 99368ab19a
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
4 changed files with 207 additions and 23 deletions

View file

@ -37,6 +37,7 @@
<!-- IF S_ATTACHMENT_SETTINGS --> <!-- IF S_ATTACHMENT_SETTINGS -->
{% import "form_macros.twig" as form_macros %}
<form id="attachsettings" method="post" action="{U_ACTION}"> <form id="attachsettings" method="post" action="{U_ACTION}">
<!-- BEGIN options --> <!-- BEGIN options -->
<!-- IF options.S_LEGEND --> <!-- IF options.S_LEGEND -->
@ -49,7 +50,20 @@
<dl> <dl>
<dt><label for="{options.KEY}">{options.TITLE}{L_COLON}</label><!-- IF options.S_EXPLAIN --><br /><span>{options.TITLE_EXPLAIN}</span><!-- ENDIF --></dt> <dt><label for="{options.KEY}">{options.TITLE}{L_COLON}</label><!-- IF options.S_EXPLAIN --><br /><span>{options.TITLE_EXPLAIN}</span><!-- ENDIF --></dt>
<dd>{options.CONTENT}</dd> <dd>
{% if options.CONTENT.tag %}
{% if options.CONTENT.tag == 'input' %}
{{ form_macros.input(options.CONTENT) }}
{% elseif options.CONTENT.tag == 'dimension' %}
{{ form_macros.dimension(options.CONTENT) }}
{% elseif options.CONTENT.tag == 'radio' %}
{{ form_macros.radio_buttons(options.CONTENT) }}
{% endif %}
{% if options.CONTENT.append %}{{ options.CONTENT.append }}{% endif %}
{% else %}
{options.CONTENT}
{% endif %}
</dd>
{% if (options.KEY == 'allow_attachments' and S_EMPTY_POST_GROUPS) or (options.KEY == 'allow_pm_attach' and S_EMPTY_PM_GROUPS) %} {% if (options.KEY == 'allow_attachments' and S_EMPTY_POST_GROUPS) or (options.KEY == 'allow_pm_attach' and S_EMPTY_PM_GROUPS) %}
<dd><span class="error">{{ lang(options.KEY == 'allow_attachments' ? 'NO_EXT_GROUP_ALLOWED_POST' : 'NO_EXT_GROUP_ALLOWED_PM', U_EXTENSION_GROUPS) }}</span></dd> <dd><span class="error">{{ lang(options.KEY == 'allow_attachments' ? 'NO_EXT_GROUP_ALLOWED_POST' : 'NO_EXT_GROUP_ALLOWED_PM', U_EXTENSION_GROUPS) }}</span></dd>
{% endif %} {% endif %}

View file

@ -13,6 +13,7 @@
</div> </div>
<!-- ENDIF --> <!-- ENDIF -->
{% import "form_macros.twig" as form_macros %}
<form id="acp_board" method="post" action="{U_ACTION}"> <form id="acp_board" method="post" action="{U_ACTION}">
<!-- BEGIN options --> <!-- BEGIN options -->
@ -27,7 +28,20 @@
<dl> <dl>
<dt><label for="{options.KEY}">{options.TITLE}{L_COLON}</label><!-- IF options.S_EXPLAIN --><br /><span>{options.TITLE_EXPLAIN}</span><!-- ENDIF --></dt> <dt><label for="{options.KEY}">{options.TITLE}{L_COLON}</label><!-- IF options.S_EXPLAIN --><br /><span>{options.TITLE_EXPLAIN}</span><!-- ENDIF --></dt>
<dd>{options.CONTENT}</dd> <dd>
{% if options.CONTENT.tag %}
{% if options.CONTENT.tag == 'input' %}
{{ form_macros.input(options.CONTENT) }}
{% elseif options.CONTENT.tag == 'dimension' %}
{{ form_macros.dimension(options.CONTENT) }}
{% elseif options.CONTENT.tag == 'radio' %}
{{ form_macros.radio_buttons(options.CONTENT) }}
{% endif %}
{% if options.CONTENT.append %}{{ options.CONTENT.append }}{% endif %}
{% else %}
{{ options.CONTENT }}
{% endif %}
</dd>
</dl> </dl>
<!-- ENDIF --> <!-- ENDIF -->

View file

@ -0,0 +1,35 @@
{% macro input(form_data) %}
<input
{% if form_data.id %}id="{{ form_data.id }}"{% endif %}
type="{{ form_data.type }}"
name="{{ form_data.name }}"
{% if form_data.size %}size="{{ form_data.size }}"{% endif %}
{% if form_data.maxlength %}maxlength="{{ form_data.maxlength }}"{% endif %}
{% if form_data.min %}min="{{ form_data.min }}"{% endif %}
{% if form_data.max %}max="{{ form_data.max }}"{% endif %}
{% if form_data.type == 'password' %}autocomplete="off"{% endif %}
{% if form_data.checked %}checked="checked"{% endif %}
{% if form_data.class %}class="{{ form_data.class }}"{% endif %}
value="{{ form_data.value }}"
>
{% endmacro %}
{% macro dimension(form_data) %}
{{ _self.input(form_data.width) }} x {{ _self.input(form_data.height) }}
{% endmacro %}
{% macro textarea(form_data) %}
<textarea
id="{{ form_data.id }}"
name="{{ form_data.name }}"
rows="{{ form_data.rows }}"
cols="{{ form_data.cols }}"
>
{{ form_data.content }}
</textarea>
{% endmacro %}
{% macro radio_buttons(form_data) %}
<label>{{ _self.input(form_data.buttons[0]) ~ form_data.buttons[0].label }}</label>
<label>{{ _self.input(form_data.buttons[1]) ~ form_data.buttons[1].label }}</label>
{% endmacro %}

View file

@ -243,13 +243,20 @@ function h_radio($name, $input_ary, $input_default = false, $id = false, $key =
} }
/** /**
* Build configuration template for acp configuration pages * HTML-less version of build_cfg_template
*/ *
function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars) * @param array $tpl_type Template type
* @param string $key Config key
* @param $new_ary
* @param $config_key
* @param $vars
* @return array
*/
function phpbb_build_cfg_template(array $tpl_type, string $key, &$new_ary, $config_key, $vars): array
{ {
global $user, $module, $phpbb_dispatcher; global $language;
$tpl = ''; $tpl = [];
$name = 'config[' . $config_key . ']'; $name = 'config[' . $config_key . ']';
// Make sure there is no notice printed out for non-existent config options (we simply set them) // Make sure there is no notice printed out for non-existent config options (we simply set them)
@ -266,6 +273,8 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
// replace passwords with asterixes // replace passwords with asterixes
$new_ary[$config_key] = '********'; $new_ary[$config_key] = '********';
} }
// no break
case 'text': case 'text':
case 'url': case 'url':
case 'email': case 'email':
@ -276,7 +285,15 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
$size = (int) $tpl_type[1]; $size = (int) $tpl_type[1];
$maxlength = (int) $tpl_type[2]; $maxlength = (int) $tpl_type[2];
$tpl = '<input id="' . $key . '" type="' . $tpl_type[0] . '"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="' . $name . '" value="' . $new_ary[$config_key] . '"' . (($tpl_type[0] === 'password') ? ' autocomplete="off"' : '') . ' />'; $tpl = [
'tag' => 'input',
'id' => $key,
'type' => $tpl_type[0],
'name' => $name,
'size' => $size ?: '',
'maxlength' => $maxlength ?: 255,
'value' => $new_ary[$config_key],
];
break; break;
case 'color': case 'color':
@ -284,7 +301,13 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
case 'datetime-local': case 'datetime-local':
case 'month': case 'month':
case 'week': case 'week':
$tpl = '<input id="' . $key . '" type="' . $tpl_type[0] . '" name="' . $name . '" value="' . $new_ary[$config_key] . '" />'; $tpl = [
'tag' => 'input',
'id' => $key,
'type' => $tpl_type[0],
'name' => $name,
'value' => $new_ary[$config_key],
];
break; break;
case 'date': case 'date':
@ -294,34 +317,125 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
$min = isset($tpl_type[1]) ? (int) $tpl_type[1] : false; $min = isset($tpl_type[1]) ? (int) $tpl_type[1] : false;
$max = isset($tpl_type[2]) ? (int) $tpl_type[2] : false; $max = isset($tpl_type[2]) ? (int) $tpl_type[2] : false;
$tpl = '<input id="' . $key . '" type="' . $tpl_type[0] . '"' . (( $min !== false ) ? ' min="' . $min . '"' : '') . (( $max !== false ) ? ' max="' . $max . '"' : '') . ' name="' . $name . '" value="' . $new_ary[$config_key] . '" />'; $tpl = [
'tag' => 'input',
'id' => $key,
'type' => $tpl_type[0],
'name' => $name,
'min' => $min !== false ? $min : '',
'max' => $max !== false ? $max : '',
'value' => $new_ary[$config_key],
];
break; break;
case 'dimension': case 'dimension':
$min = isset($tpl_type[1]) ? (int) $tpl_type[1] : false; $min = isset($tpl_type[1]) ? (int) $tpl_type[1] : false;
$max = isset($tpl_type[2]) ? (int) $tpl_type[2] : false; $max = isset($tpl_type[2]) ? (int) $tpl_type[2] : false;
$tpl = '<input id="' . $key . '" type="number"' . (( $min !== false ) ? ' min="' . $min . '"' : '') . (( $max !== false ) ? ' max="' . $max . '"' : '') . ' name="config[' . $config_key . '_width]" value="' . $new_ary[$config_key . '_width'] . '" /> x <input type="number"' . (( $min !== '' ) ? ' min="' . $min . '"' : '') . (( $max != '' ) ? ' max="' . $max . '"' : '') . ' name="config[' . $config_key . '_height]" value="' . $new_ary[$config_key . '_height'] . '" />'; $tpl = [
'tag' => 'dimension',
'width' => [
'id' => $key,
'type' => 'number',
'name' => 'config[' . $config_key . '_width]',
'min' => $min !== false ? $min : '',
'max' => $max !== false ? $max : '',
'value' => $new_ary[$config_key . '_width'],
],
'height' => [
'type' => 'number',
'name' => 'config[' . $config_key . '_height]',
'min' => $min !== false ? $min : '',
'max' => $max !== false ? $max : '',
'value' => $new_ary[$config_key . '_height'],
],
];
break; break;
case 'textarea': case 'textarea':
$rows = (int) $tpl_type[1]; $tpl = [
$cols = (int) $tpl_type[2]; 'tag' => 'textarea',
'id' => $key,
$tpl = '<textarea id="' . $key . '" name="' . $name . '" rows="' . $rows . '" cols="' . $cols . '">' . $new_ary[$config_key] . '</textarea>'; 'name' => $name,
'rows' => (int) $tpl_type[1],
'cols' => (int) $tpl_type[2],
'content' => $new_ary[$config_key],
];
break; break;
case 'radio': case 'radio':
$key_yes = ($new_ary[$config_key]) ? ' checked="checked"' : '';
$key_no = (!$new_ary[$config_key]) ? ' checked="checked"' : '';
$tpl_type_cond = explode('_', $tpl_type[1]); $tpl_type_cond = explode('_', $tpl_type[1]);
$type_no = ($tpl_type_cond[0] == 'disabled' || $tpl_type_cond[0] == 'enabled') ? false : true; $type_no = !(($tpl_type_cond[0] == 'disabled' || $tpl_type_cond[0] == 'enabled'));
$tpl_no = '<label><input type="radio" name="' . $name . '" value="0"' . $key_no . ' class="radio" /> ' . (($type_no) ? $user->lang['NO'] : $user->lang['DISABLED']) . '</label>'; $no_button = [
$tpl_yes = '<label><input type="radio" id="' . $key . '" name="' . $name . '" value="1"' . $key_yes . ' class="radio" /> ' . (($type_no) ? $user->lang['YES'] : $user->lang['ENABLED']) . '</label>'; 'type' => 'radio',
'name' => $name,
'value' => 0,
'checked' => !$new_ary[$config_key],
'label' => $type_no ? $language->lang('NO') : $language->lang('DISABLED'),
];
$tpl = ($tpl_type_cond[0] == 'yes' || $tpl_type_cond[0] == 'enabled') ? $tpl_yes . $tpl_no : $tpl_no . $tpl_yes; $yes_button = [
'id' => $key,
'type' => 'radio',
'name' => $name,
'value' => 1,
'checked' => $new_ary[$config_key],
'label' => $type_no ? $language->lang('YES') : $language->lang('ENABLED'),
];
$tpl = ['tag' => 'radio'];
if ($tpl_type_cond[0] == 'yes' || $tpl_type_cond[0] == 'enabled')
{
$tpl['buttons'] = [$yes_button, $no_button];
}
else
{
$tpl['buttons'] = [$no_button, $yes_button];
}
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; break;
case 'select': case 'select':
@ -386,7 +500,14 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
if (isset($vars['append'])) if (isset($vars['append']))
{ {
$tpl .= $vars['append']; if (is_array($tpl))
{
$tpl['append'] = $vars['append'];
}
else
{
$tpl .= $vars['append'];
}
} }
$new = $new_ary; $new = $new_ary;