Ejemplo n.º 1
0
/**
 * Returns a <select> tag populated with all the timezones in the world.
 *
 * The select_timezone_tag builds off the traditional select_tag function, and is conveniently populated with 
 * all the timezones in the world (sorted alphabetically). Each option in the list has a unique timezone identifier 
 * for its value and the timezone's locale as its display title.  The timezone data is retrieved via the sfCultureInfo
 * class, which stores a wide variety of i18n and i10n settings for various countries and cultures throughout the world.
 * Here's an example of an <option> tag generated by the select_timezone_tag:
 *
 * <b>Options:</b>
 * - display - 
 *     identifer         - Display the PHP timezone identifier (e.g. America/Denver)
 *     timezone          - Display the full timezone name (e.g. Mountain Standard Time)
 *     timezone_abbr     - Display the timezone abbreviation (e.g. MST)
 *     timzone_dst       - Display the full timezone name with daylight savings time (e.g. Mountain Daylight Time)
 *     timezone_dst_abbr - Display the timezone abbreviation with daylight savings time (e.g. MDT)
 *     city              - Display the city/region that relates to the timezone (e.g. Denver)
 * 
 * <samp>
 *  <option value="America/Denver">America/Denver</option>
 * </samp>
 *
 * <b>Examples:</b>
 * <code>
 *  echo select_timezone_tag('timezone', 'America/Denver');
 * </code>
 *
 * @param  string field name 
 * @param  string selected field value (timezone identifier)
 * @param  array  additional HTML compliant <select> tag parameters
 * @return string <select> tag populated with all the timezones in the world.
 * @see select_tag, options_for_select, sfCultureInfo
 */
function select_timezone_tag($name, $selected = null, $options = array())
{
    if (!isset($options['display'])) {
        $options['display'] = 'identifier';
    }
    $c = new sfCultureInfo(sfContext::getInstance()->getUser()->getCulture());
    $timezone_groups = $c->getTimeZones();
    $display_key = 0;
    switch ($options['display']) {
        case "identifier":
            $display_key = 0;
            break;
        case "timezone":
            $display_key = 1;
            break;
        case "timezone_abbr":
            $display_key = 2;
            break;
        case "timezone_dst":
            $display_key = 3;
            break;
        case "timezone_dst_abbr":
            $display_key = 4;
            break;
        case "city":
            $display_key = 5;
            break;
        default:
            $display_key = 0;
            break;
    }
    unset($options['display']);
    $timezones = array();
    foreach ($timezone_groups as $tz_group_key => $tz_group) {
        $array_key = null;
        foreach ($tz_group as $tz_key => $tz) {
            if ($tz_key == 0) {
                $array_key = $tz;
            }
            if ($tz_key == $display_key and !empty($tz)) {
                $timezones[$array_key] = $tz;
            }
        }
    }
    if ($timezone_option = _get_option($options, 'timezones')) {
        $diff = array_diff_key($timezones, array_flip((array) $timezone_option));
        foreach ($diff as $key => $v) {
            unset($timezones[$key]);
        }
    }
    // Remove duplicate values
    $timezones = array_unique($timezones);
    asort($timezones);
    $option_tags = options_for_select($timezones, $selected);
    return select_tag($name, $option_tags, $options);
}
$countries_en = $c_en->getCountries();
$countries_fr = $c_fr->getCountries();
$t->is($countries_en['ES'], 'Spain', '->getCountries() returns a list of countries in the language of the localized version');
$t->is($countries_fr['ES'], 'Espagne', '->getCountries() returns a list of countries in the language of the localized version');
$t->is($countries_en, $c_en->Countries, '->getCountries() is equivalent to ->Countries');
// ->getScripts()
$t->diag('->getScripts()');
$scripts_en = $c_en->getScripts();
$scripts_fr = $c_fr->getScripts();
$t->is($scripts_en['Arab'], 'Arabic', '->getScripts() returns a list of scripts in the language of the localized version');
$t->is($scripts_fr['Arab'], 'arabe', '->getScripts() returns a list of scripts in the language of the localized version');
$t->is($scripts_en, $c_en->Scripts, '->getScripts() is equivalent to ->Scripts');
// ->getTimeZones()
$t->diag('->getTimeZones()');
$time_zones_en = $c_en->getTimeZones();
$time_zones_fr = $c_fr->getTimeZones();
$t->is($time_zones_en[1][0], 'America/Los_Angeles', '->getTimeZones() returns a list of time zones in the language of the localized version');
$t->is($time_zones_fr[1][0], 'America/Vancouver', '->getTimeZones() returns a list of time zones in the language of the localized version');
$t->is($time_zones_en, $c_en->TimeZones, '->getTimeZones() is equivalent to ->TimeZones');
// ->validCulture()
$t->diag('->validCulture()');
$t->is($c->validCulture('fr'), true, '->validCulture() returns true if the culture is valid');
$t->is($c->validCulture('fr_FR'), true, '->validCulture() returns true if the culture is valid');
foreach (array('xxx', 'pp', 'frFR') as $culture) {
    $t->is($c->validCulture($culture), false, '->validCulture() returns false if the culture does not exist');
}
// ::getCultures()
$t->diag('::getCultures()');
$cultures = sfCultureInfo::getCultures();
$t->is(in_array('fr', $cultures), true, '::getCultures() returns an array of all available cultures');
$t->is(in_array('fr_FR', $cultures), true, '::getCultures() returns an array of all available cultures');