Merge pull request #14704 from pradpnayak/REF-1
[civicrm-core.git] / api / v3 / Mailing.php
index 38ebc4161edf3ad51bd28d0f875b6257d135622b..a71e5645d96f8ba969693eeecd8be2a227d24634 100644 (file)
@@ -38,7 +38,7 @@
  * @param array $params
  *
  * @return array
- *    API Success Array
+ *   API Success Array
  * @throws \API_Exception
  * @throws \Civi\API\Exception\UnauthorizedException
  */
@@ -440,7 +440,8 @@ function _civicrm_api3_mailing_event_reply_spec(&$params) {
   $params['hash']['api.required'] = 1;
   $params['hash']['title'] = 'Hash';
   $params['replyTo']['api.required'] = 0;
-  $params['replyTo']['title'] = 'Reply To';//doesn't really explain adequately
+  //doesn't really explain adequately
+  $params['replyTo']['title'] = 'Reply To';
 }
 
 /**
@@ -572,20 +573,28 @@ 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'],
+    'subject' => CRM_Utils_Array::value('Subject', $mime->headers(), ''),
     'body_html' => $mime->getHTMLBody(),
     'body_text' => $mime->getTXTBody(),
   ]);
@@ -630,22 +639,24 @@ function civicrm_api3_mailing_send_test($params) {
   $testEmailParams['is_test'] = 1;
   $testEmailParams['status'] = 'Scheduled';
   $testEmailParams['scheduled_date'] = CRM_Utils_Date::processDate(date('Y-m-d'), date('H:i:s'));
+  $testEmailParams['is_calling_function_updated_to_reflect_deprecation'] = TRUE;
   $job = civicrm_api3('MailingJob', 'create', $testEmailParams);
+  CRM_Mailing_BAO_Mailing::getRecipients($testEmailParams['mailing_id']);
   $testEmailParams['job_id'] = $job['id'];
   $testEmailParams['emails'] = array_key_exists('test_email', $testEmailParams) ? explode(',', strtolower($testEmailParams['test_email'])) : NULL;
   if (!empty($params['test_email'])) {
     $query = CRM_Utils_SQL_Select::from('civicrm_email e')
-        ->select(['e.id', 'e.contact_id', 'e.email'])
-        ->join('c', 'INNER JOIN civicrm_contact c ON e.contact_id = c.id')
-        ->where('e.email IN (@emails)', ['@emails' => $testEmailParams['emails']])
-        ->where('e.on_hold = 0')
-        ->where('c.is_opt_out = 0')
-        ->where('c.do_not_email = 0')
-        ->where('c.is_deceased = 0')
-        ->where('c.is_deleted = 0')
-        ->groupBy('e.id')
-        ->orderBy(['e.is_bulkmail DESC', 'e.is_primary DESC'])
-        ->toSQL();
+      ->select(['e.id', 'e.contact_id', 'e.email'])
+      ->join('c', 'INNER JOIN civicrm_contact c ON e.contact_id = c.id')
+      ->where('e.email IN (@emails)', ['@emails' => $testEmailParams['emails']])
+      ->where('e.on_hold = 0')
+      ->where('c.is_opt_out = 0')
+      ->where('c.do_not_email = 0')
+      ->where('c.is_deceased = 0')
+      ->where('c.is_deleted = 0')
+      ->groupBy('e.id')
+      ->orderBy(['e.is_bulkmail DESC', 'e.is_primary DESC'])
+      ->toSQL();
     $dao = CRM_Core_DAO::executeQuery($query);
     $emailDetail = [];
     // fetch contact_id and email id for all existing emails