View source
<?php
namespace Drupal\simplenews\Mail;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Render\Markup;
use Drupal\file\Entity\File;
use Drupal\simplenews\SubscriberInterface;
use Drupal\user\Entity\User;
class MailEntity implements MailInterface {
use DependencySerializationTrait;
protected $entity;
protected $build;
protected $newsletter;
protected $subscriber;
protected $key = 'test';
protected $cache;
public function __construct(ContentEntityInterface $entity, SubscriberInterface $subscriber, MailCacheInterface $mail_cache) {
$this
->setSubscriber($subscriber);
$this
->setEntity($entity);
$this->cache = $mail_cache;
$this->newsletter = $entity->simplenews_issue->entity;
}
public function setEntity(ContentEntityInterface $entity) {
$this->entity = $entity;
if ($this->entity
->hasTranslation($this
->getLanguage())) {
$this->entity = $this->entity
->getTranslation($this
->getLanguage());
}
}
public function getNewsletter() {
return $this->newsletter;
}
public function setSubscriber(SubscriberInterface $subscriber) {
$this->subscriber = $subscriber;
}
public function getSubscriber() {
return $this->subscriber;
}
public function getHeaders(array $headers) {
if ($this->newsletter->receipt) {
$headers['Disposition-Notification-To'] = $this
->getFromAddress();
$headers['X-Confirm-Reading-To'] = $this
->getFromAddress();
}
switch ($this->newsletter->priority) {
case SIMPLENEWS_PRIORITY_HIGHEST:
$headers['Priority'] = 'High';
$headers['X-Priority'] = '1';
$headers['X-MSMail-Priority'] = 'Highest';
break;
case SIMPLENEWS_PRIORITY_HIGH:
$headers['Priority'] = 'urgent';
$headers['X-Priority'] = '2';
$headers['X-MSMail-Priority'] = 'High';
break;
case SIMPLENEWS_PRIORITY_NORMAL:
$headers['Priority'] = 'normal';
$headers['X-Priority'] = '3';
$headers['X-MSMail-Priority'] = 'Normal';
break;
case SIMPLENEWS_PRIORITY_LOW:
$headers['Priority'] = 'non-urgent';
$headers['X-Priority'] = '4';
$headers['X-MSMail-Priority'] = 'Low';
break;
case SIMPLENEWS_PRIORITY_LOWEST:
$headers['Priority'] = 'non-urgent';
$headers['X-Priority'] = '5';
$headers['X-MSMail-Priority'] = 'Lowest';
break;
}
$headers['From'] = $this
->getFromFormatted();
$headers['List-Unsubscribe'] = '<' . \Drupal::token()
->replace('[simplenews-subscriber:unsubscribe-url]', $this
->getTokenContext(), array(
'sanitize' => FALSE,
)) . '>';
$headers['Precedence'] = 'bulk';
return $headers;
}
function getTokenContext() {
return array(
'newsletter' => $this
->getNewsletter(),
'simplenews_subscriber' => $this
->getSubscriber(),
$this
->getEntity()
->getEntityTypeId() => $this
->getEntity(),
);
}
function setKey($key) {
$this->key = $key;
}
function getKey() {
return $this->key;
}
function getFromFormatted() {
if (mb_substr(PHP_OS, 0, 3) == 'WIN') {
return $this
->getFromAddress();
}
return '"' . addslashes(Unicode::mimeHeaderEncode($this
->getNewsletter()->from_name)) . '" <' . $this
->getFromAddress() . '>';
}
function getFromAddress() {
return $this
->getNewsletter()->from_address;
}
function getRecipient() {
return $this
->getSubscriber()
->getMail();
}
function getFormat() {
return $this
->getNewsletter()->format;
}
function getLanguage() {
return $this
->getSubscriber()
->getLangcode();
}
function getEntity() {
return $this->entity;
}
function getSubject() {
$langcode = $this
->getLanguage();
$subject = \Drupal::token()
->replace($this
->getNewsletter()->subject, $this
->getTokenContext(), array(
'sanitize' => FALSE,
'langcode' => $langcode,
));
$subject = str_replace(array(
"\r",
"\n",
), '', $subject);
return $subject;
}
protected function setContext() {
if ($this->uid = $this
->getSubscriber()
->getUserId()) {
\Drupal::service('account_switcher')
->switchTo(User::load($this->uid));
}
}
protected function resetContext() {
if ($this->uid) {
\Drupal::service('account_switcher')
->switchBack();
}
if (!empty($this->original_language)) {
$GLOBALS['language'] = $this->original_language;
$GLOBALS['language_url'] = $this->original_language;
if (\Drupal::moduleHandler()
->moduleExists('i18n_select')) {
$GLOBALS['language_content'] = $this->original_language;
}
}
}
protected function build($format = NULL) {
if (empty($format)) {
$format = $this
->getFormat();
}
if (!empty($this->build[$format])) {
return $this->build[$format];
}
$build = \Drupal::entityTypeManager()
->getViewBuilder($this
->getEntity()
->getEntityTypeId())
->view($this
->getEntity(), 'email_' . $format, $this
->getLanguage());
$build['#entity_type'] = $this
->getEntity()
->getEntityTypeId();
unset($build['#cache']);
if ($build['#theme'] == 'entity' || $build['#theme'] == $this
->getEntity()
->getEntityTypeId()) {
unset($build['#theme']);
}
$this->build[$format] = $build;
return $this->build[$format];
}
protected function buildBody($format = NULL) {
if (empty($format)) {
$format = $this
->getFormat();
}
if ($cache = $this->cache
->get($this, 'build', 'body:' . $format)) {
return $cache;
}
$body = $this
->build($format) + array(
'#theme' => 'simplenews_newsletter_body',
'#newsletter' => $this
->getNewsletter(),
'#language' => $this
->getLanguage(),
'#simplenews_subscriber' => $this
->getSubscriber(),
'#key' => $this
->getKey(),
'#format' => $format,
);
$markup = \Drupal::service('renderer')
->renderPlain($body);
$this->cache
->set($this, 'build', 'body:' . $format, $markup);
return $markup;
}
public function getBody() {
return $this
->getBodyWithFormat($this
->getFormat());
}
public function getPlainBody() {
return $this
->getBodyWithFormat('plain');
}
protected function getBodyWithFormat($format) {
$this
->setContext();
if ($cache = $this->cache
->get($this, 'final', 'body:' . $format)) {
return $cache;
}
$body = $this
->buildBody($format);
$body = \Drupal::token()
->replace($body, $this
->getTokenContext(), array(
'langcode' => $this
->getLanguage(),
));
if ($format == 'plain') {
$body = MailFormatHelper::htmlToText($body, $this
->getNewsletter()->hyperlinks);
}
else {
$body = Markup::create($body);
}
$this->cache
->set($this, 'final', 'body:' . $format, $body);
$this
->resetContext();
return $body;
}
function getAttachments() {
if ($cache = $this->cache
->get($this, 'data', 'attachments')) {
return $cache;
}
$attachments = array();
$build = $this
->build();
$fids = array();
foreach ($this
->getEntity()
->getFieldDefinitions() as $field_name => $field_definition) {
if ($field_definition
->getType() == 'file' && isset($build[$field_name])) {
if ($items = $this
->getEntity()
->get($field_name)) {
foreach ($items as $item) {
$fids[] = $item->target_id;
}
}
}
}
if (!empty($fids)) {
$attachments = File::loadMultiple($fids);
}
$this->cache
->set($this, 'data', 'attachments', $attachments);
return $attachments;
}
}