Skip to content

Commit

Permalink
[REF] Extract code to get the from address for a recurring contribution.
Browse files Browse the repository at this point in the history
This makes the retrieval generally available, since is it part of the data model,
rather than tied to the form. It could possibly be a token, or at least retrievable on
the recurring edit workflow template - but that is beyond the scope of this
  • Loading branch information
eileenmcnaughton committed Sep 3, 2021
1 parent 3b943db commit b90fd9b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
29 changes: 29 additions & 0 deletions CRM/Contribute/BAO/ContributionRecur.php
Original file line number Diff line number Diff line change
Expand Up @@ -1085,4 +1085,33 @@ public static function buildOptions($fieldName, $context = NULL, $props = []) {
return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
}


/**
* Get the from address to use for the recurring contribution.
*
* This uses the contribution page id, if there is one, or the default domain one.
*
* @param int $id
* Recurring contribution ID.
*
* @internal
*
* @return string
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public static function getRecurFromAddress(int $id): string {
$details = Contribution::get(FALSE)
->addWhere('contribution_recur_id', '=', $id)
->addWhere('contribution_page_id', 'IS NOT NULL')
->addSelect('contribution_page_id.receipt_from_name', 'contribution_page_id.receipt_from_email')
->addOrderBy('receive_date', 'DESC')
->execute()->first();
if (empty($details['contribution_page_id.receipt_from_email'])) {
$domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
return "$domainValues[0] <$domainValues[1]>";
}
return '"' . $details['contribution_page_id.receipt_from_name'] . '" <' . $details['contribution_page_id.receipt_from_email'] . '>';
}

}
30 changes: 14 additions & 16 deletions CRM/Contribute/Form/UpdateSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ public function buildQuickForm() {

/**
* Called after the user submits the form.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public function postProcess() {
// store the submitted values in an array
Expand All @@ -203,7 +206,7 @@ public function postProcess() {
}

// if this is an update of an existing recurring contribution, pass the ID
$params['id'] = $this->_subscriptionDetails->recur_id;
$params['id'] = $this->getContributionRecurID();
$message = '';

$params['subscriptionId'] = $this->getSubscriptionDetails()->processor_id;
Expand Down Expand Up @@ -279,21 +282,7 @@ public function postProcess() {
CRM_Activity_BAO_Activity::create($activityParams);

if (!empty($params['is_notify'])) {
// send notification
if ($this->_subscriptionDetails->contribution_page_id) {
CRM_Core_DAO::commonRetrieveAll('CRM_Contribute_DAO_ContributionPage', 'id',
$this->_subscriptionDetails->contribution_page_id, $value, [
'title',
'receipt_from_name',
'receipt_from_email',
]
);
$receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$this->_subscriptionDetails->contribution_page_id]) . '" <' . $value[$this->_subscriptionDetails->contribution_page_id]['receipt_from_email'] . '>';
}
else {
$domainValues = CRM_Core_BAO_Domain::getNameAndEmail();
$receiptFrom = "$domainValues[0] <$domainValues[1]>";
}
$receiptFrom = CRM_Contribute_BAO_ContributionRecur::getRecurFromAddress($this->getContributionRecurID());

[$donorDisplayName, $donorEmail] = CRM_Contact_BAO_Contact::getContactDetails($contactID);

Expand Down Expand Up @@ -337,4 +326,13 @@ public function postProcess() {
}
}

/**
* Get the recurring contribution ID.
*
* @return int
*/
protected function getContributionRecurID(): int {
return $this->_subscriptionDetails->recur_id;
}

}

0 comments on commit b90fd9b

Please sign in to comment.