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