Checkstyle fix - one pesky whitespace
[civicrm-core.git] / api / v3 / GroupContact.php
index 2fcd3400af51e97e824d5503b3af1be3a45c0bf3..824afc828447627a74ce4f36162311da5a85c71f 100644 (file)
@@ -1,11 +1,9 @@
 <?php
-// $Id$
-
 /*
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  */
 
 /**
- * File for the CiviCRM APIv3 group contact functions
+ * This api exposes CiviCRM GroupContact records.
  *
- * @package CiviCRM_APIv3
- * @subpackage API_Group
+ * This api is for adding/removing contacts from a group,
+ * or fetching a list of groups for a contact.
+ *
+ * Important note: This api does not fetch smart groups for a contact.
+ * To fetch all contacts in a smart group, use the Contact api
+ * passing a contact_id and group_id.
+ *
+ * To create/delete groups, use the group api instead.
  *
- * @copyright CiviCRM LLC (c) 2004-2014
- * @version $Id: GroupContact.php 30171 2010-10-14 09:11:27Z mover $
+ * @package CiviCRM_APIv3
  */
 
-
 /**
- * This API will give list of the groups for particular contact.
+ * Fetch a list of groups for a contact, or contacts for a group.
  *
- * Particular status can be sent in params array.
+ * @Note: this only applies to static groups, not smart groups.
+ * To fetch all contacts in a smart group, use the Contact api
+ * passing a contact_id and group_id.
  *
  * If no status mentioned in params, by default 'added' will be used
  * to fetch the records
@@ -65,28 +69,27 @@ function civicrm_api3_group_contact_get($params) {
   $status = CRM_Utils_Array::value('status', $params, 'Added');
 
   $groupId = CRM_Utils_Array::value('group_id', $params);
-  $values = &CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], $status, NULL, FALSE, TRUE, FALSE, TRUE, $groupId);
-  return civicrm_api3_create_success($values, $params);
+  $values = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], $status, NULL, FALSE, TRUE, FALSE, TRUE, $groupId);
+  return civicrm_api3_create_success($values, $params, 'GroupContact');
 }
 
 /**
- * Add contact(s) to group(s).
+ * Adjust metadata for Create action.
  *
  * @param array $params
- *   Input parameters.
- *
- * Allowed @params array keys are:<br>
- * "contact_id" (required) : first contact to add<br>
- * "group_id" (required): first group to add contact(s) to<br>
- * "contact_id.1" etc. (optional) : another contact to add<br>
- * "group_id.1" etc. (optional) : additional group to add contact(s) to<br>
- * "status" (optional) : one of "Added", "Pending" or "Removed" (default is "Added")
- *
- * @return array
- *   Information about operation results
+ */
+function _civicrm_api3_group_contact_create_spec(&$params) {
+  $params['contact_id']['api.required'] = 1;
+  $params['group_id']['api.required'] = 1;
+}
+
+/**
+ * Add contact(s) to group(s).
  *
- *   On success, the return array will be structured as follows:
- *   <code>array(
+ * This api has a legacy/nonstandard signature.
+ * On success, the return array will be structured as follows:
+ * @code
+ * array(
  *   "is_error" => 0,
  *   "version"  => 3,
  *   "count"    => 3,
@@ -95,15 +98,29 @@ function civicrm_api3_group_contact_get($params) {
  *     "added"       => integer,
  *     "total_count" => integer
  *   )
- *   )</code>
+ * )
+ * @endcode
  *
- *   On failure, the return array will be structured as follows:
- *   <code>array(
+ * On failure, the return array will be structured as follows:
+ * @code
+ * array(
  *   'is_error' => 1,
  *   'error_message' = string,
  *   'error_data' = mixed or undefined
- *   )</code>
- *   {@getfields GroupContact_create}
+ * )
+ * @endcode
+ *
+ * @param array $params
+ *   Input parameters:
+ *   - "contact_id" (required): First contact to add, or array of Contact IDs
+ *   - "group_id" (required): First group to add contact(s) to, or array of Group IDs
+ *   - "status" (optional): "Added" (default), "Pending" or "Removed"
+ *   Legacy input parameters (will be deprecated):
+ *   - "contact_id.1" etc. (optional): Additional contact_id to add to group(s)
+ *   - "group_id.1" etc. (optional): Additional groups to add contact(s) to
+ *
+ * @return array
+ *   Information about operation results
  */
 function civicrm_api3_group_contact_create($params) {
   // Nonstandard bao - doesn't accept ID as a param, so convert id to group_id + contact_id
@@ -115,7 +132,6 @@ function civicrm_api3_group_contact_create($params) {
       $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);
 }
@@ -161,34 +177,44 @@ function civicrm_api3_group_contact_pending($params) {
 /**
  * Group contact helper function.
  *
+ * @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
+ *
  * @param array $params
  * @param string $op
  *
  * @return array
- * @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') {
 
   $contactIDs = array();
   $groupIDs = array();
+
+  // CRM-16959: Handle multiple Contact IDs and Group IDs in legacy format
+  // (contact_id.1, contact_id.2) or as an array
   foreach ($params as $n => $v) {
     if (substr($n, 0, 10) == 'contact_id') {
-      $contactIDs[] = $v;
+      if (is_array($v)) {
+        foreach ($v as $arr_v) {
+          $contactIDs[] = $arr_v;
+        }
+      }
+      else {
+        $contactIDs[] = $v;
+      }
     }
     elseif (substr($n, 0, 8) == 'group_id') {
-      $groupIDs[] = $v;
+      if (is_array($v)) {
+        foreach ($v as $arr_v) {
+          $groupIDs[] = $arr_v;
+        }
+      }
+      else {
+        $groupIDs[] = $v;
+      }
     }
   }
 
-  if (empty($contactIDs)) {
-    return civicrm_api3_create_error('contact_id is a required field');
-  }
-
-  if (empty($groupIDs)) {
-    return civicrm_api3_create_error('group_id is a required field');
-  }
-
   $method = CRM_Utils_Array::value('method', $params, 'API');
   $status = CRM_Utils_Array::value('status', $params, $op);
   $tracking = CRM_Utils_Array::value('tracking', $params);
@@ -224,8 +250,9 @@ function _civicrm_api3_group_contact_common($params, $op = 'Added') {
       $extraReturnValues['not_removed'] += $nr;
     }
   }
-  $dao = NULL;// can't pass this by reference
-  return civicrm_api3_create_success(1, $params, 'group_contact', 'create', $dao, $extraReturnValues);
+  // can't pass this by reference
+  $dao = NULL;
+  return civicrm_api3_create_success(1, $params, 'GroupContact', 'create', $dao, $extraReturnValues);
 }
 
 /**