/** * @param MailingList $list * @param $email * * @return null|Subscription */ public function findOrCreate(MailingList $list, $email) { /** @var SubscriptionRepository $repo */ $repo = $this->em->getRepository('OpiferMailingListBundle:Subscription'); $subscription = $repo->findInListByEmail($list, $email); if (!$subscription) { $subscription = new Subscription(); $subscription->setMailingList($list); $subscription->setEmail($email); } return $subscription; }
/** * @param BlockInterface $block */ public function load(BlockInterface $block) { $this->form = $this->formFactory->create(SubscribeType::class, $this->subscription); $this->form->handleRequest($this->request); if ($this->form->isValid()) { foreach ($this->getMailingLists($block) as $mailingList) { $this->subscription->setMailingList($mailingList); $this->em->persist($this->subscription); $this->em->flush($this->subscription); // Reset to add to another mailing list $this->em->detach($this->subscription); $this->subscription = clone $this->subscription; $this->subscription->setId(null); } $this->subscribed = true; } }
/** * This method is called right after the post is stored in the database during the Form submit. * * @param FormSubmitEvent $event */ public function postFormSubmit(FormSubmitEvent $event) { $post = $event->getPost(); $mailinglists = $email = null; foreach ($post->getValueSet()->getValues() as $value) { if ($value instanceof MailingListSubscribeValue && $value->getValue() == true) { $parameters = $value->getAttribute()->getParameters(); if (isset($parameters['mailingLists'])) { $mailinglists = $this->mailingListManager->getRepository()->findByIds($parameters['mailingLists']); } } elseif ($value instanceof EmailValue) { $email = $value->getValue(); } } if ($email && $mailinglists) { foreach ($mailinglists as $mailinglist) { $subscription = new Subscription(); $subscription->setEmail($email); $subscription->setMailingList($mailinglist); $this->subscriptionManager->save($subscription); } } }