5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * File for the CiviCRM APIv3 activity profile functions
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 $
41 * Include common API util functions
43 require_once 'api/v3/utils.php';
45 require_once 'CRM/Core/BAO/UFGroup.php';
46 require_once 'CRM/Core/BAO/UFField.php';
47 require_once 'CRM/Core/Permission.php';
50 * Retrieve Profile field values.
52 * @param array $params Associative array of property name/value
53 * pairs to get profile field values
55 * @return Profile field values|CRM_Error
58 * @todo add test cases
61 function civicrm_api3_profile_get($params) {
63 civicrm_api3_verify_mandatory($params, NULL, array('profile_id', 'contact_id'));
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');
69 $isContactActivityProfile = CRM_Core_BAO_UFField
::checkContactActivityProfileType($params['profile_id']);
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.');
75 $profileFields = CRM_Core_BAO_UFGroup
::getFields($params['profile_id'],
84 CRM_Core_Permission
::EDIT
88 if ($isContactActivityProfile) {
89 civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
91 require_once 'CRM/Profile/Form.php';
92 $errors = CRM_Profile_Form
::validateContactActivityProfile($params['activity_id'],
93 $params['contact_id'],
96 if (!empty($errors)) {
97 return civicrm_api3_create_error(array_pop($errors));
100 $contactFields = $activityFields = array();
101 foreach ($profileFields as $fieldName => $field) {
102 if (CRM_Utils_Array
::value('field_type', $field) == 'Activity') {
103 $activityFields[$fieldName] = $field;
106 $contactFields[$fieldName] = $field;
110 CRM_Core_BAO_UFGroup
::setProfileDefaults($params['contact_id'], $contactFields, $values, TRUE);
112 if ($params['activity_id']) {
113 CRM_Core_BAO_UFGroup
::setComponentDefaults($activityFields, $params['activity_id'], 'Activity', $values, TRUE);
117 CRM_Core_BAO_UFGroup
::setProfileDefaults($params['contact_id'], $profileFields, $values, TRUE);
120 $result = civicrm_api3_create_success();
121 $result['values'] = $values;
127 * Update Profile field values.
129 * @param array $params Associative array of property name/value
130 * pairs to update profile field values
132 * @return Updated Contact/ Activity object|CRM_Error
135 * @todo add test cases
138 function civicrm_api3_profile_set($params) {
140 civicrm_api3_verify_mandatory($params, NULL, array('profile_id'));
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');
146 $isContactActivityProfile = CRM_Core_BAO_UFField
::checkContactActivityProfileType($params['profile_id']);
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.');
152 $contactParams = $activityParams = $missingParams = array();
154 $profileFields = CRM_Core_BAO_UFGroup
::getFields($params['profile_id'],
163 CRM_Core_Permission
::EDIT
166 if ($isContactActivityProfile) {
167 civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
169 require_once 'CRM/Profile/Form.php';
170 $errors = CRM_Profile_Form
::validateContactActivityProfile($params['activity_id'],
171 $params['contact_id'],
172 $params['profile_id']
174 if (!empty($errors)) {
175 return civicrm_api3_create_error(array_pop($errors));
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;
186 if (!isset($params[$fieldName])) {
190 $value = $params[$fieldName];
191 if ($params[$fieldName] && isset($params[$fieldName . '_id'])) {
192 $value = $params[$fieldName . '_id'];
195 if ($isContactActivityProfile && CRM_Utils_Array
::value('field_type', $field) == 'Activity') {
196 $activityParams[$fieldName] = $value;
199 $contactParams[$fieldName] = $value;
203 if (!empty($missingParams)) {
204 return civicrm_api3_create_error("Missing required parameters for profile id {$params['profile_id']}: " . implode(', ', $missingParams));
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;
212 $contactProfileParams = civicrm_api3_profile_apply($contactParams);
213 if (CRM_Utils_Array
::value('is_error', $contactProfileParams)) {
214 return $contactProfileParams;
217 // Contact profile fields
218 $profileParams = $contactProfileParams['values'];
220 // If profile having activity fields
221 if ($isContactActivityProfile && !empty($activityParams)) {
222 $activityParams['id'] = $params['activity_id'];
223 $profileParams['api.activity.create'] = $activityParams;
226 $groups = $tags = array();
227 if (isset($profileParams['group'])) {
228 $groups = $profileParams['group'];
229 unset($profileParams['group']);
232 if (isset($profileParams['tag'])) {
233 $tags = $profileParams['tag'];
234 unset($profileParams['tag']);
237 $result = civicrm_api('contact', 'create', $profileParams);
238 if (CRM_Utils_Array
::value('is_error', $result)) {
242 $ufGroupDetails = array();
243 $ufGroupParams = array('id' => $params['profile_id']);
244 CRM_Core_BAO_UFGroup
::retrieve($ufGroupParams, $ufGroupDetails);
246 if (isset($profileFields['group'])) {
247 CRM_Contact_BAO_GroupContact
::create($groups,
248 $params['contact_id'],
254 if (isset($profileFields['tag'])) {
255 require_once 'CRM/Core/BAO/EntityTag.php';
256 CRM_Core_BAO_EntityTag
::create($tags,
258 $params['contact_id']
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']
273 * Provide formatted values for profile fields.
275 * @param array $params Associative array of property name/value
276 * pairs to profile field values
278 * @return formatted profile field values|CRM_Error
281 * @todo add test cases
284 function civicrm_api3_profile_apply($params) {
286 civicrm_api3_verify_mandatory($params, NULL, array('profile_id'));
287 require_once 'CRM/Contact/BAO/Contact.php';
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');
293 $profileFields = CRM_Core_BAO_UFGroup
::getFields($params['profile_id'],
302 CRM_Core_Permission
::EDIT
305 list($data, $contactDetails) = CRM_Contact_BAO_Contact
::formatProfileContactParams($params,
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)
314 return civicrm_api3_create_error('Enable to format profile parameters.');
317 return civicrm_api3_create_success($data);
321 * Return UFGroup fields
323 function civicrm_api3_profile_getfields($params) {
324 $dao = _civicrm_api3_get_DAO('UFGroup');
325 $file = str_replace('_', '/', $dao) . ".php";
326 require_once ($file);
328 $fields = $d->fields();
329 return civicrm_api3_create_success($fields);