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 $issue;
protected $build;
protected $newsletter;
protected $subscriber;
protected $key = 'test';
protected $cache;
public function __construct(ContentEntityInterface $issue, SubscriberInterface $subscriber, MailCacheInterface $mail_cache) {
$this->subscriber = $subscriber;
$this->issue = $issue;
if ($this->issue
->hasTranslation($this
->getLanguage())) {
$this->issue = $this->issue
->getTranslation($this
->getLanguage());
}
$this->cache = $mail_cache;
$this->newsletter = $issue->simplenews_issue->entity;
}
public function getNewsletter() {
return $this->newsletter;
}
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()) . '>';
$headers['Precedence'] = 'bulk';
return $headers;
}
public function getTokenContext() {
return [
'newsletter' => $this
->getNewsletter(),
'simplenews_subscriber' => $this
->getSubscriber(),
$this
->getIssue()
->getEntityTypeId() => $this
->getIssue(),
];
}
public function setKey($key) {
$this->key = $key;
}
public function getKey() {
return $this->key;
}
public function getFromFormatted() {
if (mb_substr(PHP_OS, 0, 3) == 'WIN') {
return $this
->getFromAddress();
}
return '"' . addslashes(Unicode::mimeHeaderEncode($this
->getNewsletter()->from_name)) . '" <' . $this
->getFromAddress() . '>';
}
public function getFromAddress() {
return $this
->getNewsletter()->from_address;
}
public function getRecipient() {
return $this
->getSubscriber()
->getMail();
}
public function getFormat() {
return $this
->getNewsletter()->format;
}
public function getLanguage() {
return $this
->getSubscriber()
->getLangcode();
}
public function getIssue() {
return $this->issue;
}
public function getSubject() {
$langcode = $this
->getLanguage();
$subject = simplenews_token_replace_subject($this
->getNewsletter()->subject, $this
->getTokenContext(), [
'langcode' => $langcode,
]);
$subject = str_replace([
"\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
->getIssue()
->getEntityTypeId())
->view($this
->getIssue(), 'email_' . $format, $this
->getLanguage());
$build['#entity_type'] = $this
->getIssue()
->getEntityTypeId();
unset($build['#cache']);
if ($build['#theme'] == 'entity' || $build['#theme'] == $this
->getIssue()
->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) + [
'#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(), [
'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;
}
public function getAttachments() {
if ($cache = $this->cache
->get($this, 'data', 'attachments')) {
return $cache;
}
$attachments = [];
$build = $this
->build();
$fids = [];
foreach ($this
->getIssue()
->getFieldDefinitions() as $field_name => $field_definition) {
if ($field_definition
->getType() == 'file' && isset($build[$field_name])) {
if ($items = $this
->getIssue()
->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;
}
}