/**
  * @param Message     $message
  * @param BackendUser $user
  *
  * @return mixed|void
  */
 protected function execute(Message $message, \BackendUser $user)
 {
     global $container;
     $general = new \ContaoCommunityAlliance\DcGeneral\DC_General('orm_avisota_message');
     $environment = $general->getEnvironment();
     $translator = $environment->getTranslator();
     $GLOBALS['TL_LANGUAGE'] = $message->getLanguage();
     /** @var \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher */
     $eventDispatcher = $GLOBALS['container']['event-dispatcher'];
     $user = \Input::get('recipient_user');
     /** @var \Doctrine\DBAL\Connection $connection */
     $connection = $container['doctrine.connection.default'];
     $queryBuilder = $connection->createQueryBuilder();
     /** @var \Doctrine\DBAL\Statement $statement */
     $statement = $queryBuilder->select('u.*')->from('tl_user', 'u')->where('id=:id')->setParameter(':id', $user)->execute();
     $userData = $statement->fetch();
     if (!$userData) {
         $_SESSION['AVISOTA_SEND_PREVIEW_TO_USER_EMPTY'] = true;
         header('Location: ' . $url);
         exit;
     }
     $idSerializer = new ModelId('orm_avisota_message', $message->getId());
     $pidSerializer = new ModelId('orm_avisota_message_category', $message->getCategory()->getId());
     $url = sprintf('%scontao/main.php?do=avisota_newsletter&table=orm_avisota_message&act=preview&id=%s&pid=%s', \Environment::get('base'), $idSerializer->getSerialized(), $pidSerializer->getSerialized());
     if ($message->getCategory()->getViewOnlinePage()) {
         $event = new LoadLanguageFileEvent('avisota_message');
         $eventDispatcher->dispatch(ContaoEvents::SYSTEM_LOAD_LANGUAGE_FILE, $event);
         $viewOnlineLink = sprintf($translator->translate('viewOnline', 'avisota_message'), $url);
     } else {
         $viewOnlineLink = false;
     }
     $event = new \Avisota\Contao\Core\Event\CreateFakeRecipientEvent($message);
     $eventDispatcher->dispatch(\Avisota\Contao\Core\CoreEvents::CREATE_FAKE_RECIPIENT, $event);
     $recipient = $event->getRecipient();
     $recipient->setEmail($userData['email']);
     $additionalData = array('view_online_link' => $viewOnlineLink);
     /** @var \Avisota\Contao\Message\Core\Renderer\MessageRendererInterface $renderer */
     $renderer = $container['avisota.message.renderer'];
     $messageTemplate = $renderer->renderMessage($message);
     $messageMail = $messageTemplate->render($recipient, $additionalData);
     /** @var \Avisota\Transport\TransportInterface $transport */
     $transport = $GLOBALS['container']['avisota.transport.' . $message->getQueue()->getTransport()->getId()];
     $transport->send($messageMail);
     $event = new \ContaoCommunityAlliance\Contao\Bindings\Events\System\LoadLanguageFileEvent('avisota_message_preview');
     $eventDispatcher->dispatch(\ContaoCommunityAlliance\Contao\Bindings\ContaoEvents::SYSTEM_LOAD_LANGUAGE_FILE, $event);
     $_SESSION['TL_CONFIRM'][] = sprintf($translator->translate('previewSend', 'avisota_message_preview'), $recipient->getEmail());
     header('Location: ' . $url);
     exit;
 }
Ejemplo n.º 2
0
 public function run()
 {
     global $container;
     $general = new \ContaoCommunityAlliance\DcGeneral\DC_General('orm_avisota_message');
     $environment = $general->getEnvironment();
     $translator = $environment->getTranslator();
     /** @var \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher */
     $eventDispatcher = $GLOBALS['container']['event-dispatcher'];
     $messageRepository = \Contao\Doctrine\ORM\EntityHelper::getRepository('Avisota\\Contao:Message');
     $messageId = \Input::get('id');
     /** @var \Avisota\Contao\Entity\Message $message */
     $message = $messageRepository->find($messageId);
     if (!$message) {
         header("HTTP/1.0 404 Not Found");
         echo '<h1>404 Not Found</h1>';
         exit;
     }
     $GLOBALS['TL_LANGUAGE'] = $message->getLanguage();
     $event = new \Avisota\Contao\Core\Event\CreateFakeRecipientEvent($message);
     $eventDispatcher->dispatch(\Avisota\Contao\Core\CoreEvents::CREATE_FAKE_RECIPIENT, $event);
     $recipient = $event->getRecipient();
     if ($message->getCategory()->getViewOnlinePage()) {
         // Fixme can rmove this?
         $event = new LoadLanguageFileEvent('avisota_message');
         $eventDispatcher->dispatch(ContaoEvents::SYSTEM_LOAD_LANGUAGE_FILE, $event);
         $url = sprintf($translator->translate('viewOnline', 'avisota_message'), sprintf('%ssystem/modules/avisota-message/web/preview.php?id=%s', \Environment::get('base'), $message->getId()));
     } else {
         $url = false;
     }
     $additionalData = array('view_online_link' => $url);
     /** @var \Avisota\Contao\Message\Core\Renderer\MessageRendererInterface $renderer */
     $renderer = $container['avisota.message.renderer'];
     $messageTemplate = $renderer->renderMessage($message);
     $messagePreview = $messageTemplate->renderPreview($recipient, $additionalData);
     header('Content-Type: ' . $messageTemplate->getContentType() . '; charset=' . $messageTemplate->getContentEncoding());
     header('Content-Disposition: inline; filename="' . $messageTemplate->getContentName() . '"');
     echo $messagePreview;
     exit;
 }
Ejemplo n.º 3
0
 /**
  * @param Message     $message
  * @param BackendUser $user
  *
  * @return mixed|void
  */
 protected function execute(Message $message, \BackendUser $user)
 {
     global $container;
     $general = new \ContaoCommunityAlliance\DcGeneral\DC_General('orm_avisota_message');
     $environment = $general->getEnvironment();
     $translator = $environment->getTranslator();
     $GLOBALS['TL_LANGUAGE'] = $message->getLanguage();
     $eventDispatcher = $this->getEventDispatcher();
     $entityManager = EntityHelper::getEntityManager();
     $queueData = $message->getQueue();
     $serviceName = sprintf('avisota.queue.%s', $queueData->getId());
     $queue = $container[$serviceName];
     $recipientSourceData = $message->getRecipients();
     $serviceName = sprintf('avisota.recipientSource.%s', $recipientSourceData->getId());
     /** @var \Avisota\RecipientSource\RecipientSourceInterface $recipientSource */
     $recipientSource = $container[$serviceName];
     /** @var \Avisota\Contao\Message\Core\Renderer\MessageRendererInterface $renderer */
     $renderer = $container['avisota.message.renderer'];
     $messageTemplate = $renderer->renderMessage($message);
     // Fixme can i remove this?
     $event = new LoadLanguageFileEvent('avisota_message');
     $eventDispatcher->dispatch(ContaoEvents::SYSTEM_LOAD_LANGUAGE_FILE, $event);
     $idSerializer = new ModelId('orm_avisota_message', $message->getId());
     $pidSerializer = new ModelId('orm_avisota_message_category', $message->getCategory()->getId());
     $viewOnlinePage = $message->getCategory()->getViewOnlinePage();
     if ($viewOnlinePage) {
         $getPageDetailsEvent = new GetPageDetailsEvent($viewOnlinePage);
         $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_GET_PAGE_DETAILS, $getPageDetailsEvent);
         $pageDetails = $getPageDetailsEvent->getPageDetails();
         $generateUrlEvent = new GenerateFrontendUrlEvent($pageDetails, '/' . $message->getAlias(), $pageDetails['language']);
         $eventDispatcher->dispatch(ContaoEvents::CONTROLLER_GENERATE_FRONTEND_URL, $generateUrlEvent);
         $url = $generateUrlEvent->getUrl();
         if (!preg_match('~^\\w+:~', $url)) {
             $url = \Environment::get('base') . $url;
         }
         $generateViewOnlineUrlEvent = new GenerateViewOnlineUrlEvent($message, $url);
         $eventDispatcher->dispatch(MessageEvents::GENERATE_VIEW_ONLINE_URL, $generateViewOnlineUrlEvent);
         $url = sprintf($translator->translate('viewOnline', 'avisota_message'), $generateViewOnlineUrlEvent->getUrl());
     } else {
         $url = false;
     }
     // TODO fix view online link
     $additionalData = array('view_online_link' => $url);
     $turn = \Input::get('turn');
     if (!$turn) {
         $turn = 0;
     }
     $loop = \Input::get('loop');
     if (!$loop) {
         $loop = uniqid();
     }
     $event = new \Avisota\Contao\Core\Event\PreSendImmediateEvent($message, $turn, $loop);
     $eventDispatcher->dispatch('avisota.pre-send-immediate', $event);
     $queueHelper = new \Avisota\Queue\QueueHelper();
     $queueHelper->setEventDispatcher($GLOBALS['container']['event-dispatcher']);
     $queueHelper->setQueue($queue);
     $queueHelper->setRecipientSource($recipientSource);
     $queueHelper->setMessageTemplate($messageTemplate);
     $queueHelper->setNewsletterData($additionalData);
     $count = $queueHelper->enqueue((int) $queueData->getMaxSendCount(), $turn * (int) $queueData->getMaxSendCount());
     $event = new \Avisota\Contao\Core\Event\PostSendImmediateEvent($count, $message, $turn, $loop);
     $eventDispatcher->dispatch('avisota.post-send-immediate', $event);
     if ($count || $turn * (int) $queueData->getMaxSendCount() < $recipientSource->countRecipients()) {
         $eventDispatcher->dispatch(ContaoEvents::SYSTEM_LOAD_LANGUAGE_FILE, new LoadLanguageFileEvent('avisota_message_preview'));
         $_SESSION['TL_CONFIRM'][] = sprintf($translator->translate('messagesEnqueued', 'avisota_message_preview'), $count, $turn + 1);
         $parameters = array('id' => $message->getId(), 'turn' => $turn + 1, 'loop' => $loop);
         $url = sprintf('%ssystem/modules/avisota-message/web/send_immediate.php?%s', \Environment::get('base'), http_build_query($parameters));
         $entityManager->flush();
     } else {
         $parameters = array('do' => 'avisota_outbox', 'execute' => $queueData->getId());
         $url = sprintf('%scontao/main.php?%s', \Environment::get('base'), http_build_query($parameters));
         $message->setSendOn(new \DateTime());
         $entityManager->persist($message);
         $entityManager->flush();
     }
     echo '<html><head><meta http-equiv="refresh" content="0; URL=' . $url . '"></head><body>Still generating...</body></html>';
     exit;
 }