Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add userEnteredText as generic workflow template smarty variable #27162

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions CRM/Contribute/Form/Task/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ public static function printPDF($contribIDs, &$params, $contactIds) {
'tplParams' => $tplParams,
'PDFFilename' => $pdfFileName,
'tokenContext' => ['contributionId' => $contribution->id, 'contactId' => $contribution->contact_id],
'modelProps' => [
'userEnteredText' => $params['email_comment'] ?? NULL,
],
];

// from email address
Expand Down Expand Up @@ -495,10 +498,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
7 changes: 7 additions & 0 deletions CRM/Core/BAO/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,14 @@ public static function sendTemplate(array $params): array {
$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']);
}
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 @@ -1837,6 +1837,7 @@ protected function sendReceipts($params, array $participants): array {
'PDFFilename' => ts('confirmation') . '.pdf',
'modelProps' => [
'participantID' => $participantID,
'userEnteredText' => $this->getSubmittedValue('receipt_text'),
'eventID' => $params['event_id'],
'contributionID' => $contributionID,
],
Expand Down
2 changes: 1 addition & 1 deletion CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ protected function emailReceipt($formValues) {
'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(),
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 @@ -700,6 +700,8 @@ protected function sendReceipt($membership) {
}
}

// 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',
Expand All @@ -710,7 +712,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
5 changes: 3 additions & 2 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 @@ -306,8 +307,7 @@ public function testParticipantOfflineReceipt(string $thousandSeparator): void {
'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,
Expand Down Expand Up @@ -393,6 +393,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 {contribution.balance_amount|boolean} && {contribution.is_pay_later|boolean}}
<p>{contribution.pay_later_receipt}</p>
Expand Down