Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. public function showAction(Poll $poll, Request $request)
  2. {
  3. $em = $this->getDoctrine()->getManager();
  4. $user = $this->getUser();
  5. $voteRepository = $em->getRepository('PollBundle:Vote');
  6. $lastVote = $voteRepository->findOneBy([
  7. 'poll' => $poll,
  8. 'user' => $user,
  9. 'isCurrentVote' => 1
  10. ]);
  11.  
  12. $yesVotesCount = $voteRepository->countPollVotesByIsCurrentAndChoice($poll, 0);
  13. $notSureVotesCount = $voteRepository->countPollVotesByIsCurrentAndChoice($poll, 1);
  14. $noVotesCount = $voteRepository->countPollVotesByIsCurrentAndChoice($poll, 2);
  15. $totalVotes = $yesVotesCount + $notSureVotesCount + $noVotesCount;
  16. $voteCounts = [
  17. $yesVotesCount,
  18. $notSureVotesCount,
  19. $noVotesCount
  20. ];
  21. $voteShare = [
  22. ($this->findPercent($yesVotesCount,$totalVotes)),
  23. ($this->findPercent($notSureVotesCount,$totalVotes)),
  24. ($this->findPercent($noVotesCount,$totalVotes)),
  25. ];
  26. $voteOptions = new VoteOptions();
  27. if ($lastVote) {
  28. $voteOptions->setChoice($lastVote->getVoteOptions()->getChoice());
  29. }
  30. $voteOptionsForm = $this->createForm('PollBundle\Form\VoteOptionsType', $voteOptions);
  31.  
  32. $comment = new PollComments();
  33. $commentForm = $this->createForm('PollBundle\Form\PollCommentsType', $comment);
  34.  
  35. $commentsRepository = $em->getRepository('PollBundle:PollComments');
  36. $comments = $commentsRepository->findAllCommentsAndUpVotes($poll);
  37. if ($request->isMethod('POST')) {
  38. if ($request->request->has($voteOptionsForm->getName())) {
  39. $voteOptionsForm->submit($request->request->get($voteOptionsForm->getName()));
  40. if ($voteOptionsForm->isValid()) {
  41. $em->persist($voteOptions);
  42. $vote = new Vote();
  43. if ($lastVote) {
  44. $lastVote->setIsCurrentVote(false);
  45. $em->persist($lastVote);
  46. }
  47. $vote->setUser($user);
  48. $vote->setPoll($poll);
  49. $vote->setIsCurrentVote(true);
  50. $vote->setVoteOptions($voteOptions);
  51. $em->persist($vote);
  52. $em->flush();
  53. return $this->redirectToRoute('poll_show', array('id' => $poll->getId()));
  54. }
  55. } else
  56. if ($request->request->has($commentForm->getName())) {
  57. $commentForm->submit($request->request->get($commentForm->getName()));
  58. if ($commentForm->isValid()) {
  59. $comment->setUser($user);
  60. $comment->setPoll($poll);
  61. $em->persist($comment);
  62. $em->flush();
  63. }
  64. }
  65. }
  66. $deleteForm = $this->createDeleteForm($poll);
  67. return $this->render('poll/show.html.twig', array(
  68. 'poll' => $poll,
  69. 'vote_counts' => $voteCounts,
  70. 'vote_share' => $voteShare,
  71. 'vote_options_form' => $voteOptionsForm->createView(),
  72. 'comment_form' => $commentForm->createView(),
  73. 'comments' => $comments,
  74. 'delete_form' => $deleteForm->createView(),
  75. ));
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement