(flexmailer#29) civicrm/mailing/view - Generate content via Mailing.preview API
authorTim Otten <totten@civicrm.org>
Tue, 30 Apr 2019 22:50:21 +0000 (15:50 -0700)
committerTim Otten <totten@civicrm.org>
Tue, 30 Apr 2019 22:53:25 +0000 (15:53 -0700)
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

index b8282405a3e51edcf65107a0772d19220ee04145..b06d61cf34e3046f9005998a22e06025d6bd98e5 100644 (file)
  * 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);