Move called functions with similar sharing issues into unshared code
authorEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 12 Oct 2023 22:14:51 +0000 (11:14 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 13 Oct 2023 00:52:10 +0000 (13:52 +1300)
CRM/Contribute/Form/Contribution.php
CRM/Contribute/Form/Contribution/Main.php
CRM/Price/BAO/PriceSet.php

index 7e6f3da3c5b853bc3630ccbc5691be16241b7d23..07792e40d597004973d476a9ff460a9785932851 100644 (file)
@@ -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);
   }
 
   /**
index b03a742bda38cf7d377f0fe76d5f54aa80951056..0e34054ace424736b2b602f3adced93ff1c9c31b 100644 (file)
@@ -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);
   }
 
   /**
index 2d66eac6c84d069e4375f58614ced7a19528791a..d1d8409576d96a43e2d78a05cf092e1a5ce6c4cc 100644 (file)
@@ -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
    *