/**
 * Defines 'uf field' within a group.
 *
 * @param $groupId int Valid uf_group id
 *
 * @param $params  array  Associative array of property name/value pairs to create new uf field.
 *
 * @return Newly created $ufFieldArray array
 *
 * @access public
 * {@getfields UFField_create}
 * @example UFFieldCreate.php
 */
function civicrm_api3_uf_field_create($params)
{
    civicrm_api3_verify_one_mandatory($params, NULL, array('field_name', 'uf_group_id'));
    $groupId = CRM_Utils_Array::value('uf_group_id', $params);
    if ((int) $groupId < 1) {
        return civicrm_api3_create_error('Params must be a field_name-carrying array and a positive integer.');
    }
    $field_type = CRM_Utils_Array::value('field_type', $params);
    $field_name = CRM_Utils_Array::value('field_name', $params);
    $location_type_id = CRM_Utils_Array::value('location_type_id', $params);
    $phone_type = CRM_Utils_Array::value('phone_type', $params);
    $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
    if (!CRM_Utils_Array::value('group_id', $params)) {
        $params['group_id'] = $groupId;
    }
    $ids = array();
    $ids['uf_group'] = $groupId;
    $fieldId = CRM_Utils_Array::value('id', $params);
    if (!empty($fieldId)) {
        $UFField = new CRM_core_BAO_UFField();
        $UFField->id = $fieldId;
        if ($UFField->find(TRUE)) {
            $ids['uf_group'] = $UFField->uf_group_id;
            if (!CRM_Utils_Array::value('group_id', $params)) {
                // this copied here from previous api function - not sure if required
                $params['group_id'] = $UFField->uf_group_id;
            }
        } else {
            return civicrm_api3_create_error("there is no field for this fieldId");
        }
        $ids['uf_field'] = $fieldId;
    }
    if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) {
        return civicrm_api3_create_error("The field was not added. It already exists in this profile.");
    }
    $ufField = CRM_Core_BAO_UFField::add($params, $ids);
    $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($groupId, TRUE);
    CRM_Core_BAO_UFGroup::updateGroupTypes($groupId, $fieldsType);
    _civicrm_api3_object_to_array($ufField, $ufFieldArray[$ufField->id]);
    return civicrm_api3_create_success($ufFieldArray, $params);
}
/**
 * Use this API to update uf field . See the CRM Data Model for uf_field property definitions
 *
 * @param $params  array   Associative array of property name/value pairs to update in field.
 *
 * @param $fieldId int  A valid uf field id that to be updated.
 *
 * @return  updated  $ufFieldArray array
 *
 * @access public
 */
function civicrm_uf_field_update($params, $fieldId)
{
    _civicrm_initialize();
    if (!isset($fieldId)) {
        return civicrm_create_error("parameter fieldId is not set");
    }
    if (!is_array($params)) {
        return civicrm_create_error("params is not an array ");
    }
    $field_type = CRM_Utils_Array::value('field_type', $params);
    $field_name = CRM_Utils_Array::value('field_name', $params);
    $location_type_id = CRM_Utils_Array::value('location_type_id', $params);
    $phone_type = CRM_Utils_Array::value('phone_type', $params);
    $params['field_name'] = array($field_type, $field_name, $location_type_id, $phone_type);
    require_once 'CRM/Core/BAO/UFField.php';
    $UFField = new CRM_core_BAO_UFField();
    $UFField->id = $fieldId;
    if (!CRM_Utils_Array::value('group_id', $params) && $UFField->find(TRUE)) {
        $params['group_id'] = $UFField->uf_group_id;
    }
    $ids = array();
    if ($UFField->find(TRUE)) {
        $ids['uf_group'] = $UFField->uf_group_id;
    } else {
        return civicrm_create_error("there is no field for this fieldId");
    }
    $ids['uf_field'] = $fieldId;
    if (CRM_Core_BAO_UFField::duplicateField($params, $ids)) {
        return civicrm_create_error("The field was not added. It already exists in this profile.");
    }
    $ufField = CRM_Core_BAO_UFField::add($params, $ids);
    _civicrm_object_to_array($ufField, $ufFieldArray);
    return $ufFieldArray;
}