From 42e3a033fc815f74e69bc543ccceb4249518c706 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 29 Jun 2015 18:08:50 +1200 Subject: [PATCH] Fix conflict on visibility of ->_paymentProcessors In order to resolve the conflict on 'protected' I moved functions where it was accessed form ->_paymentProcessors to an inherited form class. In general the moves make sense - ie. having a function on contribution shared by event is not good, Nor is having a function on the BAO called by forms. --- CRM/Contribute/Form/Contribution/Confirm.php | 3 +- CRM/Contribute/Form/Contribution/Main.php | 45 +--- CRM/Contribute/Form/Contribution/ThankYou.php | 3 +- CRM/Contribute/Form/ContributionBase.php | 203 +++++++++++++++++ CRM/Core/Form.php | 41 +++- CRM/Event/Form/Registration/Register.php | 3 +- CRM/Member/BAO/Membership.php | 204 ------------------ 7 files changed, 250 insertions(+), 252 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 25a78f13e5..a234d0fa4b 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -545,8 +545,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr if (isset($params['selectMembership']) && $params['selectMembership'] != 'no_thanks' ) { - CRM_Member_BAO_Membership::buildMembershipBlock($this, - $this->_id, + $this->buildMembershipBlock( $this->_membershipContactID, FALSE, $params['selectMembership'], diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 805088d3d4..f1ca8f54d2 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -67,7 +67,8 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu public function preProcess() { parent::preProcess(); - self::preProcessPaymentOptions($this); + $this->_paymentProcessors = $this->get('paymentProcessors'); + $this->preProcessPaymentOptions(); // Make the contributionPageID available to the template $this->assign('contributionPageID', $this->_id); @@ -455,8 +456,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu $this->set('useForMember', $this->_useForMember); } - $this->_separateMembershipPayment = CRM_Member_BAO_Membership::buildMembershipBlock($this, - $this->_id, + $this->_separateMembershipPayment = $this->buildMembershipBlock( $this->_membershipContactID, TRUE, NULL, FALSE, $isTest @@ -1347,45 +1347,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu } - /** - * Handle Payment Processor switching for contribution and event registration forms. - * - * @param CRM_Contribute_Form_Contribution_Main|CRM_Event_Form_Registration_Register $form - * @param bool $noFees - */ - public static function preProcessPaymentOptions(&$form, $noFees = FALSE) { - $form->_snippet = CRM_Utils_Array::value('snippet', $_GET); - - $form->_paymentProcessors = $noFees ? array() : $form->get('paymentProcessors'); - $form->_paymentProcessorID = NULL; - if ($form->_paymentProcessors) { - if (!empty($form->_submitValues)) { - $form->_paymentProcessorID = CRM_Utils_Array::value('payment_processor_id', $form->_submitValues); - $form->_paymentProcessor = CRM_Utils_Array::value($form->_paymentProcessorID, $form->_paymentProcessors); - $form->set('type', $form->_paymentProcessorID); - $form->set('mode', $form->_mode); - $form->set('paymentProcessor', $form->_paymentProcessor); - } - // Set default payment processor - else { - foreach ($form->_paymentProcessors as $values) { - if (!empty($values['is_default']) || count($form->_paymentProcessors) == 1) { - $form->_paymentProcessorID = $values['id']; - break; - } - } - } - if ($form->_paymentProcessorID) { - CRM_Core_Payment_ProcessorForm::preProcess($form); - } - else { - $form->_paymentProcessor = array(); - } - CRM_Financial_Form_Payment::addCreditCardJs(); - } - $form->assign('paymentProcessorID', $form->_paymentProcessorID); - } - /** * @param $invoiceID * @param $params diff --git a/CRM/Contribute/Form/Contribution/ThankYou.php b/CRM/Contribute/Form/Contribution/ThankYou.php index 9a78797924..f9dc5d3fb4 100644 --- a/CRM/Contribute/Form/Contribution/ThankYou.php +++ b/CRM/Contribute/Form/Contribution/ThankYou.php @@ -172,8 +172,7 @@ class CRM_Contribute_Form_Contribution_ThankYou extends CRM_Contribute_Form_Cont $this->assign('membership_amount', $membershipAmount); $this->assign('renewal_mode', $renewalMode); - CRM_Member_BAO_Membership::buildMembershipBlock($this, - $this->_id, + $this->buildMembershipBlock( $this->_membershipContactID, FALSE, $membershipTypeID, diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index bcb754f22d..7da2c4e24d 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -840,4 +840,207 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { } } + /** + * Build Membership Block in Contribution Pages. + * + * @param CRM_Core_Form $form + * Form object. + * @param int $pageID + * Unused?. + * @param int $cid + * Contact checked for having a current membership for a particular membership. + * @param bool $formItems + * @param int $selectedMembershipTypeID + * Selected membership id. + * @param bool $thankPage + * Thank you page. + * @param null $isTest + * + * @return bool + * Is this a separate membership payment + */ + protected function buildMembershipBlock( + $cid, + $formItems = FALSE, + $selectedMembershipTypeID = NULL, + $thankPage = FALSE, + $isTest = NULL + ) { + + $separateMembershipPayment = FALSE; + if ($this->_membershipBlock) { + $this->_currentMemberships = array(); + + $membershipBlock = $this->_membershipBlock; + $membershipTypeIds = $membershipTypes = $radio = array(); + $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember) ? TRUE : FALSE; + + $allowAutoRenewMembership = $autoRenewOption = FALSE; + $autoRenewMembershipTypeOptions = array(); + + $paymentProcessor = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1'); + + $separateMembershipPayment = CRM_Utils_Array::value('is_separate_payment', $membershipBlock); + + if ($membershipPriceset) { + foreach ($this->_priceSet['fields'] as $pField) { + if (empty($pField['options'])) { + continue; + } + foreach ($pField['options'] as $opId => $opValues) { + if (empty($opValues['membership_type_id'])) { + continue; + } + $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id']; + } + } + } + elseif (!empty($membershipBlock['membership_types'])) { + $membershipTypeIds = explode(',', $membershipBlock['membership_types']); + } + + if (!empty($membershipTypeIds)) { + //set status message if wrong membershipType is included in membershipBlock + if (isset($this->_mid) && !$membershipPriceset) { + $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', + $this->_mid, + 'membership_type_id' + ); + if (!in_array($membershipTypeID, $membershipTypeIds)) { + CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Invalid Membership'), 'error'); + } + } + + $membershipTypeValues = self::buildMembershipTypeValues($this, $membershipTypeIds); + $this->_membershipTypeValues = $membershipTypeValues; + $endDate = NULL; + foreach ($membershipTypeIds as $value) { + $memType = $membershipTypeValues[$value]; + if ($selectedMembershipTypeID != NULL) { + if ($memType['id'] == $selectedMembershipTypeID) { + $this->assign('minimum_fee', + CRM_Utils_Array::value('minimum_fee', $memType) + ); + $this->assign('membership_name', $memType['name']); + if (!$thankPage && $cid) { + $membership = new CRM_Member_DAO_Membership(); + $membership->contact_id = $cid; + $membership->membership_type_id = $memType['id']; + if ($membership->find(TRUE)) { + $this->assign('renewal_mode', TRUE); + $memType['current_membership'] = $membership->end_date; + $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; + } + } + $membershipTypes[] = $memType; + } + } + elseif ($memType['is_active']) { + $javascriptMethod = NULL; + $allowAutoRenewOpt = 1; + if (is_array($this->_paymentProcessors)) { + foreach ($this->_paymentProcessors as $id => $val) { + if (!$val['is_recur']) { + $allowAutoRenewOpt = 0; + continue; + } + } + } + + $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );"); + $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array::value($value, CRM_Utils_Array::value('auto_renew', $this->_membershipBlock));; + + if ($allowAutoRenewOpt) { + $allowAutoRenewMembership = TRUE; + } + + //add membership type. + $radio[$memType['id']] = $this->createElement('radio', NULL, NULL, NULL, + $memType['id'], $javascriptMethod + ); + if ($cid) { + $membership = new CRM_Member_DAO_Membership(); + $membership->contact_id = $cid; + $membership->membership_type_id = $memType['id']; + + //show current membership, skip pending and cancelled membership records, + //because we take first membership record id for renewal + $membership->whereAdd('status_id != 5 AND status_id !=6'); + + if (!is_null($isTest)) { + $membership->is_test = $isTest; + } + + //CRM-4297 + $membership->orderBy('end_date DESC'); + + if ($membership->find(TRUE)) { + if (!$membership->end_date) { + unset($radio[$memType['id']]); + $this->assign('islifetime', TRUE); + continue; + } + $this->assign('renewal_mode', TRUE); + $this->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; + $memType['current_membership'] = $membership->end_date; + if (!$endDate) { + $endDate = $memType['current_membership']; + $this->_defaultMemTypeId = $memType['id']; + } + if ($memType['current_membership'] < $endDate) { + $endDate = $memType['current_membership']; + $this->_defaultMemTypeId = $memType['id']; + } + } + } + $membershipTypes[] = $memType; + } + } + } + + $this->assign('showRadio', $formItems); + if ($formItems) { + if (!$membershipPriceset) { + if (!$membershipBlock['is_required']) { + $this->assign('showRadioNoThanks', TRUE); + $radio[''] = $this->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL); + $this->addGroup($radio, 'selectMembership', NULL); + } + elseif ($membershipBlock['is_required'] && count($radio) == 1) { + $temp = array_keys($radio); + $this->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership')); + $this->assign('singleMembership', TRUE); + $this->assign('showRadio', FALSE); + } + else { + $this->addGroup($radio, 'selectMembership', NULL); + } + + $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required'); + } + else { + $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($this->_priceSetId); + $this->assign('autoRenewOption', $autoRenewOption); + } + + if (!$this->_values['is_pay_later'] && is_array($this->_paymentProcessors) && ($allowAutoRenewMembership || $autoRenewOption)) { + $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.')); + } + + } + + $this->assign('membershipBlock', $membershipBlock); + $this->assign('membershipTypes', $membershipTypes); + $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership); + $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions)); + + //give preference to user submitted auto_renew value. + $takeUserSubmittedAutoRenew = (!empty($_POST) || $this->isSubmitted()) ? TRUE : FALSE; + $this->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew); + } + + return $separateMembershipPayment; + } + + } diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 38204edb3a..c8aae927f7 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -82,7 +82,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * @var array * An array of payment processor details with objects loaded in the 'object' field. */ - public $_paymentProcessors; + protected $_paymentProcessors; /** * Available payment processors (IDS). @@ -689,6 +689,45 @@ class CRM_Core_Form extends HTML_QuickForm_Page { } } + /** + * Handle Payment Processor switching for contribution and event registration forms. + * + * This function is shared between contribution & event forms & this is their common class. + * + * However, this should be seen as an in-progress refactor, the end goal being to also align the + * backoffice forms that action payments. + * + * This function overlaps assignPaymentProcessor, in a bad way. + */ + protected function preProcessPaymentOptions() { + $this->_paymentProcessorID = NULL; + if ($this->_paymentProcessors) { + if (!empty($this->_submitValues)) { + $this->_paymentProcessorID = CRM_Utils_Array::value('payment_processor_id', $this->_submitValues); + $this->_paymentProcessor = CRM_Utils_Array::value($this->_paymentProcessorID, $this->_paymentProcessors); + $this->set('type', $this->_paymentProcessorID); + $this->set('mode', $this->_mode); + $this->set('paymentProcessor', $this->_paymentProcessor); + } + // Set default payment processor + else { + foreach ($this->_paymentProcessors as $values) { + if (!empty($values['is_default']) || count($this->_paymentProcessors) == 1) { + $this->_paymentProcessorID = $values['id']; + break; + } + } + } + if ($this->_paymentProcessorID) { + CRM_Core_Payment_ProcessorForm::preProcess($this); + } + else { + $this->_paymentProcessor = array(); + } + CRM_Financial_Form_Payment::addCreditCardJs(); + } + $this->assign('paymentProcessorID', $this->_paymentProcessorID); + } /** * Setter function for options. diff --git a/CRM/Event/Form/Registration/Register.php b/CRM/Event/Form/Registration/Register.php index 56b2342211..94450cce6e 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -97,7 +97,8 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { // We hide the payment fields if the event is full or requires approval, // and the current user has not yet been approved CRM-12279 $this->_noFees = (($eventFull || $this->_requireApproval) && !$this->_allowConfirmation); - CRM_Contribute_Form_Contribution_Main::preProcessPaymentOptions($this, $this->_noFees); + $this->_paymentProcessors = $this->_noFees ? array() : $this->get('paymentProcessors'); + $this->preProcessPaymentOptions(); if ($this->_snippet) { return; } diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index f7679655c1..d5f82230a1 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -727,210 +727,6 @@ INNER JOIN civicrm_membership_type type ON ( type.id = membership.membership_ty return NULL; } - /** - * Build Membership Block in Contribution Pages. - * - * @param CRM_Core_Form $form - * Form object. - * @param int $pageID - * Unused?. - * @param int $cid - * Contact checked for having a current membership for a particular membership. - * @param bool $formItems - * @param int $selectedMembershipTypeID - * Selected membership id. - * @param bool $thankPage - * Thank you page. - * @param null $isTest - * - * @return bool - * Is this a separate membership payment - */ - public static function buildMembershipBlock( - &$form, - $pageID, - $cid, - $formItems = FALSE, - $selectedMembershipTypeID = NULL, - $thankPage = FALSE, - $isTest = NULL - ) { - - $separateMembershipPayment = FALSE; - if ($form->_membershipBlock) { - $form->_currentMemberships = array(); - - $membershipBlock = $form->_membershipBlock; - $membershipTypeIds = $membershipTypes = $radio = array(); - $membershipPriceset = (!empty($form->_priceSetId) && $form->_useForMember) ? TRUE : FALSE; - - $allowAutoRenewMembership = $autoRenewOption = FALSE; - $autoRenewMembershipTypeOptions = array(); - - $paymentProcessor = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1'); - - $separateMembershipPayment = CRM_Utils_Array::value('is_separate_payment', $membershipBlock); - - if ($membershipPriceset) { - foreach ($form->_priceSet['fields'] as $pField) { - if (empty($pField['options'])) { - continue; - } - foreach ($pField['options'] as $opId => $opValues) { - if (empty($opValues['membership_type_id'])) { - continue; - } - $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id']; - } - } - } - elseif (!empty($membershipBlock['membership_types'])) { - $membershipTypeIds = explode(',', $membershipBlock['membership_types']); - } - - if (!empty($membershipTypeIds)) { - //set status message if wrong membershipType is included in membershipBlock - if (isset($form->_mid) && !$membershipPriceset) { - $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', - $form->_mid, - 'membership_type_id' - ); - if (!in_array($membershipTypeID, $membershipTypeIds)) { - CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Invalid Membership'), 'error'); - } - } - - $membershipTypeValues = self::buildMembershipTypeValues($form, $membershipTypeIds); - $form->_membershipTypeValues = $membershipTypeValues; - $endDate = NULL; - foreach ($membershipTypeIds as $value) { - $memType = $membershipTypeValues[$value]; - if ($selectedMembershipTypeID != NULL) { - if ($memType['id'] == $selectedMembershipTypeID) { - $form->assign('minimum_fee', - CRM_Utils_Array::value('minimum_fee', $memType) - ); - $form->assign('membership_name', $memType['name']); - if (!$thankPage && $cid) { - $membership = new CRM_Member_DAO_Membership(); - $membership->contact_id = $cid; - $membership->membership_type_id = $memType['id']; - if ($membership->find(TRUE)) { - $form->assign('renewal_mode', TRUE); - $memType['current_membership'] = $membership->end_date; - $form->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; - } - } - $membershipTypes[] = $memType; - } - } - elseif ($memType['is_active']) { - $javascriptMethod = NULL; - $allowAutoRenewOpt = 1; - if (is_array($form->_paymentProcessors)) { - foreach ($form->_paymentProcessors as $id => $val) { - if (!$val['is_recur']) { - $allowAutoRenewOpt = 0; - continue; - } - } - } - - $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );"); - $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array::value($value, CRM_Utils_Array::value('auto_renew', $form->_membershipBlock));; - - if ($allowAutoRenewOpt) { - $allowAutoRenewMembership = TRUE; - } - - //add membership type. - $radio[$memType['id']] = $form->createElement('radio', NULL, NULL, NULL, - $memType['id'], $javascriptMethod - ); - if ($cid) { - $membership = new CRM_Member_DAO_Membership(); - $membership->contact_id = $cid; - $membership->membership_type_id = $memType['id']; - - //show current membership, skip pending and cancelled membership records, - //because we take first membership record id for renewal - $membership->whereAdd('status_id != 5 AND status_id !=6'); - - if (!is_null($isTest)) { - $membership->is_test = $isTest; - } - - //CRM-4297 - $membership->orderBy('end_date DESC'); - - if ($membership->find(TRUE)) { - if (!$membership->end_date) { - unset($radio[$memType['id']]); - $form->assign('islifetime', TRUE); - continue; - } - $form->assign('renewal_mode', TRUE); - $form->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; - $memType['current_membership'] = $membership->end_date; - if (!$endDate) { - $endDate = $memType['current_membership']; - $form->_defaultMemTypeId = $memType['id']; - } - if ($memType['current_membership'] < $endDate) { - $endDate = $memType['current_membership']; - $form->_defaultMemTypeId = $memType['id']; - } - } - } - $membershipTypes[] = $memType; - } - } - } - - $form->assign('showRadio', $formItems); - if ($formItems) { - if (!$membershipPriceset) { - if (!$membershipBlock['is_required']) { - $form->assign('showRadioNoThanks', TRUE); - $radio[''] = $form->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL); - $form->addGroup($radio, 'selectMembership', NULL); - } - elseif ($membershipBlock['is_required'] && count($radio) == 1) { - $temp = array_keys($radio); - $form->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership')); - $form->assign('singleMembership', TRUE); - $form->assign('showRadio', FALSE); - } - else { - $form->addGroup($radio, 'selectMembership', NULL); - } - - $form->addRule('selectMembership', ts('Please select one of the memberships.'), 'required'); - } - else { - $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($form->_priceSetId); - $form->assign('autoRenewOption', $autoRenewOption); - } - - if (!$form->_values['is_pay_later'] && is_array($form->_paymentProcessors) && ($allowAutoRenewMembership || $autoRenewOption)) { - $form->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.')); - } - - } - - $form->assign('membershipBlock', $membershipBlock); - $form->assign('membershipTypes', $membershipTypes); - $form->assign('allowAutoRenewMembership', $allowAutoRenewMembership); - $form->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions)); - - //give preference to user submitted auto_renew value. - $takeUserSubmittedAutoRenew = (!empty($_POST) || $form->isSubmitted()) ? TRUE : FALSE; - $form->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew); - } - - return $separateMembershipPayment; - } - /** * Return Membership Block info in Contribution Pages. * -- 2.25.1