3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This api exposes CiviCRM configuration settings.
31 * @package CiviCRM_APIv3
35 * Get fields for setting api calls.
37 * @param array $params
41 function civicrm_api3_setting_getfields($params) {
42 if (!empty($params['action']) && strtolower($params['action']) == 'getvalue') {
45 'title' => 'name of setting field',
47 'type' => CRM_Utils_Type
::T_STRING
),
50 'title' => 'Setting Group',
51 'description' => 'Settings Group. This is required if the setting is not stored in config',
52 'type' => CRM_Utils_Type
::T_STRING
),
54 return civicrm_api3_create_success($result, $params, 'Setting', 'getfields');
56 if (!empty($params['name'])) {
57 //am of two minds about special handling for 'name' as opposed to other filters - but is does make most common
59 $params['filters']['name'] = $params['name'];
61 $result = CRM_Core_BAO_Setting
::getSettingSpecification(
62 CRM_Utils_Array
::value('component_id', $params),
63 CRM_Utils_Array
::value('filters', $params, array()),
64 CRM_Utils_Array
::value('domain_id', $params, NULL),
65 CRM_Utils_Array
::value('profile', $params, NULL)
67 // find any supplemental information
68 if (!empty($params['action'])) {
69 $specFunction = '_civicrm_api3_setting_' . strtolower($params['action']) . '_spec';
70 if (function_exists($specFunction)) {
71 $specFunction($result);
74 return civicrm_api3_create_success($result, $params, 'Setting', 'getfields');
78 * Alter metadata for getfields functions.
80 * @param array $params
82 function _civicrm_api3_setting_getfields_spec(&$params) {
83 $params['filters'] = array(
85 'description' => 'Fields you wish to filter by e.g. array("group_name" => "CiviCRM Preferences")',
87 $params['component_id'] = array(
88 'title' => 'Component ID',
89 'description' => 'ID of relevant component',
91 $params['profile'] = array(
93 'description' => 'Profile is passed through to hooks & added to cachestring',
98 * Return default values for settings.
100 * We will domain key this as it could vary by domain (ie. urls)
101 * as we will be creating the option for a function rather than an value to be in the defaults
102 * Note that is not in place as yet.
104 * @param array $params
107 * @throws \CiviCRM_API3_Exception
110 function civicrm_api3_setting_getdefaults(&$params) {
111 $settings = civicrm_api3('Setting', 'getfields', $params);
112 $domains = _civicrm_api3_setting_getDomainArray($params);
114 foreach ($domains as $domainID) {
115 $defaults[$domainID] = array();
116 foreach ($settings['values'] as $setting => $spec) {
117 if (array_key_exists('default', $spec) && !is_null($spec['default'])) {
118 $defaults[$domainID][$setting] = $spec['default'];
122 return civicrm_api3_create_success($defaults, $params, 'Setting', 'getfields');
125 * Metadata for Setting create function.
127 * @param array $params
128 * Parameters as passed to the API.
130 function _civicrm_api3_setting_getdefaults_spec(&$params) {
131 $params['domain_id'] = array(
132 'api.default' => 'current_domain',
133 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the current domain
134 an array or "all" are acceptable values for multiple domains',
135 'title' => 'Setting Domain',
140 * Get options for settings.
142 * @param array $params
145 * @throws \API_Exception
147 function civicrm_api3_setting_getoptions($params) {
148 $specs = CRM_Core_BAO_Setting
::getSettingSpecification();
150 if (empty($specs[$params['field']]) ||
empty($specs[$params['field']]['pseudoconstant'])) {
151 throw new API_Exception("The field '" . $params['field'] . "' has no associated option list.");
154 $pseudoconstant = $specs[$params['field']]['pseudoconstant'];
156 // It would be nice if we could leverage CRM_Core_PseudoConstant::get() somehow,
157 // but it's tightly coupled to DAO/field. However, if you really need to support
158 // more pseudoconstant types, then probably best to refactor it. For now, KISS.
159 if (!empty($pseudoconstant['callback'])) {
160 $values = Civi\Core\Resolver
::singleton()->call($pseudoconstant['callback'], array());
161 return civicrm_api3_create_success($values, $params, 'Setting', 'getoptions');
163 elseif (!empty($pseudoconstant['optionGroupName'])) {
164 $keyColumn = 'value';
165 if (!empty($pseudoconstant['keyColumn'])) {
166 $keyColumn = $pseudoconstant['keyColumn'];
168 return civicrm_api3_create_success(
169 CRM_Core_OptionGroup
::values($pseudoconstant['optionGroupName'], FALSE, FALSE, TRUE, NULL, 'label', TRUE, FALSE, $keyColumn),
170 $params, 'Setting', 'getoptions'
174 throw new API_Exception("The field '" . $params['field'] . "' uses an unsupported option list.");
178 * Revert settings to defaults.
180 * @param array $params
185 function civicrm_api3_setting_revert(&$params) {
186 $defaults = civicrm_api('Setting', 'getdefaults', $params);
187 $fields = civicrm_api('Setting', 'getfields', $params);
188 $fields = $fields['values'];
189 $domains = _civicrm_api3_setting_getDomainArray($params);
191 foreach ($domains as $domainID) {
192 $valuesToRevert = array_intersect_key($defaults['values'][$domainID], $fields);
193 if (!empty($valuesToRevert)) {
194 $valuesToRevert['version'] = $params['version'];
195 $valuesToRevert['domain_id'] = $domainID;
196 // note that I haven't looked at how the result would appear with multiple domains in play
197 $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToRevert));
201 return civicrm_api3_create_success($result, $params, 'Setting', 'revert');
205 * Alter metadata for getfields functions.
207 * @param array $params
209 function _civicrm_api3_setting_revert_spec(&$params) {
210 $params['name'] = array(
212 'description' => 'Setting Name belongs to',
214 $params['component_id'] = array(
215 'title' => 'Component ID',
216 'description' => 'ID of relevant component',
218 $params['domain_id'] = array(
219 'api.default' => 'current_domain',
220 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the current domain'
221 . ' an array or "all" are acceptable values for multiple domains',
222 'title' => 'Setting Domain',
227 * Revert settings to defaults.
229 * @param array $params
232 * @throws \CiviCRM_API3_Exception
235 function civicrm_api3_setting_fill(&$params) {
236 $defaults = civicrm_api3('Setting', 'getdefaults', $params);
237 $domains = _civicrm_api3_setting_getDomainArray($params);
239 foreach ($domains as $domainID) {
241 'version' => $params['version'],
242 'domain_id' => $domainID,
244 $existing = civicrm_api3('Setting', 'get', $apiArray);
245 $valuesToFill = array_diff_key($defaults['values'][$domainID], $existing['values'][$domainID]);
246 if (!empty($valuesToFill)) {
247 $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToFill +
$apiArray));
250 return civicrm_api3_create_success($result, $params, 'Setting', 'fill');
254 * Alter metadata for getfields functions.
256 * @param array $params
258 function _civicrm_api3_setting_fill_spec(&$params) {
259 $params['name'] = array(
261 'description' => 'Setting Name belongs to',
263 $params['component_id'] = array(
264 'title' => 'Component ID',
265 'description' => 'ID of relevant component',
267 $params['domain_id'] = array(
268 'api.default' => 'current_domain',
269 'title' => 'Setting Domain',
270 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the '
271 . 'current domain, an array or "all" are acceptable values for multiple domains',
276 * Create or update a setting.
278 * @param array $params
279 * Parameters as per getfields.
284 function civicrm_api3_setting_create($params) {
285 $domains = _civicrm_api3_setting_getDomainArray($params);
286 $result = CRM_Core_BAO_Setting
::setItems($params, $domains);
287 return civicrm_api3_create_success($result, $params, 'Setting', 'create');
291 * Metadata for setting create function.
293 * @param array $params
294 * Parameters as passed to the API.
296 function _civicrm_api3_setting_create_spec(&$params) {
297 $params['domain_id'] = array(
298 'api.default' => 'current_domain',
299 'title' => 'Setting Domain',
300 'description' => 'if you do not pass in a domain id this will default to the current domain
301 an array or "all" are acceptable values for multiple domains',
303 $params['group'] = array(
304 'title' => 'Setting Group',
305 'description' => 'if you know the group defining it will make the api more efficient',
310 * Returns array of settings matching input parameters.
312 * @param array $params
313 * Array of one or more valid property_name=>value pairs.
316 * Array of matching settings
318 function civicrm_api3_setting_get($params) {
319 $domains = _civicrm_api3_setting_getDomainArray($params);
320 $result = CRM_Core_BAO_Setting
::getItems($params, $domains, CRM_Utils_Array
::value('return', $params, array()));
321 return civicrm_api3_create_success($result, $params, 'Setting', 'get');
324 * Metadata for setting create function.
326 * @param array $params
327 * Parameters as passed to the API.
329 function _civicrm_api3_setting_get_spec(&$params) {
330 $params['domain_id'] = array(
331 'api.default' => 'current_domain',
332 'title' => 'Setting Domain',
333 'description' => 'if you do not pass in a domain id this will default to the current domain',
335 $params['group'] = array(
336 'title' => 'Setting Group',
337 'description' => 'if you know the group defining it will make the api more efficient',
341 * Returns value for specific parameter.
343 * Function requires more fields than 'get' but is intended for
344 * runtime usage & should be quicker
346 * @param array $params
347 * Array of one or more valid.
348 * property_name=>value pairs.
353 function civicrm_api3_setting_getvalue($params) {
354 //$config = CRM_Core_Config::singleton();
355 //if (isset($config->$params['name'])) {
356 // return $config->$params['name'];
358 return CRM_Core_BAO_Setting
::getItem(
360 CRM_Utils_Array
::value('name', $params),
361 CRM_Utils_Array
::value('component_id', $params),
362 CRM_Utils_Array
::value('default_value', $params),
363 CRM_Utils_Array
::value('contact_id', $params),
364 CRM_Utils_Array
::value('domain_id', $params)
369 * Metadata for setting create function.
371 * @param array $params
372 * Parameters as passed to the API.
374 function _civicrm_api3_setting_getvalue_spec(&$params) {
376 $params['group'] = array(
377 'title' => 'Settings Group',
378 'api.required' => TRUE,
380 $params['name'] = array(
381 'title' => 'Setting Name',
382 'api.aliases' => array('return'),
384 $params['default_value'] = array(
385 'title' => 'Default Value',
387 $params['component_id'] = array(
388 'title' => 'Component Id',
390 $params['contact_id'] = array(
391 'title' => 'Contact Id',
393 $params['domain_id'] = array(
394 'title' => 'Setting Domain',
395 'description' => 'if you do not pass in a domain id this will default to the current domain',
400 * Converts domain input into an array.
402 * If an array is passed in this is used, if 'all' is passed
403 * in this is converted to 'all arrays'
405 * Really domain_id should always be set but doing an empty check because at the moment
406 * using crm-editable will pass an id & default won't be applied
407 * we did talk about id being a pseudonym for domain_id in this api so applying it here.
409 * @param array $params
414 function _civicrm_api3_setting_getDomainArray(&$params) {
415 if (empty($params['domain_id']) && isset($params['id'])) {
416 $params['domain_id'] = $params['id'];
419 if ($params['domain_id'] == 'current_domain') {
420 $params['domain_id'] = CRM_Core_Config
::domainID();
423 if ($params['domain_id'] == 'all') {
424 $domainAPIResult = civicrm_api('domain', 'get', array('version' => 3, 'return' => 'id'));
425 if (isset($domainAPIResult['values'])) {
426 $params['domain_id'] = array_keys($domainAPIResult['values']);
429 throw new Exception('All domains not retrieved - problem with Domain Get api call ' . $domainAPIResult['error_message']);
432 if (is_array($params['domain_id'])) {
433 $domains = $params['domain_id'];
436 $domains = array($params['domain_id']);