public function getVotesRemaining(Video $video, $ipAddress, $sessionId, \DateTime $day)
 {
     $q = $this->getEntityManager()->createQuery('SELECT COUNT(v.id) FROM GotChosenSiteBundle:VideoVote v
          WHERE v.video = ?1 AND v.ip4Address = ?2 AND v.dtAdded BETWEEN ?3 AND ?4 AND v.sessionId = ?5');
     $q->setParameter(1, $video->getId());
     $q->setParameter(2, $ipAddress);
     $q->setParameter(3, $day->format('Y-m-d 00:00:00'));
     $q->setParameter(4, $day->format('Y-m-d 23:59:59'));
     $q->setParameter(5, $sessionId);
     $sessionCount = $q->getSingleScalarResult();
     if ($sessionCount == 0) {
         $q = $this->getEntityManager()->createQuery('SELECT COUNT(v.id) FROM GotChosenSiteBundle:VideoVote v
              WHERE v.video = ?1 AND v.ip4Address = ?2 AND v.dtAdded BETWEEN ?3 AND ?4');
         $q->setParameter(1, $video->getId());
         $q->setParameter(2, $ipAddress);
         $q->setParameter(3, $day->format('Y-m-d 00:00:00'));
         $q->setParameter(4, $day->format('Y-m-d 23:59:59'));
         $dayCount = $q->getSingleScalarResult();
         //return $dayCount;
         return max(0, VideoVote::MAX_PER_DAY - $dayCount);
     } else {
         return 0;
     }
 }
 /**
  * @param Request $request
  * @throws AccessDeniedException
  * @return array
  *
  * @Route("/video-scholarship/submit", name="vs_submit")
  * @Template
  */
 public function submitAction(Request $request)
 {
     $sship = $this->repo('Scholarship')->getCurrentVideo();
     if (!$sship) {
         $this->flash("warning", "There us currently no Video Scholarship contest running.");
         return $this->redirectRoute('video_scholarship');
     }
     /** @var User $user */
     $user = $this->getUser();
     if ($user === null || !$user->hasRole('ROLE_USER')) {
         $this->flash('error', 'You must be registered and logged in to submit a video entry.');
         throw new AccessDeniedException();
     }
     $video = $this->repo('Video')->findOneBy(['user' => $user->getId()]);
     if ($video) {
         return $this->redirectRoute('vs_manage');
     } else {
         $sship = $this->repo('Scholarship')->getCurrentVideo();
         if ($user->hasApplied($sship)) {
         } else {
             //$this->flash('error', 'You must provide the required information in order to apply for the video scholarship and submit a video entry.');
             //$absURL = $this->get('router')->generate('scholarship_apply', array('id' => $sship->getId()), true);
             //return $this->redirect($absURL);
             return ['sship' => $sship];
         }
     }
     $fb = $this->createFormBuilder();
     $fb->add('videoTitle', 'text', ['label' => 'Video Title', 'constraints' => [new NotBlank()]])->add('videoCategory', 'entity', ['class' => 'GotChosenSiteBundle:VideoCategory', 'empty_value' => 'Pick a Category', 'empty_data' => null, 'required' => true, 'property' => 'categoryName'])->add('youtubeURL', 'text', ['label' => 'YoutubeURL', 'constraints' => [new NotBlank()]])->add('accept', 'checkbox', ['label' => 'I Accept the Rules', 'widget_checkbox_label' => 'label', 'error_type' => 'block', 'constraints' => [new NotBlank()]]);
     $form = $fb->getForm();
     $form->handleRequest($request);
     $youtubeId = '';
     // Check to see if it is a valid Youtube URL.
     $youtubeURL = trim($form->get('youtubeURL')->getData());
     if (strpos($youtubeURL, 'youtube.com') === false && strpos($youtubeURL, 'youtu.be.com') === false) {
         if ($form->isValid()) {
             $form->get('youtubeURL')->addError(new FormError('You did not enter a valid Youtube URL.'));
         }
     }
     // Check what type of Youtube URL it is.
     $urlType = '';
     $ytTemp = strstr($youtubeURL, 'v=');
     if (strlen($ytTemp) > 0) {
         // Is a standard Youtube address such as: https://www.youtube.com/watch?v=ZcekUuC7D9Y
         // Now that we know the type, we can extract the Video ID.
         $youtubeId = substr($ytTemp, 2, 11);
     }
     $ytTemp = strstr($youtubeURL, '/v/');
     if (strlen($ytTemp) > 0) {
         // Is an old embed Youtube address such as: https://www.youtube.com/v/ZcekUuC7D9Y?hl=en_US&version=3
         // Now that we know the type, we can extract the Video ID.
         $youtubeId = substr($ytTemp, 3, 11);
     }
     $ytTemp = strstr($youtubeURL, '/embed/');
     if (strlen($ytTemp) > 0) {
         // Is a new embed Youtube address such as: https://www.youtube.com/embed/ZcekUuC7D9Y
         // Now that we know the type, we can extract the Video ID.
         $youtubeId = substr($ytTemp, 7, 11);
     }
     if (strlen($youtubeId) > 0) {
         // Looks like we have a good Id to validate.
         $JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$youtubeId}?v=2&alt=json");
         $JSON_Data = json_decode($JSON);
         $videoState = $JSON_Data->{'entry'}->{'app:control'}->{'yt:state'};
         if (strlen($videoState) > 0) {
             // Video is not playable.  No good.
             if ($form->isValid()) {
                 $form->get('youtubeURL')->addError(new FormError('There is something wrong with the video you submitted.  It is either restricted, deleted or still processing.'));
             }
         } else {
             // Video is loaded, playable and was not rejected or removed.
         }
     } else {
         // Not a good Youtube URL.
         if ($form->isValid()) {
             $form->get('youtubeURL')->addError(new FormError('You did not enter a valid Youtube URL.'));
         }
     }
     if ($form->isValid()) {
         $video = new Video();
         $video->setUser($user);
         $scholarship = $this->repo('Scholarship')->getCurrentVideo();
         $video->setScholarship($scholarship);
         $formCategoryData = $form->get('videoCategory')->getData();
         if ($formCategoryData == null) {
             $form->get('videoCategory')->addError(new FormError('You must pick a category.'));
             return ['form' => $form->createView()];
         }
         $category = $this->repo('VideoCategory')->findOneBy(['id' => $formCategoryData]);
         $video->setCategory($category);
         $status = $this->repo('VideoStatus')->findOneBy(['id' => 1]);
         $video->setStatus($status);
         $video->setTitle($form->get('videoTitle')->getData());
         $video->setYoutubeURL($youtubeId);
         $video->setDTAdded(new \DateTime('now'));
         $this->em()->persist($video);
         $this->em()->flush();
         $this->flash('success', "Your video entry was submitted successfully.");
         return $this->redirectRoute('vs_manage');
     }
     return ['form' => $form->createView()];
 }