mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 22:04:23 +00:00
Order countries in alphabetical order in every language - close #360
This commit is contained in:
parent
654789d40a
commit
070b184dbe
87
Form/Type/DataTransformer/ObjectToIdTransformer.php
Normal file
87
Form/Type/DataTransformer/ObjectToIdTransformer.php
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Chill is a software for social workers
|
||||||
|
* Copyright (C) 2014 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Form\Type\DataTransformer;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\DataTransformerInterface;
|
||||||
|
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||||
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
|
|
||||||
|
class ObjectToIdTransformer implements DataTransformerInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var ObjectManager
|
||||||
|
*/
|
||||||
|
private $om;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ObjectManager $om
|
||||||
|
*/
|
||||||
|
public function __construct(ObjectManager $om, $class)
|
||||||
|
{
|
||||||
|
$this->om = $om;
|
||||||
|
$this->class = $class;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms an object to a string (id)
|
||||||
|
*
|
||||||
|
* @param Object|null $Object
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function transform($object)
|
||||||
|
{
|
||||||
|
if (!$object) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $object->getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms a string (id) to an object
|
||||||
|
*
|
||||||
|
* @param string $id
|
||||||
|
* @return Object|null
|
||||||
|
* @throws TransformationFailedException if object is not found.
|
||||||
|
*/
|
||||||
|
public function reverseTransform($id)
|
||||||
|
{
|
||||||
|
if (!$id) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$object = $this->om
|
||||||
|
->getRepository($this->class)
|
||||||
|
->find($id)
|
||||||
|
;
|
||||||
|
|
||||||
|
if (! $object) {
|
||||||
|
throw new TransformationFailedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Chill is a software for social workers
|
* Chill is a software for social workers
|
||||||
* Copyright (C) 2014 Julien Fastré <julien.fastre@champs-libres.coop>
|
* Copyright (C) 2014 Champs-Libres <info@champs-libres.coop>
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU Affero General Public License as published by
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
@ -25,6 +25,10 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
|
|||||||
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
|
||||||
use Chill\MainBundle\Entity\Country;
|
use Chill\MainBundle\Entity\Country;
|
||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
|
use Doctrine\ORM\EntityRepository;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Chill\MainBundle\Form\Type\DataTransformer\ObjectToIdTransformer;
|
||||||
|
use Doctrine\Common\Persistence\ObjectManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extends choice to allow adding select2 library on widget
|
* Extends choice to allow adding select2 library on widget
|
||||||
@ -34,33 +38,52 @@ use Symfony\Component\HttpFoundation\RequestStack;
|
|||||||
class Select2CountryType extends AbstractType
|
class Select2CountryType extends AbstractType
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @var RequestStack
|
* @var RequestStack
|
||||||
*/
|
*/
|
||||||
private $requestStack;
|
private $requestStack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ObjectManager
|
||||||
|
*/
|
||||||
|
private $em;
|
||||||
|
|
||||||
public function __construct(RequestStack $requestStack)
|
public function __construct(RequestStack $requestStack,ObjectManager $em)
|
||||||
{
|
{
|
||||||
$this->requestStack = $requestStack;
|
$this->requestStack = $requestStack;
|
||||||
|
$this->em = $em;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getName()
|
public function getName()
|
||||||
{
|
{
|
||||||
return 'select2_chill_country';
|
return 'select2_chill_country';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||||
|
{
|
||||||
|
$transformer = new ObjectToIdTransformer($this->em,'Chill\MainBundle\Entity\Country');
|
||||||
|
$builder->addModelTransformer($transformer);
|
||||||
|
}
|
||||||
|
|
||||||
public function getParent()
|
public function getParent()
|
||||||
{
|
{
|
||||||
return 'select2_entity';
|
return 'select2_choice';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
public function setDefaultOptions(OptionsResolverInterface $resolver)
|
||||||
{
|
{
|
||||||
$locale = $this->requestStack->getCurrentRequest()->getLocale();
|
$locale = $this->requestStack->getCurrentRequest()->getLocale();
|
||||||
|
$countries = $this->em->getRepository('Chill\MainBundle\Entity\Country')->findAll();
|
||||||
|
$choices = array();
|
||||||
|
|
||||||
|
foreach ($countries as $c) {
|
||||||
|
$choices[$c->getId()] = $c->getName()[$locale];
|
||||||
|
}
|
||||||
|
|
||||||
|
asort($choices);
|
||||||
|
|
||||||
$resolver->setDefaults(array(
|
$resolver->setDefaults(array(
|
||||||
'class' => 'Chill\MainBundle\Entity\Country',
|
'class' => 'Chill\MainBundle\Entity\Country',
|
||||||
'property' => 'name['.$locale.']'
|
'choices' => $choices
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,7 @@ services:
|
|||||||
class: Chill\MainBundle\Form\Type\Select2CountryType
|
class: Chill\MainBundle\Form\Type\Select2CountryType
|
||||||
arguments:
|
arguments:
|
||||||
- "@request_stack"
|
- "@request_stack"
|
||||||
|
- "@doctrine.orm.entity_manager"
|
||||||
tags:
|
tags:
|
||||||
- { name: form.type, alias: select2_chill_country }
|
- { name: form.type, alias: select2_chill_country }
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user