[ticket/17151] Improve radio input type handling

- allow more than 2 buttons count
- allow custom buttons order
- allow custom button labels

Implemented by allowing JSON data format, backward compatibility preserved.

PHPBB3-17151
This commit is contained in:
rxu 2023-09-20 17:36:46 +07:00
parent 7f365855ce
commit 1cbe1d86da
No known key found for this signature in database
GPG key ID: 955F0567380E586A
5 changed files with 49 additions and 32 deletions

View file

@ -270,6 +270,12 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
$new_ary[$config_key] = '';
}
// For BC check if parameter is json format and if yes split $tpl_type in 2 parts only
if (isset($tpl_type[1]) && strpos($tpl_type[1], '{') !== false)
{
$tpl_type = explode(':', $vars['type'], 2);
}
switch ($tpl_type[0])
{
case 'password':
@ -369,35 +375,37 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
break;
case 'radio':
$tpl_type_cond = explode('_', $tpl_type[1]);
$type_no = $tpl_type_cond[0] != 'disabled' && $tpl_type_cond[0] != 'enabled';
$no_button = [
'type' => 'radio',
'name' => $name,
'value' => 0,
'checked' => !$new_ary[$config_key],
'label' => $type_no ? $language->lang('NO') : $language->lang('DISABLED'),
];
$yes_button = [
'id' => $key,
'type' => 'radio',
'name' => $name,
'value' => 1,
'checked' => (bool) $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')
// Convert 'old' radio button parameters to json encoded string for BC
if (in_array($tpl_type[1], ['yes_no', 'enabled_disabled']))
{
$tpl['buttons'] = [$yes_button, $no_button];
$params = explode('_', $tpl_type[1]);
$tpl_type[1] = '{"' . $params[0] . '":1, "' . $params[1] . '":0}';
}
else
$params = json_decode($tpl_type[1], true);
$id_assigned = false;
$buttons = [];
foreach ($params as $param => $value)
{
$tpl['buttons'] = [$no_button, $yes_button];
}
$buttons[] = [
'type' => 'radio',
'name' => $name,
'value' => $value,
'checked' => $new_ary[$config_key] == $value,
'label' => $language->lang(strtoupper($param)),
];
// Only assign id to the one (1st) button in the list
if (!$id_assigned)
{
$buttons[key($buttons)]['id'] = $key;
}
};
$tpl = [
'tag' => 'radio',
'buttons' => $buttons,
];
break;
case 'button':

View file

@ -151,10 +151,7 @@ class forms extends AbstractExtension
try
{
return $environment->render('macros/forms/radio_buttons.twig', [
'FIRST_BUTTON' => $form_data['buttons'][0],
'FIRST_BUTTON_LABEL' => $form_data['buttons'][0]['label'],
'SECOND_BUTTON' => $form_data['buttons'][1],
'SECOND_BUTTON_LABEL' => $form_data['buttons'][1]['label'],
'BUTTONS' => $form_data['buttons'],
]);
}
catch (\Twig\Error\Error $e)

View file

@ -1,2 +1,3 @@
<label>{{ FormsInput(FIRST_BUTTON) ~ FIRST_BUTTON_LABEL }}</label>
<label>{{ FormsInput(SECOND_BUTTON) ~ SECOND_BUTTON_LABEL }}</label>
{% for button in BUTTONS %}
<label>{{ FormsInput(button) ~ button.label }}</label>
{% endfor %}

View file

@ -101,6 +101,16 @@ class phpbb_functional_extension_module_test extends phpbb_functional_test_case
$this->assertStringContainsString('SETTING_9', $crawler->filter('dl')->eq(9)->filter('dt > label[for="setting_9"]')->text());
$this->assertStringContainsString('SETTING_9_EXPLAIN', $crawler->filter('dl')->eq(9)->filter('dt > span')->text());
$this->assertEquals(2, $crawler->filter('dl')->eq(9)->filter('dd > label > input[type="radio"]')->count());
$this->assertStringContainsString('SETTING_10', $crawler->filter('dl')->eq(10)->filter('dt > label[for="setting_10"]')->text());
$this->assertStringContainsString('SETTING_10_EXPLAIN', $crawler->filter('dl')->eq(10)->filter('dt > span')->text());
$this->assertEquals(3, $crawler->filter('dl')->eq(10)->filter('dd > label > input[type="radio"]')->count());
$this->assertEquals(1, $crawler->filter('dl')->eq(10)->filter('dd > label > input[type="radio"]')->eq(0)->attr('value'));
$this->assertStringContainsString('LABEL_1', $crawler->filter('dl')->eq(10)->filter('dd > label')->eq(0)->text());
$this->assertEquals(3, $crawler->filter('dl')->eq(10)->filter('dd > label > input[type="radio"]')->eq(1)->attr('value'));
$this->assertStringContainsString('LABEL_3', $crawler->filter('dl')->eq(10)->filter('dd > label')->eq(1)->text());
$this->assertEquals(2, $crawler->filter('dl')->eq(10)->filter('dd > label > input[type="radio"]')->eq(2)->attr('value'));
$this->assertStringContainsString('LABEL_2', $crawler->filter('dl')->eq(10)->filter('dd > label')->eq(2)->text());
}
public function test_ucp()

View file

@ -49,6 +49,7 @@ class main_module
'setting_7' => ['lang' => 'SETTING_7', 'validate' => 'email', 'type' => 'email:0:100', 'explain' => true],
'setting_8' => ['lang' => 'SETTING_8', 'validate' => 'string', 'type' => 'textarea:5:30', 'explain' => true],
'setting_9' => ['lang' => 'SETTING_9', 'validate' => 'bool', 'type' => 'radio:enabled_disabled', 'explain' => true],
'setting_10'=> ['lang' => 'SETTING_10', 'validate' => 'bool', 'type' => 'radio:{"LABEL_1":1, "LABEL_3":3, "LABEL_2":2}', 'explain' => true],
]
];