5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.5 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2014 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
31 * File for the CiviCRM APIv3 group contact functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_Group
36 * @copyright CiviCRM LLC (c) 2004-2014
37 * @version $Id: GroupContact.php 30171 2010-10-14 09:11:27Z mover $
43 * This API will give list of the groups for particular contact
44 * Particualr status can be sent in params array
45 * If no status mentioned in params, by default 'added' will be used
46 * to fetch the records
48 * @param array $params name value pair of contact information
49 * {@getfields GroupContact_get}
51 * @return array list of groups, given contact subsribed to
53 function civicrm_api3_group_contact_get($params) {
55 if (empty($params['contact_id'])) {
56 if (empty($params['status'])) {
58 $params['status'] = 'Added';
60 //ie. id passed in so we have to return something
61 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
63 $status = CRM_Utils_Array
::value('status', $params, 'Added');
65 $values = &CRM_Contact_BAO_GroupContact
::getContactGroup($params['contact_id'], $status, NULL, FALSE, TRUE);
66 return civicrm_api3_create_success($values, $params);
70 * Add contact(s) to group(s)
74 * @param array $params Input parameters
76 * Allowed @params array keys are:<br>
77 * "contact_id" (required) : first contact to add<br>
78 * "group_id" (required): first group to add contact(s) to<br>
79 * "contact_id.1" etc. (optional) : another contact to add<br>
80 * "group_id.1" etc. (optional) : additional group to add contact(s) to<br>
81 * "status" (optional) : one of "Added", "Pending" or "Removed" (default is "Added")
82 * {@example GroupContactCreate.php 0}
84 * @return array Information about operation results
86 * On success, the return array will be structured as follows:
92 * "not_added" => integer,
94 * "total_count" => integer
98 * On failure, the return array will be structured as follows:
101 * 'error_message' = string,
102 * 'error_data' = mixed or undefined
104 * {@getfields GroupContact_create}
106 function civicrm_api3_group_contact_create($params) {
107 // Nonstandard bao - doesn't accept ID as a param, so convert id to group_id + contact_id
108 if (!empty($params['id'])) {
109 $getParams = array('id' => $params['id']);
110 $info = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $getParams);
111 if (!empty($info['values'][$params['id']])) {
112 $params['group_id'] = $info['values'][$params['id']]['group_id'];
113 $params['contact_id'] = $info['values'][$params['id']]['contact_id'];
116 civicrm_api3_verify_mandatory($params, NULL, array('group_id', 'contact_id'));
117 $action = CRM_Utils_Array
::value('status', $params, 'Added');
118 return _civicrm_api3_group_contact_common($params, $action);
123 * @param <type> $params
128 function civicrm_api3_group_contact_delete($params) {
129 $params['status'] = CRM_Utils_Array
::value('status', $params, empty($params['skip_undelete']) ?
'Removed' : 'Deleted');
130 // "Deleted" isn't a real option so skip the api wrapper to avoid pseudoconstant validation
131 return civicrm_api3_group_contact_create($params);
137 function _civicrm_api3_group_contact_delete_spec(&$params) {
138 // set as not required no either/or std yet
139 $params['id']['api.required'] = 0;
144 * @param <type> $params
149 function civicrm_api3_group_contact_pending($params) {
150 $params['status'] = 'Pending';
151 return civicrm_api('GroupContact', 'Create', $params);
156 * @param array $params
160 * @todo behaviour is highly non-standard - need to figure out how to make this 'behave'
161 * & at the very least return IDs & details of the groups created / changed
163 function _civicrm_api3_group_contact_common($params, $op = 'Added') {
165 $contactIDs = array();
167 foreach ($params as $n => $v) {
168 if (substr($n, 0, 10) == 'contact_id') {
171 elseif (substr($n, 0, 8) == 'group_id') {
176 if (empty($contactIDs)) {
177 return civicrm_api3_create_error('contact_id is a required field');
180 if (empty($groupIDs)) {
181 return civicrm_api3_create_error('group_id is a required field');
184 $method = CRM_Utils_Array
::value('method', $params, 'API');
185 $status = CRM_Utils_Array
::value('status', $params, $op);
186 $tracking = CRM_Utils_Array
::value('tracking', $params);
188 if ($op == 'Added' ||
$op == 'Pending') {
189 $extraReturnValues= array(
194 foreach ($groupIDs as $groupID) {
195 list($tc, $a, $na) = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIDs,
201 $extraReturnValues['total_count'] +
= $tc;
202 $extraReturnValues['added'] +
= $a;
203 $extraReturnValues['not_added'] +
= $na;
207 $extraReturnValues= array(
212 foreach ($groupIDs as $groupID) {
213 list($tc, $r, $nr) = CRM_Contact_BAO_GroupContact
::removeContactsFromGroup($contactIDs, $groupID, $method, $status, $tracking);
214 $extraReturnValues['total_count'] +
= $tc;
215 $extraReturnValues['removed'] +
= $r;
216 $extraReturnValues['not_removed'] +
= $nr;
219 $dao = null;// can't pass this by reference
220 return civicrm_api3_create_success(1,$params,'group_contact','create',$dao,$extraReturnValues);
224 * @deprecated - this should be part of create but need to know we aren't missing something
226 function civicrm_api3_group_contact_update_status($params) {
228 civicrm_api3_verify_mandatory($params, NULL, array('contact_id', 'group_id'));
230 CRM_Contact_BAO_GroupContact
::addContactsToGroup(
231 array($params['contact_id']),
233 CRM_Utils_Array
::value('method', $params, 'API'),
235 CRM_Utils_Array
::value('tracking', $params)