diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index cddcc984fcb0..f46bfd170b34 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -333,8 +333,7 @@ public function buildQuickForm() { $getOnlyPriceSetElements = FALSE; } - $this->set('priceSetId', $this->_priceSetId); - CRM_Price_BAO_PriceSet::buildPriceSet($this, 'membership', FALSE); + $this->buildMembershipPriceSet(); $optionsMembershipTypes = []; foreach ($this->_priceSet['fields'] as $pField) { @@ -576,6 +575,80 @@ public function buildQuickForm() { parent::buildQuickForm(); } + /** + * Build the price set form. + * + * @return void + * + * @deprecated this should be updated to align with the other forms that use getOrder() + */ + private function buildMembershipPriceSet() { + $priceSetId = $this->getPriceSetID(); + $form = $this; + + $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, FALSE); + $form->_priceSet = $priceSet[$priceSetId] ?? NULL; + $validPriceFieldIds = array_keys($form->_priceSet['fields']); + + // Mark which field should have the auto-renew checkbox, if any. CRM-18305 + if (!empty($form->_membershipTypeValues) && is_array($form->_membershipTypeValues)) { + $autoRenewMembershipTypes = []; + foreach ($form->_membershipTypeValues as $membershipTypeValue) { + if ($membershipTypeValue['auto_renew']) { + $autoRenewMembershipTypes[] = $membershipTypeValue['id']; + } + } + foreach ($form->_priceSet['fields'] as $field) { + if (array_key_exists('options', $field) && is_array($field['options'])) { + foreach ($field['options'] as $option) { + if (!empty($option['membership_type_id'])) { + if (in_array($option['membership_type_id'], $autoRenewMembershipTypes)) { + $form->_priceSet['auto_renew_membership_field'] = $field['id']; + // Only one field can offer auto_renew memberships, so break here. + break; + } + } + } + } + } + } + $form->_priceSet['id'] ??= $priceSetId; + $form->assign('priceSet', $form->_priceSet); + + $feeBlock = &$form->_priceSet['fields']; + + // Call the buildAmount hook. + CRM_Utils_Hook::buildAmount('membership', $form, $feeBlock); + + // CRM-14492 Admin price fields should show up on event registration if user has 'administer CiviCRM' permissions + $adminFieldVisible = CRM_Core_Permission::check('administer CiviCRM'); + $checklifetime = FALSE; + $validFieldsOnly = FALSE; + foreach ($feeBlock as $id => $field) { + if (($field['visibility'] ?? NULL) == 'public' || + (($field['visibility'] ?? NULL) == 'admin' && $adminFieldVisible == TRUE) || + !$validFieldsOnly + ) { + $options = $field['options'] ?? NULL; + if (!is_array($options) || !in_array($id, $validPriceFieldIds)) { + continue; + } + if (!empty($options)) { + CRM_Price_BAO_PriceField::addQuickFormElement($form, + 'price_' . $field['id'], + $field['id'], + FALSE, + $field['is_required'] ?? FALSE, + NULL, + $options + ); + } + } + } + $form->assign('ispricelifetime', $checklifetime); + + } + /** * Validation. * diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index ffa995cb3780..b870325ca7cf 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -711,6 +711,7 @@ public static function getCachedPriceSetDetail(int $priceSetID): array { * @deprecated since 5.68. Will be removed around 5.80. */ public static function buildPriceSet(&$form, $component = NULL, $validFieldsOnly = TRUE) { + CRM_Core_Error::deprecatedWarning('internal function'); $priceSetId = $form->get('priceSetId'); if (!$priceSetId) { return;