From 8d47034f09852f37f5fb9dabab85a031b901a7fd Mon Sep 17 00:00:00 2001 From: JKingsnorth Date: Tue, 4 Aug 2015 10:35:41 +0100 Subject: [PATCH] CRM-16959 - Support multiple contact/group IDs in a more standard format --- api/v3/GroupContact.php | 53 ++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/api/v3/GroupContact.php b/api/v3/GroupContact.php index c39f826d13..98efb573bb 100644 --- a/api/v3/GroupContact.php +++ b/api/v3/GroupContact.php @@ -73,6 +73,16 @@ function civicrm_api3_group_contact_get($params) { return civicrm_api3_create_success($values, $params, 'GroupContact'); } +/** + * Adjust metadata for Create action. + * + * @param array $params + */ +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). * @@ -101,12 +111,13 @@ function civicrm_api3_group_contact_get($params) { * @endcode * * @param array $params - * Input parameters. - * - "contact_id" (required) : first contact to add - * - "group_id" (required): first group to add contact(s) to - * - "contact_id.1" etc. (optional) : another contact to add - * - "group_id.1" etc. (optional) : additional group to add contact(s) to - * - "status" (optional) : one of "Added", "Pending" or "Removed" (default is "Added") + * 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 @@ -121,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); } @@ -179,23 +189,32 @@ 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); -- 2.25.1