/** * @param Badge $badge * * @return bool True on successfull processing, false otherwise */ public function handleEdit(Badge $badge, $badgeManager = null, $unawardBadge = false) { $this->form->setData($badge); /** @var BadgeRule[]|\Doctrine\Common\Collections\ArrayCollection $originalRules */ $originalRules = $badge->getRules(); if ($this->request->isMethod('POST')) { $this->form->handleRequest($this->request); if ($this->form->isValid()) { $this->handleUpload($this->form->get('file')->getData(), $badge); $badgeRules = $badge->getRules(); $userBadges = $badge->getUserBadges(); if (0 < count($userBadges) && $this->badgeManager->isRuleChanged($badgeRules, $originalRules)) { /** @var \Doctrine\ORM\UnitOfWork $unitOfWork */ $unitOfWork = $this->entityManager->getUnitOfWork(); $newBadge = clone $badge; $newBadge->setVersion($badge->getVersion() + 1); $unitOfWork->refresh($badge); $badge->setDeletedAt(new \DateTime()); $this->entityManager->persist($newBadge); // If the new badge has to be revoked from users already awarded, skip the next part if (!$unawardBadge) { foreach ($userBadges as $userBadge) { // Award new version to previous users $badgeManager->addBadgeToUser($newBadge, $userBadge->getUser()); } } } else { // Compute which rules was deleted foreach ($badgeRules as $rule) { if ($originalRules->contains($rule)) { $originalRules->removeElement($rule); } } // Delete rules foreach ($originalRules as $rule) { $this->entityManager->remove($rule); } } $this->entityManager->persist($badge); $this->entityManager->flush(); return true; } } return false; }