e03110b17295ee762e01ddd387e79b9374f23846
[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 * @param array $params
75 * Input parameters.
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 *
85 * @return array
86 * Information about operation results
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 */
108 function civicrm_api3_group_contact_create($params) {
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'));
119 $action = CRM_Utils_Array::value('status', $params, 'Added');
120 return _civicrm_api3_group_contact_common($params, $action);
121 }
122
123 /**
124 *
125 * @param array $params
126 *
127 * @return array
128 * <type>
129 * @deprecated
130 */
131 function civicrm_api3_group_contact_delete($params) {
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);
135 }
136
137 /**
138 * modify metadata
139 * @param array $params
140 */
141 function _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 *
148 * @param array $params
149 *
150 * @return array|int
151 * @deprecated
152 */
153 function 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
164 * @todo behaviour is highly non-standard - need to figure out how to make this 'behave'
165 * & at the very least return IDs & details of the groups created / changed
166 */
167 function _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);
191
192 if ($op == 'Added' || $op == 'Pending') {
193 $extraReturnValues = array(
194 'total_count' => 0,
195 'added' => 0,
196 'not_added' => 0,
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 {
211 $extraReturnValues = array(
212 'total_count' => 0,
213 'removed' => 0,
214 'not_removed' => 0,
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 }
226
227 /**
228 * @deprecated - this should be part of create but need to know we aren't missing something
229 * @param array $params
230 * @return bool
231 * @throws \API_Exception
232 */
233 function civicrm_api3_group_contact_update_status($params) {
234
235 civicrm_api3_verify_mandatory($params, NULL, array('contact_id', 'group_id'));
236
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 );
244
245 return TRUE;
246 }
247
248 /**
249 * @deprecated api notice
250 * @return array
251 * Array of deprecated actions
252 */
253 function _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 }