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