1: <?php
2: namespace TIC\FormBundle\Form\Type;
3:
4: #use Symfony\Component\Form\AbstractType as BaseType;
5: use TIC\FormBundle\Base\TICWidgetType as BaseType;
6:
7: use Symfony\Component\OptionsResolver\OptionsResolver;
8: use Symfony\Component\OptionsResolver\Options;
9:
10: use Symfony\Component\Intl;
11:
12: /**
13: * Sélecteur d'internationalisation (avec listes de choix prédéfinis).
14: * Remplace : LocaleType, LanguageType, CountryType, CurrencyType
15: */
16: class LocaleType extends BaseType
17: {
18: protected $form_parent = ChoiceType::class;
19: protected $default_prefix = '<globe>';
20: protected $default_suffix = '<globe>';
21:
22:
23: /**
24: * {@inheritdoc}
25: */
26: public function configureOptions(OptionsResolver $resolver): void
27: {
28: parent::configureOptions($resolver);
29:
30: $resolver->setDefaults(array(
31: 'type' => 'locale', // type de liste prédéfinie ('locale', 'language', 'country', 'currency')
32: 'prefix' => true,
33: 'with_js' => true, // application de selectize.js sur le select
34: # 'plugins' => array(), // remove_button, restore_on_backspace, optgroup_columns, drag_drop
35: # 'remove' => true, // pour ajout par défaut du remove_button quand multiple
36: # 'sortable' => false, // pour pouvoir ordonner les choix sélectionnés (active le plugin drag_drop)
37: # 'excluded_choices' => array(), // liste de valeurs à exclure de la liste des choix
38: 'only_choices' => array(),
39:
40: 'choices' => function (Options $options) {
41: switch ($options['type']) {
42: case 'locale' : return array_flip(Intl\Locales::getNames()); // fr, fr_FR
43: case 'language' : return array_flip(Intl\Languages::getNames()); // fr, fr_CA
44: case 'country' : return array_flip(Intl\Countries::getNames()); // FR
45: case 'currency' : return array_flip(Intl\Currencies::getNames()); // EUR, FRF
46: }
47: },
48: 'preferred_choices' => function (Options $options) {
49: switch ($options['type']) {
50: case 'locale' : return array('fr_FR');
51: case 'language' : return array('fr');
52: case 'country' : return array('FR');
53: case 'currency' : return array('EUR');
54: }
55: },
56: ));
57:
58: $resolver->setAllowedTypes('type', array('string'));
59: $resolver->setAllowedValues('type', array('locale', 'language', 'country', 'currency'));
60:
61: $resolver->setNormalizer('choices', function (Options $options, $choices) {
62: if ($options['only_choices']) $choices = array_intersect($choices, $options['only_choices']);
63: return array_diff($choices, $options['excluded_choices']);
64: });
65: }
66:
67:
68: }
69: