Add userEnteredText as generic workflow template smarty variable
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 25 Aug 2023 05:41:03 +0000 (17:41 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 1 Dec 2023 21:26:05 +0000 (10:26 +1300)
This makes userEnteredText a property across all WorkflowMessages - with it outputting
the smarty variable user_text

We already had receipt_text on the membership forms - but that is a bit too specific
- user is perhaps a bit impersonal - I did contemplate form_text :

16 files changed:
CRM/Contribute/Form/Task/Invoice.php
CRM/Core/BAO/MessageTemplate.php
CRM/Event/Form/Participant.php
CRM/Member/Form/Membership.php
CRM/Member/Form/MembershipRenewal.php
CRM/Member/WorkflowMessage/MembershipOfflineReceipt.php
Civi/WorkflowMessage/GenericWorkflowMessage.php
api/v3/Contribution.php
api/v3/MessageTemplate.php
tests/phpunit/CRM/Event/Form/ParticipantTest.php
tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php
xml/templates/message_templates/contribution_invoice_receipt_html.tpl
xml/templates/message_templates/event_offline_receipt_html.tpl
xml/templates/message_templates/membership_offline_receipt_html.tpl
xml/templates/message_templates/membership_offline_receipt_text.tpl
xml/templates/message_templates/membership_online_receipt_html.tpl

index a1e69e04ed2ada6fc639dbddf2c18b230d2fbf41..bf148d7e089133c4fa88047b4a3c0fc2e342aef4 100644 (file)
@@ -427,6 +427,9 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
         'tplParams' => $tplParams,
         'PDFFilename' => $pdfFileName,
         'tokenContext' => ['contributionId' => $contribution->id, 'contactId' => $contribution->contact_id],
+        'modelProps' => [
+          'userEnteredText' => $params['email_comment'] ?? NULL,
+        ],
       ];
 
       // from email address
@@ -495,10 +498,10 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
         $fileName = self::putFile($html, $pdfFileName, $pdfFormat);
         self::addActivities($subject, $contribution->contact_id, $fileName, $params, $contribution->id);
       }
-      elseif ($component == 'event') {
+      elseif ($component === 'event') {
         $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contribution->contact_id);
 
-        $sendTemplateParams['tplParams'] = array_merge($tplParams, ['email_comment' => $params['email_comment']]);
+        $sendTemplateParams['tplParams'] = $tplParams;
         $sendTemplateParams['from'] = $fromEmailAddress;
         $sendTemplateParams['toEmail'] = $email;
         $sendTemplateParams['cc'] = $values['cc_confirm'] ?? NULL;
index 1d53ed7a80a4cb9795844c77d0191907ca46dcb9..e9342273af0b46e945b9a399f12a9d5a216209b4 100644 (file)
@@ -448,7 +448,14 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate implemen
           $params['attachments'] = [];
         }
         $params['attachments'][] = CRM_Utils_Mail::appendPDF($params['PDFFilename'], $params['html'], $mailContent['format']);
+        // This specifically allows the invoice code to attach an invoice & have
+        // a different message body. It will be removed & replaced with something
+        // saner so avoid trying to leverage this. There are no universe usages outside
+        // the core invoice task as of Dec 2023
         if (isset($params['tplParams']['email_comment'])) {
+          if ($params['workflow'] !== 'contribution_invoice_receipt') {
+            CRM_Core_Error::deprecatedWarning('unsupported parameter email_comment used');
+          }
           $params['html'] = $params['tplParams']['email_comment'];
           $params['text'] = strip_tags($params['tplParams']['email_comment']);
         }
index fe384b1d26fc442e6737a306a86fb2986484c92d..11a6f833a2919ca96d73eefa439312d132b43d04 100644 (file)
@@ -1837,6 +1837,7 @@ INNER JOIN civicrm_price_field_value value ON ( value.id = lineItem.price_field_
         'PDFFilename' => ts('confirmation') . '.pdf',
         'modelProps' => [
           'participantID' => $participantID,
+          'userEnteredText' => $this->getSubmittedValue('receipt_text'),
           'eventID' => $params['event_id'],
           'contributionID' => $contributionID,
         ],
index 177fb1c835dcf47ae9cf66270fc8d418ae68ea98..4c7e4b658dd8ca14f5bbd503ae5adf3992d876a0 100644 (file)
@@ -909,7 +909,7 @@ DESC limit 1");
         'isEmailPdf' => Civi::settings()->get('invoice_is_email_pdf'),
         'isTest' => (bool) ($this->_action & CRM_Core_Action::PREVIEW),
         'modelProps' => [
-          'receiptText' => $this->getSubmittedValue('receipt_text'),
+          'userEnteredText' => $this->getSubmittedValue('receipt_text'),
           'contributionID' => $formValues['contribution_id'],
           'contactID' => $this->_receiptContactId,
           'membershipID' => $this->getMembershipID(),
index 9cd4df5c7b74ca3fb597ce78f5c6d46c5de54668..ceb123cfea7edba155ebf48b6f63f3c48ef6dce7 100644 (file)
@@ -700,6 +700,8 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form {
       }
     }
 
+    // This is being replaced by userEnteredText.
+    $this->assign('receipt_text', $this->getSubmittedValue('receipt_text'));
     [$this->isMailSent] = CRM_Core_BAO_MessageTemplate::sendTemplate(
       [
         'workflow' => 'membership_offline_receipt',
@@ -710,7 +712,7 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form {
         'PDFFilename' => ts('receipt') . '.pdf',
         'isEmailPdf' => Civi::settings()->get('invoice_is_email_pdf'),
         'modelProps' => [
-          'receiptText' => $this->getSubmittedValue('receipt_text'),
+          'userEnteredText' => $this->getSubmittedValue('receipt_text'),
           'contactID' => $this->_receiptContactId,
           'contributionID' => $this->getContributionID(),
           'membershipID' => $this->getMembershipID(),
index 9522240d5c4683222df128c8b37f09c88671d457..93648fa32691f56bb24cdace5d3d64ca7ddf07bd 100644 (file)
@@ -25,13 +25,4 @@ class CRM_Member_WorkflowMessage_MembershipOfflineReceipt extends GenericWorkflo
   use CRM_Contribute_WorkflowMessage_ContributionTrait;
   public const WORKFLOW = 'membership_offline_receipt';
 
-  /**
-   * Additional text to include in the receipt.
-   *
-   * @var string
-   *
-   * @scope tplParams as receipt_text
-   */
-  protected $receiptText;
-
 }
index b2e7148e1959c8682568d5c39c506b5a6dbb8add..1dff695b1bc3d4c4ee4171a99062bee06ae26e17 100644 (file)
@@ -26,6 +26,8 @@ use Civi\WorkflowMessage\Traits\TemplateTrait;
  * @method int|null getContactID()
  * @method $this setContact(array|null $contact)
  * @method array|null getContact()
+ * @method $this setUserEnteredText(string $text)
+ * @method $this setUserEnteredHTML(string $html)
  *
  * @support template-only
  * GenericWorkflowMessage should aim for "full" support, but it's prudent to keep
@@ -117,4 +119,30 @@ class GenericWorkflowMessage implements WorkflowMessageInterface {
     $export['smartyTokenAlias']['taxTerm'] = 'domain.tax_term';
   }
 
+  /**
+   * Additional text to include in the receipt.
+   *
+   * @var string
+   *
+   * @scope tplParams as userTextPlain
+   */
+  protected $userEnteredText;
+
+  /**
+   * Additional html to include in the receipt.
+   *
+   * @var string
+   *
+   * @scope tplParams as userText
+   */
+  protected $userEnteredHTML;
+
+  public function getUserEnteredText(): ?string {
+    return $this->userEnteredText ?: ($this->userEnteredHTML ? \CRM_Utils_String::htmlToText($this->userEnteredHTML) : NULL);
+  }
+
+  public function getUserEnteredHTML(): ?string {
+    return \CRM_Utils_String::purifyHTML($this->userEnteredHTML ?: ($this->userEnteredText ? nl2br($this->userEnteredText) : ''));
+  }
+
 }
index 8f95f781fe9be041d9f982a0d745e7ba9f69f9e9..14cf3e72aed5485dfda2975ff47a630992f89bda 100644 (file)
@@ -401,8 +401,15 @@ function civicrm_api3_contribution_sendconfirmation($params) {
     'receipt_text',
     'pay_later_receipt',
     'payment_processor_id',
+    'model',
   ];
   $input = array_intersect_key($params, array_flip($allowedParams));
+  if (!isset($input['model'])) {
+    $input['model'] = [
+      // Pass through legacy receipt_text.
+      'userEnteredText' => $input['tplParams']['receipt_text'] ?? NULL,
+    ];
+  }
   CRM_Contribute_BAO_Contribution::sendMail($input, [], $params['id']);
   return [];
 }
index 08c281f295a614539db2f370f04c002c24224c5b..d2683efbc13a5f4335356bc5f2edb219cedcf130 100644 (file)
@@ -107,6 +107,12 @@ function civicrm_api3_message_template_send($params) {
       unset($params[$field]);
     }
   }
+  if (!isset($params['model'])) {
+    $params['model'] = [
+      // Pass through legacy receipt_text.
+      'userEnteredText' => $params['tplParams']['receipt_text'] ?? NULL,
+    ];
+  }
   if (empty($params['messageTemplateID'])) {
     if (empty($params['workflow'])) {
       // Can't use civicrm_api3_verify_mandatory for this because it would give the wrong field names
index 0ee386718472d48da7f3686f99798068398e3ed9..cf585c1b5ef891c16b6be9055b71eeeee8db7d3d 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+declare(strict_types = 1);
 use Civi\Api4\Address;
 use Civi\Api4\Event;
 use Civi\Api4\LineItem;
@@ -306,8 +307,7 @@ United States<br />',
       'is_default' => 1,
     ]);
     $oldMsg = $result['values'][0]['msg_html'];
-    $pos = strpos($oldMsg, 'Please print this confirmation');
-    $newMsg = substr_replace($oldMsg, '<p>Test event type - {event.event_type_id}</p>', $pos, 0);
+    $newMsg = substr_replace($oldMsg, '<p>Test event type - {event.event_type_id}</p>', 0, 0);
     $this->callAPISuccess('MessageTemplate', 'create', [
       'id' => $result['id'],
       'msg_html' => $newMsg,
@@ -393,6 +393,7 @@ London,',
       $event = $this->eventCreateUnpaid($eventParams);
     }
     $submittedValues['event_id'] = $event['id'];
+    $submittedValues['receipt_text'] = 'Contact the Development Department if you need to make any changes to your registration.';
     return $this->getTestForm('CRM_Event_Form_Participant', $submittedValues, ['cid' => $submittedValues['contact_id']])->processForm(FormWrapper::BUILT);
   }
 
index 198266bc1829b94e2d09c591e1075f55611bc78c..c2903a7ca55788d25b95b2a69467a9f5758ea971 100644 (file)
@@ -298,7 +298,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
     $form->_mode = 'test';
     $form->_contactID = $this->_individualId;
 
-    $form->testSubmit();
+    $form->postProcess();
     $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
     $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', ['contact_id' => $this->_individualId]);
     $this->assertEquals(1, $contributionRecur['is_email_receipt']);
@@ -631,6 +631,7 @@ class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
     $form->_mode = $mode;
     $form->setEntityId($this->_membershipID);
     $form->preProcess();
+    $form->buildForm();
     return $form;
   }
 
index e32cfcaf38f988db4d71787b8da2dd5935915816..213665d7ebb72e46d0a656b6e7c62fa429b05ea9 100644 (file)
@@ -14,9 +14,9 @@
       </table>
     {/if}
     <table style="font-family: Arial, Verdana, sans-serif;" width="100%" height="100" border="0" cellpadding="5" cellspacing="0">
-      {if $email_comment}
+      {if $userText}
         <tr>
-          <td><font size="1" colspan="3">{$email_comment}</font></td>
+          <td><font size="1" colspan="3">{$userText}</font></td>
         </tr>
       {/if}
       <tr>
index b72871148258dffadd544035cb1ce48f1dae0a71..7b14fdd5b7fc7594be42714a636b03616f71d479 100644 (file)
@@ -25,8 +25,8 @@
    <td>
     {assign var="greeting" value="{contact.email_greeting_display}"}{if $greeting}<p>{$greeting},</p>{/if}
 
-    {if !empty($event.confirm_email_text) AND (empty($isOnWaitlist) AND empty($isRequireApproval))}
-     <p>{$event.confirm_email_text}</p>
+    {if $userText}
+     <p>{$userText}</p>
     {/if}
 
     {if !empty($isOnWaitlist)}
index 49987091cef69b102dca1666116f89d6bfee1d3b..b3da8e213a85fd915ae91836c05a292996732195 100644 (file)
@@ -23,8 +23,8 @@
     <tr>
       <td>
         {assign var="greeting" value="{contact.email_greeting_display}"}{if $greeting}<p>{$greeting},</p>{/if}
-        {if $receipt_text}
-          <p>{$receipt_text|htmlize}</p>
+        {if $userText}
+          <p>{$userText}</p>
         {else}
           <p>{ts}Thank you for this contribution.{/ts}</p>
         {/if}
index 13134f8b1c51e2b427401802e7378e6a5568f435..e5c60081e308cb99d5ea641819bcb99658d1e990 100644 (file)
@@ -1,7 +1,7 @@
 {assign var="greeting" value="{contact.email_greeting_display}"}{if $greeting}{$greeting},{/if}
 
-{if $receipt_text}
-{$receipt_text}
+{if $userTextPlain}
+{$userTextPlain}
 {else}{ts}Thank you for this contribution.{/ts}{/if}
 
 {if !$isShowLineItems}
index 9375ed21efc5657333f3bee18618a03653b14f1e..b9eaec87ac5efec6e5456061724a88136bdf7b7a 100644 (file)
@@ -21,8 +21,8 @@
   <tr>
    <td>
      {assign var="greeting" value="{contact.email_greeting_display}"}{if $greeting}<p>{$greeting},</p>{/if}
-    {if !empty($receipt_text)}
-     <p>{$receipt_text|htmlize}</p>
+    {if $userText}
+     <p>{$userText}</p>
     {/if}
     {if {contribution.balance_amount|boolean} && {contribution.is_pay_later|boolean}}
       <p>{contribution.pay_later_receipt}</p>