Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Lists all nurl entities.
- *
- * @Route("/{id}/upvote", name="tag_upvote")
- * @Method({"GET", "POST"})
- */
- public function upvoteAction(Tag $tag)
- {
- // Check if this tag has a username equal to the current one
- if($tag->getUserVoted() == $this->get('security.token_storage')->getToken()->getUser())
- {
- // Show an error if the user has already voted there
- $this->addFlash(
- 'notice',
- 'You have already upvoted that tag!'
- );
- // Return with the error
- return $this->redirectToRoute('tag_index');
- }
- else
- {
- // Set that that particular user has voted
- $tag -> setUserVoted($this->get('security.token_storage')->getToken()->getUser());
- // Increase the tag vote by 1 or by 5 depending on the role
- $tag -> setUpvote( $tag -> getUpvote() + 1);
- if($this->get('security.authorization_checker')->isGranted('ROLE_USER'))
- {
- $tag -> setUpvote( $tag -> getUpvote() + 5);
- }
- // Flush everything
- $em = $this->getDoctrine()->getManager();
- $em->persist($tag);
- $em->flush($tag);
- }
- return $this->redirectToRoute('tag_index');
- }
- /**
- * Lists all nurl entities.
- *
- * @Route("/{id}/downvote", name="tag_downvote")
- * @Method({"GET", "POST"})
- */
- public function downvoteAction(Tag $tag)
- {
- // Check if this tag has a username equal to the current one
- if($tag->getUserVoted() == $this->get('security.token_storage')->getToken()->getUser())
- {
- // Show an error if the user has already voted there
- $this->addFlash(
- 'notice',
- 'You have already downvoted that tag!'
- );
- // Return with the error
- return $this->redirectToRoute('tag_index');
- }
- else
- {
- // Set that that particular user has voted
- $tag -> setUserVoted($this->get('security.token_storage')->getToken()->getUser());
- // Increase the tag vote by 1 or by 5 depending on the role
- $tag -> setDownvote( $tag -> getDownvote() + 1);
- // Registered user vote counts as 5
- if($this->get('security.authorization_checker')->isGranted('ROLE_USER'))
- {
- $tag -> setDownvote( $tag -> getDownvote() + 5);
- }
- // Flush everything
- $em = $this->getDoctrine()->getManager();
- $em->persist($tag);
- $em->flush($tag);
- }
- return $this->redirectToRoute('tag_index');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement