container = $_container; $this->entityManager = $_container->get('doctrine.orm.entity_manager'); $this->repository = $this->entityManager->getRepository(FormForgeMappingEvent::class); } /** * @param FormForgeMappingEvent $element * @return bool */ private function quickUpdate(FormForgeMappingEvent $element){ if(empty($element)) return false; try { $this->entityManager->persist($element); $this->entityManager->flush(); } catch (ORMException $e) { return false; } return true; } private function delete($_id){ $element = $this->getById($_id); if($this->entityManager->contains($element)){ try { $this->entityManager->remove($element); $this->entityManager->flush(); } catch (ORMException $e) { return false; } } return true; } /** * @param int $_id * @return FormForgeMappingEvent|object|null */ public function getById(int $_id){ return $this->repository->findOneBy(['id'=>$_id]); } /** * @return array|FormForgeMappingEvent[]|object[] */ public function getAll(){ return $this->repository->findAll(); } /** * @param string $_name * @return FormForgeMappingEvent|object|null */ public function getByName(string $_name){ return $this->repository->findOneBy(['name'=>$_name]); } /** * @param string $_name * @param FormForgeReason $formForgeReason * @return FormForgeMappingEvent|null */ public function createEventName(string $_name, FormForgeReason $formForgeReason){ $element = new FormForgeMappingEvent(); $element->setName($_name); $element->addFormForgeReason($formForgeReason); if (!$this->quickUpdate($element)) return null; $formForgeReason->addFormForgeMappingEvent($element); $this->container->get('net15.form.forge.reasons.manager')->quickUpdate($formForgeReason); return $element; } /** * @param int $_id * @return bool|null */ public function deleteEventName(int $_id){ if (!$this->delete($_id)) return null; return true; } /** * @param FormForgeReason $reason * @param array $data * @return FormForgeReason */ public function addEventToReason(FormForgeReason $reason, array $data){ // Delete the first line named _csrf_token form array $data unset($data['_csrf_token']); $events = []; // Recuperate event and add it inside reason foreach ($data as $id){ $event = $this->getById($id); $events[] = $event; $reason->addFormForgeMappingEvent($event); } // We check if one of the previous events is deactivated we remove it $formForgeEvents = $reason->getFormForgeMappingEvents()->getValues(); foreach ($formForgeEvents as $forgeEvent){ if (!in_array($forgeEvent, $events)){ $reason->removeFormForgeMappingEvent($forgeEvent); } } $this->container->get('net15.form.forge.reasons.manager')->quickUpdate($reason); return $reason; } }