/** * @ParamConverter("user", class="CoreUserBundle:User", options={"mapping":{"user_username" = "username"}}) * @ParamConverter("idea", class="AppBundle:Idea", options={"mapping":{"idea_slug" = "slug"}}) * @ParamConverter("comment", class="AppBundle:Comment", options={"mapping":{"comment_id" = "id"}}) */ public function addAction(Request $request, User $user, Idea $idea, Comment $comment) { if ($idea->getUserId() !== $user->getId() || $comment->getIdeaId() !== $idea->getId()) { throw new $this->createNotFoundException(); } $doctrine = $this->getDoctrine(); $manager = $doctrine->getManager(); $voteUser = $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY') ? $this->getUser() : null; $hash = $voteUser ? null : $this->get('core.base.form')->getUserHash($request); if ($voteUser && $voteUser->getId() === $comment->getUserId()) { $this->get('session')->getFlashBag()->add('error', "You can't vote for your own idea."); } else { $exist = $doctrine->getRepository('AppBundle:Vote')->findOneBy(['user' => $voteUser, 'hash' => $hash, 'comment' => $comment]); if ($exist) { $this->get('session')->getFlashBag()->add('error', 'You have already voted for this comment.'); } else { $vote = new Vote(); $vote->setComment($comment); if ($voteUser) { $vote->setUser($voteUser); } else { $vote->setHash($hash); } $manager->persist($vote); $manager->flush(); } } return $this->end($request, $user, $idea, $comment); }
/** * Initialize Vote with required data. * * @param Vote $vote * @param User $user * @param Site $site * @param Annuaire $annuaire * @param $tracker * @param $point * * @return Vote */ public function initVote(Vote $vote, User $user, Site $site, Annuaire $annuaire, $tracker, $point) { if ($site->getAnnuaires()->contains($annuaire)) { $vote->setUser($user); $vote->setSite($site); $vote->setAnnuaire($annuaire); $vote->setTracker($tracker); $vote->setPoint($point); } else { //@todo Générer une exception ! } return $vote; }
public function load(ObjectManager $manager) { // UserSkill : 1 $repository = $manager->getRepository('AppBundle:UserSkill'); $userSkillEntity = $repository->find(1); // User : 2 $repository = $manager->getRepository('AppBundle:User'); $userEntity = $repository->find(2); $voteEntity = new Vote(); $voteEntity->setUserSkill($userSkillEntity); $voteEntity->setUser($userEntity); $manager->persist($voteEntity); $manager->flush(); }
/** * @Security("has_role('ROLE_USER')") */ public function addAction(Request $request) { if (($type = $request->request->get("type", null)) === null) { return new Response($type, 406); } $types = array(Vote::VOTE_TYPE_DISLIKE, Vote::VOTE_TYPE_LIKE, Vote::VOTE_TYPE_REPEAT, Vote::VOTE_TYPE_VOLUME_DOWN, Vote::VOTE_TYPE_VOLUME_UP); if (!in_array($type, $types)) { return new Response($type, 406); } $voteRepository = $this->getDoctrine()->getManager()->getRepository("AppBundle:Vote"); $vote = $voteRepository->findOneBy(array("user" => $this->getUser())); if ($vote != null) { return new Response($type, 403); } $vote = new Vote(); $vote->setUser($this->getUser()); $vote->setType($type); $this->getDoctrine()->getManager()->persist($vote); $this->getDoctrine()->getManager()->flush(); return new Response(); }
/** * @Route("/api/vote/{id}", name="vote") */ public function voteAction(Request $request, $id) { $isAjax = $request->isXmlHttpRequest(); if (!$isAjax) { return $this->redirectToRoute('homepage'); } try { $em = $this->getDoctrine()->getManager(); $suggestion = $em->getRepository('AppBundle:Suggestion')->findOneById($id); if (!$suggestion) { return new JsonResponse(array('error' => 'Tento podnet neexistuje.'), 404); } $vote = new Vote(); $vote->setIp($request->getClientIp()); $vote->setFingerprint($request->get('fingerprint')); $vote->setSuggestion($suggestion); $cookie = $request->cookies->get('vote-' . $id); if (!$cookie) { $generator = new SecureRandom(); $random = $generator->nextBytes(10); $cookie = new Cookie('vote-' . $id, $random); $vote->setCookie($cookie); } else { return new JsonResponse(array('error' => 'Za tento podnet ste už hlasoval.'), 409); } $em->persist($vote); $em->flush(); $response = new JsonResponse(array('msg' => 'ok')); $response->headers->setCookie($cookie); return $response; } catch (UniqueConstraintViolationException $e) { return new JsonResponse(array('error' => 'Za tento podnet ste už hlasoval.'), 409); } }
private function loadPosts(ObjectManager $manager) { $category = new Category(); $category->setName('Improvements'); $passwordEncoder = $this->container->get('security.password_encoder'); $user = new User(); $user->setUsername('vvasia'); $user->setDisplayName('Vasia Vasin'); $user->setEmail('*****@*****.**'); $user->setUuid('uuid'); $encodedPassword = $passwordEncoder->encodePassword($user, 'password'); $user->setPassword($encodedPassword); $user->setRoles(['ROLE_USER']); $manager->persist($user); $manager->flush(); $author = $manager->getRepository('AppBundle:User')->findOneBy(['email' => '*****@*****.**']); foreach (range(1, 5) as $i) { $post = new Post(); $post->setTitle($this->getRandomPostTitle())->setSummary($this->getRandomPostSummary())->setSlug($this->container->get('slugger')->slugify($post->getTitle()))->setContent($this->getPostContent())->setAuthor($author)->setPublishedAt(new \DateTime('now - ' . $i . 'days'))->setState($this->getRandomState())->setCategory($category); foreach (range(1, 5) as $j) { $comment = new Comment(); $comment->setUser($user)->setPublishedAt(new \DateTime('now + ' . ($i + $j) . 'seconds'))->setContent($this->getRandomCommentContent())->setPost($post); $manager->persist($comment); $post->addComment($comment); } if (rand(0, 1)) { $vote = new Vote(); $vote->setAuthorEmail(rand(0, 1) ? '*****@*****.**' : '*****@*****.**'); $vote->setPost($post); $vote->setVote(rand(0, 1)); } $manager->persist($post); $category->addPost($post); } $manager->flush(); }
/** * Test getter and setter of point property. * * @covers AppBundle\Entity\Vote::setPoint() * @covers AppBundle\Entity\Vote::getPoint() */ public function testPoint() { $expected = 3; $this->vote->setPoint($expected); $this->assertEquals($expected, $this->vote->getPoint()); }
/** * Creates a form to delete a Vote entity. * * @param Vote $vote The Vote entity * * @return \Symfony\Component\Form\Form The form */ private function createDeleteForm(Vote $vote) { return $this->createFormBuilder()->setAction($this->generateUrl('vote_delete', array('id' => $vote->getId())))->setMethod('DELETE')->getForm(); }
/** * @Route("/rating", name="rating", options={"expose"=true}) */ public function rateSkillAction(Request $request) { $result = 'false'; $id = $request->get('term'); $em = $this->getDoctrine()->getManager(); $userSkill = $em->getRepository('AppBundle:UserSkill')->find($id); $userSkillId = $userSkill->getUserSkillId(); $user = $this->get('security.token_storage')->getToken()->getUser(); $userId = $user->getUserId(); $userVoting = $em->getRepository('AppBundle:User')->find($userId); $userInVoteExistObject = $em->getRepository('AppBundle:Vote')->findOneBy(array('user' => $userId, 'userkill' => $userSkillId)); if ($userInVoteExistObject) { $em->remove($userInVoteExistObject); $em->flush(); } else { $voteEntity = new Vote(); $voteEntity->setUserSkill($userSkill); $voteEntity->setUser($userVoting); $em->persist($voteEntity); $em->flush(); $result = 'true'; } $counts = $em->getRepository('AppBundle:Vote')->counting($id); foreach ($counts as $count) { $total = $count['rate']; } $response = new Response(); $response->setContent(json_encode(array('total' => $total, 'status' => $result))); return $response; }
/** * @Route("/posts/vote/{id}/{agree}", name="blog_post_vote") */ public function voteAction(Post $post, $agree) { $this->denyAccessUnlessGranted(PostVoter::VIEW, $post); $userEmail = $this->getUser()->getEmail(); $voteEntity = $this->getDoctrine()->getRepository('AppBundle:Vote')->findOneBy(['authorEmail' => $userEmail, 'post' => $post]); if (!$voteEntity) { $voteEntity = new Vote(); } $voteEntity->setAuthorEmail($userEmail)->setPost($post)->setVote($agree); $em = $this->getDoctrine()->getManager(); $em->persist($voteEntity); $em->flush(); $flashMessage = $agree == Vote::LIKE ? 'flash.vote.agree' : 'flash.vote.not-agree'; $this->addFlash(FlashbagTypeEnum::SUCCESS, $this->get('translator')->trans($flashMessage)); return $this->redirectToRoute('blog_post', array('slug' => $post->getSlug())); }
/** * {@inheritDoc} */ public function getIsPositive() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getIsPositive', []); return parent::getIsPositive(); }
private function loadPosts(ObjectManager $manager) { $category = new Category(); $category->setName('Improvements'); foreach (range(1, 5) as $i) { $post = new Post(); $post->setTitle($this->getRandomPostTitle()); $post->setSummary($this->getRandomPostSummary()); $post->setSlug($this->container->get('slugger')->slugify($post->getTitle())); $post->setContent($this->getPostContent()); $post->setAuthorEmail('*****@*****.**'); $post->setPublishedAt(new \DateTime('now - ' . $i . 'days')); $post->setState($this->getRandomState()); $post->setCategory($category); foreach (range(1, 5) as $j) { $comment = new Comment(); $comment->setAuthorEmail('*****@*****.**'); $comment->setPublishedAt(new \DateTime('now + ' . ($i + $j) . 'seconds')); $comment->setContent($this->getRandomCommentContent()); $comment->setPost($post); $manager->persist($comment); $post->addComment($comment); } if (rand(0, 1)) { $vote = new Vote(); $vote->setAuthorEmail(rand(0, 1) ? '*****@*****.**' : '*****@*****.**'); $vote->setPost($post); $vote->setVote(rand(0, 1)); } $manager->persist($post); $category->addPost($post); } $manager->flush(); }