[ticket/17100] Move timezone select HTML from PHP file

PHPBB3-17100
This commit is contained in:
Marc Alexander 2022-06-01 21:08:12 +02:00
parent 540097eed7
commit 015472ab91
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
10 changed files with 113 additions and 86 deletions

View file

@ -37,10 +37,30 @@
<label>{{ _self.input(form_data.buttons[1]) ~ form_data.buttons[1].label }}</label> <label>{{ _self.input(form_data.buttons[1]) ~ form_data.buttons[1].label }}</label>
{% endmacro %} {% endmacro %}
{% macro select(form_data) %} {% macro select(form_data, class, id, name, group_only) %}
<select {% if form_data.id %}id="{{ form_data.id }}"{% endif %} name="{{ form_data.name }}"{% if form_data.toggleable %} data-togglable-settings="true"{% endif %}> {% apply replace({"\n": ' ', '\t': ''}) %}
{% for option in form_data.options %} <select
<option value="{{ option.value }}"{% if option.selected %} selected="selected"{% endif %}>{{ option.label }}</option> {% if id %}id="{{ id }}"{% endif %}
{% if class %}class="{{ class }}"{% endif %}
name="{% if name %}{{ name }}{% else %}{{ form_data.name }}{% endif %}"
{% if form_data.toggleable %}data-togglable-settings="true"{% endif %}>
{% endapply %}
{% for element in form_data.options %}
{% if not group_only and element.options %}
{% apply replace({"\n": ' ', '\t': ''}) %}
<optgroup
label="{{ element.label }}"
{% for key, value in element.data %}
data-{{ key }}="{{ value }}"
{% endfor %}>
{% endapply %}
{% for option in element.options %}
<option value="{{ option.value }}"{% if option.selected %} selected="selected"{% endif %}>{{ option.label }}</option>
{% endfor %}
</optgroup>
{% else %}
<option value="{{ element.value }}"{% if element.selected %} selected="selected"{% endif %}>{{ element.label }}</option>
{% endif %}
{% endfor %} {% endfor %}
</select> </select>
{% endmacro %} {% endmacro %}

View file

@ -1,27 +1,16 @@
<dl> <dl>
<dt><label for="timezone">{L_BOARD_TIMEZONE}{L_COLON}</label></dt> {% from "form_macros.twig" import select %}
<!-- IF .timezone_date --> <dt><label for="timezone">{{ lang('BOARD_TIMEZONE') | lang('COLON') }}</label></dt>
<dd id="tz_select_date" style="display: none;"> {% if TIMEZONE_OPTIONS %}
<select name="tz_date" id="tz_date" class="autowidth tz_select"> <dd id="tz_select_date hidden">
<option value="">{L_SELECT_CURRENT_TIME}</option> {% set tz_date_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_CURRENT_TIME') }]|merge(TIMEZONE_OPTIONS.options) }) %}
<!-- BEGIN timezone_date --> {{ select(tz_date_data, 'autowidth tz_select', 'tz_date', 'tz_date', true) }}
<option value="{timezone_date.VALUE}"<!-- IF timezone_date.SELECTED --> selected="selected"<!-- ENDIF -->>{timezone_date.TITLE}</option>
<!-- END timezone_date -->
</select>
</dd> </dd>
<!-- ENDIF --> {% endif %}
<dd> <dd>
<select name="tz" id="timezone" class="autowidth tz_select"> {% set tz_select_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }]|merge(TIMEZONE_OPTIONS.options) }) %}
<option value="">{L_SELECT_TIMEZONE}</option> {{ select(tz_select_data, 'autowidth tz_select', 'timezone') }}
<!-- BEGIN timezone_select -->
<optgroup label="{timezone_select.LABEL}" data-tz-value="{timezone_select.VALUE}">
<!-- BEGIN timezone_options -->
<option title="{timezone_select.timezone_options.TITLE}" value="{timezone_select.timezone_options.VALUE}"<!-- IF timezone_select.timezone_options.SELECTED --> selected="selected"<!-- ENDIF -->>{timezone_select.timezone_options.LABEL}</option>
<!-- END timezone_options -->
</optgroup>
<!-- END timezone_select -->
</select>
<!-- INCLUDEJS timezone.js --> {% INCLUDEJS('timezone.js') %}
</dd> </dd>
</dl> </dl>

View file

@ -1052,11 +1052,15 @@ class acp_board
*/ */
function timezone_select($value, $key) 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 '<select name="config[' . $key . ']" id="' . $key . '">' . $timezone_select . '</select>'; return [
'tag' => 'select',
'name' => 'config[' . $key . ']',
'options' => $timezone_select,
];
} }
/** /**

View file

@ -1795,7 +1795,7 @@ class acp_users
${'s_sort_' . $sort_option . '_dir'} .= '</select>'; ${'s_sort_' . $sort_option . '_dir'} .= '</select>';
} }
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']); $lang_options = phpbb_language_select($db, $data['lang']);
$user_prefs_data = array( $user_prefs_data = array(
@ -1839,6 +1839,11 @@ class acp_users
'options' => $lang_options, 'options' => $lang_options,
], ],
'S_STYLE_OPTIONS' => style_select($data['style']), 'S_STYLE_OPTIONS' => style_select($data['style']),
'TIMEZONE_OPTIONS' => [
'tag' => 'select',
'name' => 'tz',
'options' => $timezone_select,
],
); );
/** /**

View file

@ -443,14 +443,13 @@ function phpbb_get_timezone_identifiers($selected_timezone)
/** /**
* Options to pick a timezone and date/time * 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 \phpbb\user $user Object of the current user
* @param string $default A timezone to select * @param string $default A timezone to select
* @param boolean $truncate Shall we truncate the options text * @param boolean $truncate Shall we truncate the options text
* *
* @return string Returns an array containing the options for the time selector. * @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; static $timezones;
@ -482,27 +481,22 @@ function phpbb_timezone_select($template, $user, $default = '', $truncate = fals
uksort($timezones, 'phpbb_tz_select_compare'); uksort($timezones, 'phpbb_tz_select_compare');
} }
$tz_select = $opt_group = ''; $opt_group = '';
$tz_data = [];
foreach ($timezones as $key => $timezone) foreach ($timezones as $key => $timezone)
{ {
if ($opt_group != $timezone['offset']) if ($opt_group != $timezone['offset'])
{ {
// Generate tz_select for backwards compatibility $tz_data[$timezone['offset']] = [
$tz_select .= ($opt_group) ? '</optgroup>' : ''; 'label' => $user->lang(array('timezones', 'UTC_OFFSET_CURRENT'), $timezone['offset'], $timezone['current']),
$tz_select .= '<optgroup label="' . $user->lang(array('timezones', 'UTC_OFFSET_CURRENT'), $timezone['offset'], $timezone['current']) . '">'; 'value' => $key . ' - ' . $timezone['current'],
$opt_group = $timezone['offset']; 'options' => [],
$template->assign_block_vars('timezone_select', array( 'selected' => !empty($default_offset) && strpos($key, $default_offset) !== false,
'LABEL' => $user->lang(array('timezones', 'UTC_OFFSET_CURRENT'), $timezone['offset'], $timezone['current']), 'data' => ['tz-value' => $key . ' - ' . $timezone['current']],
'VALUE' => $key . ' - ' . $timezone['current'], ];
));
$selected = (!empty($default_offset) && strpos($key, $default_offset) !== false) ? ' selected="selected"' : ''; $opt_group = $timezone['offset'];
$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']),
));
} }
$label = $timezone['tz']; $label = $timezone['tz'];
@ -517,19 +511,15 @@ function phpbb_timezone_select($template, $user, $default = '', $truncate = fals
$label = truncate_string($label, 50, 255, false, '...'); $label = truncate_string($label, 50, 255, false, '...');
} }
// Also generate timezone_select for backwards compatibility $tz_data[$timezone['offset']]['options'][] = [
$selected = ($timezone['tz'] === $default) ? ' selected="selected"' : '';
$tz_select .= '<option title="' . $title . '" value="' . $timezone['tz'] . '"' . $selected . '>' . $label . '</option>';
$template->assign_block_vars('timezone_select.timezone_options', array(
'TITLE' => $title, 'TITLE' => $title,
'VALUE' => $timezone['tz'], 'value' => $timezone['tz'],
'SELECTED' => !empty($selected), 'selected' => $timezone['tz'] === $default,
'LABEL' => $label, 'label' => $label,
)); ];
} }
$tz_select .= '</optgroup>';
return $tz_select; return $tz_data;
} }
// Functions handling topic/post tracking/marking // Functions handling topic/post tracking/marking

View file

@ -111,9 +111,9 @@ function phpbb_clean_path($path)
*/ */
function tz_select($default = '', $truncate = false) 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);
} }
/** /**

View file

@ -156,7 +156,7 @@ class ucp_prefs
} }
$dateformat_options .= '>' . $user->lang['CUSTOM_DATEFORMAT'] . '</option>'; $dateformat_options .= '>' . $user->lang['CUSTOM_DATEFORMAT'] . '</option>';
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 // check if there are any user-selectable languages
$sql = 'SELECT lang_iso, lang_local_name $sql = 'SELECT lang_iso, lang_local_name
@ -206,6 +206,11 @@ class ucp_prefs
'options' => $lang_options, 'options' => $lang_options,
], ],
'S_STYLE_OPTIONS' => ($config['override_user_style']) ? '' : style_select($data['user_style'], false, $styles_row), '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_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) 'S_SELECT_NOTIFY' => ($config['jab_enable'] && $user->data['user_jabber'] && @extension_loaded('xml')) ? true : false)
); );

View file

@ -626,7 +626,7 @@ class ucp_register
} }
// Assign template vars for timezone select // 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 // Checking amount of available languages
$sql = 'SELECT lang_iso, lang_local_name $sql = 'SELECT lang_iso, lang_local_name
@ -653,6 +653,11 @@ class ucp_register
'name' => 'lang', 'name' => 'lang',
'options' => $lang_options, 'options' => $lang_options,
], ],
'TIMEZONE_OPTIONS' => [
'tag' => 'select',
'name' => 'tz',
'options' => $timezone_select,
],
'S_TZ_PRESELECT' => !$submit, 'S_TZ_PRESELECT' => !$submit,
'S_CONFIRM_REFRESH' => ($config['enable_confirm'] && $config['confirm_refresh']) ? true : false, 'S_CONFIRM_REFRESH' => ($config['enable_confirm'] && $config['confirm_refresh']) ? true : false,
'S_REGISTRATION' => true, 'S_REGISTRATION' => true,

View file

@ -37,11 +37,31 @@
<label>{{ _self.input(form_data.buttons[1]) ~ form_data.buttons[1].label }}</label> <label>{{ _self.input(form_data.buttons[1]) ~ form_data.buttons[1].label }}</label>
{% endmacro %} {% endmacro %}
{% macro select(form_data) %} {% macro select(form_data, class, id, name, group_only) %}
<select {% if form_data.id %}id="{{ form_data.id }}"{% endif %} name="{{ form_data.name }}"{% if form_data.toggleable %} data-togglable-settings="true"{% endif %}> {% apply replace({"\n": ' ', '\t': ''}) %}
{% for option in form_data.options %} <select
<option value="{{ option.value }}"{% if option.selected %} selected="selected"{% endif %}>{{ option.label }}</option> {% if id %}id="{{ id }}"{% endif %}
{% endfor %} {% if class %}class="{{ class }}"{% endif %}
name="{% if name %}{{ name }}{% else %}{{ form_data.name }}{% endif %}"
{% if form_data.toggleable %}data-togglable-settings="true"{% endif %}>
{% endapply %}
{% for element in form_data.options %}
{% if not group_only and element.options %}
{% apply replace({"\n": ' ', '\t': ''}) %}
<optgroup
label="{{ element.label }}"
{% for key, value in element.data %}
data-{{ key }}="{{ value }}"
{% endfor %}>
{% endapply %}
{% for option in element.options %}
<option value="{{ option.value }}"{% if option.selected %} selected="selected"{% endif %}>{{ option.label }}</option>
{% endfor %}
</optgroup>
{% else %}
<option value="{{ element.value }}"{% if element.selected %} selected="selected"{% endif %}>{{ element.label }}</option>
{% endif %}
{% endfor %}
</select> </select>
{% endmacro %} {% endmacro %}

View file

@ -1,28 +1,17 @@
<dl> <dl>
<dt><label for="timezone">{L_BOARD_TIMEZONE}{L_COLON}</label></dt> {% from "macros/form_macros.twig" import select %}
<!-- IF .timezone_date --> <dt><label for="timezone">{{ lang('BOARD_TIMEZONE') ~ lang('COLON') }}</label></dt>
<dd id="tz_select_date" style="display: none;"> {% if TIMEZONE_OPTIONS %}
<select name="tz_date" id="tz_date" class="autowidth tz_select"> <dd id="tz_select_date hidden">
<option value="">{L_SELECT_CURRENT_TIME}</option> {% set tz_date_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_CURRENT_TIME') }]|merge(TIMEZONE_OPTIONS.options) }) %}
<!-- BEGIN timezone_date --> {{ select(tz_date_data, 'autowidth tz_select', 'tz_date', 'tz_date', true) }}
<option value="{timezone_date.VALUE}"<!-- IF timezone_date.SELECTED --> selected="selected"<!-- ENDIF -->>{timezone_date.TITLE}</option>
<!-- END timezone_date -->
</select>
<input type="button" id="tz_select_date_suggest" class="button1 button button-form-bold" style="display: none;" timezone-preselect="<!-- IF S_TZ_PRESELECT -->true<!-- ELSE -->false<!-- ENDIF -->" data-l-suggestion="{L_TIMEZONE_DATE_SUGGESTION}" value="{L_TIMEZONE_DATE_SUGGESTION}" /> <input type="button" id="tz_select_date_suggest" class="button1 button button-form-bold" style="display: none;" timezone-preselect="<!-- IF S_TZ_PRESELECT -->true<!-- ELSE -->false<!-- ENDIF -->" data-l-suggestion="{L_TIMEZONE_DATE_SUGGESTION}" value="{L_TIMEZONE_DATE_SUGGESTION}" />
</dd> </dd>
<!-- ENDIF --> {% endif %}
<dd> <dd>
<select name="tz" id="timezone" class="autowidth tz_select timezone"> {% set tz_select_data = TIMEZONE_OPTIONS|merge({ options: [{ value: "", label: lang('SELECT_TIMEZONE') }]|merge(TIMEZONE_OPTIONS.options) }) %}
<option value="">{L_SELECT_TIMEZONE}</option> {{ select(tz_select_data, 'autowidth tz_select timezone', 'timezone') }}
<!-- BEGIN timezone_select -->
<optgroup label="{timezone_select.LABEL}" data-tz-value="{timezone_select.VALUE}">
<!-- BEGIN timezone_options -->
<option title="{timezone_select.timezone_options.TITLE}" value="{timezone_select.timezone_options.VALUE}"<!-- IF timezone_select.timezone_options.SELECTED --> selected="selected"<!-- ENDIF -->>{timezone_select.timezone_options.LABEL}</option>
<!-- END timezone_options -->
</optgroup>
<!-- END timezone_select -->
</select>
<!-- INCLUDEJS timezone.js --> {% INCLUDEJS('timezone.js') %}
</dd> </dd>
</dl> </dl>