src/Form/EventListener/Poll/ResponseQuestionsFormListener.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Poll;
  4. use App\Entity\Poll\Poll;
  5. use App\Entity\Poll\Question;
  6. use App\Entity\Poll\ResponseAnswerText;
  7. use App\Entity\Poll\ResponseAnswerSingle;
  8. use App\Entity\Poll\ResponseAnswerMulti;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  11. use Symfony\Component\Form\FormEvent;
  12. use Symfony\Component\Form\FormEvents;
  13. use Symfony\Component\Validator\Constraints\Email;
  14. use Symfony\Component\Validator\Constraints\Length;
  15. use Symfony\Component\Validator\Constraints\NotBlank;
  16. /**
  17.  * @author  Przemysław Chrupek <przemyslaw.chrupek@avt.pl>
  18.  * @package App\Form\EventListener
  19.  */
  20. class ResponseQuestionsFormListener implements EventSubscriberInterface
  21. {
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  25.     }
  26.     public function preSetData(FormEvent $event)
  27.     {
  28.         $response $event->getData();
  29.         $poll $response->getPoll();
  30.         if (!$poll instanceof Poll)
  31.             return;
  32.         $form $event->getForm();
  33.         if ($poll->getWithEmail()) {
  34.             $emailParams = [];
  35.             
  36.             if ($poll->getEmailRequired()) {
  37.                 $emailParams = [
  38.                     'required' => true,
  39.                     'constraints' => [
  40.                         new NotBlank([
  41.                             'message' => 'Wpisz email'
  42.                         ]),
  43.                         new Length([
  44.                             'max' => 250,
  45.                             'maxMessage' => 'Maksymalnie 250 znaków',
  46.                         ]),
  47.                         new Email([
  48.                             'message' => 'Adres email jest nieprawidłowy'
  49.                         ]),
  50.                     ],
  51.                 ];
  52.             }
  53.                 
  54.             $form->add(
  55.                 'email',
  56.                 EmailType::class,
  57.                 $emailParams
  58.             );
  59.         }
  60.             
  61.         foreach ($poll->getQuestions() as $question) {
  62.             if ($response->getResponseAnswers()->contains($question))
  63.                 continue;
  64.             switch ($question->getType()) {
  65.                 case Question::TYPE_SELECT:
  66.                 case Question::TYPE_RADIO:
  67.                     $responseAnswer = new ResponseAnswerSingle;
  68.                     break;
  69.                 case Question::TYPE_MULTISELECT:
  70.                 case Question::TYPE_CHECKBOX:
  71.                     $responseAnswer = new ResponseAnswerMulti;
  72.                     break;
  73.                 
  74.                 default:
  75.                     $responseAnswer = new ResponseAnswerText;
  76.                     break;
  77.             }
  78.             
  79.             $responseAnswer->setQuestion($question);
  80.             $response->addResponseAnswer($responseAnswer);
  81.         }
  82.         $event->setData($response);
  83.     }
  84. }