public function autosaveAction(Request $request) { $user = $this->getUser(); /* @var $em \Doctrine\ORM\EntityManager */ $em = $this->getDoctrine()->getManager(); $deck_id = $request->get('deck_id'); $deck = $em->getRepository('AppBundle:Deck')->find($deck_id); if (!$deck) { throw new BadRequestHttpException("Cannot find deck " . $deck_id); } if ($user->getId() != $deck->getUser()->getId()) { throw new UnauthorizedHttpException("You don't have access to this deck."); } $diff = (array) json_decode($request->get('diff')); if (count($diff) != 2) { $this->get('logger')->error("cannot use diff", $diff); throw new BadRequestHttpException("Wrong content " . json_encode($diff)); } if (count($diff[0]) || count($diff[1])) { $change = new Deckchange(); $change->setDeck($deck); $change->setVariation(json_encode($diff)); $change->setIsSaved(FALSE); $em->persist($change); $em->flush(); } return new Response($change->getDatecreation()->format('c')); }
/** * * @param unknown $user * @param Deck $deck * @param unknown $decklist_id * @param unknown $name * @param unknown $faction * @param unknown $description * @param unknown $tags * @param unknown $content * @param unknown $source_deck */ public function saveDeck($user, $deck, $decklist_id, $name, $faction, $description, $tags, $content, $source_deck) { $deck_content = []; if ($decklist_id) { $decklist = $this->doctrine->getRepository('AppBundle:Decklist')->find($decklist_id); if ($decklist) { $deck->setParent($decklist); } } $deck->setName($name); $deck->setFaction($faction); $deck->setDescriptionMd($description); $deck->setUser($user); $deck->setMinorVersion($deck->getMinorVersion() + 1); $cards = []; /* @var $latestPack \AppBundle\Entity\Pack */ $latestPack = null; foreach ($content as $card_code => $qty) { $card = $this->doctrine->getRepository('AppBundle:Card')->findOneBy(array("code" => $card_code)); if (!$card) { continue; } $pack = $card->getPack(); if (!$latestPack) { $latestPack = $pack; } else { if ($latestPack->getCycle()->getPosition() < $pack->getCycle()->getPosition()) { $latestPack = $pack; } else { if ($latestPack->getCycle()->getPosition() == $pack->getCycle()->getPosition() && $latestPack->getPosition() < $pack->getPosition()) { $latestPack = $pack; } } } $cards[$card_code] = $card; } $deck->setLastPack($latestPack); if (empty($tags)) { // tags can never be empty. if it is we put faction in $tags = [$faction->getCode()]; } if (is_string($tags)) { $tags = preg_split('/\\s+/', $tags); } $tags = implode(' ', array_unique(array_values($tags))); $deck->setTags($tags); $this->doctrine->persist($deck); // on the deck content if ($source_deck) { // compute diff between current content and saved content list($listings) = $this->diff->diffContents(array($content, $source_deck->getSlots()->getContent())); // remove all change (autosave) since last deck update (changes are sorted) $changes = $this->getUnsavedChanges($deck); foreach ($changes as $change) { $this->doctrine->remove($change); } $this->doctrine->flush(); // save new change unless empty if (count($listings[0]) || count($listings[1])) { $change = new Deckchange(); $change->setDeck($deck); $change->setVariation(json_encode($listings)); $change->setIsSaved(TRUE); $change->setVersion($deck->getVersion()); $this->doctrine->persist($change); $this->doctrine->flush(); } // copy version $deck->setMajorVersion($source_deck->getMajorVersion()); $deck->setMinorVersion($source_deck->getMinorVersion()); } foreach ($deck->getSlots() as $slot) { $deck->removeSlot($slot); $this->doctrine->remove($slot); } foreach ($content as $card_code => $qty) { $card = $cards[$card_code]; $slot = new Deckslot(); $slot->setQuantity($qty); $slot->setCard($card); $slot->setDeck($deck); $deck->addSlot($slot); $deck_content[$card_code] = array('card' => $card, 'qty' => $qty); } $deck->setProblem($this->deck_validation_helper->findProblem($deck)); return $deck->getId(); }