Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.22 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Form;
  4.  
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\CallbackTransformer;
  7. use Symfony\Component\Form\FormBuilderInterface;
  8. use Symfony\Component\OptionsResolver\OptionsResolver;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  11. use App\Entity\Information;
  12. use App\Entity\Traveler;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  15. use Symfony\Component\Form\Extension\Core\Type\FileType;
  16. use App\Entity\PossibleAnswer;
  17. use Doctrine\ORM\EntityRepository;
  18. use Symfony\Component\Validator\Constraints\File;
  19. use phpDocumentor\Reflection\Types\Null_;
  20. use Symfony\Component\Validator\Constraints\Length;
  21.  
  22. class TravelerInformationType extends AbstractType
  23. {
  24.     private $entityManager;
  25.    
  26.     public function __construct(EntityManagerInterface $entityManager)
  27.     {
  28.         $this->entityManager = $entityManager;
  29.     }
  30.    
  31.     public function buildForm(FormBuilderInterface $builder, array $options)
  32.     {
  33.        
  34.         $possibleanswerCallBackTransformer = new CallbackTransformer(
  35.             function ($possibleanswer_id) {
  36.                 if (!empty($possibleanswer_id)) {
  37.                     $possibleanswer = $this->entityManager
  38.                         ->getRepository(PossibleAnswer::class)
  39.                         ->find($possibleanswer_id)
  40.                         ;
  41.                     return $possibleanswer;
  42.                 } else {
  43.                     return null;
  44.                 }
  45.             }, function (PossibleAnswer $possibleanswer = null) {
  46.                 if ($possibleanswer !== null) {
  47.                     return $possibleanswer->getId();
  48.                 } else {
  49.                     return null;
  50.                 }
  51.             }
  52.         );
  53.        
  54.         // si une info a déjà été renseignée pour le voyageur, je dois renseigner le champ avec la valeur
  55.         $data = $builder->getData();
  56.         // je récupère la liste des questions active pour ce pays
  57.         $informations = $options['informations'];
  58.         $upload_dir = $options['upload_dir'];
  59.         /* @var Information $information */
  60.         foreach ($informations as $information) {
  61.             $field_options = array();
  62.             $type = null;
  63.             $field_options['data'] = $data[$information->getId()] ?? null;
  64.             switch ($information->getType()) {
  65.                 case 'str':
  66.                     $type = TextType::class;
  67.                     if (!empty($information->getLength())) {
  68.                         $field_options['constraints'] = new Length(array(
  69.                             'max' => $information->getLength()
  70.                         ));
  71.                     }
  72.                     break;
  73.                 case 'bool':
  74.                     $type = CheckboxType::class;
  75.                     $field_options['data'] = boolval($field_options['data']);
  76.                     break;
  77.                 case 'file':
  78.                     $type = FileType::class;
  79.                     $field_options['constraints'] = new File(array(
  80.                         'maxSize' => '5M',
  81.                         'maxSizeMessage' => 'Le fichier ne peut dépasser 5Mo',
  82.                         'mimeTypes' => ['application/pdf', 'application/x-pdf', 'image/jpg', 'image/jpeg', 'image/png'],
  83.                         'mimeTypesMessage' => 'Le fichier doit être au format .pdf, .jpg ou .png',
  84.                     ));
  85.                     $filepath = $upload_dir.'/'.$field_options['data'];
  86.                     if (file_exists($filepath) && is_file($filepath)) {
  87.                         $file = new \Symfony\Component\HttpFoundation\File\File($filepath);
  88.                         $field_options['data'] = ($field_options['data'] !== null && $file->isFile()) ? $file : null;
  89.                     }
  90.                     else {
  91.                         $field_options['data'] = null;
  92.                     }
  93.                     $field_options['empty_data'] = $field_options['data'];
  94.                     break;
  95.                 case 'list-radio':
  96.                     $field_options['expanded'] = true;
  97.                     $field_options['placeholder'] = false;
  98.                 case 'list-choice':
  99.                     $type = EntityType::class;
  100.                     $field_options['class'] = PossibleAnswer::class;
  101.                     $field_options['choice_label'] = 'value';
  102.                     $field_options['query_builder'] = function (EntityRepository $er) use ($information) {
  103.                         return $er->getActiveAnswerForInformationQuery($information);
  104.                     };
  105.                     break;
  106.             }
  107.             $field_options['required'] = false;
  108.             $field_options['label'] = $information->getName();
  109.             if ($information->getCategory() !== null) {
  110.                 $field_options['attr']['data-title'] = $information->getCategory()->getName();
  111.             }
  112.             if (!empty($information->getHelp())) {
  113.                 $field_options['help'] = $information->getHelp();
  114.             }
  115.             $builder->add($information->getId(), $type, $field_options);
  116.            
  117.             // je rajoute le data transformer pour les listes
  118.             if (substr($information->getType(), 0, 5 ) === 'list-') {
  119.                 $builder
  120.                     ->get($information->getId())
  121.                     ->addModelTransformer($possibleanswerCallBackTransformer)
  122.                 ;
  123.             }
  124.            
  125.             if (!empty($options['traveler_display'])) {
  126.                 $builder
  127.                     ->add('traveler', TravelerCollectionType::class, array(
  128.                         'data' => new Traveler(),
  129.                         'mapped' => false,
  130.                         'label' => false,
  131.                     ))
  132.                 ;
  133.             }
  134.         }
  135.     }
  136.    
  137.     public function configureOptions(OptionsResolver $resolver)
  138.     {
  139.         $resolver->setDefaults(array(
  140. //             'data_class' => User::class, // je ne travaille pas avec une entité réelle pour ce form
  141.             'informations' => null,
  142.             'upload_dir' => null,
  143.             'traveler' => null,
  144.             'traveler_hide' => null,
  145.         ));
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement