(NFC) (dev/core#878) Simplify copyright header (api/*)
[civicrm-core.git] / api / v3 / OptionValue.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * This api exposes CiviCRM option values.
14 *
15 * Values are grouped by "OptionGroup"
16 *
17 * @package CiviCRM_APIv3
18 */
19
20 /**
21 * Retrieve one or more option values.
22 *
23 * @param array $params
24 *
25 * @return array
26 * API result array
27 */
28 function civicrm_api3_option_value_get($params) {
29 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'OptionValue');
30 }
31
32 /**
33 * Adjust Metadata for get action.
34 *
35 * The metadata is used for setting defaults, documentation & validation.
36 *
37 * @param array $params
38 * Array of parameters determined by getfields.
39 */
40 function _civicrm_api3_option_value_get_spec(&$params) {
41 $params['option_group_id']['api.aliases'] = ['option_group_name'];
42 }
43
44 /**
45 * Add an OptionValue.
46 *
47 * @param array $params
48 *
49 * @throws API_Exception
50 * @return array
51 * API result array
52 */
53 function civicrm_api3_option_value_create($params) {
54 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'OptionValue');
55 if (!empty($params['id']) && !array_key_exists('option_group_id', $params)) {
56 $groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
57 $params['id'], 'option_group_id', 'id'
58 );
59 }
60 else {
61 $groupId = $params['option_group_id'];
62 }
63
64 civicrm_api('option_value', 'getfields', ['version' => 3, 'cache_clear' => 1, 'option_group_id' => $groupId]);
65 return $result;
66 }
67
68 /**
69 * Adjust Metadata for Create action.
70 *
71 * The metadata is used for setting defaults, documentation & validation.
72 *
73 * @param array $params
74 * Array of parameters determined by getfields.
75 */
76 function _civicrm_api3_option_value_create_spec(&$params) {
77 $params['is_active']['api.default'] = 1;
78 //continue to support component
79 $params['component_id']['api.aliases'] = ['component'];
80 // $params['name']['api.aliases'] = array('label');
81 $params['option_group_id']['api.required'] = TRUE;
82 }
83
84 /**
85 * Deletes an existing option value.
86 *
87 * @param array $params
88 * @return array API result array
89 * @throws API_Exception
90 */
91 function civicrm_api3_option_value_delete($params) {
92 // We will get the option group id before deleting so we can flush pseudoconstants.
93 $optionGroupID = civicrm_api('option_value', 'getvalue', ['version' => 3, 'id' => $params['id'], 'return' => 'option_group_id']);
94 $result = CRM_Core_BAO_OptionValue::del($params['id']);
95 if ($result) {
96 civicrm_api('option_value', 'getfields', ['version' => 3, 'cache_clear' => 1, 'option_group_id' => $optionGroupID]);
97 return civicrm_api3_create_success();
98 }
99 else {
100 throw new API_Exception('Could not delete OptionValue ' . $params['id']);
101 }
102 }