/** * @Route("/competition/{competitionId}/picture") * @Method({"GET", "POST"}) */ public function createAction(Request $request, $competitionId) { $competitor = $this->get('security.token_storage')->getToken()->getUser(); if (!$competitor instanceof Competitor || !$competitor->hasRole('ROLE_OAUTH_USER')) { return $this->redirectToRoute('hwi_oauth_service_redirect', ['service' => 'facebook', '_target_path' => '/competition/' . $competitionId . '/picture']); } /** @var HotOrNotBundle\Manager\CombinationManager */ $combinationManager = $this->get('hot_or_not.combination_manager'); $competition = $this->getCompetitionById($competitionId); if ($competition->isFinished()) { return $this->redirectToRoute('hotornot_voting_ranking', ['competitionId' => $competition->getId()]); } $picture = new Picture(); $picture->setCompetition($competition); $picture->setCompetitor($this->get('security.token_storage')->getToken()->getUser()); $picture->setStatus('ADDED'); $picture->setAddedAt(new \DateTime('now')); $form = $this->createForm(PictureType::class, $picture); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */ $file = $picture->getFile(); // Generate a unique name for the file before saving it $fileName = md5(uniqid()) . '.' . $file->guessExtension(); // Update the 'file' property to store the file name // instead of its contents $picture->setFile($fileName); $em = $this->getDoctrine()->getManager(); $em->persist($picture); $em->flush($picture); // update the combinations $combinationManager->updateCombinationsByCompetition($competition); // Move the file to the directory where this competitions pictures are stored $picturesDir = $this->container->getParameter('kernel.root_dir') . '/../web/uploads/pictures/' . $competition->getId() . '/'; $file->move($picturesDir, $fileName); return $this->redirectToRoute('hotornot_picture_show', ['competitionId' => $competition->getId(), 'pictureId' => $picture->getId()]); } return $this->render('HotOrNotBundle:Picture:create.html.twig', array('competition' => $competition, 'form' => $form->createView())); }
/** * Get votes for a given picture * * @param Picture $picture * @param integer $limit * * @return array */ public function getVotesForPicture(Picture $picture, $limit = 50) { return $this->repository->createQueryBuilder('v')->where('v.votedForPicture = :pictureId')->orWhere('v.votedAgainstPicture = :pictureId')->setParameter('pictureId', $picture->getId())->addOrderBy('v.votedAt', 'DESC')->setMaxResults($limit)->getQuery()->getResult(); }
/** * Get rank information for a picture * * @param Picture $picture * * @return array */ public function getRankForPicture(Picture $picture) { $ranking = $this->getRankingForCompetition($picture->getCompetition(), null); foreach ($ranking as $rank) { if ($rank['picture']->getId() === $picture->getId()) { return $rank; } } return null; }
/** * Update a combination status * * @param Competition $competition * @param Picture $leftPicture * @param Picture $rightPicture * * @return void */ private function updateCombinationStatus(Competition $competition, Picture $leftPicture, Picture $rightPicture) { if ($leftPicture->getId() < $rightPicture->getId()) { $combination = $this->getCombinationByPictures($competition, $leftPicture, $rightPicture); if (empty($combination)) { $combination = new Combination(); $combination->setCompetition($competition); $combination->setLeftPicture($leftPicture); $combination->setRightPicture($rightPicture); } $active = false; if ($leftPicture->isActive() && $rightPicture->isActive()) { $active = true; } if ($combination->getActive() !== $active) { $combination->setActive($active); $this->entityManager->persist($combination); } } }