Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /** | |
5 | * Retrieve one or more OptionValues | |
6 | * | |
7 | * @param array $ params input parameters | |
8 | * | |
9 | * {@example OptionValueGet.php 0} | |
10 | * @example OptionValueGet.php | |
11 | * | |
12 | * @return array details of found Option Values | |
13 | * {@getfields OptionValue_get} | |
14 | * @access public | |
15 | */ | |
16 | function civicrm_api3_option_value_get($params) { | |
17 | ||
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"); | |
23 | } | |
24 | $params['option_group_id'] = $optionGroup['id']; | |
25 | } | |
26 | ||
27 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
28 | } | |
29 | ||
30 | /** | |
31 | * Add a OptionValue. OptionValues are used to classify CRM entities (including Contacts, Groups and Actions). | |
32 | * | |
33 | * Allowed @params array keys are: | |
34 | * | |
35 | * {@example OptionValueCreate.php} | |
36 | * | |
37 | * @return array of newly created option_value property values. | |
38 | * {@getfields OptionValue_create} | |
39 | * @access public | |
40 | */ | |
41 | function civicrm_api3_option_value_create($params) { | |
42 | ||
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']; | |
48 | } | |
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']) | |
53 | ); | |
54 | } | |
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']) | |
62 | ); | |
63 | } | |
64 | } | |
65 | ||
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'] = ''; | |
70 | } else { | |
71 | $params['component_id'] = array_search($params['component'], CRM_Core_PseudoConstant::component()); | |
72 | } | |
73 | unset($params['component']); | |
74 | } | |
75 | ||
76 | if (CRM_Utils_Array::value('id', $params)) { | |
77 | $ids = array('optionValue' => $params['id']); | |
78 | } | |
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'])); | |
81 | $values = array(); | |
82 | _civicrm_api3_object_to_array($optionValueBAO, $values[$optionValueBAO->id]); | |
83 | return civicrm_api3_create_success($values, $params); | |
84 | } | |
85 | ||
11e09c59 | 86 | /** |
6a488035 TO |
87 | * Adjust Metadata for Create action |
88 | * | |
89 | * The metadata is used for setting defaults, documentation & validation | |
90 | * @param array $params array or parameters determined by getfields | |
91 | */ | |
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'; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Deletes an existing OptionValue | |
102 | * | |
103 | * @param array $params | |
104 | * | |
105 | * {@example OptionValueDelete.php 0} | |
106 | * | |
107 | * @return array Api result | |
108 | * {@getfields OptionValue_create} | |
109 | * @access public | |
110 | */ | |
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(); | |
117 | } | |
118 | else{ | |
119 | civicrm_api3_create_error('Could not delete OptionValue ' . $params['id']); | |
120 | } | |
121 | } | |
122 |