Merge pull request #5010 from eileenmcnaughton/rebase2
[civicrm-core.git] / CRM / Member / BAO / Membership.php
index 396b7565e795a2f2ec890da308076ca9227971c0..38f42056a23932304fb869f46a68b66855a3c493 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
@@ -38,7 +38,6 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
    * Static field for all the membership information that we can potentially import
    *
    * @var array
-   * @static
    */
   static $_importableFields = NULL;
 
@@ -70,7 +69,6 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
    *   The array that holds all the db ids.
    *
    * @return CRM_Member_BAO_Membership
-   * @static
    */
   public static function add(&$params, $ids = array()) {
     $oldStatus = $oldType = NULL;
@@ -198,7 +196,6 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
    *                        be returned
    *
    * @return CRM_Member_BAO_Membership|null the found object or null
-   * @static
    */
   public static function &getValues(&$params, &$values, $active = FALSE) {
     if (empty($params)) {
@@ -238,8 +235,7 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
    *
    * @throws CRM_Core_Exception
    *
-   * @return CRM_Member_BAO_Membership
-   * @static
+   * @return CRM_Member_BAO_Membership|CRM_Core_Error
    */
   public static function create(&$params, &$ids, $skipRedirect = FALSE, $activityType = 'Membership Signup') {
     // always calculate status if is_override/skipStatusCal is not true.
@@ -450,9 +446,8 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
    *
    * @param int $action
    *
-   * @return Array
+   * @return array
    *   array of contact_id of all related contacts.
-   * @static
    */
   public static function checkMembershipRelationship($membershipId, $contactId, $action = CRM_Core_Action::ADD) {
     $contacts = array();
@@ -520,7 +515,6 @@ class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership {
    *                        in a hierarchical manner
    *
    * @return CRM_Member_BAO_Membership
-   * @static
    */
   public static function retrieve(&$params, &$defaults) {
     $membership = new CRM_Member_DAO_Membership();
@@ -602,9 +596,8 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    * @param int $membershipId
    *   Membership id that needs to be deleted.
    *
-   * @static
    *
-   * @return $results   no of deleted Membership on success, false otherwise
+   * @return int $results  id of deleted Membership on success, false otherwise
    */
   public static function del($membershipId) {
     //delete related first and then delete parent.
@@ -618,9 +611,8 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    * @param int $membershipId
    *   Membership id that needs to be deleted.
    *
-   * @static
    *
-   * @return $results   no of deleted Membership on success, false otherwise
+   * @return int $results id of deleted Membership on success, false otherwise
    */
   public static function deleteMembership($membershipId) {
     // CRM-12147, retrieve membership data before we delete it for hooks
@@ -682,11 +674,10 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    * @param int $contactId
    *
    * @return null
-   * @static
    */
   public static function deleteRelatedMemberships($ownerMembershipId, $contactId = NULL) {
     if (!$ownerMembershipId && !$contactId) {
-      return;
+      return FALSE;
     }
 
     $membership = new CRM_Member_DAO_Membership();
@@ -715,7 +706,6 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    *
    * @return array
    *   array of memberships based on status
-   * @static
    */
   public static function activeMembers($memberships, $status = 'active') {
     $actives = array();
@@ -757,9 +747,8 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    * @return bool
    *   Is this a separate membership payment
    *
-   * @static
    */
-  static function buildMembershipBlock(
+  public static function buildMembershipBlock(
     &$form,
     $pageID,
     $cid,
@@ -952,7 +941,6 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    *
    * @return array|null
    *
-   * @static
    */
   public static function getMembershipBlock($pageID) {
     $membershipBlock = array();
@@ -998,7 +986,6 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    *   True if only Memberships with same parent org as the $memType wanted, false otherwise.
    *
    * @return array|bool
-   * @static
    */
   public static function getContactMembership($contactID, $memType, $isTest, $membershipId = NULL, $onlySameParentOrg = FALSE) {
     $dao = new CRM_Member_DAO_Membership();
@@ -1092,7 +1079,6 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    *
    * @return array
    *   array of importable Fields
-   * @static
    */
   public static function &importableFields($contactType = 'Individual', $status = TRUE) {
     if (!self::$_importableFields) {
@@ -1153,7 +1139,6 @@ INNER JOIN  civicrm_membership_type type ON ( type.id = membership.membership_ty
    * Get all exportable fields
    *
    * @retun array return array of all exportable fields
-   * @static
    */
   public static function &exportableFields() {
     $expFieldMembership = CRM_Member_DAO_Membership::export();
@@ -1485,7 +1470,6 @@ AND civicrm_membership.is_test = %2";
    * @param \CRM_Contribute_BAO_Contribution|\CRM_Contribute_DAO_Contribution $contribution
    *
    * @return void
-   * @static
    */
   static public function updateRecurMembership(CRM_Member_DAO_Membership $membership, CRM_Contribute_BAO_Contribution $contribution) {
 
@@ -1534,9 +1518,8 @@ AND civicrm_membership.is_test = %2";
    *
    * @throws CRM_Core_Exception
    *
-   * @static
    */
-  static function renewMembershipFormWrapper(
+  public static function renewMembershipFormWrapper(
     $contactID,
     $membershipTypeID,
     $is_test,
@@ -1593,7 +1576,6 @@ AND civicrm_membership.is_test = %2";
    *   to be customised, null otherwise
    *
    * @return void
-   * @static
    */
   public static function fixMembershipStatusBeforeRenew(&$currentMembership, $changeToday) {
     $today = NULL;
@@ -1677,7 +1659,6 @@ AND civicrm_membership.is_test = %2";
    *
    * @return int
    *   contribution page id
-   * @static
    */
   public static function getContributionPageId($membershipID) {
     $query = "
@@ -1700,7 +1681,6 @@ SELECT c.contribution_page_id as pageID
    *   Owner Membership Id.
    * @param array $params
    *   Formatted array of key => value.
-   * @static
    */
   public static function updateRelatedMemberships($ownerMembershipId, $params) {
     $membership = new CRM_Member_DAO_Membership();
@@ -1728,7 +1708,6 @@ SELECT c.contribution_page_id as pageID
    *
    * @return array
    *   the list of membership fields
-   * @static
    */
   public static function getMembershipFields($mode = NULL) {
     $fields = CRM_Member_DAO_Membership::export();
@@ -1753,7 +1732,6 @@ SELECT c.contribution_page_id as pageID
    *
    * @return null|string
    *   sort name of the contact if found
-   * @static
    */
   public static function sortName($id) {
     $id = CRM_Utils_Type::escape($id, 'Integer');
@@ -1778,14 +1756,13 @@ WHERE  civicrm_membership.contact_id = civicrm_contact.id
    *
    * @return null|array
    *   array of memberships if created
-   * @static
    */
   public static function createRelatedMemberships(&$params, &$dao, $reset = FALSE) {
     static $relatedContactIds = array();
     if ($reset) {
       // not sure why a static var is in use here - we need a way to reset it from the test suite
       $relatedContactIds = array();
-      return;
+      return FALSE;
     }
 
     $membership = new CRM_Member_DAO_Membership();
@@ -1921,7 +1898,8 @@ WHERE  civicrm_membership.contact_id = civicrm_contact.id
               CRM_Member_BAO_Membership::create($params, $relMemIds);
               $available--;
             }
-            else { // we have run out of inherited memberships, so delete extras
+            else {
+              // we have run out of inherited memberships, so delete extras
               self::deleteMembership($params['id']);
             }
             // we need to first check if there will remain inherited memberships, so queue it up
@@ -1945,22 +1923,22 @@ WHERE  civicrm_membership.contact_id = civicrm_contact.id
    *
    * @param int $membershipId
    *
-   * @return boolean
-   *   true if deleted false otherwise
+   * @return object
+   *   $membershipPayment deleted membership payment object
    */
   public static function deleteMembershipPayment($membershipId) {
 
-    $membesrshipPayment = new CRM_Member_DAO_MembershipPayment();
-    $membesrshipPayment->membership_id = $membershipId;
-    $membesrshipPayment->find();
+    $membershipPayment = new CRM_Member_DAO_MembershipPayment();
+    $membershipPayment->membership_id = $membershipId;
+    $membershipPayment->find();
 
-    while ($membesrshipPayment->fetch()) {
-      CRM_Contribute_BAO_Contribution::deleteContribution($membesrshipPayment->contribution_id);
-      CRM_Utils_Hook::pre('delete', 'MembershipPayment', $membesrshipPayment->id, $membesrshipPayment);
+    while ($membershipPayment->fetch()) {
+      CRM_Contribute_BAO_Contribution::deleteContribution($membershipPayment->contribution_id);
+      CRM_Utils_Hook::pre('delete', 'MembershipPayment', $membershipPayment->id, $membershipPayment);
       $membesrshipPayment->delete();
-      CRM_Utils_Hook::post('delete', 'MembershipPayment', $membesrshipPayment->id, $membesrshipPayment);
+      CRM_Utils_Hook::post('delete', 'MembershipPayment', $membershipPayment->id, $membershipPayment);
     }
-    return $membesrshipPayment;
+    return $membershipPayment;
   }
 
   /**
@@ -2031,7 +2009,6 @@ FROM   civicrm_membership_type
    * @param bool $activeOnly
    *
    * @return null|string
-   * @static
    */
   public static function getContactMembershipCount($contactID, $activeOnly = FALSE) {
     $select = "SELECT count(*) FROM civicrm_membership ";
@@ -2056,8 +2033,7 @@ FROM   civicrm_membership_type
    *
    * @param bool $isNotCancelled
    *
-   * @return boolean
-   * @static
+   * @return bool
    */
   public static function isCancelSubscriptionSupported($mid, $isNotCancelled = TRUE) {
     $cacheKeyString = "$mid";
@@ -2089,7 +2065,6 @@ FROM   civicrm_membership_type
    *
    * @return string
    *   contribution status
-   * @static
    */
   public static function isSubscriptionCancelled($mid) {
     $sql = "
@@ -2225,9 +2200,9 @@ INNER JOIN  civicrm_contact contact ON ( contact.id = membership.contact_id AND
    * @return CRM_Contribute_BAO_Contribution
    */
   public static function processSecondaryFinancialTransaction($contactID, &$form, $tempParams, $isTest, $lineItems, $minimumFee, $financialTypeID) {
-    $contributionType = new CRM_Financial_DAO_FinancialType();
-    $contributionType->id = $financialTypeID;
-    if (!$contributionType->find(TRUE)) {
+    $financialType = new CRM_Financial_DAO_FinancialType();
+    $financialType->id = $financialTypeID;
+    if (!$financialType->find(TRUE)) {
       CRM_Core_Error::fatal(ts("Could not find a system table"));
     }
     $tempParams['amount'] = $minimumFee;
@@ -2239,55 +2214,53 @@ INNER JOIN  civicrm_contact contact ON ( contact.id = membership.contact_id AND
 
       if ($form->_contributeMode == 'express') {
         $result = $payment->doExpressCheckout($tempParams);
+        if (is_a($result, 'CRM_Core_Error')) {
+          throw new CRM_Core_Exception(CRM_Core_Error::getMessages($result));
+        }
       }
       else {
-        $result = $payment->doDirectPayment($tempParams);
+        $result = $payment->doPayment($tempParams, 'contribute');
       }
     }
 
-    if (is_a($result, 'CRM_Core_Error')) {
-      throw new CRM_Core_Exception(CRM_Core_Error::getMessages($result));
-    }
-    else {
-      //assign receive date when separate membership payment
-      //and contribution amount not selected.
-      if ($form->_amount == 0) {
-        $now = date('YmdHis');
-        $form->_params['receive_date'] = $now;
-        $receiveDate = CRM_Utils_Date::mysqlToIso($now);
-        $form->set('params', $form->_params);
-        $form->assign('receive_date', $receiveDate);
-      }
-
-      $form->set('membership_trx_id', $result['trxn_id']);
-      $form->set('membership_amount', $minimumFee);
-
-      $form->assign('membership_trx_id', $result['trxn_id']);
-      $form->assign('membership_amount', $minimumFee);
-
-      // we don't need to create the user twice, so lets disable cms_create_account
-      // irrespective of the value, CRM-2888
-      $tempParams['cms_create_account'] = 0;
-
-      $pending = $form->_params['is_pay_later'] ? (($minimumFee > 0.0) ? TRUE : FALSE) : FALSE;
-
-      //set this variable as we are not creating pledge for
-      //separate membership payment contribution.
-      //so for differentiating membership contribution from
-      //main contribution.
-      $form->_params['separate_membership_payment'] = 1;
-      $membershipContribution = CRM_Contribute_Form_Contribution_Confirm::processContribution($form,
-        $tempParams,
-        $result,
-        $contactID,
-        $contributionType,
-        $pending,
-        TRUE,
-        $isTest,
-        $lineItems
-      );
-      return $membershipContribution;
+    //assign receive date when separate membership payment
+    //and contribution amount not selected.
+    if ($form->_amount == 0) {
+      $now = date('YmdHis');
+      $form->_params['receive_date'] = $now;
+      $receiveDate = CRM_Utils_Date::mysqlToIso($now);
+      $form->set('params', $form->_params);
+      $form->assign('receive_date', $receiveDate);
     }
+
+    $form->set('membership_trx_id', $result['trxn_id']);
+    $form->set('membership_amount', $minimumFee);
+
+    $form->assign('membership_trx_id', $result['trxn_id']);
+    $form->assign('membership_amount', $minimumFee);
+
+    // we don't need to create the user twice, so lets disable cms_create_account
+    // irrespective of the value, CRM-2888
+    $tempParams['cms_create_account'] = 0;
+
+    $pending = $form->_params['is_pay_later'] ? (($minimumFee > 0.0) ? TRUE : FALSE) : FALSE;
+
+    //set this variable as we are not creating pledge for
+    //separate membership payment contribution.
+    //so for differentiating membership contribution from
+    //main contribution.
+    $form->_params['separate_membership_payment'] = 1;
+    $membershipContribution = CRM_Contribute_Form_Contribution_Confirm::processContribution($form,
+      $tempParams,
+      $result,
+      $contactID,
+      $financialType,
+      $pending,
+      TRUE,
+      $isTest,
+      $lineItems
+    );
+    return $membershipContribution;
   }
 
   /**
@@ -2929,7 +2902,6 @@ WHERE      civicrm_membership.is_test = 0";
    *   (param in process of being removed - try to use params) array of ids.
    *
    * @return CRM_Contribute_BAO_Contribution
-   * @static
    */
   public static function recordMembershipContribution(&$params, $ids = array()) {
     $membershipId = $params['membership_id'];
@@ -3018,7 +2990,6 @@ WHERE      civicrm_membership.is_test = 0";
    *   Array with membership type and organization.
    * @param int $priceSetId
    *
-   * @static
    */
   public static function createLineItems(&$qf, $membershipType, &$priceSetId) {
     $qf->_priceSetId = $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name');
@@ -3072,7 +3043,6 @@ WHERE      civicrm_membership.is_test = 0";
    *
    * @return array
    *   Array of membership type
-   * @static
    */
   public static function getContactsCancelledMembership($contactID, $isTest = FALSE) {
     if (!$contactID) {
@@ -3105,4 +3075,5 @@ WHERE      civicrm_membership.is_test = 0";
     }
     return $cancelledMembershipIds;
   }
+
 }