[ticket/17151] Reimplement radio buttons yes_no / enabled_disabled shorthands

PHPBB3-17151
This commit is contained in:
rxu 2023-09-24 21:43:54 +07:00
parent 2d0969e3e1
commit 6349d3c126
No known key found for this signature in database
GPG key ID: 955F0567380E586A
3 changed files with 47 additions and 29 deletions

View file

@ -374,25 +374,25 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
break;
case 'radio':
if (!isset($vars['method']) && !isset($vars['function']))
{
if (in_array($tpl_type[1], ['yes_no', 'enabled_disabled']))
{
$options = array_reverse(explode('_', strtoupper($tpl_type[1])));
krsort($options);
$tpl_type = array_merge ($tpl_type, build_radio($new_ary[$config_key], $config_key, $options));
}
}
case 'button':
case 'select':
case 'custom':
if (isset($vars['method']))
{
$call = array($module->module, $vars['method']);
}
else if (isset($vars['function']))
{
$call = $vars['function'];
}
else
{
break;
}
$args = [];
$call = $vars['function'] ?? (isset($vars['method']) ? [$module->module, $vars['method']] : false);
if ($call)
{
if (isset($vars['params']))
{
$args = array();
foreach ($vars['params'] as $value)
{
switch ($value)
@ -413,8 +413,9 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars)
{
$args = array($new_ary[$config_key], $config_key);
}
}
$return = call_user_func_array($call, $args);
$return = $call ? call_user_func_array($call, $args) : [];
if (in_array($tpl_type[0], ['select', 'radio', 'button']))
{

View file

@ -54,6 +54,7 @@ class phpbb_functional_extension_module_test extends phpbb_functional_test_case
public function test_acp()
{
$this->add_lang('common');
$this->login();
$this->admin_login();
@ -111,6 +112,20 @@ class phpbb_functional_extension_module_test extends phpbb_functional_test_case
$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());
$this->assertStringContainsString('SETTING_11', $crawler->filter('dl')->eq(11)->filter('dt > label[for="setting_11"]')->text());
$this->assertStringContainsString('SETTING_11_EXPLAIN', $crawler->filter('dl')->eq(11)->filter('dt > span')->text());
$this->assertEquals('1', $crawler->filter('dl')->eq(11)->filter('dd > label > input[type="radio"]')->eq(0)->attr('value'));
$this->assertEquals('0', $crawler->filter('dl')->eq(11)->filter('dd > label > input[type="radio"]')->eq(1)->attr('value'));
$this->assertContainsLang('YES', $crawler->filter('dl')->eq(11)->filter('dd > label')->eq(0)->text());
$this->assertContainsLang('NO', $crawler->filter('dl')->eq(11)->filter('dd > label')->eq(1)->text());
$this->assertStringContainsString('SETTING_12', $crawler->filter('dl')->eq(12)->filter('dt > label[for="setting_12"]')->text());
$this->assertStringContainsString('SETTING_12_EXPLAIN', $crawler->filter('dl')->eq(12)->filter('dt > span')->text());
$this->assertContainsLang('ENABLED', $crawler->filter('dl')->eq(12)->filter('dd > label')->eq(0)->text());
$this->assertEquals(1, $crawler->filter('dl')->eq(12)->filter('dd > label > input[type="radio"]')->eq(0)->attr('value'));
$this->assertContainsLang('DISABLED', $crawler->filter('dl')->eq(12)->filter('dd > label')->eq(1)->text());
$this->assertEquals(0, $crawler->filter('dl')->eq(12)->filter('dd > label > input[type="radio"]')->eq(1)->attr('value'));
}
public function test_ucp()

View file

@ -50,6 +50,8 @@ class main_module
'setting_8' => ['lang' => 'SETTING_8', 'validate' => 'string', 'type' => 'textarea:5:30', 'explain' => true],
'setting_9' => ['lang' => 'SETTING_9', 'validate' => 'bool', 'type' => 'radio', 'function' => 'build_radio', 'params' => ['{CONFIG_VALUE}', '{KEY}', [1 => 'ENABLED', 0 => 'DISABLED']], 'explain' => true],
'setting_10'=> ['lang' => 'SETTING_10', 'validate' => 'bool', 'type' => 'radio', 'function' => 'build_radio', 'params' => ['{CONFIG_VALUE}', '{KEY}', [1 => 'LABEL_1', 3 => 'LABEL_3', 2 => 'LABEL_2']], 'explain' => true],
'setting_11'=> ['lang' => 'SETTING_11', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => true],
'setting_12'=> ['lang' => 'SETTING_12', 'validate' => 'bool', 'type' => 'radio:enabled_disabled', 'explain' => true],
]
];