more comment fixes
[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 37 * @version $Id: GroupContact.php 30171 2010-10-14 09:11:27Z mover $
6a488035
TO
38 */
39
40
41/**
22242c87
EM
42 * This API will give list of the groups for particular contact.
43 *
44 * Particular status can be sent in params array.
45 *
6a488035
TO
46 * If no status mentioned in params, by default 'added' will be used
47 * to fetch the records
48 *
cf470720
TO
49 * @param array $params
50 * Name value pair of contact information.
6a488035 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/**
22242c87
EM
73 * Add contact(s) to group(s).
74 *
cf470720
TO
75 * @param array $params
76 * Input parameters.
6a488035
TO
77 *
78 * Allowed @params array keys are:<br>
79 * "contact_id" (required) : first contact to add<br>
80 * "group_id" (required): first group to add contact(s) to<br>
81 * "contact_id.1" etc. (optional) : another contact to add<br>
82 * "group_id.1" etc. (optional) : additional group to add contact(s) to<br>
83 * "status" (optional) : one of "Added", "Pending" or "Removed" (default is "Added")
6a488035 84 *
a6c01b45 85 * @return array
72b3a70c 86 * Information about operation results
6a488035 87 *
5396af74 88 * On success, the return array will be structured as follows:
89 * <code>array(
6a488035
TO
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 * )
5396af74 98 * )</code>
6a488035 99 *
5396af74 100 * On failure, the return array will be structured as follows:
101 * <code>array(
6a488035
TO
102 * 'is_error' => 1,
103 * 'error_message' = string,
104 * 'error_data' = mixed or undefined
5396af74 105 * )</code>
106 * {@getfields GroupContact_create}
6a488035
TO
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 123/**
22242c87 124 * Delete group contact record.
6a488035 125 *
72b3a70c 126 * @param array $params
6a488035 127 *
a6c01b45 128 * @return array
22242c87 129 *
e0b82b44 130 * @deprecated
6a488035
TO
131 */
132function civicrm_api3_group_contact_delete($params) {
3d700d00
CW
133 $params['status'] = CRM_Utils_Array::value('status', $params, empty($params['skip_undelete']) ? 'Removed' : 'Deleted');
134 // "Deleted" isn't a real option so skip the api wrapper to avoid pseudoconstant validation
135 return civicrm_api3_group_contact_create($params);
6a488035 136}
11e09c59
TO
137
138/**
6a488035 139 * modify metadata
d0997921 140 * @param array $params
6a488035
TO
141 */
142function _civicrm_api3_group_contact_delete_spec(&$params) {
143 // set as not required no either/or std yet
144 $params['id']['api.required'] = 0;
145}
146
147/**
d1b0d05e 148 * Get pending group contacts.
6a488035 149 *
72b3a70c 150 * @param array $params
6a488035 151 *
72b3a70c 152 * @return array|int
e0b82b44 153 * @deprecated
6a488035
TO
154 */
155function civicrm_api3_group_contact_pending($params) {
156 $params['status'] = 'Pending';
157 return civicrm_api('GroupContact', 'Create', $params);
158}
159
160/**
161 *
162 * @param array $params
163 * @param string $op
164 *
5396af74 165 * @return array
2ede60ec 166 * @todo behaviour is highly non-standard - need to figure out how to make this 'behave'
6a488035
TO
167 * & at the very least return IDs & details of the groups created / changed
168 */
169function _civicrm_api3_group_contact_common($params, $op = 'Added') {
170
171 $contactIDs = array();
172 $groupIDs = array();
173 foreach ($params as $n => $v) {
174 if (substr($n, 0, 10) == 'contact_id') {
175 $contactIDs[] = $v;
176 }
177 elseif (substr($n, 0, 8) == 'group_id') {
178 $groupIDs[] = $v;
179 }
180 }
181
182 if (empty($contactIDs)) {
183 return civicrm_api3_create_error('contact_id is a required field');
184 }
185
186 if (empty($groupIDs)) {
187 return civicrm_api3_create_error('group_id is a required field');
188 }
189
190 $method = CRM_Utils_Array::value('method', $params, 'API');
191 $status = CRM_Utils_Array::value('status', $params, $op);
192 $tracking = CRM_Utils_Array::value('tracking', $params);
6a488035
TO
193
194 if ($op == 'Added' || $op == 'Pending') {
2241036a 195 $extraReturnValues = array(
6a488035
TO
196 'total_count' => 0,
197 'added' => 0,
21dfd5f5 198 'not_added' => 0,
6a488035
TO
199 );
200 foreach ($groupIDs as $groupID) {
201 list($tc, $a, $na) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs,
202 $groupID,
203 $method,
204 $status,
205 $tracking
206 );
207 $extraReturnValues['total_count'] += $tc;
208 $extraReturnValues['added'] += $a;
209 $extraReturnValues['not_added'] += $na;
210 }
211 }
212 else {
2241036a 213 $extraReturnValues = array(
6a488035
TO
214 'total_count' => 0,
215 'removed' => 0,
21dfd5f5 216 'not_removed' => 0,
6a488035
TO
217 );
218 foreach ($groupIDs as $groupID) {
219 list($tc, $r, $nr) = CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactIDs, $groupID, $method, $status, $tracking);
220 $extraReturnValues['total_count'] += $tc;
221 $extraReturnValues['removed'] += $r;
222 $extraReturnValues['not_removed'] += $nr;
223 }
224 }
5396af74 225 $dao = NULL;// can't pass this by reference
226 return civicrm_api3_create_success(1, $params, 'group_contact', 'create', $dao, $extraReturnValues);
6a488035 227}
11e09c59
TO
228
229/**
6a488035 230 * @deprecated - this should be part of create but need to know we aren't missing something
d0997921 231 * @param array $params
645ee340
EM
232 * @return bool
233 * @throws \API_Exception
6a488035
TO
234 */
235function civicrm_api3_group_contact_update_status($params) {
236
237 civicrm_api3_verify_mandatory($params, NULL, array('contact_id', 'group_id'));
238
c49a2977
CW
239 CRM_Contact_BAO_GroupContact::addContactsToGroup(
240 array($params['contact_id']),
241 $params['group_id'],
242 CRM_Utils_Array::value('method', $params, 'API'),
243 'Added',
244 CRM_Utils_Array::value('tracking', $params)
245 );
6a488035
TO
246
247 return TRUE;
248}
249
a14e9d08
CW
250/**
251 * @deprecated api notice
a6c01b45 252 * @return array
16b10e64 253 * Array of deprecated actions
a14e9d08
CW
254 */
255function _civicrm_api3_group_contact_deprecation() {
256 return array(
257 'delete' => 'GroupContact "delete" action is deprecated in favor of "create".',
258 'pending' => 'GroupContact "pending" action is deprecated in favor of "create".',
259 'update_status' => 'GroupContact "update_status" action is deprecated in favor of "create".',
260 );
261}