Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.80 KB | None | 0 0
  1. /**
  2.      * Lists all nurl entities.
  3.      *
  4.      * @Route("/{id}/upvote", name="tag_upvote")
  5.      * @Method({"GET", "POST"})
  6.      */
  7.     public function upvoteAction(Tag $tag)
  8.     {
  9.         // Check if this tag has a username equal to the current one
  10.         if($tag->getUserVoted() == $this->get('security.token_storage')->getToken()->getUser())
  11.         {
  12.             // Show an error if the user has already voted there
  13.             $this->addFlash(
  14.                 'notice',
  15.                 'You have already upvoted that tag!'
  16.             );
  17.  
  18.             // Return with the error
  19.             return $this->redirectToRoute('tag_index');
  20.         }
  21.        
  22.         else
  23.         {
  24.             // Set that that particular user has voted
  25.             $tag -> setUserVoted($this->get('security.token_storage')->getToken()->getUser());
  26.  
  27.             // Increase the tag vote by 1 or by 5 depending on the role
  28.             $tag -> setUpvote( $tag -> getUpvote() + 1);
  29.  
  30.             if($this->get('security.authorization_checker')->isGranted('ROLE_USER'))
  31.             {
  32.                 $tag -> setUpvote( $tag -> getUpvote() + 5);
  33.             }
  34.  
  35.  
  36.             // Flush everything
  37.             $em = $this->getDoctrine()->getManager();
  38.             $em->persist($tag);
  39.             $em->flush($tag);
  40.         }
  41.  
  42.         return $this->redirectToRoute('tag_index');
  43.     }
  44.  
  45.     /**
  46.      * Lists all nurl entities.
  47.      *
  48.      * @Route("/{id}/downvote", name="tag_downvote")
  49.      * @Method({"GET", "POST"})
  50.      */
  51.     public function downvoteAction(Tag $tag)
  52.     {
  53.  
  54.         // Check if this tag has a username equal to the current one
  55.         if($tag->getUserVoted() == $this->get('security.token_storage')->getToken()->getUser())
  56.         {
  57.  
  58.             // Show an error if the user has already voted there
  59.             $this->addFlash(
  60.  
  61.                 'notice',
  62.  
  63.                 'You have already downvoted that tag!'
  64.  
  65.             );
  66.  
  67.             // Return with the error
  68.             return $this->redirectToRoute('tag_index');
  69.         }
  70.  
  71.         else
  72.         {
  73.             // Set that that particular user has voted
  74.             $tag -> setUserVoted($this->get('security.token_storage')->getToken()->getUser());
  75.  
  76.             // Increase the tag vote by 1 or by 5 depending on the role
  77.             $tag -> setDownvote( $tag -> getDownvote() + 1);
  78.  
  79.             // Registered user vote counts as 5
  80.             if($this->get('security.authorization_checker')->isGranted('ROLE_USER'))
  81.             {
  82.                 $tag -> setDownvote( $tag -> getDownvote() + 5);
  83.             }
  84.  
  85.             // Flush everything
  86.             $em = $this->getDoctrine()->getManager();
  87.  
  88.             $em->persist($tag);
  89.  
  90.             $em->flush($tag);
  91.  
  92.         }
  93.  
  94.         return $this->redirectToRoute('tag_index');
  95.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement