Merge pull request #133 from pradpnayak/CRM-12061
[civicrm-core.git] / api / v3 / Profile.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
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 activity profile functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_ActivityProfile
35 * @copyright CiviCRM LLC (c) 2004-2013
36 * @version $Id: ActivityProfile.php 30486 2011-05-20 16:12:09Z rajan $
37 *
38 */
39
40 /**
41 * Include common API util functions
42 */
43 require_once 'api/v3/utils.php';
44
45 require_once 'CRM/Core/BAO/UFGroup.php';
46 require_once 'CRM/Core/BAO/UFField.php';
47 require_once 'CRM/Core/Permission.php';
48
49 /**
50 * Retrieve Profile field values.
51 *
52 * @param array $params Associative array of property name/value
53 * pairs to get profile field values
54 *
55 * @return Profile field values|CRM_Error
56 *
57 * @todo add example
58 * @todo add test cases
59 *
60 */
61 function civicrm_api3_profile_get($params) {
62
63 civicrm_api3_verify_mandatory($params, NULL, array('profile_id', 'contact_id'));
64
65 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
66 return civicrm_api3_create_error('Invalid value for profile_id');
67 }
68
69 $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($params['profile_id']);
70
71 if (CRM_Core_BAO_UFField::checkProfileType($params['profile_id']) && !$isContactActivityProfile) {
72 return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.');
73 }
74
75 $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'],
76 FALSE,
77 NULL,
78 NULL,
79 NULL,
80 FALSE,
81 NULL,
82 TRUE,
83 NULL,
84 CRM_Core_Permission::EDIT
85 );
86
87 $values = array();
88 if ($isContactActivityProfile) {
89 civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
90
91 require_once 'CRM/Profile/Form.php';
92 $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'],
93 $params['contact_id'],
94 $params['profile_id']
95 );
96 if (!empty($errors)) {
97 return civicrm_api3_create_error(array_pop($errors));
98 }
99
100 $contactFields = $activityFields = array();
101 foreach ($profileFields as $fieldName => $field) {
102 if (CRM_Utils_Array::value('field_type', $field) == 'Activity') {
103 $activityFields[$fieldName] = $field;
104 }
105 else {
106 $contactFields[$fieldName] = $field;
107 }
108 }
109
110 CRM_Core_BAO_UFGroup::setProfileDefaults($params['contact_id'], $contactFields, $values, TRUE);
111
112 if ($params['activity_id']) {
113 CRM_Core_BAO_UFGroup::setComponentDefaults($activityFields, $params['activity_id'], 'Activity', $values, TRUE);
114 }
115 }
116 else {
117 CRM_Core_BAO_UFGroup::setProfileDefaults($params['contact_id'], $profileFields, $values, TRUE);
118 }
119
120 $result = civicrm_api3_create_success();
121 $result['values'] = $values;
122
123 return $result;
124 }
125
126 /**
127 * Update Profile field values.
128 *
129 * @param array $params Associative array of property name/value
130 * pairs to update profile field values
131 *
132 * @return Updated Contact/ Activity object|CRM_Error
133 *
134 * @todo add example
135 * @todo add test cases
136 *
137 */
138 function civicrm_api3_profile_set($params) {
139
140 civicrm_api3_verify_mandatory($params, NULL, array('profile_id'));
141
142 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
143 return civicrm_api3_create_error('Invalid value for profile_id');
144 }
145
146 $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($params['profile_id']);
147
148 if (CRM_Core_BAO_UFField::checkProfileType($params['profile_id']) && !$isContactActivityProfile) {
149 return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.');
150 }
151
152 $contactParams = $activityParams = $missingParams = array();
153
154 $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'],
155 FALSE,
156 NULL,
157 NULL,
158 NULL,
159 FALSE,
160 NULL,
161 TRUE,
162 NULL,
163 CRM_Core_Permission::EDIT
164 );
165
166 if ($isContactActivityProfile) {
167 civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
168
169 require_once 'CRM/Profile/Form.php';
170 $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'],
171 $params['contact_id'],
172 $params['profile_id']
173 );
174 if (!empty($errors)) {
175 return civicrm_api3_create_error(array_pop($errors));
176 }
177 }
178
179 foreach ($profileFields as $fieldName => $field) {
180 if (CRM_Utils_Array::value('is_required', $field)) {
181 if (!CRM_Utils_Array::value($fieldName, $params) || empty($params[$fieldName])) {
182 $missingParams[] = $fieldName;
183 }
184 }
185
186 if (!isset($params[$fieldName])) {
187 continue;
188 }
189
190 $value = $params[$fieldName];
191 if ($params[$fieldName] && isset($params[$fieldName . '_id'])) {
192 $value = $params[$fieldName . '_id'];
193 }
194
195 if ($isContactActivityProfile && CRM_Utils_Array::value('field_type', $field) == 'Activity') {
196 $activityParams[$fieldName] = $value;
197 }
198 else {
199 $contactParams[$fieldName] = $value;
200 }
201 }
202
203 if (!empty($missingParams)) {
204 return civicrm_api3_create_error("Missing required parameters for profile id {$params['profile_id']}: " . implode(', ', $missingParams));
205 }
206
207 $contactParams['version'] = 3;
208 $contactParams['contact_id'] = CRM_Utils_Array::value('contact_id', $params);
209 $contactParams['profile_id'] = $params['profile_id'];
210 $contactParams['skip_custom'] = 1;
211
212 $contactProfileParams = civicrm_api3_profile_apply($contactParams);
213 if (CRM_Utils_Array::value('is_error', $contactProfileParams)) {
214 return $contactProfileParams;
215 }
216
217 // Contact profile fields
218 $profileParams = $contactProfileParams['values'];
219
220 // If profile having activity fields
221 if ($isContactActivityProfile && !empty($activityParams)) {
222 $activityParams['id'] = $params['activity_id'];
223 $profileParams['api.activity.create'] = $activityParams;
224 }
225
226 $groups = $tags = array();
227 if (isset($profileParams['group'])) {
228 $groups = $profileParams['group'];
229 unset($profileParams['group']);
230 }
231
232 if (isset($profileParams['tag'])) {
233 $tags = $profileParams['tag'];
234 unset($profileParams['tag']);
235 }
236
237 $result = civicrm_api('contact', 'create', $profileParams);
238 if (CRM_Utils_Array::value('is_error', $result)) {
239 return $result;
240 }
241
242 $ufGroupDetails = array();
243 $ufGroupParams = array('id' => $params['profile_id']);
244 CRM_Core_BAO_UFGroup::retrieve($ufGroupParams, $ufGroupDetails);
245
246 if (isset($profileFields['group'])) {
247 CRM_Contact_BAO_GroupContact::create($groups,
248 $params['contact_id'],
249 FALSE,
250 'Admin'
251 );
252 }
253
254 if (isset($profileFields['tag'])) {
255 require_once 'CRM/Core/BAO/EntityTag.php';
256 CRM_Core_BAO_EntityTag::create($tags,
257 'civicrm_contact',
258 $params['contact_id']
259 );
260 }
261
262 if (CRM_Utils_Array::value('add_to_group_id', $ufGroupDetails)) {
263 $contactIds = array($params['contact_id']);
264 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds,
265 $ufGroupDetails['add_to_group_id']
266 );
267 }
268
269 return $result;
270 }
271
272 /**
273 * Provide formatted values for profile fields.
274 *
275 * @param array $params Associative array of property name/value
276 * pairs to profile field values
277 *
278 * @return formatted profile field values|CRM_Error
279 *
280 * @todo add example
281 * @todo add test cases
282 *
283 */
284 function civicrm_api3_profile_apply($params) {
285
286 civicrm_api3_verify_mandatory($params, NULL, array('profile_id'));
287 require_once 'CRM/Contact/BAO/Contact.php';
288
289 if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
290 return civicrm_api3_create_error('Invalid value for profile_id');
291 }
292
293 $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'],
294 FALSE,
295 NULL,
296 NULL,
297 NULL,
298 FALSE,
299 NULL,
300 TRUE,
301 NULL,
302 CRM_Core_Permission::EDIT
303 );
304
305 list($data, $contactDetails) = CRM_Contact_BAO_Contact::formatProfileContactParams($params,
306 $profileFields,
307 CRM_Utils_Array::value('contact_id', $params),
308 $params['profile_id'],
309 CRM_Utils_Array::value('contact_type', $params),
310 CRM_Utils_Array::value('skip_custom', $params, FALSE)
311 );
312
313 if (empty($data)) {
314 return civicrm_api3_create_error('Enable to format profile parameters.');
315 }
316
317 return civicrm_api3_create_success($data);
318 }
319
320 /**
321 * Return UFGroup fields
322 */
323 function civicrm_api3_profile_getfields($params) {
324 $dao = _civicrm_api3_get_DAO('UFGroup');
325 $file = str_replace('_', '/', $dao) . ".php";
326 require_once ($file);
327 $d = new $dao();
328 $fields = $d->fields();
329 return civicrm_api3_create_success($fields);
330 }
331