Merge pull request #2843 from kurund/CRM-14427
[civicrm-core.git] / api / v3 / Membership.php
index 95e661e3e3ef2375ba9e6c33afbd8d55df19861f..856ae2b967ff394962accaeaf33a576070a43cf2 100644 (file)
@@ -2,7 +2,7 @@
 
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -154,7 +154,8 @@ function _civicrm_api3_membership_create_spec(&$params) {
     'title' => 'Skip status calculation. By default this is 0 if id is not set and 1 if it is set.'
   );
   $params['num_terms'] = array(
-    'title' => 'Number of terms to add/renew. If this parameter is passed, dates will be calculated automatically. If no id is passed (new membership) and no dates are given, num_terms will be assumed to be 1.'
+    'title' => 'Number of terms to add/renew. If this parameter is passed, dates will be calculated automatically. If no id is passed (new membership) and no dates are given, num_terms will be assumed to be 1.',
+    'type' => CRM_Utils_Type::T_INT,
   );
 }
 /**
@@ -186,24 +187,63 @@ function civicrm_api3_membership_get($params) {
   $activeOnly = $membershipTypeId = $membershipType = NULL;
 
   $contactID = CRM_Utils_Array::value('contact_id', $params);
-  if (!empty($params['filters']) && is_array($params['filters'])) {
-    $activeOnly = CRM_Utils_Array::value('is_current', $params['filters'], FALSE);
+  if (!empty($params['filters']) && is_array($params['filters']) && isset($params['filters']['is_current'])) {
+    $activeOnly = $params['filters']['is_current'];
+    unset($params['filters']['is_current']);
   }
   $activeOnly = CRM_Utils_Array::value('active_only', $params, $activeOnly);
-
-  if (CRM_Utils_Array::value('contact_id', $params)) {
-    $membershipValues = _civicrm_api3_membership_get_customv2behaviour($params, $contactID, $membershipTypeId, $activeOnly );
+  if($activeOnly && empty($params['status_id'])) {
+    $params['status_id'] = array('IN' => CRM_Member_BAO_MembershipStatus::getMembershipStatusCurrent());
   }
-  else {
-    //legacy behaviour only ever worked when contact_id passed in - use standard api function otherwise
-    $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
+  $options = _civicrm_api3_get_options_from_params($params, TRUE,'membership', 'get');
+  if($options['is_count']) {
+    return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
+  }
+  $membershipValues = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, FALSE);
+
+
+  $return = $options['return'];
+  if(empty($membershipValues) ||
+    (!empty($return)
+      && !array_key_exists('related_contact_id', $return)
+      && !array_key_exists('relationship_name', $return)
+    )
+    ) {
+    return civicrm_api3_create_success($membershipValues, $params, 'membership', 'get');
   }
 
-  if (empty($membershipValues)) {
-    // No results is NOT an error!
-    return civicrm_api3_create_success($membershipValues, $params);
+  $members =  _civicrm_api3_membership_relationsship_get_customv2behaviour($params, $membershipValues, $contactID );
+  return civicrm_api3_create_success($members, $params, 'membership', 'get');
+}
+
+/**
+ * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of
+ * membership a complicated return array is constructed. The original
+ * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id
+ * is passed in as part of the reasonable expectation developers have that we will keep the api
+ * as stable as possible
+ *
+ * @param array $params parameters passed into get function
+ * @return array result for calling function
+ */
+function _civicrm_api3_membership_get_customv2behaviour(&$params, $membershipTypeId, $activeOnly) {
+  // get the membership for the given contact ID
+  $membershipParams = array('contact_id' => $params['contact_id']);
+  if ($membershipTypeId) {
+    $membershipParams['membership_type_id'] = $membershipTypeId;
   }
+  $membershipValues = array();
+  CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly);
+  return $membershipValues;
+}
+
 
+/**
+ * non-standard behaviour inherited from v2
+* @param array $params parameters passed into get function
+* @return array result for calling function
+*/
+function _civicrm_api3_membership_relationsship_get_customv2behaviour(&$params, $membershipValues, $contactID) {
   $relationships = array();
   foreach ($membershipValues as $membershipId => $values) {
     // populate the membership type name for the membership type id
@@ -246,27 +286,5 @@ function civicrm_api3_membership_get($params) {
 
     }
   }
-
-  return civicrm_api3_create_success($members, $params, 'membership', 'get');
-}
-
-/**
- * When we copied apiv3 from api v2 we brought across some custom behaviours - in the case of
- * membership a complicated return array is constructed. The original
- * behaviour made contact_id a required field. We still need to keep this for v3 when contact_id
- * is passed in as part of the reasonable expectation developers have that we will keep the api
- * as stable as possible
- *
- * @param array $params parameters passed into get function
- * @return array result for calling function
- */
-function _civicrm_api3_membership_get_customv2behaviour(&$params, $contactID, $membershipTypeId, $activeOnly) {
-  // get the membership for the given contact ID
-  $membershipParams = array('contact_id' => $contactID);
-  if ($membershipTypeId) {
-    $membershipParams['membership_type_id'] = $membershipTypeId;
-  }
-  $membershipValues = array();
-  CRM_Member_BAO_Membership::getValues($membershipParams, $membershipValues, $activeOnly);
-  return $membershipValues;
+  return $members;
 }