Ejemplo n.º 1
0
/**
 * Create a 'custom field' within a custom field group.
 * We also empty the static var in the getfields
 * function after deletion so that the field is available for us (getfields manages date conversion
 * among other things
 *
 * @param $params array  Associative array of property name/value pairs to create new custom field.
 *
 * @return Newly API success object
 *
 * @access public
 *
 * @example CustomFieldCreate.php
 * {@getfields CustomField_create}
 * {@example CustomFieldCreate.php 0}
 *
 */
function civicrm_api3_custom_field_create($params)
{
    // Array created for passing options in params
    if (isset($params['option_values']) && is_array($params['option_values'])) {
        foreach ($params['option_values'] as $key => $value) {
            $params['option_label'][$key] = $value['label'];
            $params['option_value'][$key] = $value['value'];
            $params['option_status'][$key] = $value['is_active'];
            $params['option_weight'][$key] = $value['weight'];
        }
    }
    $values = array();
    $customField = CRM_Core_BAO_CustomField::create($params);
    _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]);
    _civicrm_api3_custom_field_flush_static_caches();
    return civicrm_api3_create_success($values, $params, 'custom_field', $customField);
}
Ejemplo n.º 2
0
/**
 * Create a 'custom field' within a custom field group.
 *
 * We also empty the static var in the getfields
 * function after deletion so that the field is available for us (getfields manages date conversion
 * among other things
 *
 * @param array $params
 *   Array per getfields metadata.
 *
 * @return array
 *   API success array
 */
function civicrm_api3_custom_field_create($params)
{
    // Array created for passing options in params.
    if (isset($params['option_values']) && is_array($params['option_values'])) {
        $weight = 0;
        foreach ($params['option_values'] as $key => $value) {
            // Translate simple key/value pairs into full-blown option values
            if (!is_array($value)) {
                $value = array('label' => $value, 'value' => $key, 'is_active' => 1, 'weight' => $weight);
                $key = $weight++;
            }
            $params['option_label'][$key] = $value['label'];
            $params['option_value'][$key] = $value['value'];
            $params['option_status'][$key] = $value['is_active'];
            $params['option_weight'][$key] = $value['weight'];
        }
    }
    $values = array();
    $customField = CRM_Core_BAO_CustomField::create($params);
    _civicrm_api3_object_to_array_unique_fields($customField, $values[$customField->id]);
    _civicrm_api3_custom_field_flush_static_caches();
    return civicrm_api3_create_success($values, $params, 'CustomField', $customField);
}