4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
30 * File for the CiviCRM APIv3 activity profile functions
32 * @package CiviCRM_APIv3
33 * @subpackage API_ActivityProfile
34 * @copyright CiviCRM LLC (c) 2004-2013
35 * @version $Id: ActivityProfile.php 30486 2011-05-20 16:12:09Z rajan $
40 * Include common API util functions
42 require_once 'api/v3/utils.php';
45 * Retrieve Profile field values.
47 * @param array $params Associative array of property name/value
48 * pairs to get profile field values
50 * @return Profile field values|CRM_Error
53 * @todo add test cases
56 function civicrm_api3_profile_get($params) {
58 civicrm_api3_verify_mandatory($params, NULL, array('profile_id', 'contact_id'));
60 if (!CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
61 return civicrm_api3_create_error('Invalid value for profile_id');
64 $isContactActivityProfile = CRM_Core_BAO_UFField
::checkContactActivityProfileType($params['profile_id']);
66 if (CRM_Core_BAO_UFField
::checkProfileType($params['profile_id']) && !$isContactActivityProfile) {
67 return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.');
70 $profileFields = CRM_Core_BAO_UFGroup
::getFields($params['profile_id'],
79 CRM_Core_Permission
::EDIT
83 if ($isContactActivityProfile) {
84 civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
86 $errors = CRM_Profile_Form
::validateContactActivityProfile($params['activity_id'],
87 $params['contact_id'],
90 if (!empty($errors)) {
91 return civicrm_api3_create_error(array_pop($errors));
94 $contactFields = $activityFields = array();
95 foreach ($profileFields as $fieldName => $field) {
96 if (CRM_Utils_Array
::value('field_type', $field) == 'Activity') {
97 $activityFields[$fieldName] = $field;
100 $contactFields[$fieldName] = $field;
104 CRM_Core_BAO_UFGroup
::setProfileDefaults($params['contact_id'], $contactFields, $values, TRUE);
106 if ($params['activity_id']) {
107 CRM_Core_BAO_UFGroup
::setComponentDefaults($activityFields, $params['activity_id'], 'Activity', $values, TRUE);
111 CRM_Core_BAO_UFGroup
::setProfileDefaults($params['contact_id'], $profileFields, $values, TRUE);
114 $result = civicrm_api3_create_success();
115 $result['values'] = $values;
121 * Update Profile field values.
123 * @param array $params Associative array of property name/value
124 * pairs to update profile field values
126 * @return Updated Contact/ Activity object|CRM_Error
129 * @todo add test cases
132 function civicrm_api3_profile_set($params) {
134 civicrm_api3_verify_mandatory($params, NULL, array('profile_id'));
136 if (!CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
137 return civicrm_api3_create_error('Invalid value for profile_id');
140 $isContactActivityProfile = CRM_Core_BAO_UFField
::checkContactActivityProfileType($params['profile_id']);
142 if (CRM_Core_BAO_UFField
::checkProfileType($params['profile_id']) && !$isContactActivityProfile) {
143 return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.');
146 $contactParams = $activityParams = $missingParams = array();
148 $profileFields = CRM_Core_BAO_UFGroup
::getFields($params['profile_id'],
157 CRM_Core_Permission
::EDIT
160 if ($isContactActivityProfile) {
161 civicrm_api3_verify_mandatory($params, NULL, array('activity_id'));
163 $errors = CRM_Profile_Form
::validateContactActivityProfile($params['activity_id'],
164 $params['contact_id'],
165 $params['profile_id']
167 if (!empty($errors)) {
168 return civicrm_api3_create_error(array_pop($errors));
172 foreach ($profileFields as $fieldName => $field) {
173 if (CRM_Utils_Array
::value('is_required', $field)) {
174 if (!CRM_Utils_Array
::value($fieldName, $params) ||
empty($params[$fieldName])) {
175 $missingParams[] = $fieldName;
179 if (!isset($params[$fieldName])) {
183 $value = $params[$fieldName];
184 if ($params[$fieldName] && isset($params[$fieldName . '_id'])) {
185 $value = $params[$fieldName . '_id'];
188 if ($isContactActivityProfile && CRM_Utils_Array
::value('field_type', $field) == 'Activity') {
189 $activityParams[$fieldName] = $value;
192 $contactParams[$fieldName] = $value;
196 if (!empty($missingParams)) {
197 return civicrm_api3_create_error("Missing required parameters for profile id {$params['profile_id']}: " . implode(', ', $missingParams));
200 $contactParams['version'] = 3;
201 $contactParams['contact_id'] = CRM_Utils_Array
::value('contact_id', $params);
202 $contactParams['profile_id'] = $params['profile_id'];
203 $contactParams['skip_custom'] = 1;
205 $contactProfileParams = civicrm_api3_profile_apply($contactParams);
206 if (CRM_Utils_Array
::value('is_error', $contactProfileParams)) {
207 return $contactProfileParams;
210 // Contact profile fields
211 $profileParams = $contactProfileParams['values'];
213 // If profile having activity fields
214 if ($isContactActivityProfile && !empty($activityParams)) {
215 $activityParams['id'] = $params['activity_id'];
216 $profileParams['api.activity.create'] = $activityParams;
219 $groups = $tags = array();
220 if (isset($profileParams['group'])) {
221 $groups = $profileParams['group'];
222 unset($profileParams['group']);
225 if (isset($profileParams['tag'])) {
226 $tags = $profileParams['tag'];
227 unset($profileParams['tag']);
230 $result = civicrm_api('contact', 'create', $profileParams);
231 if (CRM_Utils_Array
::value('is_error', $result)) {
235 $ufGroupDetails = array();
236 $ufGroupParams = array('id' => $params['profile_id']);
237 CRM_Core_BAO_UFGroup
::retrieve($ufGroupParams, $ufGroupDetails);
239 if (isset($profileFields['group'])) {
240 CRM_Contact_BAO_GroupContact
::create($groups,
241 $params['contact_id'],
247 if (isset($profileFields['tag'])) {
248 CRM_Core_BAO_EntityTag
::create($tags,
250 $params['contact_id']
254 if (CRM_Utils_Array
::value('add_to_group_id', $ufGroupDetails)) {
255 $contactIds = array($params['contact_id']);
256 CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactIds,
257 $ufGroupDetails['add_to_group_id']
265 * Provide formatted values for profile fields.
267 * @param array $params Associative array of property name/value
268 * pairs to profile field values
270 * @return formatted profile field values|CRM_Error
273 * @todo add test cases
276 function civicrm_api3_profile_apply($params) {
278 civicrm_api3_verify_mandatory($params, NULL, array('profile_id'));
280 if (!CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) {
281 return civicrm_api3_create_error('Invalid value for profile_id');
284 $profileFields = CRM_Core_BAO_UFGroup
::getFields($params['profile_id'],
293 CRM_Core_Permission
::EDIT
296 list($data, $contactDetails) = CRM_Contact_BAO_Contact
::formatProfileContactParams($params,
298 CRM_Utils_Array
::value('contact_id', $params),
299 $params['profile_id'],
300 CRM_Utils_Array
::value('contact_type', $params),
301 CRM_Utils_Array
::value('skip_custom', $params, FALSE)
305 return civicrm_api3_create_error('Enable to format profile parameters.');
308 return civicrm_api3_create_success($data);
312 * Return UFGroup fields
314 function civicrm_api3_profile_getfields($params) {
315 $dao = _civicrm_api3_get_DAO('UFGroup');
317 $fields = $d->fields();
318 return civicrm_api3_create_success($fields);