Merge pull request #2062 from dlobo/CRM-13806
[civicrm-core.git] / CRM / Member / BAO / Membership.php
index 1972792e08641e183d182998afb5c37e7e4ad84a..5ba8d740fc1dea05300ad38f32a019a7cc9a7938 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -236,7 +236,7 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
     ) {
       $dates = array('start_date', 'end_date', 'join_date');
       foreach ($dates as $date) {
-        $$date = CRM_Utils_Date::processDate(CRM_Utils_Array::value($date, $params), NULL, TRUE, 'Ymd');
+        $$date = $params[$date] = CRM_Utils_Date::processDate(CRM_Utils_Array::value($date, $params), NULL, TRUE, 'Ymd');
       }
 
       //fix for CRM-3570, during import exclude the statuses those having is_admin = 1
@@ -1445,7 +1445,7 @@ AND civicrm_membership.is_test = %2";
         'legacy_redirect_path' => 'civicrm/contribute/transact',
         'legacy_redirect_query' => "_qf_Main_display=true&qfKey={$form->_params['qfKey']}",
       );
-      throw new CiviCRM_Exception($message, 0, $errorParams);
+      throw new CRM_Core_Exception($message, 0, $errorParams);
     }
 
     // CRM-7851
@@ -1487,6 +1487,10 @@ AND civicrm_membership.is_test = %2";
   }
 
   /**
+   * @todo - this form method needs to have the interaction with the form layer removed from it
+   * as a BAO function. Note that the api now supports membership renewals & it is not clear this function does anything
+   * not done by the membership.create api (with a lot less unit tests)
+   *
    * This method will renew / create the membership depending on
    * whether the given contact has a membership or not. And will add
    * the modified dates for membership and in the log table.
@@ -1526,6 +1530,8 @@ AND civicrm_membership.is_test = %2";
 
     // check is it pending. - CRM-4555
     $pending = FALSE;
+    //@todo this is a BAO function & should not inspect the form - the form should do this
+    // & pass required params to the BAO
     if (CRM_Utils_Array::value('minimum_fee', $membershipTypeDetails) > 0.0) {
       if (((isset($form->_contributeMode) && $form->_contributeMode == 'notify') ||
           CRM_Utils_Array::value('is_pay_later', $form->_params) ||
@@ -2458,6 +2464,7 @@ INNER JOIN  civicrm_contact contact ON ( contact.id = membership.contact_id AND
     $allTypes      = CRM_Member_PseudoConstant::membershipType();
     $contribStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
 
+    // get only memberships with active membership types
     $query = "
 SELECT     civicrm_membership.id                    as membership_id,
            civicrm_membership.is_override           as is_override,
@@ -2473,6 +2480,8 @@ SELECT     civicrm_membership.id                    as membership_id,
            civicrm_membership.contribution_recur_id as recur_id
 FROM       civicrm_membership
 INNER JOIN civicrm_contact ON ( civicrm_membership.contact_id = civicrm_contact.id )
+INNER JOIN civicrm_membership_type ON
+  (civicrm_membership.membership_type_id = civicrm_membership_type.id AND civicrm_membership_type.is_active = 1)
 WHERE      civicrm_membership.is_test = 0";
 
     $params = array();
@@ -2488,20 +2497,6 @@ WHERE      civicrm_membership.is_test = 0";
       // echo ".";
       $processCount++;
 
-      /**
-       $count++;
-       echo $dao->contact_id . ', '. CRM_Utils_System::memory( ) . "<p>\n";
-
-       CRM_Core_Error::debug( 'fBegin', count( $GLOBALS['_DB_DATAOBJECT']['RESULTS'] ) );
-       if ( $count > 2 ) {
-       foreach ( $GLOBALS['_DB_DATAOBJECT']['RESULTS'] as $r ) {
-       CRM_Core_Error::debug( 'r', $r->query );
-       }
-       // CRM_Core_Error::debug( 'f', $GLOBALS['_DB_DATAOBJECT']['RESULTS'] );
-       exit( );
-       }
-       **/
-
       // Put common parameters into array for easy access
       $memberParams = array(
         'id' => $dao->membership_id,