diff --git a/Form/Type/DataTransformer/ObjectToIdTransformer.php b/Form/Type/DataTransformer/ObjectToIdTransformer.php new file mode 100644 index 000000000..e09efc1c2 --- /dev/null +++ b/Form/Type/DataTransformer/ObjectToIdTransformer.php @@ -0,0 +1,87 @@ + + * + * 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 . + */ + +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; + } +} \ No newline at end of file diff --git a/Form/Type/Select2CountryType.php b/Form/Type/Select2CountryType.php index a83a30e37..a2bb3d13d 100644 --- a/Form/Type/Select2CountryType.php +++ b/Form/Type/Select2CountryType.php @@ -2,7 +2,7 @@ /* * Chill is a software for social workers - * Copyright (C) 2014 Julien Fastré + * Copyright (C) 2014 Champs-Libres * * 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 @@ -25,6 +25,10 @@ use Chill\MainBundle\Templating\TranslatableStringHelper; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Chill\MainBundle\Entity\Country; 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 @@ -34,33 +38,52 @@ use Symfony\Component\HttpFoundation\RequestStack; class Select2CountryType extends AbstractType { /** - * * @var RequestStack */ private $requestStack; + + /** + * @var ObjectManager + */ + private $em; - public function __construct(RequestStack $requestStack) + public function __construct(RequestStack $requestStack,ObjectManager $em) { $this->requestStack = $requestStack; + $this->em = $em; } public function getName() { 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() { - return 'select2_entity'; + return 'select2_choice'; } public function setDefaultOptions(OptionsResolverInterface $resolver) { $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( 'class' => 'Chill\MainBundle\Entity\Country', - 'property' => 'name['.$locale.']' + 'choices' => $choices )); } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 5267c0fae..d6f49c64c 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -52,6 +52,7 @@ services: class: Chill\MainBundle\Form\Type\Select2CountryType arguments: - "@request_stack" + - "@doctrine.orm.entity_manager" tags: - { name: form.type, alias: select2_chill_country }