Merge pull request #4893 from colemanw/INFRA-132
[civicrm-core.git] / api / v3 / GroupContact.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.6 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2014 |
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 *
36 * @copyright CiviCRM LLC (c) 2004-2014
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
44 * Particular 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
47 *
48 * @param array $params
49 * Name value pair of contact information.
50 * {@getfields GroupContact_get}
51 *
52 * @return array
53 * list of groups, given contact subsribed to
54 */
55 function 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
63 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
64 }
65 $status = CRM_Utils_Array::value('status', $params, 'Added');
66
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);
69 return civicrm_api3_create_success($values, $params);
70 }
71
72 /**
73 * Add contact(s) to group(s)
74 *
75 * @access public
76 *
77 * @param array $params
78 * Input parameters.
79 *
80 * Allowed @params array keys are:<br>
81 * "contact_id" (required) : first contact to add<br>
82 * "group_id" (required): first group to add contact(s) to<br>
83 * "contact_id.1" etc. (optional) : another contact to add<br>
84 * "group_id.1" etc. (optional) : additional group to add contact(s) to<br>
85 * "status" (optional) : one of "Added", "Pending" or "Removed" (default is "Added")
86 * {@example GroupContactCreate.php 0}
87 *
88 * @return array
89 * Information about operation results
90 *
91 * On success, the return array will be structured as follows:
92 * <code>array(
93 * "is_error" => 0,
94 * "version" => 3,
95 * "count" => 3,
96 * "values" => array(
97 * "not_added" => integer,
98 * "added" => integer,
99 * "total_count" => integer
100 * )
101 * )</code>
102 *
103 * On failure, the return array will be structured as follows:
104 * <code>array(
105 * 'is_error' => 1,
106 * 'error_message' = string,
107 * 'error_data' = mixed or undefined
108 * )</code>
109 * {@getfields GroupContact_create}
110 */
111 function civicrm_api3_group_contact_create($params) {
112 // Nonstandard bao - doesn't accept ID as a param, so convert id to group_id + contact_id
113 if (!empty($params['id'])) {
114 $getParams = array('id' => $params['id']);
115 $info = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $getParams);
116 if (!empty($info['values'][$params['id']])) {
117 $params['group_id'] = $info['values'][$params['id']]['group_id'];
118 $params['contact_id'] = $info['values'][$params['id']]['contact_id'];
119 }
120 }
121 civicrm_api3_verify_mandatory($params, NULL, array('group_id', 'contact_id'));
122 $action = CRM_Utils_Array::value('status', $params, 'Added');
123 return _civicrm_api3_group_contact_common($params, $action);
124 }
125
126 /**
127 *
128 * @param <type> $params
129 *
130 * @return array
131 * <type>
132 * @deprecated
133 */
134 function civicrm_api3_group_contact_delete($params) {
135 $params['status'] = CRM_Utils_Array::value('status', $params, empty($params['skip_undelete']) ? 'Removed' : 'Deleted');
136 // "Deleted" isn't a real option so skip the api wrapper to avoid pseudoconstant validation
137 return civicrm_api3_group_contact_create($params);
138 }
139
140 /**
141 * modify metadata
142 */
143 function _civicrm_api3_group_contact_delete_spec(&$params) {
144 // set as not required no either/or std yet
145 $params['id']['api.required'] = 0;
146 }
147
148 /**
149 *
150 * @param <type> $params
151 *
152 * @return array|int <type>
153 * @deprecated
154 */
155 function 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 *
165 * @return Array
166 * @todo behaviour is highly non-standard - need to figure out how to make this 'behave'
167 * & at the very least return IDs & details of the groups created / changed
168 */
169 function _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);
193
194 if ($op == 'Added' || $op == 'Pending') {
195 $extraReturnValues = array(
196 'total_count' => 0,
197 'added' => 0,
198 'not_added' => 0,
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 {
213 $extraReturnValues = array(
214 'total_count' => 0,
215 'removed' => 0,
216 'not_removed' => 0,
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 }
225 $dao = null;// can't pass this by reference
226 return civicrm_api3_create_success(1,$params,'group_contact','create',$dao,$extraReturnValues);
227 }
228
229 /**
230 * @deprecated - this should be part of create but need to know we aren't missing something
231 */
232 function civicrm_api3_group_contact_update_status($params) {
233
234 civicrm_api3_verify_mandatory($params, NULL, array('contact_id', 'group_id'));
235
236 CRM_Contact_BAO_GroupContact::addContactsToGroup(
237 array($params['contact_id']),
238 $params['group_id'],
239 CRM_Utils_Array::value('method', $params, 'API'),
240 'Added',
241 CRM_Utils_Array::value('tracking', $params)
242 );
243
244 return TRUE;
245 }
246
247 /**
248 * @deprecated api notice
249 * @return array
250 * of deprecated actions
251 */
252 function _civicrm_api3_group_contact_deprecation() {
253 return array(
254 'delete' => 'GroupContact "delete" action is deprecated in favor of "create".',
255 'pending' => 'GroupContact "pending" action is deprecated in favor of "create".',
256 'update_status' => 'GroupContact "update_status" action is deprecated in favor of "create".',
257 );
258 }
259