CRM-15578 - Mailing.create API - Set defaults that match UX
[civicrm-core.git] / api / v3 / OptionValue.php
CommitLineData
6a488035 1<?php
c28e1768
CW
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 * This api exposes CiviCRM option values.
30 * Values are grouped by "OptionGroup"
31 *
32 * @package CiviCRM_APIv3
c28e1768 33 */
6a488035
TO
34
35/**
9d32e6f7 36 * Retrieve one or more option values.
6a488035 37 *
c490a46a 38 * @param array $params
6a488035 39 *
a6c01b45 40 * @return array
9d32e6f7 41 * Details of found Option Values
6a488035
TO
42 */
43function civicrm_api3_option_value_get($params) {
44
45 if (empty($params['option_group_id']) && !empty($params['option_group_name'])) {
46 $opt = array('version' => 3, 'name' => $params['option_group_name']);
47 $optionGroup = civicrm_api('OptionGroup', 'Get', $opt);
48 if (empty($optionGroup['id'])) {
49 return civicrm_api3_create_error("option group name does not correlate to a single option group");
50 }
51 $params['option_group_id'] = $optionGroup['id'];
52 }
53
54 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
55}
56
57/**
9d32e6f7 58 * Add an OptionValue.
6a488035 59 *
c490a46a 60 * @param array $params
1fd111c8
EM
61 *
62 * @throws API_Exception
a6c01b45 63 * @return array
16b10e64 64 * Array of newly created option_value property values.
6a488035
TO
65 */
66function civicrm_api3_option_value_create($params) {
c87bbced 67 $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
89ab5601
PJ
68 if (!empty($params['id']) && !array_key_exists('option_group_id', $params)) {
69 $groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue',
70 $params['id'], 'option_group_id', 'id'
71 );
72 }
73 else {
74 $groupId = $params['option_group_id'];
75 }
76
77 civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1, 'option_group_id' => $groupId));
c87bbced 78 return $result;
6a488035
TO
79}
80
11e09c59 81/**
0aa0303c
EM
82 * Adjust Metadata for Create action.
83 *
84 * The metadata is used for setting defaults, documentation & validation.
6a488035 85 *
cf470720 86 * @param array $params
b081365f 87 * Array of parameters determined by getfields.
6a488035
TO
88 */
89function _civicrm_api3_option_value_create_spec(&$params) {
90 $params['is_active']['api.default'] = 1;
c87bbced 91 //continue to support component
92 $params['component_id']['api.aliases'] = array('component');
6a488035 93 $params['name']['api.aliases'] = array('label');
c87bbced 94 $params['option_group_id']['api.required'] = TRUE;
6a488035
TO
95}
96
97/**
9d32e6f7 98 * Deletes an existing option value.
6a488035 99 *
cf470720 100 * @param array $params
6a488035 101 *
a6c01b45 102 * @return array
72b3a70c 103 * Api result
6a488035
TO
104 */
105function civicrm_api3_option_value_delete($params) {
9d32e6f7 106 // We will get the option group id before deleting so we can flush pseudoconstants.
6a488035 107 $optionGroupID = civicrm_api('option_value', 'getvalue', array('version' => 3, 'id' => $params['id'], 'return' => 'option_group_id'));
9b873358 108 if (CRM_Core_BAO_OptionValue::del((int) $params['id'])) {
6a488035
TO
109 civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1, 'option_group_id' => $optionGroupID));
110 return civicrm_api3_create_success();
111 }
92e4c2a5 112 else {
6a488035
TO
113 civicrm_api3_create_error('Could not delete OptionValue ' . $params['id']);
114 }
115}