$field) { if (CRM_Utils_Array::value('field_type', $field) == 'Activity') { $activityFields[$fieldName] = $field; } else { $contactFields[$fieldName] = $field; } } CRM_Core_BAO_UFGroup::setProfileDefaults($params['contact_id'], $contactFields, $values, TRUE); if ($params['activity_id']) { CRM_Core_BAO_UFGroup::setComponentDefaults($activityFields, $params['activity_id'], 'Activity', $values, TRUE); } } else { CRM_Core_BAO_UFGroup::setProfileDefaults($params['contact_id'], $profileFields, $values, TRUE); } $result = civicrm_api3_create_success(); $result['values'] = $values; return $result; } /** * Update Profile field values. * * @param array $params Associative array of property name/value * pairs to update profile field values * * @return Updated Contact/ Activity object|CRM_Error * * @todo add example * @todo add test cases * */ function civicrm_api3_profile_set($params) { civicrm_api3_verify_mandatory($params, NULL, array('profile_id')); if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) { return civicrm_api3_create_error('Invalid value for profile_id'); } $isContactActivityProfile = CRM_Core_BAO_UFField::checkContactActivityProfileType($params['profile_id']); if (CRM_Core_BAO_UFField::checkProfileType($params['profile_id']) && !$isContactActivityProfile) { return civicrm_api3_create_error('Can not retrieve values for profiles include fields for more than one record type.'); } $contactParams = $activityParams = $missingParams = array(); $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'], FALSE, NULL, NULL, NULL, FALSE, NULL, TRUE, NULL, CRM_Core_Permission::EDIT ); if ($isContactActivityProfile) { civicrm_api3_verify_mandatory($params, NULL, array('activity_id')); $errors = CRM_Profile_Form::validateContactActivityProfile($params['activity_id'], $params['contact_id'], $params['profile_id'] ); if (!empty($errors)) { return civicrm_api3_create_error(array_pop($errors)); } } foreach ($profileFields as $fieldName => $field) { if (CRM_Utils_Array::value('is_required', $field)) { if (!CRM_Utils_Array::value($fieldName, $params) || empty($params[$fieldName])) { $missingParams[] = $fieldName; } } if (!isset($params[$fieldName])) { continue; } $value = $params[$fieldName]; if ($params[$fieldName] && isset($params[$fieldName . '_id'])) { $value = $params[$fieldName . '_id']; } if ($isContactActivityProfile && CRM_Utils_Array::value('field_type', $field) == 'Activity') { $activityParams[$fieldName] = $value; } else { $contactParams[$fieldName] = $value; } } if (!empty($missingParams)) { return civicrm_api3_create_error("Missing required parameters for profile id {$params['profile_id']}: " . implode(', ', $missingParams)); } $contactParams['version'] = 3; $contactParams['contact_id'] = CRM_Utils_Array::value('contact_id', $params); $contactParams['profile_id'] = $params['profile_id']; $contactParams['skip_custom'] = 1; $contactProfileParams = civicrm_api3_profile_apply($contactParams); if (CRM_Utils_Array::value('is_error', $contactProfileParams)) { return $contactProfileParams; } // Contact profile fields $profileParams = $contactProfileParams['values']; // If profile having activity fields if ($isContactActivityProfile && !empty($activityParams)) { $activityParams['id'] = $params['activity_id']; $profileParams['api.activity.create'] = $activityParams; } $groups = $tags = array(); if (isset($profileParams['group'])) { $groups = $profileParams['group']; unset($profileParams['group']); } if (isset($profileParams['tag'])) { $tags = $profileParams['tag']; unset($profileParams['tag']); } $result = civicrm_api('contact', 'create', $profileParams); if (CRM_Utils_Array::value('is_error', $result)) { return $result; } $ufGroupDetails = array(); $ufGroupParams = array('id' => $params['profile_id']); CRM_Core_BAO_UFGroup::retrieve($ufGroupParams, $ufGroupDetails); if (isset($profileFields['group'])) { CRM_Contact_BAO_GroupContact::create($groups, $params['contact_id'], FALSE, 'Admin' ); } if (isset($profileFields['tag'])) { CRM_Core_BAO_EntityTag::create($tags, 'civicrm_contact', $params['contact_id'] ); } if (CRM_Utils_Array::value('add_to_group_id', $ufGroupDetails)) { $contactIds = array($params['contact_id']); CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $ufGroupDetails['add_to_group_id'] ); } return $result; } /** * Provide formatted values for profile fields. * * @param array $params Associative array of property name/value * pairs to profile field values * * @return formatted profile field values|CRM_Error * * @todo add example * @todo add test cases * */ function civicrm_api3_profile_apply($params) { civicrm_api3_verify_mandatory($params, NULL, array('profile_id')); if (!CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $params['profile_id'], 'is_active')) { return civicrm_api3_create_error('Invalid value for profile_id'); } $profileFields = CRM_Core_BAO_UFGroup::getFields($params['profile_id'], FALSE, NULL, NULL, NULL, FALSE, NULL, TRUE, NULL, CRM_Core_Permission::EDIT ); list($data, $contactDetails) = CRM_Contact_BAO_Contact::formatProfileContactParams($params, $profileFields, CRM_Utils_Array::value('contact_id', $params), $params['profile_id'], CRM_Utils_Array::value('contact_type', $params), CRM_Utils_Array::value('skip_custom', $params, FALSE) ); if (empty($data)) { return civicrm_api3_create_error('Enable to format profile parameters.'); } return civicrm_api3_create_success($data); } /** * Return UFGroup fields */ function civicrm_api3_profile_getfields($params) { $dao = _civicrm_api3_get_DAO('UFGroup'); $d = new $dao(); $fields = $d->fields(); return civicrm_api3_create_success($fields); }