Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function showAction(Poll $poll, Request $request)
- {
- $em = $this->getDoctrine()->getManager();
- $user = $this->getUser();
- $voteRepository = $em->getRepository('PollBundle:Vote');
- $lastVote = $voteRepository->findOneBy([
- 'poll' => $poll,
- 'user' => $user,
- 'isCurrentVote' => 1
- ]);
- $yesVotesCount = $voteRepository->countPollVotesByIsCurrentAndChoice($poll, 0);
- $notSureVotesCount = $voteRepository->countPollVotesByIsCurrentAndChoice($poll, 1);
- $noVotesCount = $voteRepository->countPollVotesByIsCurrentAndChoice($poll, 2);
- $totalVotes = $yesVotesCount + $notSureVotesCount + $noVotesCount;
- $voteCounts = [
- $yesVotesCount,
- $notSureVotesCount,
- $noVotesCount
- ];
- $voteShare = [
- ($this->findPercent($yesVotesCount,$totalVotes)),
- ($this->findPercent($notSureVotesCount,$totalVotes)),
- ($this->findPercent($noVotesCount,$totalVotes)),
- ];
- $voteOptions = new VoteOptions();
- if ($lastVote) {
- $voteOptions->setChoice($lastVote->getVoteOptions()->getChoice());
- }
- $voteOptionsForm = $this->createForm('PollBundle\Form\VoteOptionsType', $voteOptions);
- $comment = new PollComments();
- $commentForm = $this->createForm('PollBundle\Form\PollCommentsType', $comment);
- $commentsRepository = $em->getRepository('PollBundle:PollComments');
- $comments = $commentsRepository->findAllCommentsAndUpVotes($poll);
- if ($request->isMethod('POST')) {
- if ($request->request->has($voteOptionsForm->getName())) {
- $voteOptionsForm->submit($request->request->get($voteOptionsForm->getName()));
- if ($voteOptionsForm->isValid()) {
- $em->persist($voteOptions);
- $vote = new Vote();
- if ($lastVote) {
- $lastVote->setIsCurrentVote(false);
- $em->persist($lastVote);
- }
- $vote->setUser($user);
- $vote->setPoll($poll);
- $vote->setIsCurrentVote(true);
- $vote->setVoteOptions($voteOptions);
- $em->persist($vote);
- $em->flush();
- return $this->redirectToRoute('poll_show', array('id' => $poll->getId()));
- }
- } else
- if ($request->request->has($commentForm->getName())) {
- $commentForm->submit($request->request->get($commentForm->getName()));
- if ($commentForm->isValid()) {
- $comment->setUser($user);
- $comment->setPoll($poll);
- $em->persist($comment);
- $em->flush();
- }
- }
- }
- $deleteForm = $this->createDeleteForm($poll);
- return $this->render('poll/show.html.twig', array(
- 'poll' => $poll,
- 'vote_counts' => $voteCounts,
- 'vote_share' => $voteShare,
- 'vote_options_form' => $voteOptionsForm->createView(),
- 'comment_form' => $commentForm->createView(),
- 'comments' => $comments,
- 'delete_form' => $deleteForm->createView(),
- ));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement