From: Eileen McNaughton Date: Thu, 12 Oct 2023 22:14:51 +0000 (+1300) Subject: Move called functions with similar sharing issues into unshared code X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=6069c0d851aeb44fd6820125622737a621f39833;p=civicrm-core.git Move called functions with similar sharing issues into unshared code --- diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 7e6f3da3c5..07792e40d5 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -969,7 +969,34 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP // Call the buildAmount hook. CRM_Utils_Hook::buildAmount($component ?? 'contribution', $form, $feeBlock); - CRM_Price_BAO_PriceSet::addPriceFieldsToForm($form, $feeBlock, $validFieldsOnly, __CLASS__, $validPriceFieldIds); + $hideAdminValues = !CRM_Core_Permission::check('edit contributions'); + // 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; + 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, + CRM_Utils_Array::value('is_required', $field, FALSE), + NULL, + $options + ); + } + } + } + $form->assign('ispricelifetime', $checklifetime); } /** diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index b03a742bda..0e34054ace 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -514,7 +514,48 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu // Call the buildAmount hook. CRM_Utils_Hook::buildAmount($component ?? 'contribution', $form, $feeBlock); - CRM_Price_BAO_PriceSet::addPriceFieldsToForm($form, $feeBlock, $validFieldsOnly, __CLASS__, $validPriceFieldIds); + $hideAdminValues = !CRM_Core_Permission::check('edit contributions'); + // 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; + foreach ($feeBlock as $id => $field) { + if (($field['visibility'] ?? NULL) == 'public' || + (($field['visibility'] ?? NULL) == 'admin' && $adminFieldVisible == TRUE) || + !$validFieldsOnly + ) { + $options = $field['options'] ?? NULL; + $contactId = $form->getVar('_membershipContactID'); + if ($contactId && $options) { + $contactsLifetimeMemberships = CRM_Member_BAO_Membership::getAllContactMembership($contactId, FALSE, TRUE); + $contactsLifetimeMembershipTypes = array_column($contactsLifetimeMemberships, 'membership_type_id'); + $memTypeIdsInPriceField = array_column($options, 'membership_type_id'); + $isCurrentMember = (bool) array_intersect($memTypeIdsInPriceField, $contactsLifetimeMembershipTypes); + $checklifetime = $checklifetime ?: $isCurrentMember; + } + + if (!is_array($options) || !in_array($id, $validPriceFieldIds)) { + continue; + } + elseif ($hideAdminValues) { + foreach ($options as $key => $currentOption) { + if ($currentOption['visibility_id'] == CRM_Price_BAO_PriceField::getVisibilityOptionID('admin')) { + unset($options[$key]); + } + } + } + if (!empty($options)) { + CRM_Price_BAO_PriceField::addQuickFormElement($form, + 'price_' . $field['id'], + $field['id'], + FALSE, + CRM_Utils_Array::value('is_required', $field, FALSE), + NULL, + $options + ); + } + } + } + $form->assign('ispricelifetime', $checklifetime); } /** diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index 2d66eac6c8..d1d8409576 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -855,7 +855,11 @@ WHERE id = %1"; if ($className == 'CRM_Contribute_Form_Contribution_Main' && $component = 'membership') { $contactId = $form->getVar('_membershipContactID'); if ($contactId && $options) { - $checklifetime = $checklifetime ?: self::checkCurrentMembership($options, $contactId); + $contactsLifetimeMemberships = CRM_Member_BAO_Membership::getAllContactMembership($contactId, FALSE, TRUE); + $contactsLifetimeMembershipTypes = array_column($contactsLifetimeMemberships, 'membership_type_id'); + $memTypeIdsInPriceField = array_column($options, 'membership_type_id'); + $isCurrentMember = (bool) array_intersect($memTypeIdsInPriceField, $contactsLifetimeMembershipTypes); + $checklifetime = $checklifetime ?: $isCurrentMember; } } @@ -887,21 +891,6 @@ WHERE id = %1"; } - /** - * Check for lifetime membership types this contact has that are in this price field. - * - * @param array $options - * @param int $contactId - * - * @return bool - */ - private static function checkCurrentMembership(array $options, int $contactId) : bool { - $contactsLifetimeMemberships = CRM_Member_BAO_Membership::getAllContactMembership($contactId, FALSE, TRUE); - $contactsLifetimeMembershipTypes = array_column($contactsLifetimeMemberships, 'membership_type_id'); - $memTypeIdsInPriceField = array_column($options, 'membership_type_id'); - return (bool) array_intersect($memTypeIdsInPriceField, $contactsLifetimeMembershipTypes); - } - /** * Set daefult the price set fields. * @@ -1657,65 +1646,6 @@ WHERE ct.id = cp.financial_type_id AND return [$params, $lineItem]; } - /** - * Add the relevant price fields to the form. - * - * DO NOT use form outside core tested code - function is public for - * refactoring purposes only. - * - * @internal - * - * @param \CRM_Core_Form $form - * @param array $feeBlock - * @param bool $validFieldsOnly - * @param string $className - * @param array $validPriceFieldIds - */ - public static function addPriceFieldsToForm(CRM_Core_Form $form, $feeBlock, bool $validFieldsOnly, string $className, array $validPriceFieldIds) { - $hideAdminValues = !CRM_Core_Permission::check('edit contributions'); - // 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; - foreach ($feeBlock as $id => $field) { - if (($field['visibility'] ?? NULL) == 'public' || - (($field['visibility'] ?? NULL) == 'admin' && $adminFieldVisible == TRUE) || - !$validFieldsOnly - ) { - $options = $field['options'] ?? NULL; - if ($className == 'CRM_Contribute_Form_Contribution_Main' && $component = 'membership') { - $contactId = $form->getVar('_membershipContactID'); - if ($contactId && $options) { - $checklifetime = $checklifetime ?: self::checkCurrentMembership($options, $contactId); - } - } - - $formClasses = ['CRM_Contribute_Form_Contribution', 'CRM_Member_Form_Membership']; - - if (!is_array($options) || !in_array($id, $validPriceFieldIds)) { - continue; - } - elseif ($hideAdminValues && !in_array($className, $formClasses)) { - foreach ($options as $key => $currentOption) { - if ($currentOption['visibility_id'] == CRM_Price_BAO_PriceField::getVisibilityOptionID('admin')) { - unset($options[$key]); - } - } - } - if (!empty($options)) { - CRM_Price_BAO_PriceField::addQuickFormElement($form, - 'price_' . $field['id'], - $field['id'], - FALSE, - CRM_Utils_Array::value('is_required', $field, FALSE), - NULL, - $options - ); - } - } - } - $form->assign('ispricelifetime', $checklifetime); - } - /** * Pseudoconstant options for the `extends` field *