Unverified Commit 7f1ac1a4 authored by Seamus Lee's avatar Seamus Lee Committed by GitHub

Merge pull request #14163 from seamuslee001/flexmailer_26

(flexmailer#29) civicrm/mailing/view - Use Mailing.preview API. Fix compatibility with Flexmailer.
parents f77a81cb 3d8aa9b2
......@@ -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);
......
......@@ -573,18 +573,26 @@ function civicrm_api3_mailing_preview($params) {
$returnProperties = $mailing->getReturnProperties();
$contactID = CRM_Utils_Array::value('contact_id', $params);
if (!$contactID) {
$contactID = $session->get('userID');
// If we still don't have a userID in a session because we are annon then set contactID to be 0
$contactID = empty($session->get('userID')) ? 0 : $session->get('userID');
}
$mailingParams = ['contact_id' => $contactID];
$details = CRM_Utils_Token::getTokenDetails($mailingParams, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens());
if (!$contactID) {
$details = CRM_Utils_Token::getAnonymousTokenDetails($mailingParams, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens());
$details = CRM_Utils_Array::value(0, $details[0]);
}
else {
$details = CRM_Utils_Token::getTokenDetails($mailingParams, $returnProperties, TRUE, TRUE, NULL, $mailing->getFlattenedTokens());
$details = $details[0][$contactID];
}
$mime = $mailing->compose(NULL, NULL, NULL, $session->get('userID'), $fromEmail, $fromEmail,
TRUE, $details[0][$contactID], $attachments
$mime = $mailing->compose(NULL, NULL, NULL, $contactID, $fromEmail, $fromEmail,
TRUE, $details, $attachments
);
return civicrm_api3_create_success([
'id' => $params['id'],
'id' => $mailingID,
'contact_id' => $contactID,
'subject' => $mime->headers()['Subject'],
'body_html' => $mime->getHTMLBody(),
......
......@@ -276,6 +276,23 @@ class api_v3_MailingTest extends CiviUnitTestCase {
$this->assertContains("<p>This is $displayName.</p>", $previewResult['values']['body_html']);
}
public function testMailerPreviewUnknownContact() {
$params = $this->_params;
$params['api.Mailing.preview'] = array(
'id' => '$value.id',
);
$result = $this->callAPISuccess('mailing', 'create', $params);
// NOTE: It's highly debatable what's best to do with contact-tokens for an
// unknown-contact. However, changes should be purposeful, so we'll test
// for the current behavior (i.e. returning blanks).
$previewResult = $result['values'][$result['id']]['api.Mailing.preview'];
$this->assertEquals("Hello ", $previewResult['values']['subject']);
$this->assertContains("This is .", $previewResult['values']['body_text']);
$this->assertContains("<p>This is .</p>", $previewResult['values']['body_html']);
}
public function testMailerPreviewRecipients() {
// BEGIN SAMPLE DATA
$groupIDs['inc'] = $this->groupCreate(array('name' => 'Example include group', 'title' => 'Example include group'));
......
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