/**
  * Generate the content element
  */
 public function compile()
 {
     global $container;
     /** @var SubscriptionManager $subscriptionManager */
     $subscriptionManager = $container['avisota.subscription'];
     /** @var EventDispatcher $eventDispatcher */
     $eventDispatcher = $container['event-dispatcher'];
     $token = (array) \Input::get('token');
     if (count($token)) {
         $subscriptions = $subscriptionManager->confirmByToken($token);
         \Session::getInstance()->set('AVISOTA_LAST_SUBSCRIPTIONS', $subscriptions);
         if ($this->avisota_activation_confirmation_page) {
             $event = new GetPageDetailsEvent($this->avisota_activation_confirmation_page);
             $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_GET_PAGE_DETAILS, $event);
             $event = new GenerateFrontendUrlEvent($event->getPageDetails());
             $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_GENERATE_FRONTEND_URL, $event);
             $event = new RedirectEvent($event->getUrl());
             $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_REDIRECT, $event);
         }
         $this->Template->confirmed = $subscriptions;
     } else {
         if ($this->avisota_activation_redirect_page) {
             $event = new GetPageDetailsEvent($this->avisota_activation_redirect_page);
             $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_GET_PAGE_DETAILS, $event);
             $event = new GenerateFrontendUrlEvent($event->getPageDetails());
             $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_GENERATE_FRONTEND_URL, $event);
             $event = new RedirectEvent($event->getUrl());
             $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_REDIRECT, $event);
         }
     }
 }
 /**
  * Handle a redirect event.
  *
  * @param RedirectEvent $event The event.
  *
  * @return void
  */
 public static function handleRedirect(RedirectEvent $event)
 {
     \Controller::redirect($event->getNewLocation(), $event->getStatusCode());
 }
 /**
  * @param $form
  * @param $recipientFields
  * @param $mailingListIds
  */
 protected function parseSubscriptions($form, $recipientFields, $mailingListIds)
 {
     if (!$form->validate()) {
         return;
     }
     global $container;
     /** @var SubscriptionManager $subscriptionManager */
     $subscriptionManager = $container['avisota.subscription'];
     /** @var EventDispatcher $eventDispatcher */
     $eventDispatcher = $container['event-dispatcher'];
     $values = $form->fetchAll();
     $email = $values['email'];
     $repository = EntityHelper::getRepository('Avisota\\Contao:Recipient');
     $recipient = $repository->findOneBy(array('email' => $email));
     if (!$recipient) {
         $recipient = new Recipient();
     }
     $this->setPropertiesToRecipient($recipient, $values);
     $entityManager = EntityHelper::getEntityManager();
     $entityManager->persist($recipient);
     if (in_array('mailingLists', $recipientFields)) {
         $mailingLists = $this->loadMailingLists($values['mailingLists']);
     } else {
         $mailingLists = $this->loadMailingLists($mailingListIds);
     }
     $subscriptions = $subscriptionManager->subscribe($recipient, $mailingLists, SubscriptionManager::OPT_IGNORE_BLACKLIST | SubscriptionManager::OPT_INCLUDE_EXISTING);
     $subscriptions = array_filter($subscriptions, function (Subscription $subscription) {
         return !$subscription->getActive();
     });
     /** @var Subscription[] $subscriptions */
     \Session::getInstance()->set('AVISOTA_LAST_SUBSCRIPTIONS', $subscriptions);
     $entityManager->flush();
     if (count($subscriptions)) {
         $this->subscribeConfirmationMessage($subscriptions, $recipient);
         if ($this->avisota_subscribe_confirmation_page) {
             $event = new GetPageDetailsEvent($this->avisota_subscribe_confirmation_page);
             $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_GET_PAGE_DETAILS, $event);
             $event = new GenerateFrontendUrlEvent($event->getPageDetails());
             $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_GENERATE_FRONTEND_URL, $event);
             $event = new RedirectEvent($event->getUrl());
             $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_REDIRECT, $event);
         }
     }
     $this->Template->subscriptions = $subscriptions;
 }
 /**
  * @param Form $form
  */
 protected function validateFormAndSendMail(Form $form)
 {
     if (!$form->validate()) {
         return;
     }
     global $container;
     /** @var EventDispatcher $eventDispatcher */
     $eventDispatcher = $container['event-dispatcher'];
     /** @var TransportInterface $transport */
     $transport = $container['avisota.transport.default'];
     $values = $form->fetchAll();
     $email = $values['email'];
     $repository = EntityHelper::getRepository('Avisota\\Contao:Recipient');
     $recipient = $repository->findOneBy(array('email' => $email));
     if ($recipient) {
         /** @var \Avisota\Contao\Entity\Recipient $recipient */
         if ($this->avisota_unsubscribe_show_mailing_lists) {
             $mailingListIds = $values['mailingLists'];
         }
         if (!$this->avisota_unsubscribe_show_mailing_lists) {
             $mailingListIds = deserialize($this->avisota_mailing_lists);
         }
         $subscriptions = $recipient->getSubscriptions();
         $subscriptions = array_filter($subscriptions->toArray(), function (\Avisota\Contao\Entity\Subscription $subscription) use($mailingListIds) {
             return $subscription->getMailingList() && in_array($subscription->getMailingList()->getId(), $mailingListIds);
         });
         /** @var SubscriptionManager $subscriptionManager */
         $subscriptionManager = $container['avisota.subscription'];
         /** @var Subscription[] $subscriptions */
         $subscriptionManager->unsubscribe($subscriptions);
         \Session::getInstance()->set('AVISOTA_LAST_SUBSCRIPTIONS', $subscriptions);
         if (count($subscriptions)) {
             if ($this->avisota_unsubscribe_confirmation_message) {
                 $messageRepository = EntityHelper::getRepository('Avisota\\Contao:Message');
                 $message = $messageRepository->find($this->avisota_unsubscribe_confirmation_message);
                 if ($message) {
                     /** @var MessageRendererInterface $renderer */
                     $renderer = $container['avisota.message.renderer'];
                     $data = array('subscriptions' => $subscriptions);
                     $template = $renderer->renderMessage($message);
                     $mail = $template->render($recipient, $data);
                     $transport->send($mail);
                 }
             }
             if ($this->avisota_unsubscribe_confirmation_page) {
                 $event = new GetPageDetailsEvent($this->avisota_unsubscribe_confirmation_page);
                 $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_GET_PAGE_DETAILS, $event);
                 $event = new GenerateFrontendUrlEvent($event->getPageDetails());
                 $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_GENERATE_FRONTEND_URL, $event);
                 $event = new RedirectEvent($event->getUrl());
                 $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_REDIRECT, $event);
             }
         }
         $this->Template->subscriptions = $subscriptions;
     } else {
         $this->Template->subscriptions = array();
     }
 }