/** * @Route("/user/challenge/{slug}", name="challenge", requirements={"slug": "\d+"}) */ public function challengeAction(Request $request, $slug) { $player = $this->getDoctrine()->getRepository('AppBundle:User')->find($slug); $lostMatches = $this->getDoctrine()->getRepository('AppBundle:User')->getLostMatches($slug); $wonMatches = $this->getDoctrine()->getRepository('AppBundle:User')->getWonMatches($slug); $challenge = new Challenge(); $message = new Message(); $user = $this->getUser(); $tokenStorage = $this->container->get('security.token_storage'); $form = $this->createForm(new ChallengeType($tokenStorage), $challenge); $form->handleRequest($request); if ($form->isValid()) { $data = $form->getData(); $challenge->setPlayer1($user->getId()); $challenge->setPlayer2($request->get('player2')); $challenge->setPublishedDate(new \DateTime('now')); $challenge->setClub($data->getClub()->getId()); $message->setSenderId($user->getId()); $message->setReceiverId($request->get('player2')); $message->setStatus(0); $message->setMessage($data->getMessage()); $message->setDate(new \DateTime('now')); try { //Saving Challenge in the DataBase $em = $this->getDoctrine()->getManager(); $em->persist($challenge); $em->flush(); $message->setType($challenge->getId()); $em->persist($message); $em->flush(); $this->addFlash('notice', 'Your request was sent correctly.'); return $this->render('user/challenge_notification.html.twig', array('icon' => "fa fa-check fa-2x", 'class' => "text-success")); } catch (Exception $e) { $this->addFlash('error', 'Something went wrong!'); return $this->render('user/challenge_notification.html.twig', array('icon' => "fa fa-exclamation-circle fa-2x", 'class' => "text-danger")); } } return $this->render('user/user_challenge.html.twig', array('form' => $form->createView(), 'slug' => $slug, 'player' => $player, 'won' => $wonMatches, 'lost' => $lostMatches)); }