Merge pull request #6454 from eileenmcnaughton/master
[civicrm-core.git] / CRM / Member / Form.php
index 1e873943e018f6567c1ef549eee9e8035aae1b5b..06a3c8f8c6046a6be543a708b5a07f62ad8a2acb 100644 (file)
@@ -58,6 +58,20 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment {
    */
   protected $_fromEmails = array();
 
+  /**
+   * Details of all enabled membership types.
+   *
+   * @var array
+   */
+  protected $allMembershipTypeDetails = array();
+
+  /**
+   * Array of membership type IDs and whether they permit autorenewal.
+   *
+   * @var array
+   */
+  protected $membershipTypeRenewalStatus = array();
+
   public function preProcess() {
     // Check for edit permission.
     if (!CRM_Core_Permission::checkActionPermission('CiviMember', $this->_action)) {
@@ -73,6 +87,13 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment {
 
     $this->assign('context', $this->_context);
     $this->assign('membershipMode', $this->_mode);
+    $this->allMembershipTypeDetails = CRM_Member_BAO_Membership::buildMembershipTypeValues($this, array(), TRUE);
+    foreach ($this->allMembershipTypeDetails as $index => $membershipType) {
+      if ($membershipType['auto_renew']) {
+        $this->_recurMembershipTypes[$index] = $membershipType;
+        $this->membershipTypeRenewalStatus[$index] = $membershipType['auto_renew'];
+      }
+    }
   }
 
   /**
@@ -122,56 +143,23 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment {
         $this->_processors, TRUE,
         array('onChange' => "buildAutoRenew( null, this.value );")
       );
-      CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE);
+      CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE);
     }
     // Build the form for auto renew. This is displayed when in credit card mode or update mode.
     // The reason for showing it in update mode is not that clear.
-    $autoRenew = array();
-    $recurProcessor = array();
     if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) {
-      if (!empty($recurProcessor)) {
-        $autoRenew = array();
-        if (!empty($membershipType)) {
-          $sql = '
-SELECT  id,
-        auto_renew,
-        duration_unit,
-        duration_interval
- FROM   civicrm_membership_type
-WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
-          $recurMembershipTypes = CRM_Core_DAO::executeQuery($sql);
-          while ($recurMembershipTypes->fetch()) {
-            $autoRenew[$recurMembershipTypes->id] = $recurMembershipTypes->auto_renew;
-            foreach (array(
-                       'id',
-                       'auto_renew',
-                       'duration_unit',
-                       'duration_interval',
-                     ) as $fld) {
-              $this->_recurMembershipTypes[$recurMembershipTypes->id][$fld] = $recurMembershipTypes->$fld;
-            }
-          }
-        }
-
-        if ($this->_mode) {
-          if (!empty($this->_recurPaymentProcessors)) {
-            $this->assign('allowAutoRenew', TRUE);
-          }
-        }
-
-        $this->assign('autoRenew', json_encode($autoRenew));
-        $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'),
-          NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );")
-        );
-        if ($this->_action & CRM_Core_Action::UPDATE) {
-          $autoRenewElement->freeze();
-        }
+      if (!empty($this->_recurPaymentProcessors)) {
+        $this->assign('allowAutoRenew', TRUE);
       }
 
-    }
-    $this->assign('recurProcessor', json_encode($recurProcessor));
+      $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'),
+        NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );")
+      );
+      if ($this->_action & CRM_Core_Action::UPDATE) {
+        $autoRenewElement->freeze();
+      }
 
-    if ($this->_mode || ($this->_action & CRM_Core_Action::UPDATE)) {
+      $this->assign('recurProcessor', json_encode($this->_recurPaymentProcessors));
       $this->addElement('checkbox',
         'auto_renew',
         ts('Membership renewed automatically'),
@@ -181,7 +169,7 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
 
       $this->assignPaymentRelatedVariables();
     }
-    $this->assign('autoRenewOptions', json_encode($autoRenew));
+    $this->assign('autoRenewOptions', json_encode($this->membershipTypeRenewalStatus));
 
     if ($this->_action & CRM_Core_Action::RENEW) {
       $this->addButtons(array(
@@ -300,4 +288,50 @@ WHERE   id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
     $this->_fromEmails = CRM_Core_BAO_Email::getFromEmail();
   }
 
+  /**
+   * Create a recurring contribution record.
+   *
+   * Recurring contribution parameters are set explicitly rather than merging paymentParams because it's hard
+   * to know the downstream impacts if we keep passing around the same array.
+   *
+   * @param $paymentParams
+   *
+   * @return array
+   * @throws \CiviCRM_API3_Exception
+   */
+  protected function processRecurringContribution($paymentParams) {
+    $membershipID = $paymentParams['membership_type_id'][1];
+    $contributionRecurParams = array(
+      'contact_id' => $paymentParams['contactID'],
+      'amount' => $paymentParams['total_amount'],
+      'payment_processor_id' => $paymentParams['payment_processor_id'],
+      'campaign_id' => CRM_Utils_Array::value('campaign_id', $paymentParams),
+      'financial_type_id' => $paymentParams['financial_type_id'],
+      'is_email_receipt' => CRM_Utils_Array::value('is_email_receipt', $paymentParams),
+      // This is not great as it could also be direct debit - but is consistent with elsewhere & all need fixing.
+      'payment_instrument_id' => 1,
+      'invoice_id' => CRM_Utils_Array::value('invoiceID ', $paymentParams),
+    );
+
+    $mapping = array(
+      'frequency_interval' => 'duration_interval',
+      'frequency_unit' => 'duration_unit',
+    );
+    $membershipType = civicrm_api3('MembershipType', 'getsingle', array(
+      'id' => $membershipID,
+      'return' => $mapping,
+    ));
+
+    foreach ($mapping as $recurringFieldName => $membershipTypeFieldName) {
+      $contributionRecurParams[$recurringFieldName] = $membershipType[$membershipTypeFieldName];
+    }
+
+    $contributionRecur = civicrm_api3('ContributionRecur', 'create', $contributionRecurParams);
+    $returnParams = array(
+      'contributionRecurID' => $contributionRecur['id'],
+      'is_recur' => TRUE,
+    );
+    return $returnParams;
+  }
+
 }