/** * @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())); }