Merge pull request #2514 from colemanw/ac2
[civicrm-core.git] / api / v3 / GroupContact.php
index 050e93ce861df4de9967fdf9113bb37672d394ee..4b40cff003fb1346410bb195c94b430f39459549 100644 (file)
@@ -3,7 +3,7 @@
 
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -58,7 +58,7 @@ function civicrm_api3_group_contact_get($params) {
       $params['status'] = 'Added';
     }
     //ie. id passed in so we have to return something
-    return _civicrm_api3_basic_get('CRM_Contact_BAO_GroupContact', $params);
+    return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
   }
   $status = CRM_Utils_Array::value('status', $params, 'Added');
 
@@ -104,20 +104,19 @@ function civicrm_api3_group_contact_get($params) {
  * {@getfields GroupContact_create}
  */
 function civicrm_api3_group_contact_create($params) {
-
+  // Nonstandard bao - doesn't accept ID as a param, so convert id to group_id + contact_id
+  if (!empty($params['id'])) {
+    $getParams = array('id' => $params['id']);
+    $info = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $getParams);
+    if (!empty($info['values'][$params['id']])) {
+      $params['group_id'] = $info['values'][$params['id']]['group_id'];
+      $params['contact_id'] = $info['values'][$params['id']]['contact_id'];
+    }
+  }
+  civicrm_api3_verify_mandatory($params, NULL, array('group_id', 'contact_id'));
   $action = CRM_Utils_Array::value('status', $params, 'Added');
   return _civicrm_api3_group_contact_common($params, $action);
 }
-/*
- * Adjust Metadata for Create action
- * 
- * The metadata is used for setting defaults, documentation & validation
- * @param array $params array or parameters determined by getfields
- */
-function _civicrm_api3_group_contact_create_spec(&$params) {
-  $params['group_id']['api.required'] = 1;
-  $params['contact_id']['api.required'] = 1;
-}
 
 /**
  *
@@ -127,10 +126,12 @@ function _civicrm_api3_group_contact_create_spec(&$params) {
  * @deprecated
  */
 function civicrm_api3_group_contact_delete($params) {
-  $params['status'] = 'Removed';
-  return civicrm_api('GroupContact', 'Create', $params);
+  $params['status'] = CRM_Utils_Array::value('status', $params, empty($params['skip_undelete']) ? 'Removed' : 'Deleted');
+  // "Deleted" isn't a real option so skip the api wrapper to avoid pseudoconstant validation
+  return civicrm_api3_group_contact_create($params);
 }
-/*
+
+/**
  * modify metadata
  */
 function _civicrm_api3_group_contact_delete_spec(&$params) {
@@ -156,7 +157,7 @@ function civicrm_api3_group_contact_pending($params) {
  * @param string $op
  *
  * @return Array
- * @todo behaviour is highly non-standard - need to figure out how to make this 'behave' 
+ * @todo behaviour is highly non-standard - need to figure out how to make this 'behave'
  * & at the very least return IDs & details of the groups created / changed
  */
 function _civicrm_api3_group_contact_common($params, $op = 'Added') {
@@ -183,7 +184,6 @@ function _civicrm_api3_group_contact_common($params, $op = 'Added') {
   $method = CRM_Utils_Array::value('method', $params, 'API');
   $status = CRM_Utils_Array::value('status', $params, $op);
   $tracking = CRM_Utils_Array::value('tracking', $params);
-  
 
   if ($op == 'Added' || $op == 'Pending') {
     $extraReturnValues= array(
@@ -219,17 +219,21 @@ function _civicrm_api3_group_contact_common($params, $op = 'Added') {
   $dao = null;// can't pass this by reference
   return civicrm_api3_create_success(1,$params,'group_contact','create',$dao,$extraReturnValues);
 }
-/*
+
+/**
  * @deprecated - this should be part of create but need to know we aren't missing something
  */
 function civicrm_api3_group_contact_update_status($params) {
 
   civicrm_api3_verify_mandatory($params, NULL, array('contact_id', 'group_id'));
 
-  $method = CRM_Utils_Array::value('method', $params, 'API');
-  $tracking = CRM_Utils_Array::value('tracking', $params);
-
-  CRM_Contact_BAO_GroupContact::updateGroupMembershipStatus($params['contact_id'], $params['group_id'], $method, $tracking);
+  CRM_Contact_BAO_GroupContact::addContactsToGroup(
+    array($params['contact_id']),
+    $params['group_id'],
+    CRM_Utils_Array::value('method', $params, 'API'),
+    'Added',
+    CRM_Utils_Array::value('tracking', $params)
+  );
 
   return TRUE;
 }