Commit 640d3ea6 authored by totten's avatar totten

(flexmailer#29) civicrm/mailing/view - Generate content via Mailing.preview API

A root cause of flexmailer#29 is that the flexmailer has to override
multiple parts of CiviMail.  Case in point: it overrides the
`civicrm/mailing/view` and forces it to generate content via
`Mailing.preview` API.  This is unfortunate because flexmailer's variant is
missing other features (regarding permissioning and contact IDs).

This revision makes it unnecessary for flexmailer to override
`civicrm/mailing/view`.
parent 1246fd4c
......@@ -35,6 +35,12 @@
* A page for mailing preview.
*/
class CRM_Mailing_Page_View extends CRM_Core_Page {
/**
* @var Signal to Flexmailer that this version of the class is usable.
*/
const USES_MAILING_PREVIEW_API = 1;
protected $_mailingID;
protected $_mailing;
protected $_contactID;
......@@ -132,59 +138,27 @@ class CRM_Mailing_Page_View extends CRM_Core_Page {
return NULL;
}
CRM_Mailing_BAO_Mailing::tokenReplace($this->_mailing);
// get and format attachments
$attachments = CRM_Core_BAO_File::getEntityFile('civicrm_mailing',
$this->_mailing->id
);
// get contact detail and compose if contact id exists
$returnProperties = $this->_mailing->getReturnProperties();
if (isset($this->_contactID)) {
// get details of contact with token value including Custom Field Token Values.CRM-3734
$params = ['contact_id' => $this->_contactID];
$details = CRM_Utils_Token::getTokenDetails($params,
$returnProperties,
FALSE, TRUE, NULL,
$this->_mailing->getFlattenedTokens(),
get_class($this)
);
$details = $details[0][$this->_contactID];
$contactId = $this->_contactID;
}
else {
// get tokens that are not contact specific resolved
$params = ['contact_id' => 0];
$details = CRM_Utils_Token::getAnonymousTokenDetails($params,
$returnProperties,
TRUE, TRUE, NULL,
$this->_mailing->getFlattenedTokens(),
get_class($this)
);
$details = CRM_Utils_Array::value(0, $details[0]);
$contactId = 0;
}
$mime = $this->_mailing->compose(NULL, NULL, NULL, $contactId,
$this->_mailing->from_email,
$this->_mailing->from_email,
TRUE, $details, $attachments
);
$contactId = isset($this->_contactID) ? $this->_contactID : 0;
$result = civicrm_api3('Mailing', 'preview', [
'id' => $this->_mailingID,
'contact_id' => $contactId,
]);
$mailing = \CRM_Utils_Array::value('values', $result);
$title = NULL;
if (isset($this->_mailing->body_html) && empty($_GET['text'])) {
if (isset($mailing['body_html']) && empty($_GET['text'])) {
$header = 'text/html; charset=utf-8';
$content = $mime->getHTMLBody();
$content = $mailing['body_html'];
if (strpos($content, '<head>') === FALSE && strpos($content, '<title>') === FALSE) {
$title = '<head><title>' . $this->_mailing->subject . '</title></head>';
$title = '<head><title>' . $mailing['subject'] . '</title></head>';
}
}
else {
$header = 'text/plain; charset=utf-8';
$content = $mime->getTXTBody();
$content = $mailing['body_text'];
}
CRM_Utils_System::setTitle($this->_mailing->subject);
CRM_Utils_System::setTitle($mailing['subject']);
if (CRM_Utils_Array::value('snippet', $_GET) === 'json') {
CRM_Core_Page_AJAX::returnJsonResponse($content);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment