30b7e26be114d16705fdd807f1a359ed73c5ed5c
5 * Retrieve one or more OptionValues
7 * @param array $ params input parameters
9 * {@example OptionValueGet.php 0}
10 * @example OptionValueGet.php
12 * @return array details of found Option Values
13 * {@getfields OptionValue_get}
16 function civicrm_api3_option_value_get($params) {
18 if (empty($params['option_group_id']) && !empty($params['option_group_name'])) {
19 $opt = array('version' => 3, 'name' => $params['option_group_name']);
20 $optionGroup = civicrm_api('OptionGroup', 'Get', $opt);
21 if (empty($optionGroup['id'])) {
22 return civicrm_api3_create_error("option group name does not correlate to a single option group");
24 $params['option_group_id'] = $optionGroup['id'];
27 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
31 * Add a OptionValue. OptionValues are used to classify CRM entities (including Contacts, Groups and Actions).
33 * Allowed @params array keys are:
35 * {@example OptionValueCreate.php}
37 * @return array of newly created option_value property values.
38 * {@getfields OptionValue_create}
41 function civicrm_api3_option_value_create($params) {
43 // CRM-10921: do not fill-in defaults if this is an update
44 if (!CRM_Utils_Array
::value('id', $params)) {
45 if (!CRM_Utils_Array
::value('label', $params) && CRM_Utils_Array
::value('name', $params)) {
46 // 'label' defaults to 'name'
47 $params['label'] = $params['name'];
49 if (!CRM_Utils_Array
::value('value', $params) && CRM_Utils_Array
::value('option_group_id', $params)) {
50 // 'value' defaults to next weight in option_group
51 $params['value'] = (int) CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue',
52 array('option_group_id' => $params['option_group_id'])
55 if (!CRM_Utils_Array
::value('weight', $params) && CRM_Utils_Array
::value('value', $params)) {
56 // 'weight' defaults to 'value'
57 $params['weight'] = $params['value'];
58 } elseif (CRM_Utils_Array
::value('weight', $params) && $params['weight'] == 'next' && CRM_Utils_Array
::value('option_group_id', $params)) {
59 // weight is numeric, so it's safe-ish to treat symbol 'next' as magical value
60 $params['weight'] = (int) CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_OptionValue',
61 array('option_group_id' => $params['option_group_id'])
66 if (isset($params['component'])) {// allow a component to be reset to ''
67 // convert 'component' to 'component_id'
68 if (empty($params['component'])) {
69 $params['component_id'] = '';
71 $params['component_id'] = array_search($params['component'], CRM_Core_PseudoConstant
::component());
73 unset($params['component']);
76 if (CRM_Utils_Array
::value('id', $params)) {
77 $ids = array('optionValue' => $params['id']);
79 $optionValueBAO = CRM_Core_BAO_OptionValue
::add($params, $ids);
80 civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1, 'option_group_id' => $params['option_group_id']));
82 _civicrm_api3_object_to_array($optionValueBAO, $values[$optionValueBAO->id
]);
83 return civicrm_api3_create_success($values, $params);
87 * Adjust Metadata for Create action
89 * The metadata is used for setting defaults, documentation & validation
90 * @param array $params array or parameters determined by getfields
92 function _civicrm_api3_option_value_create_spec(&$params) {
93 $params['is_active']['api.default'] = 1;
94 $params['component']['type'] = CRM_Utils_Type
::T_STRING
;
95 $params['component']['options'] = array_values(CRM_Core_PseudoConstant
::component());
96 $params['name']['api.aliases'] = array('label');
97 // $params['component_id']['pseudoconstant'] = 'component';
101 * Deletes an existing OptionValue
103 * @param array $params
105 * {@example OptionValueDelete.php 0}
107 * @return array Api result
108 * {@getfields OptionValue_create}
111 function civicrm_api3_option_value_delete($params) {
112 // we will get the option group id before deleting so we can flush pseudoconstants
113 $optionGroupID = civicrm_api('option_value', 'getvalue', array('version' => 3, 'id' => $params['id'], 'return' => 'option_group_id'));
114 if(CRM_Core_BAO_OptionValue
::del((int) $params['id'])){
115 civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1, 'option_group_id' => $optionGroupID));
116 return civicrm_api3_create_success();
119 civicrm_api3_create_error('Could not delete OptionValue ' . $params['id']);