Merge pull request #16545 from eileenmcnaughton/part_pend
[civicrm-core.git] / api / v3 / OptionValue.php
CommitLineData
6a488035 1<?php
c28e1768
CW
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
c28e1768 5 | |
a30c801b
TO
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 |
c28e1768
CW
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * This api exposes CiviCRM option values.
244bbdd8 14 *
c28e1768
CW
15 * Values are grouped by "OptionGroup"
16 *
17 * @package CiviCRM_APIv3
c28e1768 18 */
6a488035
TO
19
20/**
9d32e6f7 21 * Retrieve one or more option values.
6a488035 22 *
c490a46a 23 * @param array $params
6a488035 24 *
a6c01b45 25 * @return array
00f8641b 26 * API result array
6a488035
TO
27 */
28function civicrm_api3_option_value_get($params) {
a25b46e9 29 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'OptionValue');
6a488035
TO
30}
31
eea0a60f
CW
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 */
40function _civicrm_api3_option_value_get_spec(&$params) {
cf8f0fff 41 $params['option_group_id']['api.aliases'] = ['option_group_name'];
eea0a60f
CW
42}
43
6a488035 44/**
9d32e6f7 45 * Add an OptionValue.
6a488035 46 *
c490a46a 47 * @param array $params
1fd111c8
EM
48 *
49 * @throws API_Exception
a6c01b45 50 * @return array
00f8641b 51 * API result array
6a488035
TO
52 */
53function civicrm_api3_option_value_create($params) {
98fd592b 54 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'OptionValue');
89ab5601
PJ
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
cf8f0fff 64 civicrm_api('option_value', 'getfields', ['version' => 3, 'cache_clear' => 1, 'option_group_id' => $groupId]);
c87bbced 65 return $result;
6a488035
TO
66}
67
11e09c59 68/**
0aa0303c
EM
69 * Adjust Metadata for Create action.
70 *
71 * The metadata is used for setting defaults, documentation & validation.
6a488035 72 *
cf470720 73 * @param array $params
b081365f 74 * Array of parameters determined by getfields.
6a488035
TO
75 */
76function _civicrm_api3_option_value_create_spec(&$params) {
77 $params['is_active']['api.default'] = 1;
c87bbced 78 //continue to support component
cf8f0fff 79 $params['component_id']['api.aliases'] = ['component'];
ad0121ed 80 // $params['name']['api.aliases'] = array('label');
c87bbced 81 $params['option_group_id']['api.required'] = TRUE;
6a488035
TO
82}
83
84/**
9d32e6f7 85 * Deletes an existing option value.
6a488035 86 *
cf470720 87 * @param array $params
8089541a 88 * @return array API result array
8089541a 89 * @throws API_Exception
6a488035
TO
90 */
91function civicrm_api3_option_value_delete($params) {
9d32e6f7 92 // We will get the option group id before deleting so we can flush pseudoconstants.
cf8f0fff 93 $optionGroupID = civicrm_api('option_value', 'getvalue', ['version' => 3, 'id' => $params['id'], 'return' => 'option_group_id']);
a60c0bc8
SL
94 $result = CRM_Core_BAO_OptionValue::del($params['id']);
95 if ($result) {
cf8f0fff 96 civicrm_api('option_value', 'getfields', ['version' => 3, 'cache_clear' => 1, 'option_group_id' => $optionGroupID]);
6a488035
TO
97 return civicrm_api3_create_success();
98 }
92e4c2a5 99 else {
a60c0bc8 100 throw new API_Exception('Could not delete OptionValue ' . $params['id']);
6a488035
TO
101 }
102}