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';
46 * Retrieve Profile field values.
48 * @param array $params Associative array of property name/value
49 * pairs to get profile field values
51 * @return Profile field values|CRM_Error
54 * @todo add test cases
57 function civicrm_api3_profile_get($params) {
59 civicrm_api3_verify_mandatory($params, NULL, array('profile_id', 'contact_id'));
61 if (!CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
62 return civicrm_api3_create_error('Invalid value for profile_id');
65 $isContactActivityProfile = CRM_Core_BAO_UFField
::checkContactActivityProfileType($params['profile_id']);
67 if (CRM_Core_BAO_UFField
::checkProfileType($params['profile_id']) && !$isContactActivityProfile) {
68 return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.');
71 $profileFields = CRM_Core_BAO_UFGroup
::getFields($params['profile_id'],
80 CRM_Core_Permission
::EDIT
84 if ($isContactActivityProfile) {
85 civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
87 $errors = CRM_Profile_Form
::validateContactActivityProfile($params['activity_id'],
88 $params['contact_id'],
91 if (!empty($errors)) {
92 return civicrm_api3_create_error(array_pop($errors));
95 $contactFields = $activityFields = array();
96 foreach ($profileFields as $fieldName => $field) {
97 if (CRM_Utils_Array
::value('field_type', $field) == 'Activity') {
98 $activityFields[$fieldName] = $field;
101 $contactFields[$fieldName] = $field;
105 CRM_Core_BAO_UFGroup
::setProfileDefaults($params['contact_id'], $contactFields, $values, TRUE);
107 if ($params['activity_id']) {
108 CRM_Core_BAO_UFGroup
::setComponentDefaults($activityFields, $params['activity_id'], 'Activity', $values, TRUE);
112 CRM_Core_BAO_UFGroup
::setProfileDefaults($params['contact_id'], $profileFields, $values, TRUE);
115 $result = civicrm_api3_create_success();
116 $result['values'] = $values;
122 * Update Profile field values.
124 * @param array $params Associative array of property name/value
125 * pairs to update profile field values
127 * @return Updated Contact/ Activity object|CRM_Error
130 * @todo add test cases
133 function civicrm_api3_profile_set($params) {
135 civicrm_api3_verify_mandatory($params, NULL, array('profile_id'));
137 if (!CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
138 return civicrm_api3_create_error('Invalid value for profile_id');
141 $isContactActivityProfile = CRM_Core_BAO_UFField
::checkContactActivityProfileType($params['profile_id']);
143 if (CRM_Core_BAO_UFField
::checkProfileType($params['profile_id']) && !$isContactActivityProfile) {
144 return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.');
147 $contactParams = $activityParams = $missingParams = array();
149 $profileFields = CRM_Core_BAO_UFGroup
::getFields($params['profile_id'],
158 CRM_Core_Permission
::EDIT
161 if ($isContactActivityProfile) {
162 civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
164 $errors = CRM_Profile_Form
::validateContactActivityProfile($params['activity_id'],
165 $params['contact_id'],
166 $params['profile_id']
168 if (!empty($errors)) {
169 return civicrm_api3_create_error(array_pop($errors));
173 foreach ($profileFields as $fieldName => $field) {
174 if (CRM_Utils_Array
::value('is_required', $field)) {
175 if (!CRM_Utils_Array
::value($fieldName, $params) ||
empty($params[$fieldName])) {
176 $missingParams[] = $fieldName;
180 if (!isset($params[$fieldName])) {
184 $value = $params[$fieldName];
185 if ($params[$fieldName] && isset($params[$fieldName . '_id'])) {
186 $value = $params[$fieldName . '_id'];
189 if ($isContactActivityProfile && CRM_Utils_Array
::value('field_type', $field) == 'Activity') {
190 $activityParams[$fieldName] = $value;
193 $contactParams[$fieldName] = $value;
197 if (!empty($missingParams)) {
198 return civicrm_api3_create_error("Missing required parameters for profile id {$params['profile_id']}: " . implode(', ', $missingParams));
201 $contactParams['version'] = 3;
202 $contactParams['contact_id'] = CRM_Utils_Array
::value('contact_id', $params);
203 $contactParams['profile_id'] = $params['profile_id'];
204 $contactParams['skip_custom'] = 1;
206 $contactProfileParams = civicrm_api3_profile_apply($contactParams);
207 if (CRM_Utils_Array
::value('is_error', $contactProfileParams)) {
208 return $contactProfileParams;
211 // Contact profile fields
212 $profileParams = $contactProfileParams['values'];
214 // If profile having activity fields
215 if ($isContactActivityProfile && !empty($activityParams)) {
216 $activityParams['id'] = $params['activity_id'];
217 $profileParams['api.activity.create'] = $activityParams;
220 $groups = $tags = array();
221 if (isset($profileParams['group'])) {
222 $groups = $profileParams['group'];
223 unset($profileParams['group']);
226 if (isset($profileParams['tag'])) {
227 $tags = $profileParams['tag'];
228 unset($profileParams['tag']);
231 $result = civicrm_api('contact', 'create', $profileParams);
232 if (CRM_Utils_Array
::value('is_error', $result)) {
236 $ufGroupDetails = array();
237 $ufGroupParams = array('id' => $params['profile_id']);
238 CRM_Core_BAO_UFGroup
::retrieve($ufGroupParams, $ufGroupDetails);
240 if (isset($profileFields['group'])) {
241 CRM_Contact_BAO_GroupContact
::create($groups,
242 $params['contact_id'],
248 if (isset($profileFields['tag'])) {
249 CRM_Core_BAO_EntityTag
::create($tags,
251 $params['contact_id']
255 if (CRM_Utils_Array
::value('add_to_group_id', $ufGroupDetails)) {
256 $contactIds = array($params['contact_id']);
257 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIds,
258 $ufGroupDetails['add_to_group_id']
266 * Provide formatted values for profile fields.
268 * @param array $params Associative array of property name/value
269 * pairs to profile field values
271 * @return formatted profile field values|CRM_Error
274 * @todo add test cases
277 function civicrm_api3_profile_apply($params) {
279 civicrm_api3_verify_mandatory($params, NULL, array('profile_id'));
281 if (!CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
282 return civicrm_api3_create_error('Invalid value for profile_id');
285 $profileFields = CRM_Core_BAO_UFGroup
::getFields($params['profile_id'],
294 CRM_Core_Permission
::EDIT
297 list($data, $contactDetails) = CRM_Contact_BAO_Contact
::formatProfileContactParams($params,
299 CRM_Utils_Array
::value('contact_id', $params),
300 $params['profile_id'],
301 CRM_Utils_Array
::value('contact_type', $params),
302 CRM_Utils_Array
::value('skip_custom', $params, FALSE)
306 return civicrm_api3_create_error('Enable to format profile parameters.');
309 return civicrm_api3_create_success($data);
313 * Return UFGroup fields
315 function civicrm_api3_profile_getfields($params) {
316 $dao = _civicrm_api3_get_DAO('UFGroup');
318 $fields = $d->fields();
319 return civicrm_api3_create_success($fields);