public function createDecklistFromDeck(Deck $deck, $name = null, $descriptionMd = null) { $lastPack = $deck->getLastPack(); if (!$lastPack->getDateRelease() || $lastPack->getDateRelease() > new \DateTime()) { throw new \Exception("You cannot publish this deck yet, because it has unreleased cards."); } $problem = $this->deckValidationHelper->findProblem($deck); if ($problem) { throw new \Exception('This deck cannot be published because it is invalid: "' . $this->deckValidationHelper->getProblemLabel($problem) . '".'); } // all good for decklist publication // increasing deck version $deck->setMinorVersion(0); $deck->setMajorVersion($deck->getMajorVersion() + 1); if (empty($name)) { $name = $deck->getName(); } $name = substr($name, 0, 60); if (empty($descriptionMd)) { $descriptionMd = $deck->getDescriptionMd(); } $description = $this->texts->markdown($descriptionMd); $new_content = json_encode($deck->getSlots()->getContent()); $new_signature = md5($new_content); $decklist = new Decklist(); $decklist->setName($name); $decklist->setVersion($deck->getVersion()); $decklist->setNameCanonical($this->texts->slugify($name) . '-' . $decklist->getVersion()); $decklist->setDescriptionMd($descriptionMd); $decklist->setDescriptionHtml($description); $decklist->setDateCreation(new \DateTime()); $decklist->setDateUpdate(new \DateTime()); $decklist->setSignature($new_signature); $decklist->setFaction($deck->getFaction()); $decklist->setLastPack($deck->getLastPack()); $decklist->setNbVotes(0); $decklist->setNbfavorites(0); $decklist->setNbcomments(0); $decklist->setUser($deck->getUser()); foreach ($deck->getSlots() as $slot) { $decklistslot = new Decklistslot(); $decklistslot->setQuantity($slot->getQuantity()); $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); $deck->setMinorVersion(1); return $decklist; }
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()))); }