From 640d3ea6f7459d7d1df55ef784fa3e57de814bf6 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Tue, 30 Apr 2019 15:50:21 -0700 Subject: [PATCH] (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`. --- CRM/Mailing/Page/View.php | 62 ++++++++++++--------------------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/CRM/Mailing/Page/View.php b/CRM/Mailing/Page/View.php index b8282405a3..b06d61cf34 100644 --- a/CRM/Mailing/Page/View.php +++ b/CRM/Mailing/Page/View.php @@ -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, '') === FALSE && strpos($content, '') === FALSE) { - $title = '<head><title>' . $this->_mailing->subject . ''; + $title = '' . $mailing['subject'] . ''; } } 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); -- 2.25.1