Esempio n. 1
0
/**
* Options to pick a timezone and date/time
*
* @param	\src\template\template $template	src template object
* @param	\src\user	$user				Object of the current user
* @param	string		$default			A timezone to select
* @param	boolean		$truncate			Shall we truncate the options text
*
* @return		array		Returns an array containing the options for the time selector.
*/
function src_timezone_select($template, $user, $default = '', $truncate = false)
{
    static $timezones;
    $default_offset = '';
    if (!isset($timezones)) {
        $unsorted_timezones = src_get_timezone_identifiers($default);
        $timezones = array();
        foreach ($unsorted_timezones as $timezone) {
            $tz = new DateTimeZone($timezone);
            $dt = $user->create_datetime('now', $tz);
            $offset = $dt->getOffset();
            $current_time = $dt->format($user->lang['DATETIME_FORMAT'], true);
            $offset_string = src_format_timezone_offset($offset, true);
            $timezones['UTC' . $offset_string . ' - ' . $timezone] = array('tz' => $timezone, 'offset' => $offset_string, 'current' => $current_time);
            if ($timezone === $default) {
                $default_offset = 'UTC' . $offset_string;
            }
        }
        unset($unsorted_timezones);
        uksort($timezones, 'src_tz_select_compare');
    }
    $tz_select = $opt_group = '';
    foreach ($timezones as $key => $timezone) {
        if ($opt_group != $timezone['offset']) {
            // Generate tz_select for backwards compatibility
            $tz_select .= $opt_group ? '</optgroup>' : '';
            $tz_select .= '<optgroup label="' . $user->lang(array('timezones', 'UTC_OFFSET_CURRENT'), $timezone['offset'], $timezone['current']) . '">';
            $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']));
            $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'])));
        }
        $label = $timezone['tz'];
        if (isset($user->lang['timezones'][$label])) {
            $label = $user->lang['timezones'][$label];
        }
        $title = $user->lang(array('timezones', 'UTC_OFFSET_CURRENT'), $timezone['offset'], $label);
        if ($truncate) {
            $label = truncate_string($label, 50, 255, false, '...');
        }
        // Also generate timezone_select for backwards compatibility
        $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, 'VALUE' => $timezone['tz'], 'SELECTED' => !empty($selected), 'LABEL' => $label));
    }
    $tz_select .= '</optgroup>';
    return $tz_select;
}
Esempio n. 2
0
/**
* Validate Timezone Name
*
* Tests whether a timezone name is valid
*
* @param string $timezone	The timezone string to test
*
* @return bool|string		Either false if validation succeeded or
*							a string which will be used as the error message
*							(with the variable name appended)
*/
function src_validate_timezone($timezone)
{
    return in_array($timezone, src_get_timezone_identifiers($timezone)) ? false : 'TIMEZONE_INVALID';
}