CRM-18465 fully resolve contributions before resolving tokens
authoreileenmcnaugton <eileen@fuzion.co.nz>
Sun, 24 Apr 2016 21:46:01 +0000 (09:46 +1200)
committereileenmcnaugton <eileen@fuzion.co.nz>
Sun, 24 Apr 2016 21:46:01 +0000 (09:46 +1200)
CRM/Contribute/Form/Task/PDFLetterCommon.php

index 56059052078d7ff15a5b6b2462d94bb8c2907633..bd87f08561efc4b5e35c437bab74b31d48abb3c1 100644 (file)
@@ -230,6 +230,7 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF
         $task
       );
       $contribution = $contributions[$contributionId] = $contribution[$contributionId];
+
       if ($form->_includesSoftCredits) {
         //@todo find out why this happens & add comments
         list($contactID) = explode('-', $item);
@@ -239,15 +240,7 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF
         $contactID = $contribution['contact_id'];
       }
       if (!isset($contacts[$contactID])) {
-        list($contact) = CRM_Utils_Token::getTokenDetails(array('contact_id' => $contactID),
-          $returnProperties,
-          $skipOnHold,
-          $skipDeceased,
-          NULL,
-          $messageToken,
-          $task
-        );
-        $contacts[$contactID] = $contact[$contactID];
+        $contacts[$contactID] = array();
         $contacts[$contactID]['contact_aggregate'] = 0;
         $contacts[$contactID]['combined'] = $contacts[$contactID]['contribution_ids'] = array();
       }
@@ -265,6 +258,22 @@ class CRM_Contribute_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDF
         $contacts[$contactID]['aggregates'][$groupBy][$groupByID] += $contribution['total_amount'];
       }
     }
+    // Assign the available contributions before calling tokens so hooks parsing smarty can access it.
+    // Note that in core code you can only use smarty here if enable if for the whole site, incl
+    // CiviMail, with a big performance impact.
+    // Hooks allow more nuanced smarty usage here.
+    CRM_Core_Smarty::singleton()->assign('contributions', $contributions);
+    foreach ($contacts as $contactID => $contact) {
+      $tokenResolvedContacts = CRM_Utils_Token::getTokenDetails(array('contact_id' => $contactID),
+        $returnProperties,
+        $skipOnHold,
+        $skipDeceased,
+        NULL,
+        $messageToken,
+        $task
+      );
+      $contacts[$contactID] = array_merge($tokenResolvedContacts[0][$contactID], $contact);
+    }
     return array($contributions, $contacts);
   }