Beispiel #1
0
 /**
  * Creates form and validates it or saves data in case some data
  * were already submitted.
  *
  * @param mixed $id Id of poll to be created
  *
  * @throws NotFoundHttpException
  * @throws Exception
  */
 public function create($id, Response &$response, $options = array())
 {
     $this->id = $id;
     $this->poll = $this->pollManager->findOneById($id);
     if (!$this->poll || $this->poll->getDeletedAt()) {
         throw new NotFoundHttpException(sprintf("Poll with id '%s' was not found.", $id));
     }
     if (!$this->poll->isActive()) {
         $this->isActive = false;
         return;
     }
     $this->form = $this->formFactory->create($id);
     $formName = $this->form->getName();
     if ($this->request->getMethod() === 'POST' && $this->request->request->has($formName) && !$this->answerGroupManager->hasAnswered($this->poll)) {
         $this->form->bindRequest($this->request);
         if ($this->form->isValid()) {
             $data = $this->form->getData();
             $answers = array();
             $answerGroup = $this->answerGroupManager->create($this->poll);
             foreach ($data as $fieldId => $userAnswer) {
                 $fieldId = str_replace('field_', '', $fieldId);
                 $field = $this->objectManager->getReference($this->fieldClass, $fieldId);
                 if ($field->getType() === FieldInterface::TYPE_FILE) {
                     if ($field->isRequired()) {
                         $userAnswers = (array) $userAnswer->getClientOriginalName();
                     }
                 } else {
                     $userAnswers = (array) $userAnswer;
                 }
                 foreach ($userAnswers as $userAnswer) {
                     $answer = $this->answerManager->create($field, $userAnswer, $answerGroup);
                     $answers[] = $answer;
                 }
             }
             $response = new RedirectResponse($this->request->getUri());
             $pollType = $this->poll->getType();
             // Checks if poll type is proper one, defined in PollInterface
             $fieldClassReflection = new \ReflectionClass($this->pollClass);
             $fieldConstants = $fieldClassReflection->getConstants();
             if (!in_array($pollType, $fieldConstants)) {
                 throw new \Exception(sprintf('"%s" is incorrect poll type.', $pollType));
             }
             // Checks what multi-answer prevention mechanisms should be triggered
             // and error that might occur.
             $doPersist = false;
             if ($this->poll instanceof SignedPollInterface) {
                 $isAuthenticated = $this->securityContext->isGranted('IS_AUTHENTICATED_FULLY');
                 if (in_array($pollType, array(PollInterface::POLL_TYPE_USER, PollInterface::POLL_TYPE_MIXED)) && $isAuthenticated) {
                     $user = $this->securityContext->getToken()->getUser();
                     $answerGroup->setAuthor($user);
                     $doPersist = true;
                 }
             } else {
                 if (in_array($pollType, array(PollInterface::POLL_TYPE_USER, PollInterface::POLL_TYPE_MIXED))) {
                     throw new \Exception(sprintf('Poll type is "%s", but your Poll class (%s) doesn\'t implement Bait\\PollBundle\\Model\\SignedPollInterface', $pollType, $this->pollClass));
                 }
             }
             if (in_array($pollType, array(PollInterface::POLL_TYPE_ANONYMOUS, PollInterface::POLL_TYPE_MIXED))) {
                 $cookieDuration = array_key_exists('cookieDuration', $options) ? (int) $options['cookieDuration'] : $this->cookieDuration;
                 $cookie = new Cookie(sprintf('%sanswered_%s', $this->cookiePrefix, $id), true, time() + $cookieDuration);
                 $response->headers->setCookie($cookie);
                 $doPersist = true;
             }
             // Checks if any upload occurred/should have occurred
             // and handles it
             if ($this->fieldManager->hasUploadFields($this->poll)) {
                 if ("" == $this->uploadDir) {
                     throw new \Exception("You should configure the bait_poll.upload_dir directive in your config.yml");
                 }
                 if (!is_writable($this->uploadDir . '/')) {
                     throw new \Exception(sprintf('"%s" is not a writable folder for uploads.', $this->uploadDir));
                 }
                 $answerGroup = $this->answerGroupManager->save($answerGroup);
                 $folder = $this->uploadDir . '/' . $this->poll->getId() . '/answergroups/' . $answerGroup->getId();
                 mkdir($folder, 0777, true);
                 foreach ($data as $fieldName => $field) {
                     if ($field instanceof UploadedFile) {
                         $field->move($folder, $fieldName . '.' . $field->guessExtension());
                     }
                 }
             }
             // If everything went ok, save all answers
             if ($doPersist) {
                 $this->answerGroupManager->save($answerGroup);
                 $this->answerManager->save($answers);
             }
         }
     }
 }
Beispiel #2
0
 /**
  * Creates form and validates it or saves data in case some data
  * were already submitted.
  *
  * @param mixed $id Id of poll to be created
  *
  * @throws NotFoundHttpException
  * @throws Exception
  */
 public function create($id, Response &$response)
 {
     $this->id = $id;
     $this->poll = $this->pollManager->findOneById($id);
     if (!$this->poll) {
         throw new NotFoundHttpException(sprintf("Poll with id '%s' was not found.", $id));
     }
     if (!$this->poll->isActive()) {
         $this->isActive = false;
         return;
     }
     $this->form = $this->formFactory->create($id);
     $formName = $this->form->getName();
     if ($this->request->getMethod() === 'POST' && $this->request->request->has($formName) && !$this->voteManager->hasVoted($this->poll)) {
         $this->form->bindRequest($this->request);
         if ($this->form->isValid()) {
             $data = $this->form->getData();
             $votes = array();
             foreach ($data as $fieldId => $answer) {
                 $fieldId = str_replace('field_', '', $fieldId);
                 $answers = (array) $answer;
                 $field = $this->objectManager->getReference($this->fieldClass, $fieldId);
                 foreach ($answers as $answer) {
                     $vote = $this->voteManager->create($field, $answer);
                     $votes[] = $vote;
                 }
             }
             $response = new RedirectResponse($this->request->getUri());
             $pollType = $this->poll->getType();
             // Checks if poll type is proper one, defined in PollInterface
             $fieldClassReflection = new \ReflectionClass($this->pollClass);
             $fieldConstants = $fieldClassReflection->getConstants();
             if (!in_array($pollType, $fieldConstants)) {
                 throw new \Exception(sprintf('"%s" is incorrect poll type.', $pollType));
             }
             // Checks what multi-vote prevention mechanisms should be triggered
             // and error that might occur.
             $doPersist = false;
             if ($this->poll instanceof SignedPollInterface) {
                 $isAuthenticated = $this->securityContext->isGranted('IS_AUTHENTICATED_FULLY');
                 if (in_array($pollType, array(PollInterface::POLL_TYPE_USER, PollInterface::POLL_TYPE_MIXED)) && $isAuthenticated) {
                     $user = $this->securityContext->getToken()->getUser();
                     foreach ($votes as $vote) {
                         $vote->setAuthor($user);
                     }
                     $doPersist = true;
                 }
             } else {
                 if (in_array($pollType, array(PollInterface::POLL_TYPE_USER, PollInterface::POLL_TYPE_MIXED))) {
                     throw new \Exception(sprintf('Poll type is "%s", but your Poll class doesn\'t (%s) implement Bait\\PollBundle\\Model\\SignedPollInterface', $pollType, $this->pollClass));
                 }
             }
             if (in_array($pollType, array(PollInterface::POLL_TYPE_ANONYMOUS, PollInterface::POLL_TYPE_MIXED))) {
                 $cookie = new Cookie(sprintf('%svoted_%s', $this->cookiePrefix, $id), true, time() + $this->cookieDuration);
                 $response->headers->setCookie($cookie);
                 $doPersist = true;
             }
             // If everything went ok, save all votes
             if ($doPersist) {
                 $this->voteManager->save($votes);
             }
         }
     }
 }