silverstripe-framework/forms/LanguageDropdownField.php
Ingo Schommer 095084fb22 BUGFIX Using array_diff_key() instead of array_diff() in LanguageDropdownField to avoid language with existing translations showing up in the "available translations" dropdown for page CMS fields
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@70321 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-01-18 23:07:51 +00:00

65 lines
2.0 KiB
PHP
Executable File

<?php
/**
* An extension to dropdown field, pre-configured to list languages.
* The languages already used in the site will be on top.
* @package forms
* @subpackage fields-relational
*/
class LanguageDropdownField extends GroupedDropdownField {
/**
* Create a new LanguageDropdownField
* @param string $name
* @param string $title
* @param array $dontInclude list of languages that won't be included
* @param string $translatingClass Name of the class with translated instances where to look for used languages
* @param string $list Indicates the source language list. Can be either Common-English, Common-Native Locale
*/
function __construct($name, $title, $dontInclude = array(), $translatingClass = 'SiteTree', $list = 'Common-English' ) {
$usedlangs = array_diff(
Translatable::get_existing_content_languages($translatingClass),
$dontInclude
);
// we accept in dontInclude both language codes and names, so another diff is required
$usedlangs = array_diff_key(
$usedlangs,
array_flip($dontInclude)
);
if (isset($usedlangs[Translatable::default_lang()])) unset($usedlangs[Translatable::default_lang()]);
if ('Common-English' == $list) $languageList = i18n::get_common_languages();
else if ('Common-Native' == $list) $languageList = i18n::get_common_languages(true);
else $languageList = i18n::get_locale_list();
$alllangs = array_diff(
$languageList,
(array)$usedlangs,
$dontInclude
);
$alllangs = array_flip(array_diff(
array_flip($alllangs),
$dontInclude
));
if (isset($alllangs[Translatable::default_lang()])) unset($alllangs[Translatable::default_lang()]);
asort($alllangs);
if (count($usedlangs)) {
asort($usedlangs);
$labelAvail = _t('Form.LANGAVAIL', "Available languages");
$labelOther = _t('Form.LANGAOTHER', "Other languages");
parent::__construct($name, $title, array(
$labelAvail => $usedlangs,
$labelOther => $alllangs
),
reset($usedlangs)
);
}
else {
parent::__construct($name, $title, $alllangs);
}
}
}
?>