コード例 #1
0
 public function newAction(Request $request)
 {
     /* @var $em \Doctrine\ORM\EntityManager */
     $em = $this->get('doctrine')->getManager();
     $deck_id = filter_var($request->request->get('deck_id'), FILTER_SANITIZE_NUMBER_INT);
     /* @var $deck \AppBundle\Entity\Deck */
     $deck = $this->getDoctrine()->getRepository('AppBundle:Deck')->find($deck_id);
     if ($this->getUser()->getId() != $deck->getUser()->getId()) {
         throw new UnauthorizedHttpException("You don't have access to this deck.");
     }
     $problem = $this->get('deck_validation_helper')->findProblem($deck);
     if ($problem) {
         return $this->render('AppBundle:Default:error.html.twig', ['pagetitle' => "Error", 'error' => 'You cannot publish this deck because it is invalid: "' . $this->get('deck_validation_helper')->getProblemLabel($problem) . '".']);
     }
     $new_content = json_encode($deck->getSlots()->getContent());
     $new_signature = md5($new_content);
     $old_decklists = $this->getDoctrine()->getRepository('AppBundle:Decklist')->findBy(['signature' => $new_signature]);
     foreach ($old_decklists as $decklist) {
         if (json_encode($decklist->getSlots()->getContent()) == $new_content) {
             throw new AccessDeniedHttpException('That decklist already exists.');
         }
     }
     $name = filter_var($request->request->get('name'), FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
     $name = substr($name, 0, 60);
     if (empty($name)) {
         $name = "Untitled";
     }
     $rawdescription = trim($request->request->get('description'));
     $description = $this->get('texts')->markdown($rawdescription);
     $tournament_id = filter_var($request->request->get('tournament'), FILTER_SANITIZE_NUMBER_INT);
     $tournament = $em->getRepository('AppBundle:Tournament')->find($tournament_id);
     $decklist = new Decklist();
     $decklist->setName($name);
     $decklist->setNameCanonical(preg_replace('/[^a-z0-9]+/', '-', mb_strtolower($name)));
     $decklist->setDescriptionMd($rawdescription);
     $decklist->setDescriptionHtml($description);
     $decklist->setUser($this->getUser());
     $decklist->setDateCreation(new \DateTime());
     $decklist->setDateUpdate(new \DateTime());
     $decklist->setSignature($new_signature);
     $decklist->setCoreWorld($deck->getCoreWorld());
     $decklist->setFaction($deck->getFaction());
     $decklist->setLastPack($deck->getLastPack());
     $decklist->setnbVotes(0);
     $decklist->setNbfavorites(0);
     $decklist->setNbcomments(0);
     $decklist->setTournament($tournament);
     foreach ($deck->getSlots() as $slot) {
         $decklistslot = new Decklistslot();
         $decklistslot->setQuantity($slot->getQuantity());
         $decklistslot->setMainDeck($slot->getMainDeck());
         $decklistslot->setTechPool($slot->getTechPool());
         $decklistslot->setCard($slot->getCard());
         $decklistslot->setDecklist($decklist);
         $decklist->getSlots()->add($decklistslot);
     }
     if (count($deck->getChildren())) {
         $decklist->setPrecedent($deck->getChildren()[0]);
     } else {
         if ($deck->getParent()) {
             $decklist->setPrecedent($deck->getParent());
         }
     }
     $decklist->setParent($deck);
     $em->persist($decklist);
     $em->flush();
     return $this->redirect($this->generateUrl('decklist_detail', array('decklist_id' => $decklist->getId(), 'decklist_name' => $decklist->getNameCanonical())));
 }