From 97933380bb5706a1bedb9e1e1610f343b99be0cb Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 21 Feb 2022 22:37:54 +0100 Subject: [PATCH 01/27] [ticket/17100] Move legend generation to group helper ... and moved HTML to actual HTML files. PHPBB3-17100 --- phpBB/index.php | 45 +--------------- phpBB/phpbb/group/helper.php | 52 ++++++++++++++++++- .../styles/prosilver/template/index_body.html | 18 ++++++- .../prosilver/template/viewonline_body.html | 17 +++++- phpBB/viewonline.php | 39 +------------- 5 files changed, 85 insertions(+), 86 deletions(-) diff --git a/phpBB/index.php b/phpBB/index.php index d797ae1230..ac6ab8a134 100644 --- a/phpBB/index.php +++ b/phpBB/index.php @@ -86,52 +86,10 @@ if (($mark_notification = $request->variable('mark_notification', 0))) display_forums('', $config['load_moderators']); -$order_legend = ($config['legend_sort_groupname']) ? 'group_name' : 'group_legend'; -// Grab group details for legend display -if ($auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel')) -{ - $sql = 'SELECT group_id, group_name, group_colour, group_type, group_legend - FROM ' . GROUPS_TABLE . ' - WHERE group_legend > 0 - ORDER BY ' . $order_legend . ' ASC'; -} -else -{ - $sql = 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type, g.group_legend - FROM ' . GROUPS_TABLE . ' g - LEFT JOIN ' . USER_GROUP_TABLE . ' ug - ON ( - g.group_id = ug.group_id - AND ug.user_id = ' . $user->data['user_id'] . ' - AND ug.user_pending = 0 - ) - WHERE g.group_legend > 0 - AND (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $user->data['user_id'] . ') - ORDER BY g.' . $order_legend . ' ASC'; -} -$result = $db->sql_query($sql); - /** @var \phpbb\group\helper $group_helper */ $group_helper = $phpbb_container->get('group_helper'); -$legend = array(); -while ($row = $db->sql_fetchrow($result)) -{ - $colour_text = ($row['group_colour']) ? ' style="color:#' . $row['group_colour'] . '"' : ''; - $group_name = $group_helper->get_name($row['group_name']); - - if ($row['group_name'] == 'BOTS' || ($user->data['user_id'] != ANONYMOUS && !$auth->acl_get('u_viewprofile'))) - { - $legend[] = '' . $group_name . ''; - } - else - { - $legend[] = '' . $group_name . ''; - } -} -$db->sql_freeresult($result); - -$legend = implode($user->lang['COMMA_SEPARATOR'], $legend); +$group_helper->display_legend($db, $template); // Generate birthday list if required ... $show_birthdays = ($config['load_birthdays'] && $config['allow_birthdays'] && $auth->acl_gets('u_viewprofile', 'a_user', 'a_useradd', 'a_userdel')); @@ -219,7 +177,6 @@ $template->assign_vars(array( 'TOTAL_USERS' => $user->lang('TOTAL_USERS', (int) $config['num_users']), 'NEWEST_USER' => $user->lang('NEWEST_USER', get_username_string('full', $config['newest_user_id'], $config['newest_username'], $config['newest_user_colour'])), - 'LEGEND' => $legend, 'BIRTHDAY_LIST' => (empty($birthday_list)) ? '' : implode($user->lang['COMMA_SEPARATOR'], $birthday_list), 'S_LOGIN_ACTION' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=login'), diff --git a/phpBB/phpbb/group/helper.php b/phpBB/phpbb/group/helper.php index c56c08c0a4..4098846709 100644 --- a/phpBB/phpbb/group/helper.php +++ b/phpBB/phpbb/group/helper.php @@ -17,9 +17,11 @@ use phpbb\auth\auth; use phpbb\avatar\helper as avatar_helper; use phpbb\cache\service as cache; use phpbb\config\config; +use phpbb\db\driver\driver_interface; use phpbb\language\language; use phpbb\event\dispatcher_interface; use phpbb\path_helper; +use phpbb\template\template; use phpbb\user; class helper @@ -294,8 +296,56 @@ class helper * * @return array Avatar data */ - function get_avatar($group_row, $alt = 'GROUP_AVATAR', $ignore_config = false, $lazy = false) + public function get_avatar($group_row, $alt = 'GROUP_AVATAR', $ignore_config = false, $lazy = false) { return $this->avatar_helper->get_group_avatar($group_row, $alt, $ignore_config, $lazy); } + + /** + * Display groups legend + * + * @param driver_interface $db + * @param template $template + * @return void + */ + public function display_legend(driver_interface $db, template $template): void + { + $order_legend = $this->config['legend_sort_groupname'] ? 'group_name' : 'group_legend'; + + // Grab group details for legend display + if ($this->auth->acl_gets('a_group', 'a_groupadd', 'a_groupdel')) + { + $sql = 'SELECT group_id, group_name, group_colour, group_type, group_legend + FROM ' . GROUPS_TABLE . ' + WHERE group_legend > 0 + ORDER BY ' . $order_legend . ' ASC'; + } + else + { + $sql = 'SELECT g.group_id, g.group_name, g.group_colour, g.group_type, g.group_legend + FROM ' . GROUPS_TABLE . ' g + LEFT JOIN ' . USER_GROUP_TABLE . ' ug + ON ( + g.group_id = ug.group_id + AND ug.user_id = ' . $this->user->data['user_id'] . ' + AND ug.user_pending = 0 + ) + WHERE g.group_legend > 0 + AND (g.group_type <> ' . GROUP_HIDDEN . ' OR ug.user_id = ' . $this->user->data['user_id'] . ') + ORDER BY g.' . $order_legend . ' ASC'; + } + $result = $db->sql_query($sql); + + while ($row = $db->sql_fetchrow($result)) + { + $show_group_url = $row['group_name'] != 'BOTS' && $this->auth->acl_get('u_viewprofile'); + + $template->assign_block_vars('LEGEND', [ + 'GROUP_COLOR' => $row['group_colour'] ?: '', + 'GROUP_NAME' => $this->get_name($row['group_name']), + 'GROUP_URL' => $show_group_url ? append_sid("{$this->path_helper->get_phpbb_root_path()}memberlist.{$this->path_helper->get_php_ext()}", 'mode=group&g=' . $row['group_id']) : '', + ]); + } + $db->sql_freeresult($result); + } } diff --git a/phpBB/styles/prosilver/template/index_body.html b/phpBB/styles/prosilver/template/index_body.html index 4b41e2b83c..7b69b92a5f 100644 --- a/phpBB/styles/prosilver/template/index_body.html +++ b/phpBB/styles/prosilver/template/index_body.html @@ -41,10 +41,24 @@

{L_WHO_IS_ONLINE}

{L_WHO_IS_ONLINE}

- {TOTAL_USERS_ONLINE} ({L_ONLINE_EXPLAIN})
{RECORD_USERS}
+ {TOTAL_USERS_ONLINE} ({L_ONLINE_EXPLAIN})
{RECORD_USERS}

{LOGGED_IN_USER_LIST} -
{L_LEGEND}{L_COLON} {LEGEND} + {% if LEGEND|length > 0 %} + {% apply spaceless %} +
+ + {{ lang('LEGEND') ~ lang('COLON') }} + {% for group in LEGEND %} + {% if group.GROUP_URL %} + {{ group.GROUP_NAME }}{% if not loop.last %}, {% endif %} + {% else %} + {{ group.GROUP_NAME }}{% if not loop.last %}, {% endif %} + {% endif %} + {% endfor %} + + {% endapply %} + {% endif %}

diff --git a/phpBB/styles/prosilver/template/viewonline_body.html b/phpBB/styles/prosilver/template/viewonline_body.html index c019977179..acbfb71ba5 100644 --- a/phpBB/styles/prosilver/template/viewonline_body.html +++ b/phpBB/styles/prosilver/template/viewonline_body.html @@ -47,7 +47,22 @@ -

{L_LEGEND}{L_COLON} {LEGEND}

+{% if LEGEND|length > 0 %} + {% apply spaceless %} +

+ + {{ lang('LEGEND') ~ lang('COLON') }} + {% for group in LEGEND %} + {% if group.GROUP_URL %} + {{ group.GROUP_NAME }}{% if not loop.last %}, {% endif %} + {% else %} + {{ group.GROUP_NAME }}{% if not loop.last %}, {% endif %} + {% endif %} + {% endfor %} + +

+ {% endapply %} +{% endif %}
+{% import "form_macros.twig" as form_macros %} @@ -27,7 +28,20 @@

{options.TITLE_EXPLAIN}
-
{options.CONTENT}
+
+ {% 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 %} +
diff --git a/phpBB/adm/style/form_macros.twig b/phpBB/adm/style/form_macros.twig new file mode 100644 index 0000000000..847b8b5f07 --- /dev/null +++ b/phpBB/adm/style/form_macros.twig @@ -0,0 +1,35 @@ +{% macro input(form_data) %} + +{% endmacro %} + +{% macro dimension(form_data) %} + {{ _self.input(form_data.width) }} x {{ _self.input(form_data.height) }} +{% endmacro %} + +{% macro textarea(form_data) %} + +{% endmacro %} + +{% macro radio_buttons(form_data) %} + + +{% endmacro %} diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php index 9a1d4b6a70..ff7673155c 100644 --- a/phpBB/includes/functions_acp.php +++ b/phpBB/includes/functions_acp.php @@ -243,13 +243,20 @@ function h_radio($name, $input_ary, $input_default = false, $id = false, $key = } /** -* Build configuration template for acp configuration pages -*/ -function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars) + * HTML-less version of build_cfg_template + * + * @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 . ']'; // 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 $new_ary[$config_key] = '********'; } + // no break + case 'text': case 'url': case 'email': @@ -276,7 +285,15 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars) $size = (int) $tpl_type[1]; $maxlength = (int) $tpl_type[2]; - $tpl = ''; + $tpl = [ + 'tag' => 'input', + 'id' => $key, + 'type' => $tpl_type[0], + 'name' => $name, + 'size' => $size ?: '', + 'maxlength' => $maxlength ?: 255, + 'value' => $new_ary[$config_key], + ]; break; case 'color': @@ -284,7 +301,13 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars) case 'datetime-local': case 'month': case 'week': - $tpl = ''; + $tpl = [ + 'tag' => 'input', + 'id' => $key, + 'type' => $tpl_type[0], + 'name' => $name, + 'value' => $new_ary[$config_key], + ]; break; 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; $max = isset($tpl_type[2]) ? (int) $tpl_type[2] : false; - $tpl = ''; + $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; case 'dimension': $min = isset($tpl_type[1]) ? (int) $tpl_type[1] : false; $max = isset($tpl_type[2]) ? (int) $tpl_type[2] : false; - $tpl = ' x '; + $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; case 'textarea': - $rows = (int) $tpl_type[1]; - $cols = (int) $tpl_type[2]; - - $tpl = ''; + $tpl = [ + 'tag' => 'textarea', + 'id' => $key, + 'name' => $name, + 'rows' => (int) $tpl_type[1], + 'cols' => (int) $tpl_type[2], + 'content' => $new_ary[$config_key], + ]; break; 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]); - $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 = ''; - $tpl_yes = ''; + $no_button = [ + '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; case 'select': @@ -386,7 +500,14 @@ function build_cfg_template($tpl_type, $key, &$new_ary, $config_key, $vars) if (isset($vars['append'])) { - $tpl .= $vars['append']; + if (is_array($tpl)) + { + $tpl['append'] = $vars['append']; + } + else + { + $tpl .= $vars['append']; + } } $new = $new_ary; From 516d5313ad21b9e3585349744d99855488d5bc86 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 18 Apr 2022 14:27:38 +0200 Subject: [PATCH 07/27] [ticket/17100] Update tests for changed build_cfg_template PHPBB3-17100 --- phpBB/includes/functions_acp.php | 2 +- .../functions_acp/build_cfg_template_test.php | 206 ++++++++++++++++-- 2 files changed, 190 insertions(+), 18 deletions(-) diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php index ff7673155c..b6c9c7b39d 100644 --- a/phpBB/includes/functions_acp.php +++ b/phpBB/includes/functions_acp.php @@ -380,7 +380,7 @@ function phpbb_build_cfg_template(array $tpl_type, string $key, &$new_ary, $conf 'type' => 'radio', 'name' => $name, 'value' => 1, - 'checked' => $new_ary[$config_key], + 'checked' => (bool) $new_ary[$config_key], 'label' => $type_no ? $language->lang('YES') : $language->lang('ENABLED'), ]; diff --git a/tests/functions_acp/build_cfg_template_test.php b/tests/functions_acp/build_cfg_template_test.php index 339a9a1d61..9d3b244bc0 100644 --- a/tests/functions_acp/build_cfg_template_test.php +++ b/tests/functions_acp/build_cfg_template_test.php @@ -24,7 +24,15 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => '1'), 'config_key_name', array(), - '', + [ + 'tag' => 'input', + 'id' => 'key_name', + 'type' => 'text', + 'name' => 'config[config_key_name]', + 'size' => 20, + 'maxlength' => 255, + 'value' => '1', + ], ), array( array('password', 20, 128), @@ -32,7 +40,15 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => '2'), 'config_key_name', array(), - '', + [ + 'tag' => 'input', + 'id' => 'key_name', + 'type' => 'password', + 'name' => 'config[config_key_name]', + 'size' => 20, + 'maxlength' => 128, + 'value' => '********', + ], ), array( array('text', 0, 255), @@ -40,7 +56,15 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => '3'), 'config_key_name', array(), - '', + [ + 'tag' => 'input', + 'id' => 'key_name', + 'type' => 'text', + 'name' => 'config[config_key_name]', + 'maxlength' => 255, + 'value' => '3', + 'size' => '', + ], ), ); } @@ -68,7 +92,24 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name_width' => 10, 'config_key_name_height' => 20), 'config_key_name', array(), - ' x ', + [ + 'tag' => 'dimension', + 'width' => [ + 'id' => 'number_key_name', + 'type' => 'number', + 'name' => 'config[config_key_name_width]', + 'min' => 5, + 'max' => 15, + 'value' => 10, + ], + 'height' => [ + 'type' => 'number', + 'name' => 'config[config_key_name_height]', + 'min' => 5, + 'max' => 15, + 'value' => 20, + ], + ], ), array( array('dimension', 0, 15), @@ -76,7 +117,24 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name_width' => 10, 'config_key_name_height' => 20), 'config_key_name', array(), - ' x ', + [ + 'tag' => 'dimension', + 'width' => [ + 'id' => 'number_key_name', + 'type' => 'number', + 'name' => 'config[config_key_name_width]', + 'min' => 0, + 'max' => 15, + 'value' => 10, + ], + 'height' => [ + 'type' => 'number', + 'name' => 'config[config_key_name_height]', + 'min' => 0, + 'max' => 15, + 'value' => 20, + ], + ], ), ); } @@ -104,7 +162,15 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => 10), 'config_key_name', array(), - '', + [ + 'tag' => 'input', + 'id' => 'number_key_name', + 'type' => 'number', + 'name' => 'config[config_key_name]', + 'min' => 5, + 'max' => 15, + 'value' => 10, + ], ), array( array('number', -1, 9999), @@ -112,7 +178,15 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => 10), 'config_key_name', array(), - '', + [ + 'tag' => 'input', + 'id' => 'number_key_name', + 'type' => 'number', + 'name' => 'config[config_key_name]', + 'min' => -1, + 'max' => 9999, + 'value' => 10, + ], ), array( array('number', 0, 9999), @@ -120,7 +194,15 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => 10), 'config_key_name', array(), - '', + [ + 'tag' => 'input', + 'id' => 'number_key_name', + 'type' => 'number', + 'name' => 'config[config_key_name]', + 'min' => 0, + 'max' => 9999, + 'value' => 10, + ], ), ); } @@ -148,7 +230,14 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => 'phpBB'), 'config_key_name', array(), - '', + [ + 'tag' => 'textarea', + 'id' => 'key_name', + 'name' => 'config[config_key_name]', + 'rows' => 5, + 'cols' => 30, + 'content' => 'phpBB', + ] ), ); } @@ -176,7 +265,26 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => '0'), 'config_key_name', array(), - '', + [ + 'tag' => 'radio', + 'buttons' => [ + [ + 'id' => 'key_name', + 'type' => 'radio', + 'value' => 1, + 'name' => 'config[config_key_name]', + 'label' => 'ENABLED', + 'checked' => false, + ], + [ + 'type' => 'radio', + 'value' => 0, + 'checked' => true, + 'name' => 'config[config_key_name]', + 'label' => 'DISABLED', + ], + ], + ], ), array( array('radio', 'enabled_disabled'), @@ -184,7 +292,26 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => '1'), 'config_key_name', array(), - '', + [ + 'tag' => 'radio', + 'buttons' => [ + [ + 'id' => 'key_name', + 'type' => 'radio', + 'value' => 1, + 'name' => 'config[config_key_name]', + 'label' => 'ENABLED', + 'checked' => true, + ], + [ + 'type' => 'radio', + 'value' => 0, + 'checked' => false, + 'name' => 'config[config_key_name]', + 'label' => 'DISABLED', + ], + ], + ], ), array( array('radio', 'yes_no'), @@ -192,7 +319,26 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => '0'), 'config_key_name', array(), - '', + [ + 'tag' => 'radio', + 'buttons' => [ + [ + 'id' => 'key_name', + 'type' => 'radio', + 'value' => 1, + 'name' => 'config[config_key_name]', + 'label' => 'YES', + 'checked' => false, + ], + [ + 'type' => 'radio', + 'value' => 0, + 'checked' => true, + 'name' => 'config[config_key_name]', + 'label' => 'NO', + ], + ], + ], ), array( array('radio', 'yes_no'), @@ -200,7 +346,26 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => '1'), 'config_key_name', array(), - '', + [ + 'tag' => 'radio', + 'buttons' => [ + [ + 'id' => 'key_name', + 'type' => 'radio', + 'value' => 1, + 'name' => 'config[config_key_name]', + 'label' => 'YES', + 'checked' => true, + ], + [ + 'type' => 'radio', + 'value' => 0, + 'checked' => false, + 'name' => 'config[config_key_name]', + 'label' => 'NO', + ], + ], + ], ), ); } @@ -210,11 +375,10 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case */ public function test_build_cfg_template_radio($tpl_type, $key, $new, $config_key, $vars, $expected) { - global $user, $phpbb_dispatcher; + global $language, $phpbb_dispatcher; $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); - $user = new phpbb_mock_user(); - $user->lang = new phpbb_mock_lang(); + $language = new \phpbb_mock_lang(); $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); } @@ -228,7 +392,15 @@ class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case array('config_key_name' => 'phpBB'), 'config_key_name', array('append' => 'Bertie is cool!'), - 'Bertie is cool!', + [ + 'tag' => 'textarea', + 'id' => 'key_name', + 'name' => 'config[config_key_name]', + 'rows' => 5, + 'cols' => 30, + 'content' => 'phpBB', + 'append' => 'Bertie is cool!', + ] ), ); } From 2dfe5ebe6d5c5e1a8a7329b4019d7c26a9f7353a Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 18 Apr 2022 20:10:02 +0200 Subject: [PATCH 08/27] [ticket/17100] Refactor code to be more reusable PHPBB3-17100 --- phpBB/adm/style/acp_attachments.html | 11 +--- phpBB/adm/style/acp_board.html | 11 +--- phpBB/adm/style/acp_bots.html | 9 +-- phpBB/adm/style/acp_users_prefs.html | 9 +-- phpBB/adm/style/form_macros.twig | 23 +++++++ phpBB/includes/acp/acp_bots.php | 10 ++- phpBB/includes/acp/acp_users.php | 7 ++- phpBB/includes/functions.php | 15 +++-- phpBB/includes/ucp/ucp_prefs.php | 7 ++- phpBB/includes/ucp/ucp_register.php | 19 +++++- phpBB/phpbb/message/topic_form.php | 7 ++- .../all/template/macros/form_macros.twig | 58 +++++++++++++++++ .../prosilver/template/ucp_agreement.html | 13 ++-- .../template/ucp_prefs_personal.html | 9 +-- .../prosilver/template/ucp_register.html | 20 +++--- tests/functions/language_select_test.php | 62 ++++++++----------- 16 files changed, 189 insertions(+), 101 deletions(-) create mode 100644 phpBB/styles/all/template/macros/form_macros.twig diff --git a/phpBB/adm/style/acp_attachments.html b/phpBB/adm/style/acp_attachments.html index 77a89ff571..78d937b967 100644 --- a/phpBB/adm/style/acp_attachments.html +++ b/phpBB/adm/style/acp_attachments.html @@ -51,15 +51,8 @@

{options.TITLE_EXPLAIN}
- {% 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 %} + {% if options.CONTENT is iterable %} + {{ form_macros.build_template(options.CONTENT)}} {% else %} {options.CONTENT} {% endif %} diff --git a/phpBB/adm/style/acp_board.html b/phpBB/adm/style/acp_board.html index 3cc5d2dea2..8d539de10c 100644 --- a/phpBB/adm/style/acp_board.html +++ b/phpBB/adm/style/acp_board.html @@ -29,15 +29,8 @@

{options.TITLE_EXPLAIN}
- {% 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 %} + {% if options.CONTENT is iterable %} + {{ form_macros.build_template(options.CONTENT)}} {% else %} {{ options.CONTENT }} {% endif %} diff --git a/phpBB/adm/style/acp_bots.html b/phpBB/adm/style/acp_bots.html index 603d7c0ab0..94103e0ec9 100644 --- a/phpBB/adm/style/acp_bots.html +++ b/phpBB/adm/style/acp_bots.html @@ -30,13 +30,10 @@
-

{L_BOT_LANG_EXPLAIN}
+

{L_BOT_LANG_EXPLAIN}
- + {% import "form_macros.twig" as form_macros %} + {{ form_macros.select(LANG_OPTIONS) }}
diff --git a/phpBB/adm/style/acp_users_prefs.html b/phpBB/adm/style/acp_users_prefs.html index cdac6891e5..0cf21e19a6 100644 --- a/phpBB/adm/style/acp_users_prefs.html +++ b/phpBB/adm/style/acp_users_prefs.html @@ -41,13 +41,10 @@
-
+
- + {% import "form_macros.twig" as form_macros %} + {{ form_macros.select(LANG_OPTIONS) }}
diff --git a/phpBB/adm/style/form_macros.twig b/phpBB/adm/style/form_macros.twig index 847b8b5f07..0937a10b4b 100644 --- a/phpBB/adm/style/form_macros.twig +++ b/phpBB/adm/style/form_macros.twig @@ -33,3 +33,26 @@ {% endmacro %} + +{% macro select(form_data) %} + +{% 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[0] %} + {% for element in form_data %} + {{ _self.build_template(element) }} + {% endfor %} + {% endif %} + {% if form_data.append %}{{ form_data.append }}{% endif %} +{% endmacro %} diff --git a/phpBB/includes/acp/acp_bots.php b/phpBB/includes/acp/acp_bots.php index 8c296e180a..323dd1baee 100644 --- a/phpBB/includes/acp/acp_bots.php +++ b/phpBB/includes/acp/acp_bots.php @@ -330,7 +330,7 @@ class acp_bots } $style_select = style_select($bot_row['bot_style'], true); - phpbb_language_select($db, $template, $bot_row['bot_lang']); + $lang_options = phpbb_language_select($db, $bot_row['bot_lang']); $l_title = ($action == 'edit') ? 'EDIT' : 'ADD'; @@ -347,9 +347,13 @@ class acp_bots 'S_EDIT_BOT' => true, 'S_ACTIVE_OPTIONS' => $s_active_options, 'S_STYLE_OPTIONS' => $style_select, + 'LANG_OPTIONS' => [ + 'id' => 'bot_lang', + 'name' => 'bot_lang', + 'options' => $lang_options, + ], 'S_ERROR' => (count($error)) ? true : false, - ) - ); + )); return; diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php index d75b866bd5..9aa3333e77 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1796,7 +1796,7 @@ class acp_users } phpbb_timezone_select($template, $user, $data['tz'], true); - phpbb_language_select($db, $template, $data['lang']); + $lang_options = phpbb_language_select($db, $data['lang']); $user_prefs_data = array( 'S_PREFS' => true, @@ -1833,6 +1833,11 @@ class acp_users 'DEFAULT_DATEFORMAT' => $config['default_dateformat'], 'A_DEFAULT_DATEFORMAT' => addslashes($config['default_dateformat']), + 'LANG_OPTIONS' => [ + 'id' => 'lang', + 'name' => 'lang', + 'options' => $lang_options, + ], 'S_STYLE_OPTIONS' => style_select($data['style']), ); diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 48ac572295..ae8b978c69 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -270,7 +270,7 @@ function phpbb_version_compare(string $version1, string $version2, string $opera * @param string $default Language ISO code to be selected by default in the dropdown list * @param array $langdata Language data in format of array(array('lang_iso' => string, lang_local_name => string), ...) */ -function phpbb_language_select(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, string $default = '', array $langdata = []): void +function phpbb_language_select(\phpbb\db\driver\driver_interface $db, string $default = '', array $langdata = []): array { if (empty($langdata)) { @@ -282,14 +282,17 @@ function phpbb_language_select(\phpbb\db\driver\driver_interface $db, \phpbb\tem $db->sql_freeresult($result); } + $lang_options = []; foreach ($langdata as $row) { - $template->assign_block_vars('lang_options', [ - 'SELECTED' => $row['lang_iso'] == $default, - 'LANG_ISO' => $row['lang_iso'], - 'LANG_LOCAL_NAME' => $row['lang_local_name'] - ]); + $lang_options[] = [ + 'value' => $row['lang_iso'], + 'label' => $row['lang_local_name'], + 'selected' => $row['lang_iso'] === $default, + ]; } + + return $lang_options; } /** diff --git a/phpBB/includes/ucp/ucp_prefs.php b/phpBB/includes/ucp/ucp_prefs.php index 9a51cc65b2..625ade741c 100644 --- a/phpBB/includes/ucp/ucp_prefs.php +++ b/phpBB/includes/ucp/ucp_prefs.php @@ -177,7 +177,7 @@ class ucp_prefs $db->sql_freeresult($result); $s_more_styles = count($styles_row) > 1; - phpbb_language_select($db, $template, $data['lang'], $lang_row); + $lang_options = phpbb_language_select($db, $data['lang'], $lang_row); $template->assign_vars(array( 'ERROR' => (count($error)) ? implode('
', $error) : '', @@ -200,6 +200,11 @@ class ucp_prefs 'S_MORE_LANGUAGES' => $s_more_languages, 'S_MORE_STYLES' => $s_more_styles, + 'LANG_OPTIONS' => [ + 'id' => 'lang', + 'name' => 'lang', + 'options' => $lang_options, + ], 'S_STYLE_OPTIONS' => ($config['override_user_style']) ? '' : style_select($data['user_style'], false, $styles_row), 'S_CAN_HIDE_ONLINE' => ($auth->acl_get('u_hideonline')) ? true : false, 'S_SELECT_NOTIFY' => ($config['jab_enable'] && $user->data['user_jabber'] && @extension_loaded('xml')) ? true : false) diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php index 9601582f51..bdece67890 100644 --- a/phpBB/includes/ucp/ucp_register.php +++ b/phpBB/includes/ucp/ucp_register.php @@ -157,7 +157,7 @@ class ucp_register $lang_row = (array) $db->sql_fetchrowset($result); $db->sql_freeresult($result); - phpbb_language_select($db, $template, $user_lang, $lang_row); + $lang_options = phpbb_language_select($db, $user_lang, $lang_row); if ($coppa === false && $config['coppa_enable']) { @@ -169,6 +169,11 @@ class ucp_register unset($now); $template_vars = array( + 'LANG_OPTIONS' => [ + 'id' => 'lang', + 'name' => 'lang', + 'options' => $lang_options, + ], 'L_COPPA_NO' => $user->lang('UCP_COPPA_BEFORE', $coppa_birthday), 'L_COPPA_YES' => $user->lang('UCP_COPPA_ON_AFTER', $coppa_birthday), @@ -183,6 +188,11 @@ class ucp_register else { $template_vars = array( + 'LANG_OPTIONS' => [ + 'id' => 'lang', + 'name' => 'lang', + 'options' => $lang_options, + ], 'L_TERMS_OF_USE' => sprintf($user->lang['TERMS_OF_USE_CONTENT'], $config['sitename'], generate_board_url()), 'S_SHOW_COPPA' => false, @@ -626,7 +636,7 @@ class ucp_register $lang_row = (array) $db->sql_fetchrowset($result); $db->sql_freeresult($result); - phpbb_language_select($db, $template, $data['lang'], $lang_row); + $lang_options = phpbb_language_select($db, $data['lang'], $lang_row); $template_vars = array( 'USERNAME' => $data['username'], @@ -638,6 +648,11 @@ class ucp_register 'L_USERNAME_EXPLAIN' => $user->lang($config['allow_name_chars'] . '_EXPLAIN', $user->lang('CHARACTERS', (int) $config['min_name_chars']), $user->lang('CHARACTERS', (int) $config['max_name_chars'])), 'L_PASSWORD_EXPLAIN' => $user->lang($config['pass_complex'] . '_EXPLAIN', $user->lang('CHARACTERS', (int) $config['min_pass_chars'])), + 'LANG_OPTIONS' => [ + 'id' => 'lang', + 'name' => 'lang', + 'options' => $lang_options, + ], 'S_TZ_PRESELECT' => !$submit, 'S_CONFIRM_REFRESH' => ($config['enable_confirm'] && $config['confirm_refresh']) ? true : false, 'S_REGISTRATION' => true, diff --git a/phpBB/phpbb/message/topic_form.php b/phpBB/phpbb/message/topic_form.php index befc9791dc..e8344417ef 100644 --- a/phpBB/phpbb/message/topic_form.php +++ b/phpBB/phpbb/message/topic_form.php @@ -154,11 +154,16 @@ class topic_form extends form $this->user->add_lang('viewtopic'); - phpbb_language_select($this->db, $template, $this->recipient_lang); + $lang_options = phpbb_language_select($this->db, $this->recipient_lang); $template->assign_vars(array( 'EMAIL' => $this->recipient_address, 'NAME' => $this->recipient_name, + 'LANG_OPTIONS' => [ + 'id' => 'lang', + 'name' => 'lang', + 'options' => $lang_options, + ], 'MESSAGE' => $this->body, 'L_EMAIL_BODY_EXPLAIN' => $this->user->lang['EMAIL_TOPIC_EXPLAIN'], diff --git a/phpBB/styles/all/template/macros/form_macros.twig b/phpBB/styles/all/template/macros/form_macros.twig new file mode 100644 index 0000000000..0937a10b4b --- /dev/null +++ b/phpBB/styles/all/template/macros/form_macros.twig @@ -0,0 +1,58 @@ +{% macro input(form_data) %} + +{% endmacro %} + +{% macro dimension(form_data) %} + {{ _self.input(form_data.width) }} x {{ _self.input(form_data.height) }} +{% endmacro %} + +{% macro textarea(form_data) %} + +{% endmacro %} + +{% macro radio_buttons(form_data) %} + + +{% endmacro %} + +{% macro select(form_data) %} + +{% 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[0] %} + {% for element in form_data %} + {{ _self.build_template(element) }} + {% endfor %} + {% endif %} + {% if form_data.append %}{{ form_data.append }}{% endif %} +{% endmacro %} diff --git a/phpBB/styles/prosilver/template/ucp_agreement.html b/phpBB/styles/prosilver/template/ucp_agreement.html index 5891bbe60e..69458e198a 100644 --- a/phpBB/styles/prosilver/template/ucp_agreement.html +++ b/phpBB/styles/prosilver/template/ucp_agreement.html @@ -2,15 +2,12 @@ -{% if lang_options|length > 0 %} +{% if LANG_OPTIONS %}

- - + + {% import "macros/form_macros.twig" as form_macros %} + {{ form_macros.select(LANG_OPTIONS) }} {S_HIDDEN_FIELDS}

@@ -21,7 +18,7 @@ /** * Change language on change */ - document.querySelector('#lang').addEventListener('change', (event) => { + document.querySelector("{{ '#' ~ LANG_OPTIONS.id }}").addEventListener('change', (event) => { const langIso = event.target.value; document.cookie = '{{ COOKIE_NAME }}_lang=' + langIso + '; path={{ COOKIE_PATH }}'; document.forms['register'].change_lang.value = langIso; diff --git a/phpBB/styles/prosilver/template/ucp_prefs_personal.html b/phpBB/styles/prosilver/template/ucp_prefs_personal.html index 120c9b146c..2c5ff918e5 100644 --- a/phpBB/styles/prosilver/template/ucp_prefs_personal.html +++ b/phpBB/styles/prosilver/template/ucp_prefs_personal.html @@ -52,13 +52,10 @@
-
+
- + {% import "macros/form_macros.twig" as form_macros %} + {{ form_macros.select(LANG_OPTIONS) }}
diff --git a/phpBB/styles/prosilver/template/ucp_register.html b/phpBB/styles/prosilver/template/ucp_register.html index 1b92ad7c15..ad45812ab0 100644 --- a/phpBB/styles/prosilver/template/ucp_register.html +++ b/phpBB/styles/prosilver/template/ucp_register.html @@ -57,17 +57,23 @@
- {% if lang_options|length > 0 %} + {% if LANG_OPTIONS %}
-
+
- + {% import "macros/form_macros.twig" as form_macros %} + {{ form_macros.select(LANG_OPTIONS) }}
+ {% endif %} diff --git a/tests/functions/language_select_test.php b/tests/functions/language_select_test.php index e41a42c6b6..5445866fd8 100644 --- a/tests/functions/language_select_test.php +++ b/tests/functions/language_select_test.php @@ -25,14 +25,14 @@ class phpbb_functions_language_select_test extends phpbb_database_test_case '', [ [ - 'SELECTED' => false, - 'LANG_ISO' => 'cs', - 'LANG_LOCAL_NAME' => 'Čeština', + 'selected' => false, + 'value' => 'cs', + 'label' => 'Čeština', ], [ - 'SELECTED' => false, - 'LANG_ISO' => 'en', - 'LANG_LOCAL_NAME' => 'English', + 'selected' => false, + 'value' => 'en', + 'label' => 'English', ], ] ], @@ -40,14 +40,14 @@ class phpbb_functions_language_select_test extends phpbb_database_test_case 'en', [ [ - 'SELECTED' => false, - 'LANG_ISO' => 'cs', - 'LANG_LOCAL_NAME' => 'Čeština', + 'selected' => false, + 'value' => 'cs', + 'label' => 'Čeština', ], [ - 'SELECTED' => true, - 'LANG_ISO' => 'en', - 'LANG_LOCAL_NAME' => 'English', + 'selected' => true, + 'value' => 'en', + 'label' => 'English', ], ] ], @@ -55,14 +55,14 @@ class phpbb_functions_language_select_test extends phpbb_database_test_case 'cs', [ [ - 'SELECTED' => true, - 'LANG_ISO' => 'cs', - 'LANG_LOCAL_NAME' => 'Čeština', + 'selected' => true, + 'value' => 'cs', + 'label' => 'Čeština', ], [ - 'SELECTED' => false, - 'LANG_ISO' => 'en', - 'LANG_LOCAL_NAME' => 'English', + 'selected' => false, + 'value' => 'en', + 'label' => 'English', ], ] ], @@ -70,14 +70,14 @@ class phpbb_functions_language_select_test extends phpbb_database_test_case 'de', [ [ - 'SELECTED' => false, - 'LANG_ISO' => 'cs', - 'LANG_LOCAL_NAME' => 'Čeština', + 'selected' => false, + 'value' => 'cs', + 'label' => 'Čeština', ], [ - 'SELECTED' => false, - 'LANG_ISO' => 'en', - 'LANG_LOCAL_NAME' => 'English', + 'selected' => false, + 'value' => 'en', + 'label' => 'English', ], ] ], @@ -91,19 +91,9 @@ class phpbb_functions_language_select_test extends phpbb_database_test_case { global $db; $db = $this->new_dbal(); - $template = $this->getMockBuilder('\phpbb\template\base') - ->disableOriginalConstructor() - ->getMock(); - $template_data = []; - $template->expects($this->any()) - ->method('assign_block_vars') - ->willReturnCallback(function ($blockname, array $vararray) use (&$template_data) { - $template_data[$blockname][] = $vararray; - return null; - }); - phpbb_language_select($db, $template, $default); + $lang_options = phpbb_language_select($db, $default); - $this->assertEquals($expected, $template_data['lang_options']); + $this->assertEquals($expected, $lang_options); } } From 003b606781446eabfb1ec456396d5ce828f9cc44 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 18 Apr 2022 20:50:23 +0200 Subject: [PATCH 09/27] [ticket/17100] Improve output of unwrapped macros PHPBB3-17100 --- phpBB/adm/style/form_macros.twig | 46 +++++++++++++++++--------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/phpBB/adm/style/form_macros.twig b/phpBB/adm/style/form_macros.twig index 0937a10b4b..a0cba50855 100644 --- a/phpBB/adm/style/form_macros.twig +++ b/phpBB/adm/style/form_macros.twig @@ -1,17 +1,18 @@ {% macro input(form_data) %} - + {% apply replace({"\n": ' ', '\t': ''}) %} + + {% endapply %} {% endmacro %} {% macro dimension(form_data) %} @@ -19,14 +20,15 @@ {% endmacro %} {% macro textarea(form_data) %} - + {% apply replace({"\n": ' ', '\t': ''}) %} + + {% endapply %} {% endmacro %} {% macro radio_buttons(form_data) %} @@ -35,7 +37,7 @@ {% endmacro %} {% macro select(form_data) %} - {% for option in form_data.options %} {% endfor %} From 8e05baca873cd3d514f19a651c99ab99f7ecb12b Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 18 Apr 2022 21:07:50 +0200 Subject: [PATCH 10/27] [ticket/17100] Improve support for selects PHPBB3-17100 --- phpBB/adm/style/form_macros.twig | 6 +++- phpBB/includes/acp/acp_board.php | 47 +++++++++++++++++++++++++++----- phpBB/includes/functions_acp.php | 19 +++++++++++-- 3 files changed, 61 insertions(+), 11 deletions(-) diff --git a/phpBB/adm/style/form_macros.twig b/phpBB/adm/style/form_macros.twig index a0cba50855..124f066093 100644 --- a/phpBB/adm/style/form_macros.twig +++ b/phpBB/adm/style/form_macros.twig @@ -37,7 +37,7 @@ {% endmacro %} {% macro select(form_data) %} - {% for option in form_data.options %} {% endfor %} @@ -51,6 +51,10 @@ {{ _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) }} diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index 8c3950d65c..18fccae5a5 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -64,7 +64,7 @@ class acp_board 'board_index_text' => array('lang' => 'BOARD_INDEX_TEXT', 'validate' => 'string', 'type' => 'text:40:255', 'explain' => true), 'board_disable' => array('lang' => 'DISABLE_BOARD', 'validate' => 'bool', 'type' => 'custom', 'method' => 'board_disable', 'explain' => true), 'board_disable_msg' => false, - 'default_lang' => array('lang' => 'DEFAULT_LANGUAGE', 'validate' => 'lang', 'type' => 'select', 'function' => 'language_select', 'params' => array('{CONFIG_VALUE}'), 'explain' => false), + 'default_lang' => array('lang' => 'DEFAULT_LANGUAGE', 'validate' => 'lang', 'type' => 'select', 'method' => 'language_select', 'params' => array('{CONFIG_VALUE}'), 'explain' => false), 'default_dateformat' => array('lang' => 'DEFAULT_DATE_FORMAT', 'validate' => 'string', 'type' => 'custom', 'method' => 'dateformat_select', 'explain' => true), 'board_timezone' => array('lang' => 'SYSTEM_TIMEZONE', 'validate' => 'timezone', 'type' => 'custom', 'method' => 'timezone_select', 'explain' => true), @@ -976,19 +976,52 @@ class acp_board /** * Select bump interval */ - function bump_interval($value, $key) + public function bump_interval($value, $key): array { - global $user; + global $language; - $s_bump_type = ''; + $bump_type_options = []; $types = array('m' => 'MINUTES', 'h' => 'HOURS', 'd' => 'DAYS'); foreach ($types as $type => $lang) { - $selected = ($this->new_config['bump_type'] == $type) ? ' selected="selected"' : ''; - $s_bump_type .= ''; + $bump_type_options[] = [ + 'value' => $type, + 'selected' => $this->new_config['bump_type'] == $type, + 'label' => $language->lang($lang), + ]; } - return ' '; + return [ + [ + 'tag' => 'input', + 'id' => $key, + 'type' => 'text', + 'size' => 3, + 'maxlength' => 4, + 'name' => 'config[bump_interval]', + 'value' => $value, + ], + [ + 'tag' => 'select', + 'name' => 'config[bump_type]', + 'options' => $bump_type_options, + ], + ]; + } + + /** + * Wrapper function for phpbb_language_select() + * + * @param string $default + * @param array $langdata + * + * @return array + */ + public function language_select(string $default = '', array $langdata = []): array + { + global $db; + + return phpbb_language_select($db, $default, $langdata); } /** diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php index b6c9c7b39d..106ca8bf66 100644 --- a/phpBB/includes/functions_acp.php +++ b/phpBB/includes/functions_acp.php @@ -482,10 +482,23 @@ 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; - $data_toggle = (!empty($tpl_type[2])) ? ' data-togglable-settings="true"' : ''; + 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 = ''; + $tpl = ''; + } + else + { + $tpl = [ + 'tag' => 'select', + 'id' => $key, + 'name' => $name, + 'toggleable' => !empty($tpl_type[2]), + 'options' => $return, + ]; + } } else { From d91f11d55f23d876f2ce5368c6da4b18e99ebffb Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 18 Apr 2022 21:08:35 +0200 Subject: [PATCH 11/27] [ticket/17100] Update styles/all macros PHPBB3-17100 --- .../all/template/macros/form_macros.twig | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/phpBB/styles/all/template/macros/form_macros.twig b/phpBB/styles/all/template/macros/form_macros.twig index 0937a10b4b..124f066093 100644 --- a/phpBB/styles/all/template/macros/form_macros.twig +++ b/phpBB/styles/all/template/macros/form_macros.twig @@ -1,17 +1,18 @@ {% macro input(form_data) %} - + {% apply replace({"\n": ' ', '\t': ''}) %} + + {% endapply %} {% endmacro %} {% macro dimension(form_data) %} @@ -19,14 +20,15 @@ {% endmacro %} {% macro textarea(form_data) %} - + {% apply replace({"\n": ' ', '\t': ''}) %} + + {% endapply %} {% endmacro %} {% macro radio_buttons(form_data) %} @@ -35,7 +37,7 @@ {% endmacro %} {% macro select(form_data) %} - {% for option in form_data.options %} {% endfor %} @@ -49,6 +51,10 @@ {{ _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) }} From b09af35c907e23cf2546b1064e05171039237c85 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 18 Apr 2022 22:33:30 +0200 Subject: [PATCH 12/27] [ticket/17100] Move html of build_select to templates PHPBB3-17100 --- phpBB/adm/style/acp_inactive.html | 3 ++- phpBB/includes/acp/acp_inactive.php | 5 ++++- phpBB/includes/acp/acp_main.php | 11 +---------- phpBB/includes/functions_acp.php | 15 +++++++++------ 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/phpBB/adm/style/acp_inactive.html b/phpBB/adm/style/acp_inactive.html index 1b0b6d46de..19a766fd8c 100644 --- a/phpBB/adm/style/acp_inactive.html +++ b/phpBB/adm/style/acp_inactive.html @@ -65,7 +65,8 @@
- + {% from 'form_macros.twig' import select %} + {{ select(INACTIVE_OPTIONS) }}

{L_MARK_ALL}{L_UNMARK_ALL}

{S_FORM_TOKEN} diff --git a/phpBB/includes/acp/acp_inactive.php b/phpBB/includes/acp/acp_inactive.php index 7b4536f755..02412aa526 100644 --- a/phpBB/includes/acp/acp_inactive.php +++ b/phpBB/includes/acp/acp_inactive.php @@ -306,7 +306,10 @@ class acp_inactive $template->assign_vars(array( 'S_INACTIVE_USERS' => true, - 'S_INACTIVE_OPTIONS' => build_select($option_ary), + 'INACTIVE_OPTIONS' => [ + 'name' => 'action', + 'options' => build_select($option_ary), + ], 'S_LIMIT_DAYS' => $s_limit_days, 'S_SORT_KEY' => $s_sort_key, diff --git a/phpBB/includes/acp/acp_main.php b/phpBB/includes/acp/acp_main.php index 71e9ed7a2e..d99d1995db 100644 --- a/phpBB/includes/acp/acp_main.php +++ b/phpBB/includes/acp/acp_main.php @@ -626,16 +626,7 @@ class acp_main )); } - $option_ary = array('activate' => 'ACTIVATE', 'delete' => 'DELETE'); - if ($config['email_enable']) - { - $option_ary += array('remind' => 'REMIND'); - } - - $template->assign_vars(array( - 'S_INACTIVE_USERS' => true, - 'S_INACTIVE_OPTIONS' => build_select($option_ary)) - ); + $template->assign_var('S_INACTIVE_USERS', true); } // Warn if install is still present diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php index 106ca8bf66..716d9f6d53 100644 --- a/phpBB/includes/functions_acp.php +++ b/phpBB/includes/functions_acp.php @@ -209,18 +209,21 @@ function adm_back_link($u_action) /** * Build select field options in acp pages */ -function build_select($option_ary, $option_default = false) +function build_select($option_ary, $option_default = false): array { - global $user; + global $language; - $html = ''; + $options = []; foreach ($option_ary as $value => $title) { - $selected = ($option_default !== false && $value == $option_default) ? ' selected="selected"' : ''; - $html .= ''; + $options[] = [ + 'value' => $value, + 'selected' => $option_default !== false && $value == $option_default, + 'label' => $language->lang($title), + ]; } - return $html; + return $options; } /** From 540097eed73af5e056c0fd96aa263aceeb50db05 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Tue, 19 Apr 2022 21:45:02 +0200 Subject: [PATCH 13/27] [ticket/17100] Move filesize config HTML from PHP files PHPBB3-17100 --- phpBB/adm/style/acp_attachments.html | 6 ++++- phpBB/adm/style/form_macros.twig | 1 + phpBB/includes/acp/acp_attachments.php | 24 ++++++++++++++++--- phpBB/includes/functions_admin.php | 15 +++++++----- .../all/template/macros/form_macros.twig | 1 + 5 files changed, 37 insertions(+), 10 deletions(-) diff --git a/phpBB/adm/style/acp_attachments.html b/phpBB/adm/style/acp_attachments.html index 78d937b967..6448c51c80 100644 --- a/phpBB/adm/style/acp_attachments.html +++ b/phpBB/adm/style/acp_attachments.html @@ -208,7 +208,11 @@
-
+
+ + {% from 'form_macros.twig' import select %} + {{ select(EXT_GROUP_SIZE_OPTIONS) }} +
diff --git a/phpBB/adm/style/form_macros.twig b/phpBB/adm/style/form_macros.twig index 124f066093..845d039ddd 100644 --- a/phpBB/adm/style/form_macros.twig +++ b/phpBB/adm/style/form_macros.twig @@ -8,6 +8,7 @@ {% 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 %} diff --git a/phpBB/includes/acp/acp_attachments.php b/phpBB/includes/acp/acp_attachments.php index c03cda951a..329900130e 100644 --- a/phpBB/includes/acp/acp_attachments.php +++ b/phpBB/includes/acp/acp_attachments.php @@ -794,7 +794,10 @@ class acp_attachments 'ASSIGNED_EXTENSIONS' => $assigned_extensions, 'S_CATEGORY_SELECT' => $this->category_select('special_category', $group_id, 'category'), - 'S_EXT_GROUP_SIZE_OPTIONS' => size_select_options($size_format), + 'EXT_GROUP_SIZE_OPTIONS' => [ + 'name' => 'size_select', + 'options' => size_select_options($size_format), + ], 'S_EXTENSION_OPTIONS' => $s_extension_options, 'S_FILENAME_LIST' => $filename_list, 'S_EDIT_GROUP' => true, @@ -1723,8 +1726,23 @@ class acp_attachments $size_var = $filesize['si_identifier']; $value = $filesize['value']; - // size and maxlength must not be specified for input of type number - return ' '; + return [ + [ + 'tag' => 'input', + 'id' => $key, + 'type' => 'number', + 'name' => 'config[' . $key . ']', + 'min' => 0, + 'max' => 999999999999999, + 'step' => 'any', + 'value' => $value, + ], + [ + 'tag' => 'select', + 'name' => $key, + 'options' => size_select_options($size_var), + ] + ]; } /** diff --git a/phpBB/includes/functions_admin.php b/phpBB/includes/functions_admin.php index 621d1a284e..4eaedc4041 100644 --- a/phpBB/includes/functions_admin.php +++ b/phpBB/includes/functions_admin.php @@ -160,20 +160,23 @@ function make_forum_select($select_id = false, $ignore_id = false, $ignore_acl = */ function size_select_options($size_compare) { - global $user; + global $language; - $size_types_text = array($user->lang['BYTES'], $user->lang['KIB'], $user->lang['MIB']); + $size_types_text = array($language->lang('BYTES'), $language->lang('KIB'), $language->lang('MIB')); $size_types = array('b', 'kb', 'mb'); - $s_size_options = ''; + $size_options = []; for ($i = 0, $size = count($size_types_text); $i < $size; $i++) { - $selected = ($size_compare == $size_types[$i]) ? ' selected="selected"' : ''; - $s_size_options .= ''; + $size_options[] = [ + 'value' => $size_types[$i], + 'selected' => $size_compare == $size_types[$i], + 'label' => $size_types_text[$i], + ]; } - return $s_size_options; + return $size_options; } /** diff --git a/phpBB/styles/all/template/macros/form_macros.twig b/phpBB/styles/all/template/macros/form_macros.twig index 124f066093..845d039ddd 100644 --- a/phpBB/styles/all/template/macros/form_macros.twig +++ b/phpBB/styles/all/template/macros/form_macros.twig @@ -8,6 +8,7 @@ {% 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 %} From 015472ab9130b3b355d858e5274c2f2bf9296106 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Wed, 1 Jun 2022 21:08:12 +0200 Subject: [PATCH 14/27] [ticket/17100] Move timezone select HTML from PHP file PHPBB3-17100 --- phpBB/adm/style/form_macros.twig | 28 ++++++++++-- phpBB/adm/style/timezone_option.html | 31 +++++-------- phpBB/includes/acp/acp_board.php | 10 +++-- phpBB/includes/acp/acp_users.php | 7 ++- phpBB/includes/functions.php | 44 +++++++------------ phpBB/includes/functions_compatibility.php | 4 +- phpBB/includes/ucp/ucp_prefs.php | 7 ++- phpBB/includes/ucp/ucp_register.php | 7 ++- .../all/template/macros/form_macros.twig | 30 ++++++++++--- .../prosilver/template/timezone_option.html | 31 +++++-------- 10 files changed, 113 insertions(+), 86 deletions(-) diff --git a/phpBB/adm/style/form_macros.twig b/phpBB/adm/style/form_macros.twig index 845d039ddd..35fe1bf4df 100644 --- a/phpBB/adm/style/form_macros.twig +++ b/phpBB/adm/style/form_macros.twig @@ -37,10 +37,30 @@ {% endmacro %} -{% macro select(form_data) %} - + {% endapply %} + {% for element in form_data.options %} + {% if not group_only and element.options %} + {% apply replace({"\n": ' ', '\t': ''}) %} + + {% endapply %} + {% for option in element.options %} + + {% endfor %} + + {% else %} + + {% endif %} {% endfor %} {% endmacro %} diff --git a/phpBB/adm/style/timezone_option.html b/phpBB/adm/style/timezone_option.html index acfff30184..7ca0e3be26 100644 --- a/phpBB/adm/style/timezone_option.html +++ b/phpBB/adm/style/timezone_option.html @@ -1,27 +1,16 @@
-
- -
+ {% if TIMEZONE_OPTIONS %} +
+ {% set tz_date_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_CURRENT_TIME') }]|merge(TIMEZONE_OPTIONS.options) }) %} + {{ select(tz_date_data, 'autowidth tz_select', 'tz_date', 'tz_date', true) }}
- + {% endif %}
- + {% set tz_select_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }]|merge(TIMEZONE_OPTIONS.options) }) %} + {{ select(tz_select_data, 'autowidth tz_select', 'timezone') }} - + {% INCLUDEJS('timezone.js') %}
diff --git a/phpBB/includes/acp/acp_board.php b/phpBB/includes/acp/acp_board.php index 18fccae5a5..e394424a9f 100644 --- a/phpBB/includes/acp/acp_board.php +++ b/phpBB/includes/acp/acp_board.php @@ -1052,11 +1052,15 @@ class acp_board */ function timezone_select($value, $key) { - global $template, $user; + global $user; - $timezone_select = phpbb_timezone_select($template, $user, $value, true); + $timezone_select = phpbb_timezone_select($user, $value, true); - return ''; + return [ + 'tag' => 'select', + 'name' => 'config[' . $key . ']', + 'options' => $timezone_select, + ]; } /** diff --git a/phpBB/includes/acp/acp_users.php b/phpBB/includes/acp/acp_users.php index 9aa3333e77..8be78c24f2 100644 --- a/phpBB/includes/acp/acp_users.php +++ b/phpBB/includes/acp/acp_users.php @@ -1795,7 +1795,7 @@ class acp_users ${'s_sort_' . $sort_option . '_dir'} .= ''; } - phpbb_timezone_select($template, $user, $data['tz'], true); + $timezone_select = phpbb_timezone_select($user, $data['tz'], true); $lang_options = phpbb_language_select($db, $data['lang']); $user_prefs_data = array( @@ -1839,6 +1839,11 @@ class acp_users 'options' => $lang_options, ], 'S_STYLE_OPTIONS' => style_select($data['style']), + 'TIMEZONE_OPTIONS' => [ + 'tag' => 'select', + 'name' => 'tz', + 'options' => $timezone_select, + ], ); /** diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index ae8b978c69..48efdca0d8 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -443,14 +443,13 @@ function phpbb_get_timezone_identifiers($selected_timezone) /** * Options to pick a timezone and date/time * -* @param \phpbb\template\template $template phpBB template object * @param \phpbb\user $user Object of the current user * @param string $default A timezone to select * @param boolean $truncate Shall we truncate the options text * * @return string Returns an array containing the options for the time selector. */ -function phpbb_timezone_select($template, $user, $default = '', $truncate = false) +function phpbb_timezone_select($user, $default = '', $truncate = false) { static $timezones; @@ -482,27 +481,22 @@ function phpbb_timezone_select($template, $user, $default = '', $truncate = fals uksort($timezones, 'phpbb_tz_select_compare'); } - $tz_select = $opt_group = ''; + $opt_group = ''; + $tz_data = []; foreach ($timezones as $key => $timezone) { if ($opt_group != $timezone['offset']) { - // Generate tz_select for backwards compatibility - $tz_select .= ($opt_group) ? '' : ''; - $tz_select .= ''; - $opt_group = $timezone['offset']; - $template->assign_block_vars('timezone_select', array( - 'LABEL' => $user->lang(array('timezones', 'UTC_OFFSET_CURRENT'), $timezone['offset'], $timezone['current']), - 'VALUE' => $key . ' - ' . $timezone['current'], - )); + $tz_data[$timezone['offset']] = [ + 'label' => $user->lang(array('timezones', 'UTC_OFFSET_CURRENT'), $timezone['offset'], $timezone['current']), + 'value' => $key . ' - ' . $timezone['current'], + 'options' => [], + 'selected' => !empty($default_offset) && strpos($key, $default_offset) !== false, + 'data' => ['tz-value' => $key . ' - ' . $timezone['current']], + ]; - $selected = (!empty($default_offset) && strpos($key, $default_offset) !== false) ? ' selected="selected"' : ''; - $template->assign_block_vars('timezone_date', array( - 'VALUE' => $key . ' - ' . $timezone['current'], - 'SELECTED' => !empty($selected), - 'TITLE' => $user->lang(array('timezones', 'UTC_OFFSET_CURRENT'), $timezone['offset'], $timezone['current']), - )); + $opt_group = $timezone['offset']; } $label = $timezone['tz']; @@ -517,19 +511,15 @@ function phpbb_timezone_select($template, $user, $default = '', $truncate = fals $label = truncate_string($label, 50, 255, false, '...'); } - // Also generate timezone_select for backwards compatibility - $selected = ($timezone['tz'] === $default) ? ' selected="selected"' : ''; - $tz_select .= ''; - $template->assign_block_vars('timezone_select.timezone_options', array( + $tz_data[$timezone['offset']]['options'][] = [ 'TITLE' => $title, - 'VALUE' => $timezone['tz'], - 'SELECTED' => !empty($selected), - 'LABEL' => $label, - )); + 'value' => $timezone['tz'], + 'selected' => $timezone['tz'] === $default, + 'label' => $label, + ]; } - $tz_select .= ''; - return $tz_select; + return $tz_data; } // Functions handling topic/post tracking/marking diff --git a/phpBB/includes/functions_compatibility.php b/phpBB/includes/functions_compatibility.php index 77b2ef4220..95def7b7e9 100644 --- a/phpBB/includes/functions_compatibility.php +++ b/phpBB/includes/functions_compatibility.php @@ -111,9 +111,9 @@ function phpbb_clean_path($path) */ function tz_select($default = '', $truncate = false) { - global $template, $user; + global $user; - return phpbb_timezone_select($template, $user, $default, $truncate); + return phpbb_timezone_select($user, $default, $truncate); } /** diff --git a/phpBB/includes/ucp/ucp_prefs.php b/phpBB/includes/ucp/ucp_prefs.php index 625ade741c..dcbd2624f2 100644 --- a/phpBB/includes/ucp/ucp_prefs.php +++ b/phpBB/includes/ucp/ucp_prefs.php @@ -156,7 +156,7 @@ class ucp_prefs } $dateformat_options .= '>' . $user->lang['CUSTOM_DATEFORMAT'] . ''; - phpbb_timezone_select($template, $user, $data['tz'], true); + $timezone_select = phpbb_timezone_select($user, $data['tz'], true); // check if there are any user-selectable languages $sql = 'SELECT lang_iso, lang_local_name @@ -206,6 +206,11 @@ class ucp_prefs 'options' => $lang_options, ], 'S_STYLE_OPTIONS' => ($config['override_user_style']) ? '' : style_select($data['user_style'], false, $styles_row), + 'TIMEZONE_OPTIONS' => [ + 'tag' => 'select', + 'name' => 'tz', + 'options' => $timezone_select, + ], 'S_CAN_HIDE_ONLINE' => ($auth->acl_get('u_hideonline')) ? true : false, 'S_SELECT_NOTIFY' => ($config['jab_enable'] && $user->data['user_jabber'] && @extension_loaded('xml')) ? true : false) ); diff --git a/phpBB/includes/ucp/ucp_register.php b/phpBB/includes/ucp/ucp_register.php index bdece67890..b739cb6ce8 100644 --- a/phpBB/includes/ucp/ucp_register.php +++ b/phpBB/includes/ucp/ucp_register.php @@ -626,7 +626,7 @@ class ucp_register } // Assign template vars for timezone select - phpbb_timezone_select($template, $user, $data['tz'], true); + $timezone_select = phpbb_timezone_select($user, $data['tz'], true); // Checking amount of available languages $sql = 'SELECT lang_iso, lang_local_name @@ -653,6 +653,11 @@ class ucp_register 'name' => 'lang', 'options' => $lang_options, ], + 'TIMEZONE_OPTIONS' => [ + 'tag' => 'select', + 'name' => 'tz', + 'options' => $timezone_select, + ], 'S_TZ_PRESELECT' => !$submit, 'S_CONFIRM_REFRESH' => ($config['enable_confirm'] && $config['confirm_refresh']) ? true : false, 'S_REGISTRATION' => true, diff --git a/phpBB/styles/all/template/macros/form_macros.twig b/phpBB/styles/all/template/macros/form_macros.twig index 845d039ddd..074891a5ee 100644 --- a/phpBB/styles/all/template/macros/form_macros.twig +++ b/phpBB/styles/all/template/macros/form_macros.twig @@ -37,11 +37,31 @@ {% endmacro %} -{% macro select(form_data) %} - + {% endapply %} + {% for element in form_data.options %} + {% if not group_only and element.options %} + {% apply replace({"\n": ' ', '\t': ''}) %} + + {% endapply %} + {% for option in element.options %} + + {% endfor %} + + {% else %} + + {% endif %} + {% endfor %} {% endmacro %} diff --git a/phpBB/styles/prosilver/template/timezone_option.html b/phpBB/styles/prosilver/template/timezone_option.html index 01786d5ef4..47da5f63c1 100644 --- a/phpBB/styles/prosilver/template/timezone_option.html +++ b/phpBB/styles/prosilver/template/timezone_option.html @@ -1,28 +1,17 @@
-
- -
+ {% if TIMEZONE_OPTIONS %} +
+ {% set tz_date_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_CURRENT_TIME') }]|merge(TIMEZONE_OPTIONS.options) }) %} + {{ select(tz_date_data, 'autowidth tz_select', 'tz_date', 'tz_date', true) }}
- + {% endif %}
- + {% set tz_select_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }]|merge(TIMEZONE_OPTIONS.options) }) %} + {{ select(tz_select_data, 'autowidth tz_select timezone', 'timezone') }} - + {% INCLUDEJS('timezone.js') %}
From 6473167d6e3c4a77bdf99fbeec19669e94f45cfe Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Mon, 30 Jan 2023 16:48:18 +0100 Subject: [PATCH 15/27] [ticket/17100] Reuse form_macros in ACP and fix tests PHPBB3-17100 --- phpBB/adm/index.php | 19 +++-- phpBB/adm/style/acp_attachments.html | 4 +- phpBB/adm/style/acp_board.html | 2 +- phpBB/adm/style/acp_bots.html | 2 +- phpBB/adm/style/acp_inactive.html | 2 +- phpBB/adm/style/acp_users_prefs.html | 2 +- phpBB/adm/style/form_macros.twig | 85 ------------------- phpBB/adm/style/timezone_option.html | 2 +- phpBB/includes/functions_acp.php | 9 +- .../all/template/macros/form_macros.twig | 3 +- .../functions_acp/build_cfg_template_test.php | 83 ++++++++++++++---- tests/functions_acp/build_select_test.php | 43 +++++++++- 12 files changed, 135 insertions(+), 121 deletions(-) delete mode 100644 phpBB/adm/style/form_macros.twig diff --git a/phpBB/adm/index.php b/phpBB/adm/index.php index 3f0aa4009b..68f8ee634a 100644 --- a/phpBB/adm/index.php +++ b/phpBB/adm/index.php @@ -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'); diff --git a/phpBB/adm/style/acp_attachments.html b/phpBB/adm/style/acp_attachments.html index 6448c51c80..38be1baac0 100644 --- a/phpBB/adm/style/acp_attachments.html +++ b/phpBB/adm/style/acp_attachments.html @@ -37,7 +37,7 @@ - {% import "form_macros.twig" as form_macros %} + {% import 'macros/form_macros.twig' as form_macros %}
@@ -210,7 +210,7 @@
- {% from 'form_macros.twig' import select %} + {% from 'macros/form_macros.twig' import select %} {{ select(EXT_GROUP_SIZE_OPTIONS) }}
diff --git a/phpBB/adm/style/acp_board.html b/phpBB/adm/style/acp_board.html index 8d539de10c..b809e2dfef 100644 --- a/phpBB/adm/style/acp_board.html +++ b/phpBB/adm/style/acp_board.html @@ -13,7 +13,7 @@
-{% import "form_macros.twig" as form_macros %} +{% import 'macros/form_macros.twig' as form_macros %} diff --git a/phpBB/adm/style/acp_bots.html b/phpBB/adm/style/acp_bots.html index 94103e0ec9..2b9fec668f 100644 --- a/phpBB/adm/style/acp_bots.html +++ b/phpBB/adm/style/acp_bots.html @@ -32,7 +32,7 @@

{L_BOT_LANG_EXPLAIN}
- {% import "form_macros.twig" as form_macros %} + {% import 'macros/form_macros.twig' as form_macros %} {{ form_macros.select(LANG_OPTIONS) }}
diff --git a/phpBB/adm/style/acp_inactive.html b/phpBB/adm/style/acp_inactive.html index 19a766fd8c..81c019c47c 100644 --- a/phpBB/adm/style/acp_inactive.html +++ b/phpBB/adm/style/acp_inactive.html @@ -65,7 +65,7 @@
- {% from 'form_macros.twig' import select %} + {% from 'macros/form_macros.twig' import select %} {{ select(INACTIVE_OPTIONS) }}

{L_MARK_ALL}{L_UNMARK_ALL}

diff --git a/phpBB/adm/style/acp_users_prefs.html b/phpBB/adm/style/acp_users_prefs.html index 0cf21e19a6..11f6a71e20 100644 --- a/phpBB/adm/style/acp_users_prefs.html +++ b/phpBB/adm/style/acp_users_prefs.html @@ -43,7 +43,7 @@
- {% import "form_macros.twig" as form_macros %} + {% import 'macros/form_macros.twig' as form_macros %} {{ form_macros.select(LANG_OPTIONS) }}
diff --git a/phpBB/adm/style/form_macros.twig b/phpBB/adm/style/form_macros.twig deleted file mode 100644 index 35fe1bf4df..0000000000 --- a/phpBB/adm/style/form_macros.twig +++ /dev/null @@ -1,85 +0,0 @@ -{% macro input(form_data) %} - {% apply replace({"\n": ' ', '\t': ''}) %} - - {% 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': ''}) %} - - {% endapply %} -{% endmacro %} - -{% macro radio_buttons(form_data) %} - - -{% endmacro %} - -{% macro select(form_data, class, id, name, group_only) %} - {% apply replace({"\n": ' ', '\t': ''}) %} - -{% 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 %} diff --git a/phpBB/adm/style/timezone_option.html b/phpBB/adm/style/timezone_option.html index 7ca0e3be26..58b88cf6b4 100644 --- a/phpBB/adm/style/timezone_option.html +++ b/phpBB/adm/style/timezone_option.html @@ -1,5 +1,5 @@
- {% from "form_macros.twig" import select %} + {% from "macros/form_macros.twig" import select %}
{% if TIMEZONE_OPTIONS %}
diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php index 716d9f6d53..007582c6f8 100644 --- a/phpBB/includes/functions_acp.php +++ b/phpBB/includes/functions_acp.php @@ -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 = ''; @@ -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 diff --git a/phpBB/styles/all/template/macros/form_macros.twig b/phpBB/styles/all/template/macros/form_macros.twig index 074891a5ee..a4fa779ff8 100644 --- a/phpBB/styles/all/template/macros/form_macros.twig +++ b/phpBB/styles/all/template/macros/form_macros.twig @@ -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 %} diff --git a/tests/functions_acp/build_cfg_template_test.php b/tests/functions_acp/build_cfg_template_test.php index 9d3b244bc0..76558152ff 100644 --- a/tests/functions_acp/build_cfg_template_test.php +++ b/tests/functions_acp/build_cfg_template_test.php @@ -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'), - '', - ), - 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'), - '', - ), - ); + ['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; diff --git a/tests/functions_acp/build_select_test.php b/tests/functions_acp/build_select_test.php index ed90454c9f..e59f25e977 100644 --- a/tests/functions_acp/build_select_test.php +++ b/tests/functions_acp/build_select_test.php @@ -34,7 +34,18 @@ class phpbb_functions_acp_built_select_test extends phpbb_test_case 'second' => 'SEC_OPTION', ), false, - '', + [ + [ + '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', - '', + [ + [ + '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', - '', + [ + [ + '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)); } } From 69f5d5f40f055e0137b0e04162dfde04896b41f5 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sat, 4 Feb 2023 09:29:24 +0100 Subject: [PATCH 16/27] [ticket/17100] Add missing space PHPBB3-17100 --- phpBB/includes/functions_acp.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php index 007582c6f8..441f1f5add 100644 --- a/phpBB/includes/functions_acp.php +++ b/phpBB/includes/functions_acp.php @@ -485,7 +485,7 @@ 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; + $size = (isset($tpl_type[1])) ? (int) $tpl_type[1] : 1; if (is_string($return)) { From 60dacf0bfe2b1d018316d681aef1d4239f2b4534 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 9 Apr 2023 13:12:39 +0200 Subject: [PATCH 17/27] [ticket/17100] Use twig functions for form elements PHPBB3-17100 --- phpBB/adm/style/acp_attachments.html | 6 +- phpBB/adm/style/acp_board.html | 3 +- phpBB/adm/style/acp_bots.html | 3 +- phpBB/adm/style/acp_inactive.html | 3 +- phpBB/adm/style/acp_users_prefs.html | 3 +- phpBB/adm/style/timezone_option.html | 11 +- .../default/container/services_twig.yml | 7 + phpBB/phpbb/template/twig/environment.php | 2 +- phpBB/phpbb/template/twig/extension/forms.php | 217 ++++++++++++++++++ phpBB/phpbb/template/twig/twig.php | 1 + .../all/template/macros/form_macros.twig | 86 ------- .../template/macros/forms/build_template.twig | 16 ++ .../all/template/macros/forms/dimension.twig | 1 + .../all/template/macros/forms/input.twig | 15 ++ .../template/macros/forms/radio_buttons.twig | 2 + .../all/template/macros/forms/select.twig | 26 +++ .../all/template/macros/forms/textarea.twig | 9 + .../prosilver/template/timezone_option.html | 9 +- .../prosilver/template/ucp_agreement.html | 3 +- .../template/ucp_prefs_personal.html | 3 +- .../prosilver/template/ucp_register.html | 3 +- 21 files changed, 313 insertions(+), 116 deletions(-) create mode 100644 phpBB/phpbb/template/twig/extension/forms.php delete mode 100644 phpBB/styles/all/template/macros/form_macros.twig create mode 100644 phpBB/styles/all/template/macros/forms/build_template.twig create mode 100644 phpBB/styles/all/template/macros/forms/dimension.twig create mode 100644 phpBB/styles/all/template/macros/forms/input.twig create mode 100644 phpBB/styles/all/template/macros/forms/radio_buttons.twig create mode 100644 phpBB/styles/all/template/macros/forms/select.twig create mode 100644 phpBB/styles/all/template/macros/forms/textarea.twig diff --git a/phpBB/adm/style/acp_attachments.html b/phpBB/adm/style/acp_attachments.html index 38be1baac0..c33d30b1c1 100644 --- a/phpBB/adm/style/acp_attachments.html +++ b/phpBB/adm/style/acp_attachments.html @@ -37,7 +37,6 @@ - {% import 'macros/form_macros.twig' as form_macros %} @@ -52,7 +51,7 @@

{options.TITLE_EXPLAIN}
{% if options.CONTENT is iterable %} - {{ form_macros.build_template(options.CONTENT)}} + {{ FormsBuildTemplate(options.CONTENT)}} {% else %} {options.CONTENT} {% endif %} @@ -210,8 +209,7 @@
- {% from 'macros/form_macros.twig' import select %} - {{ select(EXT_GROUP_SIZE_OPTIONS) }} + {{ FormsSelect(EXT_GROUP_SIZE_OPTIONS) }}
diff --git a/phpBB/adm/style/acp_board.html b/phpBB/adm/style/acp_board.html index b809e2dfef..ae1525d586 100644 --- a/phpBB/adm/style/acp_board.html +++ b/phpBB/adm/style/acp_board.html @@ -13,7 +13,6 @@ -{% import 'macros/form_macros.twig' as form_macros %} @@ -30,7 +29,7 @@

{options.TITLE_EXPLAIN}
{% if options.CONTENT is iterable %} - {{ form_macros.build_template(options.CONTENT)}} + {{ FormsBuildTemplate(options.CONTENT)}} {% else %} {{ options.CONTENT }} {% endif %} diff --git a/phpBB/adm/style/acp_bots.html b/phpBB/adm/style/acp_bots.html index 2b9fec668f..b4f8ea5072 100644 --- a/phpBB/adm/style/acp_bots.html +++ b/phpBB/adm/style/acp_bots.html @@ -32,8 +32,7 @@

{L_BOT_LANG_EXPLAIN}
- {% import 'macros/form_macros.twig' as form_macros %} - {{ form_macros.select(LANG_OPTIONS) }} + {{ FormsSelect(LANG_OPTIONS) }}
diff --git a/phpBB/adm/style/acp_inactive.html b/phpBB/adm/style/acp_inactive.html index 81c019c47c..153a93fdb4 100644 --- a/phpBB/adm/style/acp_inactive.html +++ b/phpBB/adm/style/acp_inactive.html @@ -65,8 +65,7 @@
- {% from 'macros/form_macros.twig' import select %} - {{ select(INACTIVE_OPTIONS) }} + {{ FormsSelect(INACTIVE_OPTIONS) }}

{L_MARK_ALL}{L_UNMARK_ALL}

{S_FORM_TOKEN} diff --git a/phpBB/adm/style/acp_users_prefs.html b/phpBB/adm/style/acp_users_prefs.html index 11f6a71e20..68420389b4 100644 --- a/phpBB/adm/style/acp_users_prefs.html +++ b/phpBB/adm/style/acp_users_prefs.html @@ -43,8 +43,7 @@
- {% import 'macros/form_macros.twig' as form_macros %} - {{ form_macros.select(LANG_OPTIONS) }} + {{ FormsSelect(LANG_OPTIONS) }}
diff --git a/phpBB/adm/style/timezone_option.html b/phpBB/adm/style/timezone_option.html index 58b88cf6b4..9b0817902a 100644 --- a/phpBB/adm/style/timezone_option.html +++ b/phpBB/adm/style/timezone_option.html @@ -1,15 +1,14 @@
- {% from "macros/form_macros.twig" import select %} -
+
{% if TIMEZONE_OPTIONS %}
- {% set tz_date_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_CURRENT_TIME') }]|merge(TIMEZONE_OPTIONS.options) }) %} - {{ select(tz_date_data, 'autowidth tz_select', 'tz_date', 'tz_date', true) }} + {% set tz_date_data = TIMEZONE_OPTIONS | merge({options: [{ value: "", label: lang('SELECT_CURRENT_TIME')}] | merge(TIMEZONE_OPTIONS.options) }) %} + {{ FormsSelect(tz_date_data | merge({class: 'autowidth tz_select', id: 'tz_date', name: 'tz_date', group_only: true})) }}
{% endif %}
- {% set tz_select_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }]|merge(TIMEZONE_OPTIONS.options) }) %} - {{ select(tz_select_data, 'autowidth tz_select', 'timezone') }} + {% set tz_select_data = TIMEZONE_OPTIONS | merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }] | merge(TIMEZONE_OPTIONS.options) }) %} + {{ FormsSelect(tz_select_data | merge({class: 'autowidth tz_select', id: 'timezone'})) }} {% INCLUDEJS('timezone.js') %}
diff --git a/phpBB/config/default/container/services_twig.yml b/phpBB/config/default/container/services_twig.yml index 747d2ee155..8700630a73 100644 --- a/phpBB/config/default/container/services_twig.yml +++ b/phpBB/config/default/container/services_twig.yml @@ -69,6 +69,13 @@ services: template.twig.extensions.debug: class: Twig\Extension\DebugExtension + template.twig.extensions.forms: + class: phpbb\template\twig\extension\forms + arguments: + - '@user' + tags: + - { name: twig.extension } + template: class: phpbb\template\twig\twig arguments: diff --git a/phpBB/phpbb/template/twig/environment.php b/phpBB/phpbb/template/twig/environment.php index 5843cc0762..86c04195de 100644 --- a/phpBB/phpbb/template/twig/environment.php +++ b/phpBB/phpbb/template/twig/environment.php @@ -75,7 +75,7 @@ class environment extends \Twig\Environment $options = array_merge(array( 'cache' => (defined('IN_INSTALL')) ? false : $cache_path, - 'debug' => false, + 'debug' => true, 'auto_reload' => (bool) $this->phpbb_config['load_tplcompile'], 'autoescape' => false, ), $options); diff --git a/phpBB/phpbb/template/twig/extension/forms.php b/phpBB/phpbb/template/twig/extension/forms.php new file mode 100644 index 0000000000..21dcd79dca --- /dev/null +++ b/phpBB/phpbb/template/twig/extension/forms.php @@ -0,0 +1,217 @@ + + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +namespace phpbb\template\twig\extension; + +use phpbb\template\twig\environment; +use phpbb\user; +use Twig\Extension\AbstractExtension; +use Twig\TwigFunction; + +class forms extends AbstractExtension +{ + /** @var user */ + protected $user; + + /** + * Constructor. + * + * @param user $user User object + */ + public function __construct(user $user) + { + $this->user = $user; + } + + /** + * Returns the name of this extension. + * + * @return string The extension name + */ + public function getName() + { + return 'forms'; + } + + /** + * Returns a list of functions to add to the existing list. + * + * @return TwigFunction[] Array of twig functions + */ + public function getFunctions(): array + { + return [ + new TwigFunction('FormsBuildTemplate', [$this, 'build_template'], ['needs_environment' => true]), + new TwigFunction('FormsDimension', [$this, 'dimension'], ['needs_environment' => true]), + new TwigFunction('FormsInput', [$this, 'input'], ['needs_environment' => true]), + new TwigFunction('FormsRadioButtons', [$this, 'radio_buttons'], ['needs_environment' => true]), + new TwigFunction('FormsSelect', [$this, 'select'], ['needs_environment' => true]), + new TwigFunction('FormsTextarea', [$this, 'textarea'], ['needs_environment' => true]), + ]; + } + + /** + * Renders a form template + * + * @param environment $environment + * @param array $form_data + * + * @return string Rendered form template + */ + public function build_template(environment $environment, array $form_data): string + { + try + { + return $environment->render('macros/forms/build_template.twig', [ + 'form_data' => $form_data ?? [], + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } + + /** + * Renders form dimension fields + * + * @param environment $environment The twig environment + * @param array $form_data The form data + * + * @return string Form dimension fields + */ + public function dimension(environment $environment, array $form_data): string + { + try + { + return $environment->render('macros/forms/dimension.twig', [ + 'WIDTH' => $form_data['width'], + 'HEIGHT' => $form_data['height'], + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } + + /** + * Renders a form input field + * + * @param environment $environment The twig environment + * @param array $form_data The form data + * + * @return string Form input field + */ + public function input(environment $environment, array $form_data): string + { + try + { + return $environment->render('macros/forms/input.twig', [ + 'ID' => (string) $form_data['id'] ?? '', + 'TYPE' => (string) $form_data['type'], + 'NAME' => (string) $form_data['name'], + 'SIZE' => (int) $form_data['size'] ?? 0, + 'MAXLENGTH' => (int) $form_data['maxlength'] ?? 0, + 'MIN' => (int) $form_data['min'] ?? 0, + 'MAX' => (int) $form_data['max'] ?? 0, + 'STEP' => (int) $form_data['step'] ?? 0, + 'CHECKED' => (bool) $form_data['checked'] ?? false, + 'CLASS' => (string) $form_data['class'] ?? '', + 'VALUE' => (string) $form_data['value'], + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } + + /** + * Renders form radio buttons + * + * @param environment $environment The twig environment + * @param array $form_data The form data + * + * @return string Form radio buttons + */ + public function radio_buttons(environment $environment, array $form_data): string + { + 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'], + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } + + /** + * Renders a form select field + * + * @param environment $environment The twig environment + * @param array $form_data The form data + * + * @return string Form select field + */ + public function select(environment $environment, array $form_data): string + { + try + { + return $environment->render('macros/forms/select.twig', [ + 'ID' => (string) $form_data['id'] ?? '', + 'CLASS' => (string) $form_data['class'] ?? '', + 'NAME' => (string) $form_data['name'], + 'TOGGLEABLE' => (bool) $form_data['toggleable'] ?? false, + 'OPTIONS' => $form_data['options'] ?? [], + 'GROUP_ONLY' => (bool) $form_data['group_only'] ?? false, + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } + + /** + * Renders a form textarea field + * + * @param environment $environment + * @param array $form_data + * + * @return string Form textarea field + */ + public function textarea(environment $environment, array $form_data): string + { + try + { + return $environment->render('macros/forms/textarea.twig', [ + 'ID' => (string) $form_data['id'] ?? '', + 'NAME' => (string) $form_data['name'] ?? '', + 'ROWS' => (int) $form_data['rows'] ?? 1, + 'COLS' => (int) $form_data['cols'] ?? 1, + 'CONTENT' => (string) $form_data['content'] ?? '', + ]); + } + catch (\Twig\Error\Error $e) + { + return ''; + } + } +} diff --git a/phpBB/phpbb/template/twig/twig.php b/phpBB/phpbb/template/twig/twig.php index 1259618175..14dd5734ce 100644 --- a/phpBB/phpbb/template/twig/twig.php +++ b/phpBB/phpbb/template/twig/twig.php @@ -90,6 +90,7 @@ class twig extends \phpbb\template\base { $this->twig->addExtension($extension); } + $this->twig->addExtension(new \Twig\Extension\DebugExtension()); // Add admin namespace if ($this->path_helper->get_adm_relative_path() !== null diff --git a/phpBB/styles/all/template/macros/form_macros.twig b/phpBB/styles/all/template/macros/form_macros.twig deleted file mode 100644 index a4fa779ff8..0000000000 --- a/phpBB/styles/all/template/macros/form_macros.twig +++ /dev/null @@ -1,86 +0,0 @@ -{% macro input(form_data) %} - {% apply replace({"\n": ' ', '\t': ''}) %} - - {% 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': ''}) %} - - {% endapply %} -{% endmacro %} - -{% macro radio_buttons(form_data) %} - - -{% endmacro %} - -{% macro select(form_data, class, id, name, group_only) %} - {% apply replace({"\n": ' ', '\t': ''}) %} - -{% 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 %} diff --git a/phpBB/styles/all/template/macros/forms/build_template.twig b/phpBB/styles/all/template/macros/forms/build_template.twig new file mode 100644 index 0000000000..fb63e3e1ed --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/build_template.twig @@ -0,0 +1,16 @@ +{% if form_data.tag == 'input' %} + {{ FormsInput(form_data) }} +{% elseif form_data.tag == 'dimension' %} + {{ FormsDimension(form_data) }} +{% elseif form_data.tag == 'radio' %} + {{ FormsRadioButtons(form_data) }} +{% elseif form_data.tag == 'select' %} + {{ FormsSelect(form_data) }} +{% elseif form_data.tag == 'textarea' %} + {{ FormsTextarea(form_data) }} +{% elseif form_data[0] %} + {% for element in form_data %} + {{ FormsBuildTemplate(element) }} + {% endfor %} +{% endif %} +{% if form_data.append %}{{ form_data.append }}{% endif %} diff --git a/phpBB/styles/all/template/macros/forms/dimension.twig b/phpBB/styles/all/template/macros/forms/dimension.twig new file mode 100644 index 0000000000..05bf563168 --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/dimension.twig @@ -0,0 +1 @@ +{{ FormsInput(WIDTH) }} x {{ FormsInput(HEIGHT) }} diff --git a/phpBB/styles/all/template/macros/forms/input.twig b/phpBB/styles/all/template/macros/forms/input.twig new file mode 100644 index 0000000000..cf58826235 --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/input.twig @@ -0,0 +1,15 @@ +{% apply replace({"\n": ' ', "\t": ''}) %} + +{% endapply %} diff --git a/phpBB/styles/all/template/macros/forms/radio_buttons.twig b/phpBB/styles/all/template/macros/forms/radio_buttons.twig new file mode 100644 index 0000000000..1ef7804c29 --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/radio_buttons.twig @@ -0,0 +1,2 @@ + + diff --git a/phpBB/styles/all/template/macros/forms/select.twig b/phpBB/styles/all/template/macros/forms/select.twig new file mode 100644 index 0000000000..2f16a1afe2 --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/select.twig @@ -0,0 +1,26 @@ +{% apply replace({"\n": ' ', "\t": ''}) %} + diff --git a/phpBB/styles/all/template/macros/forms/textarea.twig b/phpBB/styles/all/template/macros/forms/textarea.twig new file mode 100644 index 0000000000..51a4c98ee6 --- /dev/null +++ b/phpBB/styles/all/template/macros/forms/textarea.twig @@ -0,0 +1,9 @@ +{% apply replace({"\n": ' ', '\t': ''}) %} + +{% endapply %} diff --git a/phpBB/styles/prosilver/template/timezone_option.html b/phpBB/styles/prosilver/template/timezone_option.html index 47da5f63c1..0c7be8de74 100644 --- a/phpBB/styles/prosilver/template/timezone_option.html +++ b/phpBB/styles/prosilver/template/timezone_option.html @@ -1,16 +1,15 @@
- {% from "macros/form_macros.twig" import select %}
{% if TIMEZONE_OPTIONS %}
- {% set tz_date_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_CURRENT_TIME') }]|merge(TIMEZONE_OPTIONS.options) }) %} - {{ select(tz_date_data, 'autowidth tz_select', 'tz_date', 'tz_date', true) }} + {% set tz_date_data = TIMEZONE_OPTIONS | merge({ options: [{ value: "", label: lang('SELECT_CURRENT_TIME') }] | merge(TIMEZONE_OPTIONS.options) }) %} + {{ FormsSelect(tz_date_data | merge({class: 'autowidth tz_select', id: 'tz_date', name: 'tz_date', group_only: true})) }}
{% endif %}
- {% set tz_select_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }]|merge(TIMEZONE_OPTIONS.options) }) %} - {{ select(tz_select_data, 'autowidth tz_select timezone', 'timezone') }} + {% set tz_select_data = TIMEZONE_OPTIONS | merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }] | merge(TIMEZONE_OPTIONS.options) }) %} + {{ FormsSelect(tz_select_data | merge({class: 'autowidth tz_select', id: 'timezone'})) }} {% INCLUDEJS('timezone.js') %}
diff --git a/phpBB/styles/prosilver/template/ucp_agreement.html b/phpBB/styles/prosilver/template/ucp_agreement.html index 69458e198a..94d280a31a 100644 --- a/phpBB/styles/prosilver/template/ucp_agreement.html +++ b/phpBB/styles/prosilver/template/ucp_agreement.html @@ -6,8 +6,7 @@

- {% import "macros/form_macros.twig" as form_macros %} - {{ form_macros.select(LANG_OPTIONS) }} + {{ FormsSelect(LANG_OPTIONS) }} {S_HIDDEN_FIELDS}

diff --git a/phpBB/styles/prosilver/template/ucp_prefs_personal.html b/phpBB/styles/prosilver/template/ucp_prefs_personal.html index 2c5ff918e5..4e81b78fb3 100644 --- a/phpBB/styles/prosilver/template/ucp_prefs_personal.html +++ b/phpBB/styles/prosilver/template/ucp_prefs_personal.html @@ -54,8 +54,7 @@
- {% import "macros/form_macros.twig" as form_macros %} - {{ form_macros.select(LANG_OPTIONS) }} + {{ FormsSelect(LANG_OPTIONS) }}
diff --git a/phpBB/styles/prosilver/template/ucp_register.html b/phpBB/styles/prosilver/template/ucp_register.html index ad45812ab0..0956317497 100644 --- a/phpBB/styles/prosilver/template/ucp_register.html +++ b/phpBB/styles/prosilver/template/ucp_register.html @@ -61,8 +61,7 @@
- {% import "macros/form_macros.twig" as form_macros %} - {{ form_macros.select(LANG_OPTIONS) }} + {{ FormsSelect(LANG_OPTIONS) }}