From: Eileen McNaughton Date: Thu, 2 Jul 2015 12:50:14 +0000 (+1200) Subject: CRM-16788 regression on membership backoffice not showing auto-renew X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=ab30e033a008673986b38aecc36214edc6e0ddf1;p=civicrm-core.git CRM-16788 regression on membership backoffice not showing auto-renew --- diff --git a/CRM/Contribute/BAO/ContributionPage.php b/CRM/Contribute/BAO/ContributionPage.php index 5d30e5f976..39274fe745 100644 --- a/CRM/Contribute/BAO/ContributionPage.php +++ b/CRM/Contribute/BAO/ContributionPage.php @@ -454,14 +454,15 @@ class CRM_Contribute_BAO_ContributionPage extends CRM_Contribute_DAO_Contributio 'cc_receipt', 'bcc_receipt', )); + $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]); } - - $isEmailReceipt = CRM_Utils_Array::value('is_email_receipt', $value[$pageID]); - $isOfflineRecur = FALSE; - if (!$pageID && $recur->id) { - $isOfflineRecur = TRUE; + elseif ($recur->id) { + // This means we are coming from back-office - ie. no page ID, but recurring. + // Ideally this information would be passed into the function clearly rather than guessing by convention. + $isEmailReceipt = TRUE; } - if ($isEmailReceipt || $isOfflineRecur) { + + if ($isEmailReceipt) { if ($pageID) { $receiptFrom = '"' . CRM_Utils_Array::value('receipt_from_name', $value[$pageID]) . '" <' . $value[$pageID]['receipt_from_email'] . '>'; diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index 1d2b3839af..a62f68a1ee 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -1740,14 +1740,23 @@ WHERE civicrm_membership.contact_id = civicrm_contact.id } /** + * Build an array of available membership types. + * * @param CRM_Core_Form $form * @param int $membershipTypeID + * @param bool $activeOnly + * Do we only want active ones? + * (probably this should default to TRUE but as a newly added parameter we are leaving default b + * behaviour unchanged). * * @return array */ - public static function buildMembershipTypeValues(&$form, $membershipTypeID = NULL) { + public static function buildMembershipTypeValues(&$form, $membershipTypeID = NULL, $activeOnly = FALSE) { $whereClause = " WHERE domain_id = " . CRM_Core_Config::domainID(); + if ($activeOnly) { + $whereClause .= " AND is_active = 1 "; + } if (is_array($membershipTypeID)) { $allIDs = implode(',', $membershipTypeID); $whereClause .= " AND id IN ( $allIDs )"; @@ -1778,6 +1787,8 @@ FROM civicrm_membership_type 'relationship_type_id', 'relationship_direction', 'max_related', + 'duration_unit', + 'duration_interval', ); while ($dao->fetch()) { diff --git a/CRM/Member/Form.php b/CRM/Member/Form.php index 46b0ac44a9..808cba5611 100644 --- a/CRM/Member/Form.php +++ b/CRM/Member/Form.php @@ -58,6 +58,20 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { */ protected $_fromEmails = array(); + /** + * Details of all enabled membership types. + * + * @var array + */ + protected $allMembershipTypeDetails = array(); + + /** + * Array of membership type IDs and whether they permit autorenewal. + * + * @var array + */ + protected $membershipTypeRenewalStatus = array(); + public function preProcess() { // Check for edit permission. if (!CRM_Core_Permission::checkActionPermission('CiviMember', $this->_action)) { @@ -73,6 +87,13 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { $this->assign('context', $this->_context); $this->assign('membershipMode', $this->_mode); + $this->allMembershipTypeDetails = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, NULL, TRUE); + foreach ($this->allMembershipTypeDetails as $index => $membershipType) { + if ($membershipType['auto_renew']) { + $this->_recurMembershipTypes[$index] = $membershipType; + $this->membershipTypeRenewalStatus[$index] = $membershipType['auto_renew']; + } + } } /** @@ -126,52 +147,19 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { } // Build the form for auto renew. This is displayed when in credit card mode or update mode. // The reason for showing it in update mode is not that clear. - $autoRenew = array(); - $recurProcessor = array(); if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) { - if (!empty($recurProcessor)) { - $autoRenew = array(); - if (!empty($membershipType)) { - $sql = ' -SELECT id, - auto_renew, - duration_unit, - duration_interval - FROM civicrm_membership_type -WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )'; - $recurMembershipTypes = CRM_Core_DAO::executeQuery($sql); - while ($recurMembershipTypes->fetch()) { - $autoRenew[$recurMembershipTypes->id] = $recurMembershipTypes->auto_renew; - foreach (array( - 'id', - 'auto_renew', - 'duration_unit', - 'duration_interval', - ) as $fld) { - $this->_recurMembershipTypes[$recurMembershipTypes->id][$fld] = $recurMembershipTypes->$fld; - } - } - } - - if ($this->_mode) { - if (!empty($this->_recurPaymentProcessors)) { - $this->assign('allowAutoRenew', TRUE); - } - } - - $this->assign('autoRenew', json_encode($autoRenew)); - $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'), - NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );") - ); - if ($this->_action & CRM_Core_Action::UPDATE) { - $autoRenewElement->freeze(); - } + if (!empty($this->_recurPaymentProcessors)) { + $this->assign('allowAutoRenew', TRUE); } - } - $this->assign('recurProcessor', json_encode($recurProcessor)); + $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'), + NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );") + ); + if ($this->_action & CRM_Core_Action::UPDATE) { + $autoRenewElement->freeze(); + } - if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) { + $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors)); $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'), @@ -181,7 +169,7 @@ WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )'; $this->assignPaymentRelatedVariables(); } - $this->assign('autoRenewOptions', json_encode($autoRenew)); + $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus)); if ($this->_action & CRM_Core_Action::RENEW) { $this->addButtons(array( diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index 3797db6b73..5a022b02df 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -513,48 +513,39 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -'); - $dao = new CRM_Member_DAO_MembershipType(); - $dao->domain_id = CRM_Core_Config::domainID(); - $dao->find(); - // retrieve all memberships - $allMemberships = CRM_Member_BAO_Membership::buildMembershipTypeValues($this); + $allMembershipInfo = array(); + foreach ($this->allMembershipTypeDetails as $key => $values) { + if ($this->_mode && empty($values['minimum_fee'])) { + continue; + } + else { + $memberOfContactId = CRM_Utils_Array::value('member_of_contact_id', $values); + if (empty($selMemTypeOrg[$memberOfContactId])) { + $selMemTypeOrg[$memberOfContactId] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', + $memberOfContactId, + 'display_name', + 'id' + ); - $allMembershipInfo = $membershipType = array(); - foreach ($allMemberships as $key => $values) { - if (!empty($values['is_active'])) { - $membershipType[$key] = CRM_Utils_Array::value('name', $values); - if ($this->_mode && empty($values['minimum_fee'])) { - continue; + $selOrgMemType[$memberOfContactId][0] = ts('- select -'); } - else { - $memberOfContactId = CRM_Utils_Array::value('member_of_contact_id', $values); - if (empty($selMemTypeOrg[$memberOfContactId])) { - $selMemTypeOrg[$memberOfContactId] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', - $memberOfContactId, - 'display_name', - 'id' - ); - - $selOrgMemType[$memberOfContactId][0] = ts('- select -'); - } - if (empty($selOrgMemType[$memberOfContactId][$key])) { - $selOrgMemType[$memberOfContactId][$key] = CRM_Utils_Array::value('name', $values); - } + if (empty($selOrgMemType[$memberOfContactId][$key])) { + $selOrgMemType[$memberOfContactId][$key] = CRM_Utils_Array::value('name', $values); } - - // build membership info array, which is used when membership type is selected to: - // - set the payment information block - // - set the max related block - $allMembershipInfo[$key] = array( - 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $values), - 'total_amount' => CRM_Utils_Money::format($values['minimum_fee'], NULL, '%a'), - 'total_amount_numeric' => CRM_Utils_Array::value('minimum_fee', $values), - 'auto_renew' => CRM_Utils_Array::value('auto_renew', $values), - 'has_related' => isset($values['relationship_type_id']), - 'max_related' => CRM_Utils_Array::value('max_related', $values), - ); } + + // build membership info array, which is used when membership type is selected to: + // - set the payment information block + // - set the max related block + $allMembershipInfo[$key] = array( + 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $values), + 'total_amount' => CRM_Utils_Money::format($values['minimum_fee'], NULL, '%a'), + 'total_amount_numeric' => CRM_Utils_Array::value('minimum_fee', $values), + 'auto_renew' => CRM_Utils_Array::value('auto_renew', $values), + 'has_related' => isset($values['relationship_type_id']), + 'max_related' => CRM_Utils_Array::value('max_related', $values), + ); } $this->assign('allMembershipInfo', json_encode($allMembershipInfo)); @@ -571,10 +562,14 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $selOrgMemType[$index] = $orgMembershipType; } - $memTypeJs = array('onChange' => "CRM.buildCustomData( 'Membership', this.value );"); + $memTypeJs = array( + 'onChange' => "buildMaxRelated(this.value,true); CRM.buildCustomData('Membership', this.value);", + ); + if (!empty($this->_recurPaymentProcessors)) { + $memTypeJs['onChange'] = "" . $memTypeJs['onChange'] . 'buildAutoRenew(this.value, null);'; + } - // for max_related: a little JS to show/hide & set default value - $memTypeJs['onChange'] = "buildMaxRelated(this.value,true); " . $memTypeJs['onChange']; + ; $this->add('text', 'max_related', ts('Max related'), CRM_Core_DAO::getAttribute('CRM_Member_DAO_Membership', 'max_related') ); @@ -1316,8 +1311,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $ids['membership'] = $params['id'] = $this->_id; } - $session = CRM_Core_Session::singleton(); - $ids['userId'] = $session->get('userID'); + $ids['userId'] = CRM_Core_Session::singleton()->get('userID'); // membership type custom data foreach ($this->_memTypeSelected as $memType) { @@ -1336,7 +1330,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $membershipType = implode(', ', $membershipTypes); // Retrieve the name and email of the current user - this will be the FROM for the receipt email - list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($ids['userId']); + list($userName) = CRM_Contact_BAO_Contact_Location::getEmailDetails($ids['userId']); //CRM-13981, allow different person as a soft-contributor of chosen type if ($this->_contributorContactID != $this->_contactID) { @@ -1507,9 +1501,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { if (!empty($paymentParams['is_recur'])) { $contributionType = new CRM_Financial_DAO_FinancialType(); $contributionType->id = $params['financial_type_id']; - if (!$contributionType->find(TRUE)) { - CRM_Core_Error::fatal('Could not find a system table'); - } + $contributionType->find(TRUE); $contribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution($this, $paymentParams, @@ -1541,13 +1533,17 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { } if ($params['total_amount'] > 0.0) { - $payment = CRM_Core_Payment::singleton($this->_mode, $this->_paymentProcessor, $this); + $payment = $this->_paymentProcessor['object']; try { $result = $payment->doPayment($paymentParams); + $this->_params = array_merge($this->_params, $result); + // Assign amount to template if payment was successful. + $this->assign('amount', $params['total_amount']); } catch (PaymentProcessorException $e) { if (!empty($paymentParams['contributionID'])) { - CRM_Contribute_BAO_Contribution::failPayment($paymentParams['contributionID'], $e->getMessage()); + CRM_Contribute_BAO_Contribution::failPayment($paymentParams['contributionID'], $this->_contactID, + $e->getMessage()); } if (!empty($paymentParams['contributionRecurID'])) { CRM_Contribute_BAO_ContributionRecur::deleteRecurContribution($paymentParams['contributionRecurID']); diff --git a/CRM/Member/Form/MembershipRenewal.php b/CRM/Member/Form/MembershipRenewal.php index 448d4e61b8..3f29fd4d1e 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -242,16 +242,13 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { $this->assign('entityID', $this->_id); $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -'); - $allMemberships = CRM_Member_BAO_Membership::buildMembershipTypeValues($this); - - $allMembershipInfo = $membershipType = array(); + $allMembershipInfo = array(); // auto renew options if enabled for the membership $options = CRM_Core_SelectValues::memberAutoRenew(); - foreach ($allMemberships as $key => $values) { + foreach ($this->allMembershipTypeDetails as $key => $values) { if (!empty($values['is_active'])) { - $membershipType[$key] = CRM_Utils_Array::value('name', $values); if ($this->_mode && empty($values['minimum_fee'])) { continue; } @@ -288,8 +285,8 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { $this->assign('allMembershipInfo', json_encode($allMembershipInfo)); if ($this->_memType) { - $this->assign('orgName', $selMemTypeOrg[$allMemberships[$this->_memType]['member_of_contact_id']]); - $this->assign('memType', $allMemberships[$this->_memType]['name']); + $this->assign('orgName', $selMemTypeOrg[$this->allMembershipTypeDetails[$this->_memType]['member_of_contact_id']]); + $this->assign('memType', $this->allMembershipTypeDetails[$this->_memType]['name']); } // force select of organization by default, if only one organization in @@ -304,7 +301,7 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { $selOrgMemType[$index] = $orgMembershipType; } - $js = array('onChange' => "setPaymentBlock( ); CRM.buildCustomData( 'Membership', this.value );"); + $js = array('onChange' => "setPaymentBlock(); CRM.buildCustomData('Membership', this.value);"); $sel = &$this->addElement('hierselect', 'membership_type_id', ts('Renewal Membership Organization and Type'), $js diff --git a/templates/CRM/Member/Form/MembershipRenewal.tpl b/templates/CRM/Member/Form/MembershipRenewal.tpl index 1e8615fd95..bb9e9b5ed3 100644 --- a/templates/CRM/Member/Form/MembershipRenewal.tpl +++ b/templates/CRM/Member/Form/MembershipRenewal.tpl @@ -90,19 +90,6 @@ {$form.renewal_date.label} {include file="CRM/common/jcalendar.tpl" elementName=renewal_date} - {if $membershipMode} - {if !empty($form.auto_renew)} - - {$form.auto_renew.label} {help id="id-auto_renew" file="CRM/Member/Form/Membership.hlp" action=$action} - {$form.auto_renew.html} - - {/if} - - {$form.financial_type_id.label} - {$form.financial_type_id.html}
- {ts}Select the appropriate financial type for this payment.{/ts} - - {/if} {if $accessContribution and ! $membershipMode} {$form.record_contribution.label} diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index cc2ee1ae5f..d2d1dfa926 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -304,7 +304,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { $var = array(); $this->params['recur_frequency_unit'] = 'month'; $this->setUpMembershipContributionPage(); - $dummyPP = CRM_Core_Payment::singleton('live', $this->_paymentProcessor); + $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 1, 'trxn_id' => 'create_first_success')); $submitParams = array( @@ -366,7 +366,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { $this->params['is_recur'] = 1; $this->params['recur_frequency_unit'] = 'month'; $this->setUpMembershipContributionPage(); - $dummyPP = CRM_Core_Payment::singleton('live', $this->_paymentProcessor); + $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor); $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 2)); $submitParams = array(