Skip to content

Commit

Permalink
Add userEnteredText as generic workflow template smarty variable
Browse files Browse the repository at this point in the history
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 :
  • Loading branch information
eileenmcnaughton committed Oct 10, 2023
1 parent 1f76d7c commit b096fb8
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 26 deletions.
6 changes: 3 additions & 3 deletions CRM/Contribute/Form/Task/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public static function printPDF($contribIDs, &$params, $contactIds) {
elseif ($component === 'contribute') {
$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_receipt'] ?? NULL;
Expand All @@ -504,10 +504,10 @@ public static function printPDF($contribIDs, &$params, $contactIds) {
$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;
Expand Down
1 change: 1 addition & 0 deletions CRM/Event/Form/Participant.php
Original file line number Diff line number Diff line change
Expand Up @@ -2105,6 +2105,7 @@ protected function sendReceipts($params, array $participants, $lineItem, $additi
'PDFFilename' => ts('confirmation') . '.pdf',
'modelProps' => [
'participantID' => $participantID,
'userEnteredText' => $this->getSubmittedValue('receipt_text'),
'eventID' => $params['event_id'],
'contributionID' => $contributionID,
],
Expand Down
4 changes: 3 additions & 1 deletion CRM/Member/Form/MembershipRenewal.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,8 @@ protected function sendReceipt($membership) {
}
}

// This is being replaced by userEnteredText.
$this->assign('receipt_text', $this->getSubmittedValue('receipt_text'));
list($this->isMailSent) = CRM_Core_BAO_MessageTemplate::sendTemplate(
[
'workflow' => 'membership_offline_receipt',
Expand All @@ -701,7 +703,7 @@ protected function sendReceipt($membership) {
'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(),
Expand Down
9 changes: 0 additions & 9 deletions CRM/Member/WorkflowMessage/MembershipOfflineReceipt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
28 changes: 28 additions & 0 deletions Civi/WorkflowMessage/GenericWorkflowMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* @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
Expand Down Expand Up @@ -117,4 +119,30 @@ protected function exportExtraTokenContext(array &$export): void {
$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) : ''));
}

}
7 changes: 7 additions & 0 deletions api/v3/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
}
Expand Down
6 changes: 6 additions & 0 deletions api/v3/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/CRM/Event/Form/ParticipantTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types = 1);
use Civi\Api4\Address;
use Civi\Api4\Event;
use Civi\Api4\LineItem;
Expand Down Expand Up @@ -389,6 +390,7 @@ protected function getForm(array $eventParams = [], array $submittedValues = [],
$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);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/CRM/Member/Form/MembershipRenewalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function testSubmitRecur(): void {
$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']);
Expand Down Expand Up @@ -631,6 +631,7 @@ protected function getForm($formValues = [], $mode = 'test'): CRM_Member_Form_Me
$form->_mode = $mode;
$form->setEntityId($this->_membershipID);
$form->preProcess();
$form->buildForm();
return $form;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Original file line number Diff line number Diff line change
@@ -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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 $is_pay_later}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{assign var="greeting" value="{contact.email_greeting_display}"}{if $greeting}{$greeting},{/if}
{if !empty($receipt_text)}
{$receipt_text}
{if $userTextPlain}
{$userTextPlain}
{/if}
{if $is_pay_later}

Expand Down

0 comments on commit b096fb8

Please sign in to comment.