[ticket/17100] Reuse form_macros in ACP and fix tests

PHPBB3-17100
This commit is contained in:
Marc Alexander 2023-01-30 16:48:18 +01:00
parent 015472ab91
commit 6473167d6e
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
12 changed files with 135 additions and 121 deletions

View file

@ -53,12 +53,19 @@ $module_id = $request->variable('i', '');
$mode = $request->variable('mode', '');
// Set custom style for admin area
$template->set_custom_style(array(
array(
'name' => 'adm',
'ext_path' => 'adm/style/',
),
), $phpbb_admin_path . 'style');
/** @var \phpbb\template\base $template */
$template->set_custom_style(
[
[
'name' => 'adm',
'ext_path' => 'adm/style/',
]
],
[
$phpbb_admin_path . 'style',
$phpbb_root_path . 'styles/all/template/',
],
);
$template->assign_var('T_ASSETS_PATH', $phpbb_root_path . 'assets');
$template->assign_var('T_TEMPLATE_PATH', $phpbb_admin_path . 'style');

View file

@ -37,7 +37,7 @@
<!-- IF S_ATTACHMENT_SETTINGS -->
{% import "form_macros.twig" as form_macros %}
{% import 'macros/form_macros.twig' as form_macros %}
<form id="attachsettings" method="post" action="{U_ACTION}">
<!-- BEGIN options -->
<!-- IF options.S_LEGEND -->
@ -210,7 +210,7 @@
<dt><label for="extgroup_filesize">{L_MAX_EXTGROUP_FILESIZE}{L_COLON}</label></dt>
<dd>
<input type="number" id="extgroup_filesize" min="0" max="999999999999999" step="any" name="max_filesize" value="{EXTGROUP_FILESIZE}" />
{% from 'form_macros.twig' import select %}
{% from 'macros/form_macros.twig' import select %}
{{ select(EXT_GROUP_SIZE_OPTIONS) }}
</dd>
</dl>

View file

@ -13,7 +13,7 @@
</div>
<!-- ENDIF -->
{% import "form_macros.twig" as form_macros %}
{% import 'macros/form_macros.twig' as form_macros %}
<form id="acp_board" method="post" action="{U_ACTION}">
<!-- BEGIN options -->

View file

@ -32,7 +32,7 @@
<dl>
<dt><label for="{{ LANG_OPTIONS.id }}">{L_BOT_LANG}{L_COLON}</label><br /><span>{L_BOT_LANG_EXPLAIN}</span></dt>
<dd>
{% import "form_macros.twig" as form_macros %}
{% import 'macros/form_macros.twig' as form_macros %}
{{ form_macros.select(LANG_OPTIONS) }}
</dd>
</dl>

View file

@ -65,7 +65,7 @@
<!-- ENDIF -->
<fieldset class="quick">
{% from 'form_macros.twig' import select %}
{% from 'macros/form_macros.twig' import select %}
{{ select(INACTIVE_OPTIONS) }}
<input class="button2" type="submit" name="submit" value="{L_SUBMIT}" />
<p class="small"><a href="#" onclick="marklist('inactive', 'mark', true); return false;">{L_MARK_ALL}</a> &bull; <a href="#" onclick="marklist('inactive', 'mark', false); return false;">{L_UNMARK_ALL}</a></p>

View file

@ -43,7 +43,7 @@
<dl>
<dt><label for="{{ LANG_OPTIONS.id }}">{L_BOARD_LANGUAGE}{L_COLON}</label></dt>
<dd>
{% import "form_macros.twig" as form_macros %}
{% import 'macros/form_macros.twig' as form_macros %}
{{ form_macros.select(LANG_OPTIONS) }}
</dd>
</dl>

View file

@ -1,85 +0,0 @@
{% macro input(form_data) %}
{% apply replace({"\n": ' ', '\t': ''}) %}
<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.step %}step="{{ form_data.step }}"{% 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 }}">
{% endapply %}
{% endmacro %}
{% macro dimension(form_data) %}
{{ _self.input(form_data.width) }} x {{ _self.input(form_data.height) }}
{% endmacro %}
{% macro textarea(form_data) %}
{% apply replace({"\n": ' ', '\t': ''}) %}
<textarea
id="{{ form_data.id }}"
name="{{ form_data.name }}"
rows="{{ form_data.rows }}"
cols="{{ form_data.cols }}">
{{ form_data.content }}
</textarea>
{% endapply %}
{% 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 %}
{% macro select(form_data, class, id, name, group_only) %}
{% apply replace({"\n": ' ', '\t': ''}) %}
<select
{% if id %}id="{{ id }}"{% endif %}
{% if class %}class="{{ class }}"{% endif %}
name="{% if name %}{{ name }}{% else %}{{ form_data.name }}{% endif %}"
{% if form_data.toggleable %}data-togglable-settings="true"{% endif %}>
{% endapply %}
{% for element in form_data.options %}
{% if not group_only and element.options %}
{% apply replace({"\n": ' ', '\t': ''}) %}
<optgroup
label="{{ element.label }}"
{% for key, value in element.data %}
data-{{ key }}="{{ value }}"
{% endfor %}>
{% endapply %}
{% for option in element.options %}
<option value="{{ option.value }}"{% if option.selected %} selected="selected"{% endif %}>{{ option.label }}</option>
{% endfor %}
</optgroup>
{% else %}
<option value="{{ element.value }}"{% if element.selected %} selected="selected"{% endif %}>{{ element.label }}</option>
{% endif %}
{% endfor %}
</select>
{% endmacro %}
{% macro build_template(form_data) %}
{% if form_data.tag == 'input' %}
{{ _self.input(form_data) }}
{% elseif form_data.tag == 'dimension' %}
{{ _self.dimension(form_data) }}
{% elseif form_data.tag == 'radio' %}
{{ _self.radio_buttons(form_data) }}
{% elseif form_data.tag == 'select' %}
{{ _self.select(form_data) }}
{% elseif form_data.tag == 'textarea' %}
{{ _self.textarea(form_data) }}
{% elseif form_data[0] %}
{% for element in form_data %}
{{ _self.build_template(element) }}
{% endfor %}
{% endif %}
{% if form_data.append %}{{ form_data.append }}{% endif %}
{% endmacro %}

View file

@ -1,5 +1,5 @@
<dl>
{% from "form_macros.twig" import select %}
{% from "macros/form_macros.twig" import select %}
<dt><label for="timezone">{{ lang('BOARD_TIMEZONE') | lang('COLON') }}</label></dt>
{% if TIMEZONE_OPTIONS %}
<dd id="tz_select_date hidden">

View file

@ -485,9 +485,10 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
if ($tpl_type[0] == 'select')
{
$size = (isset($tpl_type[1])) ? (int)$tpl_type[1] : 1;
if (is_string($return))
{
$size = (isset($tpl_type[1])) ? (int)$tpl_type[1] : 1;
$data_toggle = (!empty($tpl_type[2])) ? ' data-togglable-settings="true"' : '';
$tpl = '<select id="' . $key . '" name="' . $name . '"' . (($size > 1) ? ' size="' . $size . '"' : '') . $data_toggle . '>' . $return . '</select>';
@ -501,6 +502,12 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
'toggleable' => !empty($tpl_type[2]),
'options' => $return,
];
// Add size if it differs from default value of 1
if ($size != 1)
{
$tpl['size'] = $size;
}
}
}
else

View file

@ -43,7 +43,8 @@
{% if id %}id="{{ id }}"{% endif %}
{% if class %}class="{{ class }}"{% endif %}
name="{% if name %}{{ name }}{% else %}{{ form_data.name }}{% endif %}"
{% if form_data.toggleable %}data-togglable-settings="true"{% endif %}>
{% if form_data.toggleable %}data-togglable-settings="true"{% endif %}
{% if form_data.size %}size="{{ form_data.size }}"{% endif %}>
{% endapply %}
{% for element in form_data.options %}
{% if not group_only and element.options %}

View file

@ -74,11 +74,12 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case
*/
public function test_build_cfg_template_text($tpl_type, $key, $new, $config_key, $vars, $expected)
{
global $user, $phpbb_dispatcher;
global $user, $phpbb_dispatcher, $language;
$phpbb_dispatcher = new phpbb_mock_event_dispatcher();
$user = new phpbb_mock_user();
$user->lang = new phpbb_mock_lang();
$language = new phpbb_mock_lang();
$user->lang = $language;
$this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars));
}
@ -421,24 +422,69 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case
public function build_cfg_template_select_data()
{
return array(
array(
array('select'),
return [
[
['select'],
'key_name',
array('config_key_name' => '0'),
['config_key_name' => '0'],
'config_key_name',
array('method' => 'select_helper'),
'<select id="key_name" name="config[config_key_name]"><option value="1">First_Option</option><option value="2" selected="selected">Second_Option</option><option value="3">Third_Option</option></select>',
),
array(
array('select', 8),
['method' => 'select_helper'],
[
'tag' => 'select',
'id' => 'key_name',
'name' => 'config[config_key_name]',
'options' => [
[
'value' => 1,
'label' => 'First_Option',
'selected' => false,
],
[
'value' => 2,
'label' => 'Second_Option',
'selected' => true,
],
[
'value' => 3,
'label' => 'Third_Option',
'selected' => false,
]
],
'toggleable' => false,
],
],
[
['select', 8],
'key_name',
array('config_key_name' => '1'),
['config_key_name' => '1'],
'config_key_name',
array('method' => 'select_helper'),
'<select id="key_name" name="config[config_key_name]" size="8"><option value="1">First_Option</option><option value="2" selected="selected">Second_Option</option><option value="3">Third_Option</option></select>',
),
);
['method' => 'select_helper'],
[
'tag' => 'select',
'id' => 'key_name',
'name' => 'config[config_key_name]',
'size' => 8,
'options' => [
[
'value' => 1,
'label' => 'First_Option',
'selected' => false,
],
[
'value' => 2,
'label' => 'Second_Option',
'selected' => true,
],
[
'value' => 3,
'label' => 'Third_Option',
'selected' => false,
]
],
'toggleable' => false,
],
],
];
}
/**
@ -446,11 +492,12 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case
*/
public function test_build_cfg_template_select($tpl_type, $key, $new, $config_key, $vars, $expected)
{
global $module, $user, $phpbb_dispatcher;
global $module, $user, $phpbb_dispatcher, $language;
$phpbb_dispatcher = new phpbb_mock_event_dispatcher();
$user = new phpbb_mock_user();
$user->lang = new phpbb_mock_lang();
$language = new phpbb_mock_lang();
$user->lang = $language;
$user->module = $this;
$module = $user;

View file

@ -34,7 +34,18 @@ class phpbb_functions_acp_built_select_test extends phpbb_test_case
'second' => 'SEC_OPTION',
),
false,
'<option value="test">TEST</option><option value="second">SEC_OPTION</option>',
[
[
'value' => 'test',
'label' => 'TEST',
'selected' => false,
],
[
'value' => 'second',
'label' => 'SEC_OPTION',
'selected' => false,
],
],
),
array(
array(
@ -42,7 +53,18 @@ class phpbb_functions_acp_built_select_test extends phpbb_test_case
'second' => 'SEC_OPTION',
),
'test',
'<option value="test" selected="selected">TEST</option><option value="second">SEC_OPTION</option>',
[
[
'value' => 'test',
'label' => 'TEST',
'selected' => true,
],
[
'value' => 'second',
'label' => 'SEC_OPTION',
'selected' => false,
],
],
),
array(
array(
@ -50,7 +72,18 @@ class phpbb_functions_acp_built_select_test extends phpbb_test_case
'second' => 'SEC_OPTION',
),
'second',
'<option value="test">TEST</option><option value="second" selected="selected">SEC_OPTION</option>',
[
[
'value' => 'test',
'label' => 'TEST',
'selected' => false,
],
[
'value' => 'second',
'label' => 'SEC_OPTION',
'selected' => true,
],
],
),
);
}
@ -60,6 +93,10 @@ class phpbb_functions_acp_built_select_test extends phpbb_test_case
*/
public function test_build_select($option_ary, $option_default, $expected)
{
global $language;
$language = new phpbb_mock_lang();
$this->assertEquals($expected, build_select($option_ary, $option_default));
}
}