INFRA-132 - Batch 14 (g)
[civicrm-core.git] / api / v3 / GroupContact.php
CommitLineData
6a488035
TO
1<?php
2// $Id$
3
4/*
5 +--------------------------------------------------------------------+
39de6fd5 6 | CiviCRM version 4.6 |
6a488035 7 +--------------------------------------------------------------------+
731a0992 8 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
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. |
15 | |
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. |
20 | |
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 +--------------------------------------------------------------------+
28 */
29
30/**
31 * File for the CiviCRM APIv3 group contact functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_Group
35 *
731a0992 36 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
37 * @version $Id: GroupContact.php 30171 2010-10-14 09:11:27Z mover $
38 *
39 */
40
41
42/**
43 * This API will give list of the groups for particular contact
c206647d 44 * Particular status can be sent in params array
6a488035
TO
45 * If no status mentioned in params, by default 'added' will be used
46 * to fetch the records
47 *
cf470720
TO
48 * @param array $params
49 * Name value pair of contact information.
6a488035
TO
50 * {@getfields GroupContact_get}
51 *
a6c01b45 52 * @return array
72b3a70c 53 * list of groups, given contact subsribed to
6a488035
TO
54 */
55function civicrm_api3_group_contact_get($params) {
56
57 if (empty($params['contact_id'])) {
58 if (empty($params['status'])) {
59 //default to 'Added'
60 $params['status'] = 'Added';
61 }
62 //ie. id passed in so we have to return something
3d700d00 63 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
6a488035
TO
64 }
65 $status = CRM_Utils_Array::value('status', $params, 'Added');
66
49fb4e06
RK
67 $groupId = CRM_Utils_Array::value('group_id', $params);
68 $values = &CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], $status, NULL, FALSE, TRUE, FALSE, TRUE, $groupId);
6a488035
TO
69 return civicrm_api3_create_success($values, $params);
70}
71
72/**
73 * Add contact(s) to group(s)
cf470720
TO
74 * @param array $params
75 * Input parameters.
6a488035
TO
76 *
77 * Allowed @params array keys are:<br>
78 * "contact_id" (required) : first contact to add<br>
79 * "group_id" (required): first group to add contact(s) to<br>
80 * "contact_id.1" etc. (optional) : another contact to add<br>
81 * "group_id.1" etc. (optional) : additional group to add contact(s) to<br>
82 * "status" (optional) : one of "Added", "Pending" or "Removed" (default is "Added")
83 * {@example GroupContactCreate.php 0}
84 *
a6c01b45 85 * @return array
72b3a70c 86 * Information about operation results
6a488035
TO
87 *
88 * On success, the return array will be structured as follows:
89 * <code>array(
90 * "is_error" => 0,
91 * "version" => 3,
92 * "count" => 3,
93 * "values" => array(
94 * "not_added" => integer,
95 * "added" => integer,
96 * "total_count" => integer
97 * )
98 * )</code>
99 *
100 * On failure, the return array will be structured as follows:
101 * <code>array(
102 * 'is_error' => 1,
103 * 'error_message' = string,
104 * 'error_data' = mixed or undefined
105 * )</code>
106 * {@getfields GroupContact_create}
107 */
108function civicrm_api3_group_contact_create($params) {
3d700d00
CW
109 // Nonstandard bao - doesn't accept ID as a param, so convert id to group_id + contact_id
110 if (!empty($params['id'])) {
111 $getParams = array('id' => $params['id']);
112 $info = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $getParams);
113 if (!empty($info['values'][$params['id']])) {
114 $params['group_id'] = $info['values'][$params['id']]['group_id'];
115 $params['contact_id'] = $info['values'][$params['id']]['contact_id'];
116 }
117 }
118 civicrm_api3_verify_mandatory($params, NULL, array('group_id', 'contact_id'));
6a488035
TO
119 $action = CRM_Utils_Array::value('status', $params, 'Added');
120 return _civicrm_api3_group_contact_common($params, $action);
121}
11e09c59 122
6a488035
TO
123/**
124 *
72b3a70c 125 * @param array $params
6a488035 126 *
a6c01b45 127 * @return array
72b3a70c 128 * <type>
e0b82b44 129 * @deprecated
6a488035
TO
130 */
131function civicrm_api3_group_contact_delete($params) {
3d700d00
CW
132 $params['status'] = CRM_Utils_Array::value('status', $params, empty($params['skip_undelete']) ? 'Removed' : 'Deleted');
133 // "Deleted" isn't a real option so skip the api wrapper to avoid pseudoconstant validation
134 return civicrm_api3_group_contact_create($params);
6a488035 135}
11e09c59
TO
136
137/**
6a488035 138 * modify metadata
d0997921 139 * @param array $params
6a488035
TO
140 */
141function _civicrm_api3_group_contact_delete_spec(&$params) {
142 // set as not required no either/or std yet
143 $params['id']['api.required'] = 0;
144}
145
146/**
147 *
72b3a70c 148 * @param array $params
6a488035 149 *
72b3a70c 150 * @return array|int
e0b82b44 151 * @deprecated
6a488035
TO
152 */
153function civicrm_api3_group_contact_pending($params) {
154 $params['status'] = 'Pending';
155 return civicrm_api('GroupContact', 'Create', $params);
156}
157
158/**
159 *
160 * @param array $params
161 * @param string $op
162 *
163 * @return Array
2ede60ec 164 * @todo behaviour is highly non-standard - need to figure out how to make this 'behave'
6a488035
TO
165 * & at the very least return IDs & details of the groups created / changed
166 */
167function _civicrm_api3_group_contact_common($params, $op = 'Added') {
168
169 $contactIDs = array();
170 $groupIDs = array();
171 foreach ($params as $n => $v) {
172 if (substr($n, 0, 10) == 'contact_id') {
173 $contactIDs[] = $v;
174 }
175 elseif (substr($n, 0, 8) == 'group_id') {
176 $groupIDs[] = $v;
177 }
178 }
179
180 if (empty($contactIDs)) {
181 return civicrm_api3_create_error('contact_id is a required field');
182 }
183
184 if (empty($groupIDs)) {
185 return civicrm_api3_create_error('group_id is a required field');
186 }
187
188 $method = CRM_Utils_Array::value('method', $params, 'API');
189 $status = CRM_Utils_Array::value('status', $params, $op);
190 $tracking = CRM_Utils_Array::value('tracking', $params);
6a488035
TO
191
192 if ($op == 'Added' || $op == 'Pending') {
2241036a 193 $extraReturnValues = array(
6a488035
TO
194 'total_count' => 0,
195 'added' => 0,
21dfd5f5 196 'not_added' => 0,
6a488035
TO
197 );
198 foreach ($groupIDs as $groupID) {
199 list($tc, $a, $na) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs,
200 $groupID,
201 $method,
202 $status,
203 $tracking
204 );
205 $extraReturnValues['total_count'] += $tc;
206 $extraReturnValues['added'] += $a;
207 $extraReturnValues['not_added'] += $na;
208 }
209 }
210 else {
2241036a 211 $extraReturnValues = array(
6a488035
TO
212 'total_count' => 0,
213 'removed' => 0,
21dfd5f5 214 'not_removed' => 0,
6a488035
TO
215 );
216 foreach ($groupIDs as $groupID) {
217 list($tc, $r, $nr) = CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactIDs, $groupID, $method, $status, $tracking);
218 $extraReturnValues['total_count'] += $tc;
219 $extraReturnValues['removed'] += $r;
220 $extraReturnValues['not_removed'] += $nr;
221 }
222 }
223 $dao = null;// can't pass this by reference
224 return civicrm_api3_create_success(1,$params,'group_contact','create',$dao,$extraReturnValues);
225}
11e09c59
TO
226
227/**
6a488035 228 * @deprecated - this should be part of create but need to know we aren't missing something
d0997921 229 * @param array $params
645ee340
EM
230 * @return bool
231 * @throws \API_Exception
6a488035
TO
232 */
233function civicrm_api3_group_contact_update_status($params) {
234
235 civicrm_api3_verify_mandatory($params, NULL, array('contact_id', 'group_id'));
236
c49a2977
CW
237 CRM_Contact_BAO_GroupContact::addContactsToGroup(
238 array($params['contact_id']),
239 $params['group_id'],
240 CRM_Utils_Array::value('method', $params, 'API'),
241 'Added',
242 CRM_Utils_Array::value('tracking', $params)
243 );
6a488035
TO
244
245 return TRUE;
246}
247
a14e9d08
CW
248/**
249 * @deprecated api notice
a6c01b45 250 * @return array
16b10e64 251 * Array of deprecated actions
a14e9d08
CW
252 */
253function _civicrm_api3_group_contact_deprecation() {
254 return array(
255 'delete' => 'GroupContact "delete" action is deprecated in favor of "create".',
256 'pending' => 'GroupContact "pending" action is deprecated in favor of "create".',
257 'update_status' => 'GroupContact "update_status" action is deprecated in favor of "create".',
258 );
259}