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 $noDefaults = array();
117 foreach ($settings['values'] as $setting => $spec) {
118 if (array_key_exists('default', $spec) && !is_null($spec['default'])) {
119 $defaults[$domainID][$setting] = $spec['default'];
122 $noDefaults[$setting] = 1;
125 if (!empty($params['debug'])) {
126 // we are only tracking 'noDefaults' to help us check the xml
127 print_r($noDefaults);
130 return civicrm_api3_create_success($defaults, $params, 'Setting', 'getfields');
133 * Metadata for Setting create function.
135 * @param array $params
136 * Parameters as passed to the API.
138 function _civicrm_api3_setting_getdefaults_spec(&$params) {
139 $params['domain_id'] = array(
140 'api.default' => 'current_domain',
141 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the current domain
142 an array or "all" are acceptable values for multiple domains',
143 'title' => 'Setting Domain',
148 * Get options for settings.
150 * @param array $params
153 * @throws \API_Exception
155 function civicrm_api3_setting_getoptions($params) {
156 $specs = CRM_Core_BAO_Setting
::getSettingSpecification();
158 if (empty($specs[$params['field']]) ||
empty($specs[$params['field']]['pseudoconstant'])) {
159 throw new API_Exception("The field '" . $params['field'] . "' has no associated option list.");
162 $pseudoconstant = $specs[$params['field']]['pseudoconstant'];
164 // It would be nice if we could leverage CRM_Core_PseudoConstant::get() somehow,
165 // but it's tightly coupled to DAO/field. However, if you really need to support
166 // more pseudoconstant types, then probably best to refactor it. For now, KISS.
167 if (!empty($pseudoconstant['callback'])) {
168 $values = Civi\Core\Resolver
::singleton()->call($pseudoconstant['callback'], array());
169 return civicrm_api3_create_success($values, $params, 'Setting', 'getoptions');
172 throw new API_Exception("The field '" . $params['field'] . "' uses an unsupported option list.");
176 * Revert settings to defaults.
178 * @param array $params
183 function civicrm_api3_setting_revert(&$params) {
184 $defaults = civicrm_api('Setting', 'getdefaults', $params);
185 $fields = civicrm_api('Setting', 'getfields', $params);
186 $fields = $fields['values'];
187 $domains = _civicrm_api3_setting_getDomainArray($params);
189 foreach ($domains as $domainID) {
190 $valuesToRevert = array_intersect_key($defaults['values'][$domainID], $fields);
191 if (!empty($valuesToRevert)) {
192 $valuesToRevert['version'] = $params['version'];
193 $valuesToRevert['domain_id'] = $domainID;
194 // note that I haven't looked at how the result would appear with multiple domains in play
195 $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToRevert));
199 return civicrm_api3_create_success($result, $params, 'Setting', 'revert');
203 * Alter metadata for getfields functions.
205 * @param array $params
207 function _civicrm_api3_setting_revert_spec(&$params) {
208 $params['name'] = array(
210 'description' => 'Setting Name belongs to',
212 $params['component_id'] = array(
213 'title' => 'Component ID',
214 'description' => 'ID of relevant component',
216 $params['domain_id'] = array(
217 'api.default' => 'current_domain',
218 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the current domain'
219 . ' an array or "all" are acceptable values for multiple domains',
220 'title' => 'Setting Domain',
225 * Revert settings to defaults.
227 * @param array $params
230 * @throws \CiviCRM_API3_Exception
233 function civicrm_api3_setting_fill(&$params) {
234 $defaults = civicrm_api3('Setting', 'getdefaults', $params);
235 $domains = _civicrm_api3_setting_getDomainArray($params);
237 foreach ($domains as $domainID) {
239 'version' => $params['version'],
240 'domain_id' => $domainID,
242 $existing = civicrm_api3('Setting', 'get', $apiArray);
243 $valuesToFill = array_diff_key($defaults['values'][$domainID], $existing['values'][$domainID]);
244 if (!empty($valuesToFill)) {
245 $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToFill +
$apiArray));
248 return civicrm_api3_create_success($result, $params, 'Setting', 'fill');
252 * Alter metadata for getfields functions.
254 * @param array $params
256 function _civicrm_api3_setting_fill_spec(&$params) {
257 $params['name'] = array(
259 'description' => 'Setting Name belongs to',
261 $params['component_id'] = array(
262 'title' => 'Component ID',
263 'description' => 'ID of relevant component',
265 $params['domain_id'] = array(
266 'api.default' => 'current_domain',
267 'title' => 'Setting Domain',
268 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the '
269 . 'current domain, an array or "all" are acceptable values for multiple domains',
274 * Create or update a setting.
276 * @param array $params
277 * Parameters as per getfields.
282 function civicrm_api3_setting_create($params) {
283 $domains = _civicrm_api3_setting_getDomainArray($params);
284 $result = CRM_Core_BAO_Setting
::setItems($params, $domains);
285 return civicrm_api3_create_success($result, $params, 'Setting', 'create');
289 * Metadata for setting create function.
291 * @param array $params
292 * Parameters as passed to the API.
294 function _civicrm_api3_setting_create_spec(&$params) {
295 $params['domain_id'] = array(
296 'api.default' => 'current_domain',
297 'title' => 'Setting Domain',
298 'description' => 'if you do not pass in a domain id this will default to the current domain
299 an array or "all" are acceptable values for multiple domains',
301 $params['group'] = array(
302 'title' => 'Setting Group',
303 'description' => 'if you know the group defining it will make the api more efficient',
308 * Returns array of settings matching input parameters.
310 * @param array $params
311 * Array of one or more valid property_name=>value pairs.
314 * Array of matching settings
316 function civicrm_api3_setting_get($params) {
317 $domains = _civicrm_api3_setting_getDomainArray($params);
318 $result = $result = CRM_Core_BAO_Setting
::getItems($params, $domains, CRM_Utils_Array
::value('return', $params, array()));
319 return civicrm_api3_create_success($result, $params, 'Setting', 'get');
322 * Metadata for setting create function.
324 * @param array $params
325 * Parameters as passed to the API.
327 function _civicrm_api3_setting_get_spec(&$params) {
328 $params['domain_id'] = array(
329 'api.default' => 'current_domain',
330 'title' => 'Setting Domain',
331 'description' => 'if you do not pass in a domain id this will default to the current domain',
333 $params['group'] = array(
334 'title' => 'Setting Group',
335 'description' => 'if you know the group defining it will make the api more efficient',
339 * Returns value for specific parameter.
341 * Function requires more fields than 'get' but is intended for
342 * runtime usage & should be quicker
344 * @param array $params
345 * Array of one or more valid.
346 * property_name=>value pairs.
351 function civicrm_api3_setting_getvalue($params) {
352 //$config = CRM_Core_Config::singleton();
353 //if (isset($config->$params['name'])) {
354 // return $config->$params['name'];
356 return CRM_Core_BAO_Setting
::getItem(
358 CRM_Utils_Array
::value('name', $params),
359 CRM_Utils_Array
::value('component_id', $params),
360 CRM_Utils_Array
::value('default_value', $params),
361 CRM_Utils_Array
::value('contact_id', $params),
362 CRM_Utils_Array
::value('domain_id', $params)
367 * Metadata for setting create function.
369 * @param array $params
370 * Parameters as passed to the API.
372 function _civicrm_api3_setting_getvalue_spec(&$params) {
374 $params['group'] = array(
375 'title' => 'Settings Group',
376 'api.required' => TRUE,
378 $params['name'] = array(
379 'title' => 'Setting Name',
380 'api.aliases' => array('return'),
382 $params['default_value'] = array(
383 'title' => 'Default Value',
385 $params['component_id'] = array(
386 'title' => 'Component Id',
388 $params['contact_id'] = array(
389 'title' => 'Contact Id',
391 $params['domain_id'] = array(
392 'title' => 'Setting Domain',
393 'description' => 'if you do not pass in a domain id this will default to the current domain',
398 * Converts domain input into an array.
400 * If an array is passed in this is used, if 'all' is passed
401 * in this is converted to 'all arrays'
403 * Really domain_id should always be set but doing an empty check because at the moment
404 * using crm-editable will pass an id & default won't be applied
405 * we did talk about id being a pseudonym for domain_id in this api so applying it here.
407 * @param array $params
412 function _civicrm_api3_setting_getDomainArray(&$params) {
413 if (empty($params['domain_id']) && isset($params['id'])) {
414 $params['domain_id'] = $params['id'];
417 if ($params['domain_id'] == 'current_domain') {
418 $params['domain_id'] = CRM_Core_Config
::domainID();
421 if ($params['domain_id'] == 'all') {
422 $domainAPIResult = civicrm_api('domain', 'get', array('version' => 3, 'return' => 'id'));
423 if (isset($domainAPIResult['values'])) {
424 $params['domain_id'] = array_keys($domainAPIResult['values']);
427 throw new Exception('All domains not retrieved - problem with Domain Get api call ' . $domainAPIResult['error_message']);
430 if (is_array($params['domain_id'])) {
431 $domains = $params['domain_id'];
434 $domains = array($params['domain_id']);