Initial simplification of buildMembershipBlock
authorMatthew Wire <mjw@mjwconsult.co.uk>
Sat, 30 Jan 2021 14:20:44 +0000 (14:20 +0000)
committerMatthew Wire <mjw@mjwconsult.co.uk>
Sat, 30 Jan 2021 14:33:31 +0000 (14:33 +0000)
CRM/Contribute/Form/Contribution/Confirm.php
CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/Contribution/ThankYou.php

index 44048a1121fecb185933ea9dadf3b4337f72565b..b0cf2cf1a3b9bed64d54513edcbffede79c84985 100644 (file)
@@ -527,9 +527,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
       ) {
         $this->buildMembershipBlock(
           $this->_membershipContactID,
-          FALSE,
-          $params['selectMembership'],
-          FALSE
+          $params['selectMembership']
         );
         if (!empty($params['auto_renew'])) {
           $this->assign('auto_renew', TRUE);
@@ -659,13 +657,8 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
    *
    * @param int $cid
    *   Contact checked for having a current membership for a particular membership.
-   * @param bool $isContributionMainPage
-   *   Is this the main page? If so add form input fields.
-   *   (or better yet don't have this functionality in a function shared with forms that don't share it).
    * @param int|array $selectedMembershipTypeID
    *   Selected membership id.
-   * @param bool $thankPage
-   *   Thank you page.
    * @param null $isTest
    *
    * @return bool
@@ -674,14 +667,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
    * @throws \CiviCRM_API3_Exception
    * @throws \CRM_Core_Exception
    */
-  protected function buildMembershipBlock(
-    $cid,
-    $isContributionMainPage = FALSE,
-    $selectedMembershipTypeID = NULL,
-    $thankPage = FALSE,
-    $isTest = NULL
-  ) {
-
+  private function buildMembershipBlock($cid, $selectedMembershipTypeID = NULL, $isTest = NULL) {
     $separateMembershipPayment = FALSE;
     if ($this->_membershipBlock) {
       $this->_currentMemberships = [];
@@ -744,7 +730,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
             if ($memType['id'] == $selectedMembershipTypeID) {
               $this->assign('minimum_fee', $memType['minimum_fee'] ?? NULL);
               $this->assign('membership_name', $memType['name']);
-              if (!$thankPage && $cid) {
+              if ($cid) {
                 $membership = new CRM_Member_DAO_Membership();
                 $membership->contact_id = $cid;
                 $membership->membership_type_id = $memType['id'];
@@ -815,7 +801,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
       }
 
       $this->assign('membershipBlock', $this->_membershipBlock);
-      $this->assign('showRadio', $isContributionMainPage);
+      $this->assign('showRadio', FALSE);
       $this->assign('membershipTypes', $membershipTypes);
       $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
       $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
@@ -833,40 +819,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr
       else {
         $this->assign('autoRenewOption', $autoRenewOption);
       }
-
-      if ($isContributionMainPage) {
-        if (!$membershipPriceset) {
-          if (!$this->_membershipBlock['is_required']) {
-            $this->assign('showRadioNoThanks', TRUE);
-            $radio['no_thanks'] = NULL;
-            $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs);
-          }
-          elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) {
-            $temp = array_keys($radio);
-            $this->add('hidden', 'selectMembership', $temp[0], ['id' => 'selectMembership']);
-            $this->assign('singleMembership', TRUE);
-            $this->assign('showRadio', FALSE);
-          }
-          else {
-            foreach ($radioOptAttrs as $opt => $attrs) {
-              $attrs['class'] = ' required';
-            }
-            $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs);
-          }
-
-          $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
-        }
-
-        if ((!$this->_values['is_pay_later'] || is_array($this->_paymentProcessors)) && ($allowAutoRenewMembership || $autoRenewOption)) {
-          if ($autoRenewOption == 2) {
-            $this->addElement('hidden', 'auto_renew', ts('Please renew my membership automatically.'));
-          }
-          else {
-            $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
-          }
-        }
-
-      }
     }
 
     return $separateMembershipPayment;
index d6cada6b0405e31fb26f838a5ceb656b9307b8b6..223d2c7805dec9bba81f725a05601d0d9b6aff6d 100644 (file)
@@ -361,7 +361,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
 
         $this->_separateMembershipPayment = $this->buildMembershipBlock(
           $this->_membershipContactID,
-          TRUE, NULL, FALSE,
+          NULL,
           $isTest
         );
       }
@@ -511,13 +511,8 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
    *
    * @param int $cid
    *   Contact checked for having a current membership for a particular membership.
-   * @param bool $isContributionMainPage
-   *   Is this the main page? If so add form input fields.
-   *   (or better yet don't have this functionality in a function shared with forms that don't share it).
    * @param int|array $selectedMembershipTypeID
    *   Selected membership id.
-   * @param bool $thankPage
-   *   Thank you page.
    * @param null $isTest
    *
    * @return bool
@@ -526,14 +521,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
    * @throws \CiviCRM_API3_Exception
    * @throws \CRM_Core_Exception
    */
-  protected function buildMembershipBlock(
-    $cid,
-    $isContributionMainPage = FALSE,
-    $selectedMembershipTypeID = NULL,
-    $thankPage = FALSE,
-    $isTest = NULL
-  ) {
-
+  private function buildMembershipBlock($cid, $selectedMembershipTypeID = NULL, $isTest = NULL) {
     $separateMembershipPayment = FALSE;
     if ($this->_membershipBlock) {
       $this->_currentMemberships = [];
@@ -596,7 +584,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
             if ($memType['id'] == $selectedMembershipTypeID) {
               $this->assign('minimum_fee', $memType['minimum_fee'] ?? NULL);
               $this->assign('membership_name', $memType['name']);
-              if (!$thankPage && $cid) {
+              if ($cid) {
                 $membership = new CRM_Member_DAO_Membership();
                 $membership->contact_id = $cid;
                 $membership->membership_type_id = $memType['id'];
@@ -667,7 +655,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       }
 
       $this->assign('membershipBlock', $this->_membershipBlock);
-      $this->assign('showRadio', $isContributionMainPage);
+      $this->assign('showRadio', TRUE);
       $this->assign('membershipTypes', $membershipTypes);
       $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
       $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
@@ -686,39 +674,37 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
         $this->assign('autoRenewOption', $autoRenewOption);
       }
 
-      if ($isContributionMainPage) {
-        if (!$membershipPriceset) {
-          if (!$this->_membershipBlock['is_required']) {
-            $this->assign('showRadioNoThanks', TRUE);
-            $radio['no_thanks'] = NULL;
-            $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs);
-          }
-          elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) {
-            $temp = array_keys($radio);
-            $this->add('hidden', 'selectMembership', $temp[0], ['id' => 'selectMembership']);
-            $this->assign('singleMembership', TRUE);
-            $this->assign('showRadio', FALSE);
-          }
-          else {
-            foreach ($radioOptAttrs as $opt => $attrs) {
-              $attrs['class'] = ' required';
-            }
-            $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs);
-          }
-
-          $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
+      if (!$membershipPriceset) {
+        if (!$this->_membershipBlock['is_required']) {
+          $this->assign('showRadioNoThanks', TRUE);
+          $radio['no_thanks'] = NULL;
+          $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs);
         }
-
-        if ((!$this->_values['is_pay_later'] || is_array($this->_paymentProcessors)) && ($allowAutoRenewMembership || $autoRenewOption)) {
-          if ($autoRenewOption == 2) {
-            $this->addElement('hidden', 'auto_renew', ts('Please renew my membership automatically.'));
-          }
-          else {
-            $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
+        elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) {
+          $temp = array_keys($radio);
+          $this->add('hidden', 'selectMembership', $temp[0], ['id' => 'selectMembership']);
+          $this->assign('singleMembership', TRUE);
+          $this->assign('showRadio', FALSE);
+        }
+        else {
+          foreach ($radioOptAttrs as $opt => $attrs) {
+            $attrs['class'] = ' required';
           }
+          $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs);
         }
 
+        $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
       }
+
+      if ((!$this->_values['is_pay_later'] || is_array($this->_paymentProcessors)) && ($allowAutoRenewMembership || $autoRenewOption)) {
+        if ($autoRenewOption == 2) {
+          $this->addElement('hidden', 'auto_renew', ts('Please renew my membership automatically.'));
+        }
+        else {
+          $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
+        }
+      }
+
     }
 
     return $separateMembershipPayment;
index 8702d61d2298fc29343958addb5c082c611ee29e..172cb1fb537d2b521ed05db2932a29f54a114c15 100644 (file)
@@ -167,9 +167,7 @@ class CRM_Contribute_Form_Contribution_ThankYou extends CRM_Contribute_Form_Cont
 
       $this->buildMembershipBlock(
         $this->_membershipContactID,
-        FALSE,
         $membershipTypeID,
-        TRUE,
         NULL
       );
 
@@ -294,13 +292,8 @@ class CRM_Contribute_Form_Contribution_ThankYou extends CRM_Contribute_Form_Cont
    *
    * @param int $cid
    *   Contact checked for having a current membership for a particular membership.
-   * @param bool $isContributionMainPage
-   *   Is this the main page? If so add form input fields.
-   *   (or better yet don't have this functionality in a function shared with forms that don't share it).
    * @param int|array $selectedMembershipTypeID
    *   Selected membership id.
-   * @param bool $thankPage
-   *   Thank you page.
    * @param null $isTest
    *
    * @return bool
@@ -309,14 +302,7 @@ class CRM_Contribute_Form_Contribution_ThankYou extends CRM_Contribute_Form_Cont
    * @throws \CiviCRM_API3_Exception
    * @throws \CRM_Core_Exception
    */
-  protected function buildMembershipBlock(
-    $cid,
-    $isContributionMainPage = FALSE,
-    $selectedMembershipTypeID = NULL,
-    $thankPage = FALSE,
-    $isTest = NULL
-  ) {
-
+  private function buildMembershipBlock($cid, $selectedMembershipTypeID = NULL, $isTest = NULL) {
     $separateMembershipPayment = FALSE;
     if ($this->_membershipBlock) {
       $this->_currentMemberships = [];
@@ -379,16 +365,6 @@ class CRM_Contribute_Form_Contribution_ThankYou extends CRM_Contribute_Form_Cont
             if ($memType['id'] == $selectedMembershipTypeID) {
               $this->assign('minimum_fee', $memType['minimum_fee'] ?? NULL);
               $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;
             }
           }
@@ -450,7 +426,7 @@ class CRM_Contribute_Form_Contribution_ThankYou extends CRM_Contribute_Form_Cont
       }
 
       $this->assign('membershipBlock', $this->_membershipBlock);
-      $this->assign('showRadio', $isContributionMainPage);
+      $this->assign('showRadio', FALSE);
       $this->assign('membershipTypes', $membershipTypes);
       $this->assign('allowAutoRenewMembership', $allowAutoRenewMembership);
       $this->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions));
@@ -468,40 +444,6 @@ class CRM_Contribute_Form_Contribution_ThankYou extends CRM_Contribute_Form_Cont
       else {
         $this->assign('autoRenewOption', $autoRenewOption);
       }
-
-      if ($isContributionMainPage) {
-        if (!$membershipPriceset) {
-          if (!$this->_membershipBlock['is_required']) {
-            $this->assign('showRadioNoThanks', TRUE);
-            $radio['no_thanks'] = NULL;
-            $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs);
-          }
-          elseif ($this->_membershipBlock['is_required'] && count($radio) == 1) {
-            $temp = array_keys($radio);
-            $this->add('hidden', 'selectMembership', $temp[0], ['id' => 'selectMembership']);
-            $this->assign('singleMembership', TRUE);
-            $this->assign('showRadio', FALSE);
-          }
-          else {
-            foreach ($radioOptAttrs as $opt => $attrs) {
-              $attrs['class'] = ' required';
-            }
-            $this->addRadio('selectMembership', NULL, $radio, [], NULL, FALSE, $radioOptAttrs);
-          }
-
-          $this->addRule('selectMembership', ts('Please select one of the memberships.'), 'required');
-        }
-
-        if ((!$this->_values['is_pay_later'] || is_array($this->_paymentProcessors)) && ($allowAutoRenewMembership || $autoRenewOption)) {
-          if ($autoRenewOption == 2) {
-            $this->addElement('hidden', 'auto_renew', ts('Please renew my membership automatically.'));
-          }
-          else {
-            $this->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.'));
-          }
-        }
-
-      }
     }
 
     return $separateMembershipPayment;