CRM-16867 fix membership-renew check to get membership types from price fields
authorEileen McNaughton <eileen@fuzion.co.nz>
Sun, 19 Jul 2015 09:31:15 +0000 (21:31 +1200)
committerEileen McNaughton <eileen@fuzion.co.nz>
Sun, 19 Jul 2015 09:49:04 +0000 (21:49 +1200)
CRM/Contribute/BAO/ContributionPage.php
CRM/Contribute/Form/ContributionPage/Amount.php
CRM/Contribute/Form/ContributionPage/Custom.php
CRM/Price/BAO/PriceSet.php

index 3c0a4efd3447783be9993db7e3cb7c25ddb47750..96caefc1f0b858be68ceb5663fdaf87f6ea27df8 100644 (file)
  */
 
 /**
- *
  * @package CRM
  * @copyright CiviCRM LLC (c) 2004-2015
- * $Id$
- *
  */
 
 /**
index 5c40c35de84bc488d9f9e24eae51615f8b24ac42..6830b6899a59dbc92b3bc66f23f753655b754185 100644 (file)
@@ -299,12 +299,20 @@ SELECT id
     $membershipBlock->entity_id = $self->_id;
     $membershipBlock->is_active = 1;
     $hasMembershipBlk = FALSE;
+
     if ($membershipBlock->find(TRUE)) {
+      $setID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $self->_id, NULL, NULL);
       if (!empty($fields['amount_block_is_active']) &&
-        ($setID = CRM_Price_BAO_PriceSet::getFor('civicrm_contribution_page', $self->_id, NULL, 1))
+        // I don't think setID could ever not be set.
+        ($setID)
       ) {
-        $extends = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $setID, 'extends');
-        if ($extends && $extends == CRM_Core_Component::getComponentID('CiviMember')) {
+        // If we are using quick config for the contribution part we need quick config for the membership part.
+        // Note that having 2 price sets against a page may allow our code to be more sensible when managing separate payments.
+        // The quick config is intended to make it possible not to force people to use price sets until the price set UI
+        // is easier to use, but should have no effect on form processing but the separate membership payment complicates this
+        // as there is no linking of the contribution price set to the page.
+        $priceSet = civicrm_api3('PriceSet', 'getsingle', array('id' => $setID));
+        if (empty($priceSet['is_quick_config']) && CRM_Utils_Array::value('extends', $priceSet) == CRM_Core_Component::getComponentID('CiviMember')) {
           $errors['amount_block_is_active'] = ts('You cannot use a Membership Price Set when the Contribution Amounts section is enabled. Click the Memberships tab above, and select your Membership Price Set on that form. Membership Price Sets may include additional fields for non-membership options that require an additional fee (e.g. magazine subscription) or an additional voluntary contribution.');
           return $errors;
         }
@@ -314,13 +322,13 @@ SELECT id
         $errors['amount_block_is_active'] = ts('To disable Contribution Amounts section you need to first disable Separate Membership Payment option from Membership Settings.');
       }
 
-      //CRM-16165, Don't allow reccuring contribution if membership block contain any renewable membership option
-      $membershipTypes = unserialize($membershipBlock->membership_types);
-      if (!empty($fields['is_recur']) && !empty($membershipTypes)) {
+      //CRM-16165, Don't allow recurring contribution if membership block contain any renewable membership option
+      $membershipTypes = CRM_Price_BAO_PriceSet::getMembershipTypesFromPriceSet($setID);
+      if (!empty($fields['is_recur']) && !empty($membershipTypes['autorenew'])) {
         if (!$membershipBlock->is_separate_payment) {
           $errors['is_recur'] = ts('You need to enable Separate Membership Payment when online contribution page is configured for both Membership and Recurring Contribution.');
         }
-        elseif (count(array_filter($membershipTypes)) != 0) {
+        else {
           $errors['is_recur'] = ts('You cannot enable both Recurring Contributions and Auto-renew memberships on the same online contribution page.');
         }
       }
index ea730d09f261adb10463eee9e91ff8f44b15c584..41af24bfe06f90eb6d6b2cd38fd293d1a29036d7 100644 (file)
  */
 
 /**
- *
  * @package CRM
  * @copyright CiviCRM LLC (c) 2004-2015
- * $Id$
- *
  */
 
 /**
- * form to process actions on the group aspect of Custom Data
+ * Form to process actions on the group aspect of Custom Data.
  */
 class CRM_Contribute_Form_ContributionPage_Custom extends CRM_Contribute_Form_ContributionPage {
 
index e79e9bdad5e386ba7eef95d8abae63188ce65dec..3d563f77bd20584a14b5ecf8a7c4d269de1d6ba4 100644 (file)
@@ -1382,6 +1382,54 @@ WHERE       ps.id = %1
     return FALSE;
   }
 
+  /**
+   * Get an array of the membership types in a price set.
+   *
+   * @param int $id
+   *
+   * @return array(
+   *  Membership types in the price set
+   */
+  static function getMembershipTypesFromPriceSet($id) {
+    $query
+      = "SELECT      pfv.id, pfv.price_field_id, pfv.name, pfv.membership_type_id, pf.html_type, mt.auto_renew
+FROM        civicrm_price_field_value pfv
+LEFT JOIN   civicrm_price_field pf ON pf.id = pfv.price_field_id
+LEFT JOIN   civicrm_price_set ps ON ps.id = pf.price_set_id
+LEFT JOIN   civicrm_membership_type mt ON mt.id = pfv.membership_type_id
+WHERE       ps.id = %1
+";
+
+    $params = array(1 => array($id, 'Integer'));
+    $dao = CRM_Core_DAO::executeQuery($query, $params);
+
+    $membershipTypes = array(
+      'all' => array(),
+      'autorenew' => array(),
+      'autorenew_required' => array(),
+      'autorenew_optional' => array(),
+    );
+    while ($dao->fetch()) {
+      if (empty($dao->membership_type_id)) {
+        continue;
+      }
+      $membershipTypes['all'][] = $dao->membership_type_id;
+      if (!empty($dao->auto_renew)) {
+        $membershipTypes['autorenew'][] = $dao->membership_type_id;
+        if ($dao->auto_renew == 2) {
+          $membershipTypes['autorenew_required'][] = $dao->membership_type_id;
+        }
+        else {
+          $membershipTypes['autorenew_optional'][] = $dao->membership_type_id;
+        }
+      }
+      else {
+        $membershipTypes['non_renew'][] = $dao->membership_type_id;
+      }
+    }
+    return $membershipTypes;
+  }
+
   /**
    * Copy priceSet when event/contibution page is copied
    *