From 8da69606729e6926d4b3ce493f268a66298164d9 Mon Sep 17 00:00:00 2001 From: eileen Date: Fri, 26 Mar 2021 19:38:23 +1300 Subject: [PATCH] [REF] [Towards membership api] Simplify message functions The amount of code confusion to avoid loading the end_date isn't really warranted for the performance boost.... --- CRM/Member/Form/Membership.php | 67 +++++++++++++++------------------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index 9d4d85d58b75..8737dd70cba0 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -1153,7 +1153,7 @@ public function submit(): void { $params['lineItems'] = $lineItem; $params['processPriceSet'] = TRUE; } - $createdMemberships = []; + if ($this->_mode) { $params['total_amount'] = CRM_Utils_Array::value('total_amount', $formValues, 0); @@ -1273,7 +1273,7 @@ public function submit(): void { $membershipTypeValues[$memType]['startDate'] = NULL; $membershipTypeValues[$memType]['endDate'] = NULL; } - $endDate = $startDate = NULL; + $startDate = NULL; } $now = CRM_Utils_Time::date('YmdHis'); $params['receive_date'] = CRM_Utils_Time::date('Y-m-d H:i:s'); @@ -1335,7 +1335,6 @@ public function submit(): void { $params['contribution'] = $membershipParams['contribution'] ?? NULL; unset($params['lineItems']); $this->_membershipIDs[] = $membership->id; - $createdMemberships[$memType] = $membership; $count++; } @@ -1365,7 +1364,6 @@ public function submit(): void { $lineItem[$this->_priceSetId][$id]['entity_table'] = 'civicrm_membership'; $this->_membershipIDs[] = $membership->id; - $createdMemberships[$membership->membership_type_id] = $membership; } $params['lineItems'] = $lineItem; if (!empty($formValues['record_contribution'])) { @@ -1373,11 +1371,10 @@ public function submit(): void { } } if (($this->_action & CRM_Core_Action::UPDATE)) { - $this->addStatusMessage($this->getStatusMessageForUpdate($membership, $endDate)); + $this->addStatusMessage($this->getStatusMessageForUpdate()); } elseif (($this->_action & CRM_Core_Action::ADD)) { - $this->addStatusMessage($this->getStatusMessageForCreate($endDate, $createdMemberships, - $this->isCreateRecurringContribution(), $calcDates)); + $this->addStatusMessage($this->getStatusMessageForCreate()); } // This would always be true as we always add price set id into both @@ -1567,17 +1564,15 @@ protected function setUserContext() { /** * Get status message for updating membership. * - * @param CRM_Member_BAO_Membership $membership - * @param string $endDate - * * @return string + * @throws \CiviCRM_API3_Exception */ - protected function getStatusMessageForUpdate($membership, $endDate) { - // End date can be modified by hooks, so if end date is set then use it. - $endDate = ($membership->end_date) ? $membership->end_date : $endDate; - + protected function getStatusMessageForUpdate(): string { + foreach ($this->getCreatedMemberships() as $membership) { + $endDate = $membership['end_date'] ?? NULL; + } $statusMsg = ts('Membership for %1 has been updated.', [1 => $this->_memberDisplayName]); - if ($endDate && $endDate !== 'null') { + if ($endDate) { $endDate = CRM_Utils_Date::customFormat($endDate); $statusMsg .= ' ' . ts('The membership End Date is %1.', [1 => $endDate]); } @@ -1587,39 +1582,25 @@ protected function getStatusMessageForUpdate($membership, $endDate) { /** * Get status message for create action. * - * @param string $endDate - * @param array $createdMemberships - * @param bool $isRecur - * @param array $calcDates - * * @return array|string + * @throws \CiviCRM_API3_Exception */ - protected function getStatusMessageForCreate($endDate, $createdMemberships, - $isRecur, $calcDates) { - // FIX ME: fix status messages - - $statusMsg = []; - foreach ($this->_memTypeSelected as $membershipTypeID) { - $statusMsg[$membershipTypeID] = ts('%1 membership for %2 has been added.', [ - 1 => $this->allMembershipTypeDetails[$membershipTypeID]['name'], + protected function getStatusMessageForCreate(): string { + foreach ($this->getCreatedMemberships() as $membership) { + $statusMsg[$membership['membership_type_id']] = ts('%1 membership for %2 has been added.', [ + 1 => $this->allMembershipTypeDetails[$membership['membership_type_id']]['name'], 2 => $this->_memberDisplayName, ]); - $membership = $createdMemberships[$membershipTypeID]; - $memEndDate = $membership->end_date ?: $endDate; + $memEndDate = $membership['end_date'] ?? NULL; - //get the end date from calculated dates. - if (!$memEndDate && !$isRecur) { - $memEndDate = $calcDates[$membershipTypeID]['end_date'] ?? NULL; - } - - if ($memEndDate && $memEndDate !== 'null') { + if ($memEndDate) { $memEndDate = CRM_Utils_Date::formatDateOnlyLong($memEndDate); - $statusMsg[$membershipTypeID] .= ' ' . ts('The new membership End Date is %1.', [1 => $memEndDate]); + $statusMsg[$membership['membership_type_id']] .= ' ' . ts('The new membership End Date is %1.', [1 => $memEndDate]); } } $statusMsg = implode('
', $statusMsg); - return $statusMsg; + return $statusMsg ?? ''; } /** @@ -1888,4 +1869,14 @@ public function getPaymentProcessorID(): int { return (int) ($this->getSubmittedValue('payment_processor_id') ?: $this->_paymentProcessor['id']); } + /** + * Get memberships submitted through the form submission. + * @return array + * + * @throws \CiviCRM_API3_Exception + */ + protected function getCreatedMemberships(): array { + return civicrm_api3('Membership', 'get', ['id' => ['IN' => $this->_membershipIDs]])['values']; + } + }