dev/core#2832 Switch membership pdf to use token processor
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 17 Sep 2021 22:41:39 +0000 (10:41 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 17 Sep 2021 22:41:39 +0000 (10:41 +1200)
Replaces final instances of https://lab.civicrm.org/dev/core/-/issues/2832

CRM/Member/Form/Task/PDFLetter.php
CRM/Member/Form/Task/PDFLetterCommon.php
CRM/Utils/Token.php
tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php

index 4b8436ddcd7abb92fda9e6d9bbec15028c0c5e52..80115f9ee012be7627e7934b13ed21fad1550f53 100644 (file)
@@ -15,6 +15,8 @@
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
+use Civi\Api4\Membership;
+
 /**
  * This class provides the functionality to create PDF letter for a group of
  * contacts or a single contact.
@@ -119,20 +121,19 @@ class CRM_Member_Form_Task_PDFLetter extends CRM_Member_Form_Task {
    *
    */
   public function generateHTML($membershipIDs, $messageToken, $html_message): array {
-    $memberships = CRM_Utils_Token::getMembershipTokenDetails($membershipIDs);
+    $memberships = Membership::get(FALSE)
+      ->addWhere('id', 'IN', $membershipIDs)
+      ->addSelect('contact_id')->execute();
     $html = [];
 
-    foreach ($membershipIDs as $membershipID) {
-      $membership = $memberships[$membershipID];
-      // get contact information
-      $contactId = $membership['contact_id'];
-      $tokenHtml = CRM_Utils_Token::replaceEntityTokens('membership', $membership, $html_message, $messageToken);
+    foreach ($memberships as $membership) {
       $html[] = CRM_Core_BAO_MessageTemplate::renderTemplate([
-        'messageTemplate' => ['msg_html' => $tokenHtml],
-        'contactId' => $contactId,
+        'messageTemplate' => ['msg_html' => $html_message],
+        'contactId' => $membership['contact_id'],
+        'schema' => ['contactId', 'membershipId'],
+        'tokenContext' => ['membershipId' => $membership['id']],
         'disableSmarty' => !defined('CIVICRM_MAIL_SMARTY') || !CIVICRM_MAIL_SMARTY,
       ])['html'];
-
     }
     return $html;
   }
index 1d36c5e806439623886b6b1190968f7b392ff008..7fdc8dce859e6f7d8c4298db60133459b6b2ac8c 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use Civi\Api4\Membership;
+
 /**
  * This class provides the common functionality for creating PDF letter for
  * members
@@ -24,7 +26,7 @@ class CRM_Member_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDFLett
   public static function postProcessMembers(&$form, $membershipIDs, $skipOnHold, $skipDeceased, $contactIDs) {
     CRM_Core_Error::deprecatedFunctionWarning('no alternative');
     $formValues = $form->controller->exportValues($form->getName());
-    list($formValues, $categories, $html_message, $messageToken, $returnProperties) = CRM_Contact_Form_Task_PDFLetterCommon::processMessageTemplate($formValues);
+    [$formValues, $categories, $html_message, $messageToken, $returnProperties] = CRM_Contact_Form_Task_PDFLetterCommon::processMessageTemplate($formValues);
 
     $html
       = self::generateHTML(
@@ -64,35 +66,27 @@ class CRM_Member_Form_Task_PDFLetterCommon extends CRM_Contact_Form_Task_PDFLett
    * @param $html_message
    * @param $categories
    *
-   * @deprecated
-   *
    * @return array
+   * @throws \API_Exception
+   * @throws \CRM_Core_Exception
+   * @throws \Civi\API\Exception\UnauthorizedException
+   * @deprecated
    */
   public static function generateHTML($membershipIDs, $returnProperties, $skipOnHold, $skipDeceased, $messageToken, $html_message, $categories) {
     CRM_Core_Error::deprecatedFunctionWarning('no alternative');
-    $memberships = CRM_Utils_Token::getMembershipTokenDetails($membershipIDs);
+    $memberships = Membership::get(FALSE)
+      ->addWhere('id', 'IN', $membershipIDs)
+      ->addSelect('contact_id')->execute();
     $html = [];
 
-    foreach ($membershipIDs as $membershipID) {
-      $membership = $memberships[$membershipID];
-      // get contact information
-      $contactId = $membership['contact_id'];
-      $params = ['contact_id' => $contactId];
-      //getTokenDetails is much like calling the api contact.get function - but - with some minor
-      // special handlings. It precedes the existence of the api
-      list($contacts) = CRM_Utils_Token::getTokenDetails(
-        $params,
-        $returnProperties,
-        $skipOnHold,
-        $skipDeceased,
-        NULL,
-        $messageToken,
-        'CRM_Contribution_Form_Task_PDFLetterCommon'
-      );
-
-      $tokenHtml = CRM_Utils_Token::replaceEntityTokens('membership', $membership, $html_message, $messageToken);
-      $html[] = CRM_Core_BAO_MessageTemplate::renderTemplate(['messageTemplate' => ['msg_html' => $tokenHtml], 'contactId' => $contactId])['html'];
-
+    foreach ($memberships as $membership) {
+      $html[] = CRM_Core_BAO_MessageTemplate::renderTemplate([
+        'messageTemplate' => ['msg_html' => $html_message],
+        'contactId' => $membership['contact_id'],
+        'schema' => ['contactId', 'membershipId'],
+        'tokenContext' => ['membershipId' => $membership['id']],
+        'disableSmarty' => !defined('CIVICRM_MAIL_SMARTY') || !CIVICRM_MAIL_SMARTY,
+      ])['html'];
     }
     return $html;
   }
index c2ec7615ab71486532b9f2b9845504feb8e791ae..37b2c56fa3ffc41f05ce9aa55ea6b7fe69c69d7b 100644 (file)
@@ -1276,6 +1276,8 @@ class CRM_Utils_Token {
    * Get Membership Token Details.
    * @param array $membershipIDs
    *   Array of membership IDS.
+   *
+   * @deprecated
    */
   public static function getMembershipTokenDetails($membershipIDs) {
     $memberships = civicrm_api3('membership', 'get', [
index 2f734737806799b242a36d75aaef300137c97739..33fd6dbb7fb3ae68983c221821c03a189d8b3c60 100644 (file)
@@ -60,8 +60,8 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase {
       $params = array_merge($params,
         [
           'fee' => '100.00',
-          'type' => 'General',
-          'status' => 'New',
+          'membership_type_id:label' => 'General',
+          'status_id:label' => 'New',
         ]
       );
 
@@ -105,8 +105,8 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase {
   public static function getSampleHTML() {
     $tokens = [
       'Test Fee' => 'fee',
-      'Test Type' => 'type',
-      'Test Status' => 'status',
+      'Test Type' => 'membership_type_id:label',
+      'Test Status' => 'status_id:label',
       'Test Join Date' => 'join_date',
       'Test Start Date' => 'start_date',
       'Test End Date' => 'end_date',