Skip to content

Commit

Permalink
[REF] Cleanup interaction with membership & membership id
Browse files Browse the repository at this point in the history
This is intended to simplify #20077
by switching to using functions to retrieve membership (as an array) and membershipID
rather than passing around variables
  • Loading branch information
eileenmcnaughton committed Apr 27, 2021
1 parent 76060cc commit b097678
Showing 1 changed file with 78 additions and 31 deletions.
109 changes: 78 additions & 31 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
*/
protected $_membershipIDs = [];

/**
* Membership created or edited on this form.
*
* If a price set creates multiple this will be the last one created.
*
* This 'last' bias reflects historical code - but it's mostly used in the receipt
* and there is all sorts of weird and wonderful handling that potentially compensates.
*
* @var array
*/
protected $membership = [];

/**
* Set entity fields to be assigned to the form.
*/
Expand Down Expand Up @@ -904,19 +916,19 @@ private function convertIsOverrideValue() {
* @param CRM_Core_Form $form
* Form object.
* @param array $formValues
* @param object $membership
* Object.
*
* @return bool
* true if mail was sent successfully
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*
* @deprecated
* This function is shared with Batch_Entry which has limited overlap
* This function was shared with Batch_Entry which had limited overlap
* & needs rationalising.
*
*/
protected function emailReceipt($form, &$formValues, $membership) {
protected function emailReceipt($form, &$formValues) {
$membership = $this->getMembership();
// retrieve 'from email id' for acknowledgement
$receiptFrom = $formValues['from_email_address'] ?? NULL;

Expand All @@ -929,7 +941,7 @@ protected function emailReceipt($form, &$formValues, $membership) {
$form->assign('module', 'Membership');
$form->assign('contactID', $formValues['contact_id']);

$form->assign('membershipID', CRM_Utils_Array::value('membership_id', $form->_params, CRM_Utils_Array::value('membership_id', $form->_defaultValues)));
$form->assign('membershipID', $this->getMembershipID());

if (!empty($formValues['contribution_id'])) {
$form->assign('contributionID', $formValues['contribution_id']);
Expand All @@ -949,23 +961,21 @@ protected function emailReceipt($form, &$formValues, $membership) {
$form->assign('receive_date', CRM_Utils_Array::value('receive_date', $formValues));
$form->assign('formValues', $formValues);

$form->assign('mem_start_date', CRM_Utils_Date::formatDateOnlyLong($membership->start_date));
if (!CRM_Utils_System::isNull($membership->end_date)) {
$form->assign('mem_end_date', CRM_Utils_Date::formatDateOnlyLong($membership->end_date));
$form->assign('mem_start_date', CRM_Utils_Date::formatDateOnlyLong($membership['start_date']));
if (!CRM_Utils_System::isNull($membership['end_date'])) {
$form->assign('mem_end_date', CRM_Utils_Date::formatDateOnlyLong($membership['end_date']));
}
$form->assign('membership_name', CRM_Member_PseudoConstant::membershipType($membership->membership_type_id));
$form->assign('membership_name', CRM_Member_PseudoConstant::membershipType($membership['membership_type_id']));

// @todo - if we have to figure out if this is for batch processing it doesn't belong in the shared function.
$isBatchProcess = is_a($form, 'CRM_Batch_Form_Entry');
if ((empty($form->_contributorDisplayName) || empty($form->_contributorEmail)) || $isBatchProcess) {
if ((empty($form->_contributorDisplayName) || empty($form->_contributorEmail))) {
// in this case the form is being called statically from the batch editing screen
// having one class in the form layer call another statically is not greate
// & we should aim to move this function to the BAO layer in future.
// however, we can assume that the contact_id passed in by the batch
// function will be the recipient
[$form->_contributorDisplayName, $form->_contributorEmail]
= CRM_Contact_BAO_Contact_Location::getEmailDetails($formValues['contact_id']);
if (empty($form->_receiptContactId) || $isBatchProcess) {
if (empty($form->_receiptContactId)) {
$form->_receiptContactId = $formValues['contact_id'];
}
}
Expand Down Expand Up @@ -1000,8 +1010,6 @@ protected function emailReceipt($form, &$formValues, $membership) {
public function submit(): void {
$this->storeContactFields($this->_params);
$this->beginPostProcess();
$endDate = NULL;
$membership = [];

$params = $softParams = $ids = [];

Expand Down Expand Up @@ -1238,7 +1246,7 @@ public function submit(): void {
$count = 0;
foreach ($this->_memTypeSelected as $memType) {
if ($count &&
($relateContribution = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id))
($relateContribution = CRM_Member_BAO_Membership::getMembershipContributionId($this->getMembershipID()))
) {
$membershipTypeValues[$memType]['relate_contribution_id'] = $relateContribution;
}
Expand All @@ -1265,10 +1273,9 @@ public function submit(): void {
}
$membershipParams['payment_instrument_id'] = $this->getPaymentInstrumentID();
// @todo stop passing $ids (membership and userId only are set above)
$membership = CRM_Member_BAO_Membership::create($membershipParams, $ids);
$this->setMembership((array) CRM_Member_BAO_Membership::create($membershipParams, $ids));
$params['contribution'] = $membershipParams['contribution'] ?? NULL;
unset($params['lineItems']);
$this->_membershipIDs[] = $membership->id;
$count++;
}

Expand All @@ -1293,11 +1300,10 @@ public function submit(): void {
$membershipParams['skipLineItem'] = TRUE;
unset($membershipParams['lineItems']);
// @todo stop passing $ids (membership and userId only are set above)
$membership = CRM_Member_BAO_Membership::create($membershipParams, $ids);
$lineItem[$this->_priceSetId][$id]['entity_id'] = $membership->id;
$this->setMembership((array) CRM_Member_BAO_Membership::create($membershipParams, $ids));
$lineItem[$this->_priceSetId][$id]['entity_id'] = $this->membership['id'];
$lineItem[$this->_priceSetId][$id]['entity_table'] = 'civicrm_membership';

$this->_membershipIDs[] = $membership->id;
}
$params['lineItems'] = $lineItem;
if (!empty($formValues['record_contribution'])) {
Expand Down Expand Up @@ -1342,12 +1348,12 @@ public function submit(): void {
$this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ? $lineItem : FALSE);

$receiptSend = FALSE;
$contributionId = CRM_Member_BAO_Membership::getMembershipContributionId($membership->id);
$contributionId = CRM_Member_BAO_Membership::getMembershipContributionId($this->getMembershipID());
$membershipIds = $this->_membershipIDs;
if ($contributionId && !empty($membershipIds)) {
$contributionDetails = CRM_Contribute_BAO_Contribution::getContributionDetails(
CRM_Export_Form_Select::MEMBER_EXPORT, $this->_membershipIDs);
if ($contributionDetails[$membership->id]['contribution_status'] === 'Completed') {
if ($contributionDetails[$this->getMembershipID()]['contribution_status'] === 'Completed') {
$receiptSend = TRUE;
}
}
Expand All @@ -1362,7 +1368,7 @@ public function submit(): void {
$formValues['receipt_text_signup'] = $formValues['receipt_text'];
// send email receipt
$this->assignBillingName();
$mailSend = $this->emailMembershipReceipt($formValues, $membership);
$mailSend = $this->emailMembershipReceipt($formValues);
$receiptSent = TRUE;
}

Expand Down Expand Up @@ -1580,14 +1586,13 @@ protected function isUpdateToExistingRecurringMembership() {
* Send a receipt for the membership.
*
* @param array $formValues
* @param \CRM_Member_BAO_Membership $membership
*
* @return bool
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
protected function emailMembershipReceipt($formValues, $membership) {
$customValues = $this->getCustomValuesForReceipt($formValues, $membership);
protected function emailMembershipReceipt($formValues) {
$customValues = $this->getCustomValuesForReceipt($formValues);
$this->assign('customValues', $customValues);
$this->assign('total_amount', $this->order->getTotalAmount());
$this->assign('totalTaxAmount', $this->order->getTotalTaxAmount());
Expand All @@ -1609,7 +1614,7 @@ protected function emailMembershipReceipt($formValues, $membership) {
$formValues['contributionType_name'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType',
$this->getFinancialTypeID()
);
return $this->emailReceipt($this, $formValues, $membership);
return $this->emailReceipt($this, $formValues);
}

/**
Expand All @@ -1618,10 +1623,9 @@ protected function emailMembershipReceipt($formValues, $membership) {
* @todo figure out why the scary code this calls does & document.
*
* @param array $formValues
* @param \CRM_Member_BAO_Membership $membership
* @return array
*/
protected function getCustomValuesForReceipt($formValues, $membership) {
protected function getCustomValuesForReceipt($formValues): array {
$customFields = $customValues = [];
if (property_exists($this, '_groupTree')
&& !empty($this->_groupTree)
Expand All @@ -1637,7 +1641,7 @@ protected function getCustomValuesForReceipt($formValues, $membership) {
}
}

$members = [['member_id', '=', $membership->id, 0, 0]];
$members = [['member_id', '=', $this->getMembershipID(), 0, 0]];
// check whether its a test drive
if ($this->_mode === 'test') {
$members[] = ['member_test', '=', 1, 0, 0];
Expand Down Expand Up @@ -1894,4 +1898,47 @@ protected function getReceiveDate(): string {
return $this->getSubmittedValue('receive_date') ?: date('YmdHis');
}

/**
* Set membership IDs.
*
* @param array $ids
*/
protected function setMembershipIDs(array $ids): void {
$this->_membershipIDs = $ids;
}

/**
* Get the created or edited membership ID.
*
* @return false|mixed
*/
protected function getMembershipID() {
return reset($this->_membershipIDs);
}

/**
* Get the membership (or last membership) created or edited on this form.
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
protected function getMembership(): array {
if (empty($this->membership)) {
$this->membership = civicrm_api3('Membership', 'get', ['id' => $this->getMembershipID()]);
}
return $this->membership;
}

/**
* Setter for membership.
*
* @param array $membership
*/
protected function setMembership(array $membership): void {
if (!in_array($membership['id'], $this->_membershipIDs, TRUE)) {
$this->_membershipIDs[] = $membership['id'];
}
$this->membership = $membership;
}

}

0 comments on commit b097678

Please sign in to comment.