/**
  * Collection post action
  * @var Request $request
  * @return View|array
  */
 public function postAction(Request $request)
 {
     //we retrieve the data from the JSON that is received.
     $file = $request->files->get('file');
     $json_data = $request->files->get('data');
     $json_data = file_get_contents($json_data);
     $data = json_decode($json_data, true);
     //We stock the data from the JSON in different variables
     $idChallenge = $data['idChallenge'];
     $repetitions = $data['repetitions'];
     //We search the user and challenge into the database (need to add security if these values are not in the database)
     $challenge = $this->getDoctrine()->getManager()->getRepository('BFSiteBundle:Challenge')->find($idChallenge);
     if (!$challenge) {
         //the challenge does not exist.
         throw $this->createNotFoundException('This challenge does not exist.');
     }
     $user = $this->container->get('security.context')->getToken()->getUser();
     $description = 'A video for the ' . $challenge->getTitleFR() . ' challenge.';
     $title = $challenge->getTitleFR();
     // On crée un objet Video
     $video = new Video();
     $video->setDate(new \Datetime())->setUser($user)->setChallenge($challenge)->setRepetitions($repetitions)->setTitle($title)->setDescription($description)->setType('challenge')->setFile($file);
     //getting the code for the duel
     $service = $this->container->get('bf_site.randomcode');
     $code = $service->generate('video');
     $video->setCode($code);
     $service = $this->container->get('bf_admin.videopoints');
     $service->videoPoints($video);
     //here we convert the video with the watermark
     $exploded = explode('/', $video->getSource());
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, 'http://v.bestfootball.fr/test/convert.php?file=' . $exploded[3]);
     $retour = curl_exec($curl);
     curl_close($curl);
     return View::create($score, Response::HTTP_CREATED);
 }
 public function duelCopyVideoAction(request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $user = $this->container->get('security.context')->getToken()->getUser();
     //get the duel.
     $duelId = $request->get('duel');
     $duel = $em->getRepository('BFSiteBundle:Duel')->find($duelId);
     //get the video that has to be duplicated
     $Usevideo = $em->getRepository('BFSiteBundle:Video')->find($request->get('video'));
     if ($duel->getHost() == $user) {
         $duel->setHostCompleted('1');
         $userRole = 'host';
     } else {
         $duel->setGuestCompleted('1');
         $userRole = 'guest';
     }
     $video = new Video();
     $video->setDate(new \Datetime())->setDuel($duel)->setUser($user)->setScore('0')->setType('duel')->setRepetitions($Usevideo->getRepetitions())->setSource($Usevideo->getSource())->setThumbUrl('jpg')->setThumbAlt('Thumbnail of ' . $user->getUsername() . ' for the ' . $duel->getChallenge()->getTitle() . ' duel.')->setTitle($Usevideo->getTitle());
     //getting the code for the duel
     $service = $this->container->get('bf_site.randomcode');
     $code = $service->generate('video');
     $video->setCode($code);
     $host = $duel->getHost();
     $guest = $duel->getGuest();
     if ($duel->getHostCompleted() == 1 && $duel->getGuestCompleted() == 1) {
         //both the players uploaded their video. We can now set the complete off the duel to 1
         $duel->setCompleted('1');
         //now we look at the video with the highest repitions and we give 50 points to the winner.
         //get the video of the other player
         if ($userRole == 'host') {
             $otherVideo = $this->getDoctrine()->getManager()->getRepository('BFSiteBundle:Video')->duelGuestVideo($guest, $duel);
             $hostscore = $video->getRepetitions();
             $guestscore = $otherVideo->getRepetitions();
         } elseif ($userRole == 'guest') {
             $otherVideo = $this->getDoctrine()->getManager()->getRepository('BFSiteBundle:Video')->duelHostVideo($host, $duel);
             $guestscore = $video->getRepetitions();
             $hostscore = $otherVideo->getRepetitions();
         }
         //we get compare the host to the guest score
         if ($hostscore > $guestscore) {
             //host wins
             $points = $host->getDuelPoints() + 100;
             $host->setDuelPoints($points);
             $wins = $host->getDuelWins() + 1;
             $host->setDuelWins($wins);
             $duel->setWinner($host);
             $em->persist($duel);
             $em->persist($host);
             //notifications
             $link = $this->generateUrl('bf_site_duel_view', array('id' => $duel->getId()));
             //host
             $message = 'Congratulations you won the duel against ' . $guest->getUsername() . ' adn you received 100 points !';
             $service = $this->container->get('bf_site.notification');
             $notificationhost = $service->create($host, $message, $duel, $link);
             //guest
             $message = 'unfortunately you lost the duel against ' . $host->getUsername();
             $service = $this->container->get('bf_site.notification');
             $notificationguest = $service->create($guest, $message, $duel, $link);
             $em->persist($notificationhost);
             $em->persist($notificationguest);
         } elseif ($hostscore < $guestscore) {
             //guest wins
             $points = $guest->getDuelPoints() + 100;
             $guest->setDuelPoints($points);
             $wins = $guest->getDuelWins() + 1;
             $guest->setDuelWins($wins);
             $duel->setWinner($guest);
             $em->persist($duel);
             $em->persist($guest);
             //notifications
             $link = $this->generateUrl('bf_site_duel_view', array('id' => $duel->getId()));
             //guest
             $message = 'Congratulations you won the duel against ' . $host->getUsername() . ' adn you received 100 points !';
             $service = $this->container->get('bf_site.notification');
             $notificationguest = $service->create($guest, $message, $duel, $link);
             //host
             $message = 'unfortunately you lost the duel against ' . $guest->getUsername();
             $service = $this->container->get('bf_site.notification');
             $notificationhost = $service->create($host, $message, $duel, $link);
             $em->persist($notificationhost);
             $em->persist($notificationguest);
         } elseif ($hostscore == $guestscore) {
             //same score,each 50 points
             //host points
             $points = $host->getDuelPoints() + 50;
             $host->setDuelPoints($points);
             $em->persist($host);
             //guest points
             $points = $guest->getDuelPoints() + 50;
             $guest->setDuelPoints($points);
             $em->persist($guest);
             //notifications
             $link = $this->generateUrl('bf_site_duel_view', array('id' => $duel->getId()));
             //host
             $message = 'Congratulations, the duel against ' . $guest->getUsername() . ' was a tie and you received 50 points !';
             $service = $this->container->get('bf_site.notification');
             $notificationhost = $service->create($host, $message, $duel, $link);
             //guest
             $message = 'Congratulations, the duel against ' . $host->getUsername() . ' was a tie and you received 50 points !';
             $service = $this->container->get('bf_site.notification');
             $notificationguest = $service->create($guest, $message, $duel, $link);
             $em->persist($notificationhost);
             $em->persist($notificationguest);
         }
     }
     //now we update the points of the user
     $em->persist($video);
     $em->persist($duel);
     $em->flush();
     $this->addFlash('success', 'Your video was uploaded to our servers.');
     return new response();
 }
 public function uploadAction(request $request, $id, $type)
 {
     //we get the user entity
     $user = $this->container->get('security.context')->getToken()->getUser();
     // On crée un objet Video
     $video = new Video();
     $video->setType($type);
     //getting the code for the video
     $service = $this->container->get('bf_site.randomcode');
     $code = $service->generate('video');
     $video->setCode($code);
     //the upload is for a challenge video
     if ($type == 'challenge') {
         $challenge = $this->getDoctrine()->getManager()->getRepository('BFSiteBundle:Challenge')->findOneBy(array('id' => $id));
         //we verify if the user already uploaded a video.
         $repository = $this->getDoctrine()->getManager()->getRepository('BFSiteBundle:Video');
         $oldVideo = $repository->checkChallenge($user, $challenge);
         if (null !== $oldVideo) {
             //The user alreday has a video in the directory.
             $oldScore = $oldVideo->getScore();
             $points = $user->getPoints() - $oldScore;
             $user->setPoints($points);
         }
         $video->setDate(new \Datetime())->setUser($user)->setChallenge($challenge);
         $form = $this->get('form.factory')->create(new VideoType(), $video);
         if ($form->handleRequest($request)->isValid()) {
             $em = $this->getDoctrine()->getManager();
             $service = $this->container->get('bf_admin.videopoints');
             $service->videoPoints($video);
             //we convert the video to the right size and with the watermark
             $exploded = explode('/', $video->getSource());
             $curl = curl_init();
             curl_setopt($curl, CURLOPT_URL, 'http://v.bestfootball.fr/test/convert.php?file=' . $exploded[3]);
             $retour = curl_exec($curl);
             curl_close($curl);
             //sending a mail to all lower repetitions
             //$challengeMailer = $this->container->get('bf_site.challengeMailer');
             //$result = $challengeMailer->ChallengeMail($video, $video->getChallenge());
             $this->addFlash('success', 'Your video was uploaded to our servers and you received ' . $video->getScore() . ' points for this video.');
             return $this->redirect($this->generateUrl('bf_site_videos'));
         }
         return $this->render('BFSiteBundle:Video:upload.html.twig', array('form' => $form->createView()));
     }
     //the upload is for a duel video
     if ($type == 'duel') {
         //we check if the user has the right to upload his video for this duel. (if it is his duel)
         $repository = $this->getDoctrine()->getManager()->getRepository('BFSiteBundle:Duel');
         $duel = $repository->findOneBy(array('id' => $id));
         if ($duel->getHost() == $user || $duel->getGuest() == $user) {
             //We verify if the user and duel correspond
             $video->setDate(new \Datetime())->setDuel($duel)->setUser($user)->setScore('0');
             //the user is the guest for the duel
             if ($duel->getGuest() == $user) {
                 //check if he already uploaded a file. In that case we redirect him to the challenges page.
                 if ($duel->getGuestCompleted() == '1') {
                     $this->addFlash('warning', 'You already uploaded a video for this duel. You can only upload one video/duel.');
                     return $this->redirectToRoute('bf_site_profile_duels');
                 } else {
                     $duel->setGuestCompleted('1');
                     $userRole = 'guest';
                 }
             }
             //the user is the Host for the duel
             if ($duel->getHost() == $user) {
                 //check if he already uploaded a file. In that case we redirect him to the challenges page.
                 if ($duel->getHostCompleted() == '1') {
                     $this->addFlash('warning', 'You already uploaded a video for this duel. You can only upload one video/duel.');
                     return $this->redirectToRoute('bf_site_profile_duels');
                 } else {
                     $duel->setHostCompleted('1');
                     $userRole = 'host';
                 }
             }
             //we check if the user has a video for this challenge.
             $highVideo = $this->getDoctrine()->getManager()->getRepository('BFSiteBundle:Video')->highestVideo($user, $duel->getChallenge());
             $form = $this->get('form.factory')->create(new VideoDuelType(), $video);
             if ($form->handleRequest($request)->isValid()) {
                 $em = $this->getDoctrine()->getManager();
                 $host = $duel->getHost();
                 $guest = $duel->getGuest();
                 if ($duel->getHostCompleted() == 1 && $duel->getGuestCompleted() == 1) {
                     //both the players uploaded their video. We can now set the complete off the duel to 1
                     $duel->setCompleted('1');
                     //now we look at the video with the highest repitions and we give 50 points to the winner.
                     //get the video of the other player
                     if ($userRole == 'host') {
                         $otherVideo = $this->getDoctrine()->getManager()->getRepository('BFSiteBundle:Video')->duelGuestVideo($guest, $duel);
                         $hostscore = $video->getRepetitions();
                         $guestscore = $otherVideo->getRepetitions();
                     } elseif ($userRole == 'guest') {
                         $otherVideo = $this->getDoctrine()->getManager()->getRepository('BFSiteBundle:Video')->duelHostVideo($host, $duel);
                         $guestscore = $video->getRepetitions();
                         $hostscore = $otherVideo->getRepetitions();
                     }
                     //we get compare the host to the guest score
                     if ($hostscore > $guestscore) {
                         //host wins
                         $points = $host->getDuelPoints() + 100;
                         $host->setDuelPoints($points);
                         $wins = $host->getDuelWins() + 1;
                         $host->setDuelWins($wins);
                         $duel->setWinner($host);
                         $em->persist($duel);
                         $em->persist($host);
                         //notifications
                         $link = $this->generateUrl('bf_site_duel_view', array('id' => $duel->getId()));
                         //host
                         $message = 'Congratulations you won the duel against ' . $guest->getUsername() . ' adn you received 100 points !';
                         $service = $this->container->get('bf_site.notification');
                         $notificationhost = $service->create($host, $message, $duel, $link);
                         //guest
                         $message = 'unfortunately you lost the duel against ' . $host->getUsername();
                         $service = $this->container->get('bf_site.notification');
                         $notificationguest = $service->create($guest, $message, $duel, $link);
                         $em->persist($notificationhost);
                         $em->persist($notificationguest);
                     } elseif ($hostscore < $guestscore) {
                         //guest wins
                         $points = $guest->getDuelPoints() + 100;
                         $guest->setDuelPoints($points);
                         $wins = $guest->getDuelWins() + 1;
                         $guest->setDuelWins($wins);
                         $duel->setWinner($guest);
                         $em->persist($duel);
                         $em->persist($guest);
                         //notifications
                         $link = $this->generateUrl('bf_site_duel_view', array('id' => $duel->getId()));
                         //guest
                         $message = 'Congratulations you won the duel against ' . $host->getUsername() . ' adn you received 100 points !';
                         $service = $this->container->get('bf_site.notification');
                         $notificationguest = $service->create($guest, $message, $duel, $link);
                         //host
                         $message = 'unfortunately you lost the duel against ' . $guest->getUsername();
                         $service = $this->container->get('bf_site.notification');
                         $notificationhost = $service->create($host, $message, $duel, $link);
                         $em->persist($notificationhost);
                         $em->persist($notificationguest);
                     } elseif ($hostscore == $guestscore) {
                         //same score,each 50 points
                         //host points
                         $points = $host->getDuelPoints() + 50;
                         $host->setDuelPoints($points);
                         $em->persist($host);
                         //guest points
                         $points = $guest->getDuelPoints() + 50;
                         $guest->setDuelPoints($points);
                         $em->persist($guest);
                         //notifications
                         $link = $this->generateUrl('bf_site_duel_view', array('id' => $duel->getId()));
                         //host
                         $message = 'Congratulations, the duel against ' . $guest->getUsername() . ' was a tie and you received 50 points !';
                         $service = $this->container->get('bf_site.notification');
                         $notificationhost = $service->create($host, $message, $duel, $link);
                         //guest
                         $message = 'Congratulations, the duel against ' . $host->getUsername() . ' was a tie and you received 50 points !';
                         $service = $this->container->get('bf_site.notification');
                         $notificationguest = $service->create($guest, $message, $duel, $link);
                         $em->persist($notificationhost);
                         $em->persist($notificationguest);
                     }
                 }
                 //now we update the points of the user
                 $em->persist($video);
                 $em->persist($duel);
                 $em->flush();
                 //we convert the video to the right size and with the watermark
                 $exploded = explode('/', $video->getSource());
                 $curl = curl_init();
                 curl_setopt($curl, CURLOPT_URL, 'http://v.bestfootball.fr/test/convert.php?file=' . $exploded[3]);
                 $retour = curl_exec($curl);
                 curl_close($curl);
                 $this->addFlash('success', 'Your video was uploaded to our servers.');
                 return $this->redirect($this->generateUrl('bf_site_videos'));
             }
             return $this->render('BFSiteBundle:Video:uploadDuel.html.twig', array('form' => $form->createView(), 'highVideo' => $highVideo, 'duel' => $duel));
         } else {
             $this->addFlash('warning', 'You are not allowed to post a video to this Duel because it is not your duel');
             return $this->redirectToRoute('bf_site_challenges');
         }
     }
     if ($type == 'freestyle') {
         $video->setDate(new \Datetime())->setUser($user)->setScore('0')->setRepetitions(0);
         //frestyle section upload
         $form = $this->get('form.factory')->create(new VideoFreestyleType(), $video);
         if ($form->handleRequest($request)->isValid()) {
             $em = $this->getDoctrine()->getManager();
             $em->persist($video);
             $em->flush();
             //we convert the video to the right size and with the watermark
             $exploded = explode('/', $video->getSource());
             $curl = curl_init();
             curl_setopt($curl, CURLOPT_URL, 'http://v.bestfootball.fr/test/convert.php?file=' . $exploded[3]);
             $retour = curl_exec($curl);
             curl_close($curl);
             $this->addFlash('success', 'Your video was uploaded to our servers.');
             return $this->redirect($this->generateUrl('bf_site_videos'));
         }
         return $this->render('BFSiteBundle:Video:uploadFreestyle.html.twig', array('form' => $form->createView()));
     }
 }