CRM-20336: Failed contributions should be set as failed, not left as pending (more...
[civicrm-core.git] / Civi / Token / TokenCompatSubscriber.php
index 152b5b7ae0fcd05ce6fc8b7bffca0a56e5a45a6f..d00acd75092236e8466f963130f1b1304ee6ff02 100644 (file)
@@ -49,9 +49,11 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
     $messageTokens = $e->getTokenProcessor()->getMessageTokens();
 
     foreach ($e->getRows() as $row) {
+      /** @var int $contactId */
+      $contactId = $row->context['contactId'];
       if (empty($row->context['contact'])) {
         $params = array(
-          array('contact_id', '=', $row->context['contactId'], 0, 0),
+          array('contact_id', '=', $contactId, 0, 0),
         );
         list($contact, $_) = \CRM_Contact_BAO_Query::apiQuery($params);
         $contact = reset($contact); //CRM-4524
@@ -59,6 +61,18 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
           // FIXME: Need to differentiate errors which kill the batch vs the individual row.
           throw new TokenException("Failed to generate token data. Invalid contact ID: " . $row->context['contactId']);
         }
+
+        //update value of custom field token
+        if (!empty($messageTokens['contact'])) {
+          foreach ($messageTokens['contact'] as $token) {
+            if (\CRM_Core_BAO_CustomField::getKeyID($token)) {
+              $contact[$token] = civicrm_api3('Contact', 'getvalue', array(
+                'return' => $token,
+                'id' => $contactId,
+              ));
+            }
+          }
+        }
       }
       else {
         $contact = $row->context['contact'];
@@ -70,16 +84,21 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
         $contact = array_merge($contact, $row->context['tmpTokenParams']);
       }
 
+      $contactArray = !is_array($contactId) ? array($contactId => $contact) : $contact;
+
       // Note: This is a small contract change from the past; data should be missing
       // less randomly.
-      //\CRM_Utils_Hook::tokenValues($contact, $row->context['contactId']);
-      \CRM_Utils_Hook::tokenValues($contact,
-        $row->context['contactId'],
+      \CRM_Utils_Hook::tokenValues($contactArray,
+        (array) $contactId,
         empty($row->context['mailingJob']) ? NULL : $row->context['mailingJob']->id,
         $messageTokens,
         $row->context['controller']
       );
 
+      // merge the custom tokens in the $contact array
+      if (!empty($contactArray[$contactId])) {
+        $contact = array_merge($contact, $contactArray[$contactId]);
+      }
       $row->context('contact', $contact);
     }
   }
@@ -93,15 +112,16 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
     $isHtml = ($e->message['format'] == 'text/html');
     $useSmarty = !empty($e->context['smarty']);
 
-    $e->string = \CRM_Utils_Token::replaceDomainTokens($e->string, \CRM_Core_BAO_Domain::getDomain(), $isHtml, $e->message['tokens'], $useSmarty);
+    $domain = \CRM_Core_BAO_Domain::getDomain();
+    $e->string = \CRM_Utils_Token::replaceDomainTokens($e->string, $domain, $isHtml, $e->message['tokens'], $useSmarty);
 
     if (!empty($e->context['contact'])) {
-      $e->string = \CRM_Utils_Token::replaceContactTokens($e->string, $e->context['contact'], $isHtml, $e->message['tokens'], FALSE, $useSmarty);
+      $e->string = \CRM_Utils_Token::replaceContactTokens($e->string, $e->context['contact'], $isHtml, $e->message['tokens'], TRUE, $useSmarty);
 
       // FIXME: This may depend on $contact being merged with hook values.
       $e->string = \CRM_Utils_Token::replaceHookTokens($e->string, $e->context['contact'], $e->context['hookTokenCategories'], $isHtml, $useSmarty);
 
-      \CRM_Utils_Token::replaceGreetingTokens($e->string, NULL, $e->context['contact']['contact_id'], NULL, $useSmarty);
+      \CRM_Utils_Token::replaceGreetingTokens($e->string, $e->context['contact'], $e->context['contact']['contact_id'], NULL, $useSmarty);
     }
 
     if ($useSmarty) {