3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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');
164 throw new API_Exception("The field '" . $params['field'] . "' uses an unsupported option list.");
168 * Revert settings to defaults.
170 * @param array $params
175 function civicrm_api3_setting_revert(&$params) {
176 $defaults = civicrm_api('Setting', 'getdefaults', $params);
177 $fields = civicrm_api('Setting', 'getfields', $params);
178 $fields = $fields['values'];
179 $domains = _civicrm_api3_setting_getDomainArray($params);
181 foreach ($domains as $domainID) {
182 $valuesToRevert = array_intersect_key($defaults['values'][$domainID], $fields);
183 if (!empty($valuesToRevert)) {
184 $valuesToRevert['version'] = $params['version'];
185 $valuesToRevert['domain_id'] = $domainID;
186 // note that I haven't looked at how the result would appear with multiple domains in play
187 $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToRevert));
191 return civicrm_api3_create_success($result, $params, 'Setting', 'revert');
195 * Alter metadata for getfields functions.
197 * @param array $params
199 function _civicrm_api3_setting_revert_spec(&$params) {
200 $params['name'] = array(
202 'description' => 'Setting Name belongs to',
204 $params['component_id'] = array(
205 'title' => 'Component ID',
206 'description' => 'ID of relevant component',
208 $params['domain_id'] = array(
209 'api.default' => 'current_domain',
210 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the current domain'
211 . ' an array or "all" are acceptable values for multiple domains',
212 'title' => 'Setting Domain',
217 * Revert settings to defaults.
219 * @param array $params
222 * @throws \CiviCRM_API3_Exception
225 function civicrm_api3_setting_fill(&$params) {
226 $defaults = civicrm_api3('Setting', 'getdefaults', $params);
227 $domains = _civicrm_api3_setting_getDomainArray($params);
229 foreach ($domains as $domainID) {
231 'version' => $params['version'],
232 'domain_id' => $domainID,
234 $existing = civicrm_api3('Setting', 'get', $apiArray);
235 $valuesToFill = array_diff_key($defaults['values'][$domainID], $existing['values'][$domainID]);
236 if (!empty($valuesToFill)) {
237 $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToFill +
$apiArray));
240 return civicrm_api3_create_success($result, $params, 'Setting', 'fill');
244 * Alter metadata for getfields functions.
246 * @param array $params
248 function _civicrm_api3_setting_fill_spec(&$params) {
249 $params['name'] = array(
251 'description' => 'Setting Name belongs to',
253 $params['component_id'] = array(
254 'title' => 'Component ID',
255 'description' => 'ID of relevant component',
257 $params['domain_id'] = array(
258 'api.default' => 'current_domain',
259 'title' => 'Setting Domain',
260 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the '
261 . 'current domain, an array or "all" are acceptable values for multiple domains',
266 * Create or update a setting.
268 * @param array $params
269 * Parameters as per getfields.
274 function civicrm_api3_setting_create($params) {
275 $domains = _civicrm_api3_setting_getDomainArray($params);
276 $result = CRM_Core_BAO_Setting
::setItems($params, $domains);
277 return civicrm_api3_create_success($result, $params, 'Setting', 'create');
281 * Metadata for setting create function.
283 * @param array $params
284 * Parameters as passed to the API.
286 function _civicrm_api3_setting_create_spec(&$params) {
287 $params['domain_id'] = array(
288 'api.default' => 'current_domain',
289 'title' => 'Setting Domain',
290 'description' => 'if you do not pass in a domain id this will default to the current domain
291 an array or "all" are acceptable values for multiple domains',
293 $params['group'] = array(
294 'title' => 'Setting Group',
295 'description' => 'if you know the group defining it will make the api more efficient',
300 * Returns array of settings matching input parameters.
302 * @param array $params
303 * Array of one or more valid property_name=>value pairs.
306 * Array of matching settings
308 function civicrm_api3_setting_get($params) {
309 $domains = _civicrm_api3_setting_getDomainArray($params);
310 $result = $result = CRM_Core_BAO_Setting
::getItems($params, $domains, CRM_Utils_Array
::value('return', $params, array()));
311 return civicrm_api3_create_success($result, $params, 'Setting', 'get');
314 * Metadata for setting create function.
316 * @param array $params
317 * Parameters as passed to the API.
319 function _civicrm_api3_setting_get_spec(&$params) {
320 $params['domain_id'] = array(
321 'api.default' => 'current_domain',
322 'title' => 'Setting Domain',
323 'description' => 'if you do not pass in a domain id this will default to the current domain',
325 $params['group'] = array(
326 'title' => 'Setting Group',
327 'description' => 'if you know the group defining it will make the api more efficient',
331 * Returns value for specific parameter.
333 * Function requires more fields than 'get' but is intended for
334 * runtime usage & should be quicker
336 * @param array $params
337 * Array of one or more valid.
338 * property_name=>value pairs.
343 function civicrm_api3_setting_getvalue($params) {
344 //$config = CRM_Core_Config::singleton();
345 //if (isset($config->$params['name'])) {
346 // return $config->$params['name'];
348 return CRM_Core_BAO_Setting
::getItem(
350 CRM_Utils_Array
::value('name', $params),
351 CRM_Utils_Array
::value('component_id', $params),
352 CRM_Utils_Array
::value('default_value', $params),
353 CRM_Utils_Array
::value('contact_id', $params),
354 CRM_Utils_Array
::value('domain_id', $params)
359 * Metadata for setting create function.
361 * @param array $params
362 * Parameters as passed to the API.
364 function _civicrm_api3_setting_getvalue_spec(&$params) {
366 $params['group'] = array(
367 'title' => 'Settings Group',
368 'api.required' => TRUE,
370 $params['name'] = array(
371 'title' => 'Setting Name',
372 'api.aliases' => array('return'),
374 $params['default_value'] = array(
375 'title' => 'Default Value',
377 $params['component_id'] = array(
378 'title' => 'Component Id',
380 $params['contact_id'] = array(
381 'title' => 'Contact Id',
383 $params['domain_id'] = array(
384 'title' => 'Setting Domain',
385 'description' => 'if you do not pass in a domain id this will default to the current domain',
390 * Converts domain input into an array.
392 * If an array is passed in this is used, if 'all' is passed
393 * in this is converted to 'all arrays'
395 * Really domain_id should always be set but doing an empty check because at the moment
396 * using crm-editable will pass an id & default won't be applied
397 * we did talk about id being a pseudonym for domain_id in this api so applying it here.
399 * @param array $params
404 function _civicrm_api3_setting_getDomainArray(&$params) {
405 if (empty($params['domain_id']) && isset($params['id'])) {
406 $params['domain_id'] = $params['id'];
409 if ($params['domain_id'] == 'current_domain') {
410 $params['domain_id'] = CRM_Core_Config
::domainID();
413 if ($params['domain_id'] == 'all') {
414 $domainAPIResult = civicrm_api('domain', 'get', array('version' => 3, 'return' => 'id'));
415 if (isset($domainAPIResult['values'])) {
416 $params['domain_id'] = array_keys($domainAPIResult['values']);
419 throw new Exception('All domains not retrieved - problem with Domain Get api call ' . $domainAPIResult['error_message']);
422 if (is_array($params['domain_id'])) {
423 $domains = $params['domain_id'];
426 $domains = array($params['domain_id']);