civicrm_api3_setting_getvalue - Skip $config. Use settings.
[civicrm-core.git] / api / v3 / Setting.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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/**
c28e1768 29 * This api exposes CiviCRM configuration settings.
6a488035 30 *
b081365f 31 * @package CiviCRM_APIv3
61fe4988
EM
32 */
33
34/**
35 * Get fields for setting api calls.
36 *
d0997921 37 * @param array $params
61fe4988 38 *
645ee340 39 * @return array
6a488035 40 */
6a488035 41function civicrm_api3_setting_getfields($params) {
9b873358 42 if (!empty($params['action']) && strtolower($params['action']) == 'getvalue') {
6a488035
TO
43 $result = array(
44 'name' => array(
45 'title' => 'name of setting field',
46 'api.required' => 1,
47 'type' => CRM_Utils_Type::T_STRING),
c14d6dd1 48 'group' => array(
6a488035 49 'api.required' => 0,
1fdb479f 50 'title' => 'Setting Group',
6a488035 51 'description' => 'Settings Group. This is required if the setting is not stored in config',
21dfd5f5 52 'type' => CRM_Utils_Type::T_STRING),
e7483cbe 53 );
244bbdd8 54 return civicrm_api3_create_success($result, $params, 'Setting', 'getfields');
6a488035 55 }
9b873358 56 if (!empty($params['name'])) {
6a488035
TO
57 //am of two minds about special handling for 'name' as opposed to other filters - but is does make most common
58 //usage really easy
59 $params['filters']['name'] = $params['name'];
60 }
61 $result = CRM_Core_BAO_Setting::getSettingSpecification(
35671d00 62 CRM_Utils_Array::value('component_id', $params),
6a488035 63 CRM_Utils_Array::value('filters', $params, array()),
35671d00
TO
64 CRM_Utils_Array::value('domain_id', $params, NULL),
65 CRM_Utils_Array::value('profile', $params, NULL)
6a488035
TO
66 );
67 // find any supplemental information
9b873358 68 if (!empty($params['action'])) {
f60289e9 69 $specFunction = '_civicrm_api3_setting_' . strtolower($params['action']) . '_spec';
6a488035
TO
70 if (function_exists($specFunction)) {
71 $specFunction($result);
72 }
73 }
244bbdd8 74 return civicrm_api3_create_success($result, $params, 'Setting', 'getfields');
6a488035 75}
11e09c59
TO
76
77/**
1747ab99
EM
78 * Alter metadata for getfields functions.
79 *
d0997921 80 * @param array $params
6a488035 81 */
f60289e9 82function _civicrm_api3_setting_getfields_spec(&$params) {
b2ed8e73
CW
83 $params['filters'] = array(
84 'title' => 'Filters',
85 'description' => 'Fields you wish to filter by e.g. array("group_name" => "CiviCRM Preferences")',
86 );
87 $params['component_id'] = array(
88 'title' => 'Component ID',
89 'description' => 'ID of relevant component',
90 );
91 $params['profile'] = array(
92 'title' => 'Profile',
93 'description' => 'Profile is passed through to hooks & added to cachestring',
94 );
6a488035 95}
11e09c59
TO
96
97/**
1747ab99
EM
98 * Return default values for settings.
99 *
100 * We will domain key this as it could vary by domain (ie. urls)
6a488035 101 * as we will be creating the option for a function rather than an value to be in the defaults
1747ab99
EM
102 * Note that is not in place as yet.
103 *
d0997921 104 * @param array $params
1747ab99 105 *
645ee340
EM
106 * @return array
107 * @throws \CiviCRM_API3_Exception
108 * @throws \Exception
6a488035 109 */
9b873358 110function civicrm_api3_setting_getdefaults(&$params) {
244bbdd8 111 $settings = civicrm_api3('Setting', 'getfields', $params);
6a488035
TO
112 $domains = _civicrm_api3_setting_getDomainArray($params);
113 $defaults = array();
9b873358 114 foreach ($domains as $domainID) {
6a488035
TO
115 $defaults[$domainID] = array();
116 $noDefaults = array();
9b873358
TO
117 foreach ($settings['values'] as $setting => $spec) {
118 if (array_key_exists('default', $spec) && !is_null($spec['default'])) {
6a488035
TO
119 $defaults[$domainID][$setting] = $spec['default'];
120 }
92e4c2a5 121 else {
6a488035
TO
122 $noDefaults[$setting] = 1;
123 }
124 }
9b873358 125 if (!empty($params['debug'])) {
6a488035
TO
126 // we are only tracking 'noDefaults' to help us check the xml
127 print_r($noDefaults);
128 }
129 }
244bbdd8 130 return civicrm_api3_create_success($defaults, $params, 'Setting', 'getfields');
6a488035 131}
11e09c59 132/**
244bbdd8 133 * Metadata for Setting create function.
11e09c59 134 *
cf470720
TO
135 * @param array $params
136 * Parameters as passed to the API.
11e09c59 137 */
f60289e9 138function _civicrm_api3_setting_getdefaults_spec(&$params) {
6a488035 139 $params['domain_id'] = array(
5a159702
EM
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',
6a488035
TO
144 );
145}
11e09c59 146
76068c6a
TO
147function civicrm_api3_setting_getoptions($params) {
148 $specs = CRM_Core_BAO_Setting::getSettingSpecification();
149
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.");
152 }
153
154 $pseudoconstant = $specs[$params['field']]['pseudoconstant'];
155
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');
162 }
163
164 throw new API_Exception("The field '" . $params['field'] . "' uses an unsupported option list.");
165}
166
11e09c59 167/**
9d32e6f7
EM
168 * Revert settings to defaults.
169 *
d0997921 170 * @param array $params
9d32e6f7 171 *
645ee340
EM
172 * @return array
173 * @throws \Exception
6a488035 174 */
9b873358 175function civicrm_api3_setting_revert(&$params) {
244bbdd8
CW
176 $defaults = civicrm_api('Setting', 'getdefaults', $params);
177 $fields = civicrm_api('Setting', 'getfields', $params);
6a488035
TO
178 $fields = $fields['values'];
179 $domains = _civicrm_api3_setting_getDomainArray($params);
180 $result = array();
9b873358 181 foreach ($domains as $domainID) {
6a488035 182 $valuesToRevert = array_intersect_key($defaults['values'][$domainID], $fields);
9b873358 183 if (!empty($valuesToRevert)) {
6a488035
TO
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
244bbdd8 187 $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToRevert));
6a488035
TO
188 }
189 }
190
244bbdd8 191 return civicrm_api3_create_success($result, $params, 'Setting', 'revert');
6a488035 192}
11e09c59
TO
193
194/**
dc64d047
EM
195 * Alter metadata for getfields functions.
196 *
d0997921 197 * @param array $params
11e09c59 198 */
f60289e9 199function _civicrm_api3_setting_revert_spec(&$params) {
b2ed8e73
CW
200 $params['name'] = array(
201 'title' => 'Name',
202 'description' => 'Setting Name belongs to',
203 );
204 $params['component_id'] = array(
205 'title' => 'Component ID',
206 'description' => 'ID of relevant component',
207 );
6a488035
TO
208 $params['domain_id'] = array(
209 'api.default' => 'current_domain',
dc64d047
EM
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',
5a159702 212 'title' => 'Setting Domain',
6a488035
TO
213 );
214}
215
11e09c59 216/**
1747ab99
EM
217 * Revert settings to defaults.
218 *
d0997921 219 * @param array $params
1747ab99 220 *
645ee340
EM
221 * @return array
222 * @throws \CiviCRM_API3_Exception
223 * @throws \Exception
11e09c59 224 */
9b873358 225function civicrm_api3_setting_fill(&$params) {
244bbdd8 226 $defaults = civicrm_api3('Setting', 'getdefaults', $params);
6a488035
TO
227 $domains = _civicrm_api3_setting_getDomainArray($params);
228 $result = array();
9b873358 229 foreach ($domains as $domainID) {
6a488035
TO
230 $apiArray = array(
231 'version' => $params['version'],
21dfd5f5 232 'domain_id' => $domainID,
6a488035 233 );
244bbdd8 234 $existing = civicrm_api3('Setting', 'get', $apiArray);
6a488035 235 $valuesToFill = array_diff_key($defaults['values'][$domainID], $existing['values'][$domainID]);
9b873358 236 if (!empty($valuesToFill)) {
244bbdd8 237 $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToFill + $apiArray));
6a488035
TO
238 }
239 }
244bbdd8 240 return civicrm_api3_create_success($result, $params, 'Setting', 'fill');
6a488035 241}
11e09c59
TO
242
243/**
9d32e6f7
EM
244 * Alter metadata for getfields functions.
245 *
d0997921 246 * @param array $params
11e09c59 247 */
f60289e9 248function _civicrm_api3_setting_fill_spec(&$params) {
b2ed8e73
CW
249 $params['name'] = array(
250 'title' => 'Name',
251 'description' => 'Setting Name belongs to',
252 );
253 $params['component_id'] = array(
254 'title' => 'Component ID',
255 'description' => 'ID of relevant component',
256 );
6a488035 257 $params['domain_id'] = array(
5a159702
EM
258 'api.default' => 'current_domain',
259 'title' => 'Setting Domain',
1747ab99 260 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the '
dc64d047 261 . 'current domain, an array or "all" are acceptable values for multiple domains',
6a488035
TO
262 );
263}
264
265/**
35823763 266 * Create or update a setting.
6a488035 267 *
cf470720 268 * @param array $params
35823763 269 * Parameters as per getfields.
6a488035 270 *
a6c01b45 271 * @return array
72b3a70c 272 * api result array
6a488035
TO
273 */
274function civicrm_api3_setting_create($params) {
275 $domains = _civicrm_api3_setting_getDomainArray($params);
276 $result = CRM_Core_BAO_Setting::setItems($params, $domains);
244bbdd8 277 return civicrm_api3_create_success($result, $params, 'Setting', 'create');
6a488035 278}
11e09c59
TO
279
280/**
1747ab99 281 * Metadata for setting create function.
6a488035 282 *
cf470720
TO
283 * @param array $params
284 * Parameters as passed to the API.
6a488035 285 */
f60289e9 286function _civicrm_api3_setting_create_spec(&$params) {
6a488035
TO
287 $params['domain_id'] = array(
288 'api.default' => 'current_domain',
5a159702 289 'title' => 'Setting Domain',
6a488035 290 'description' => 'if you do not pass in a domain id this will default to the current domain
21dfd5f5 291 an array or "all" are acceptable values for multiple domains',
e7483cbe 292 );
35671d00 293 $params['group'] = array(
e7483cbe
J
294 'title' => 'Setting Group',
295 'description' => 'if you know the group defining it will make the api more efficient',
296 );
6a488035
TO
297}
298
299/**
35823763 300 * Returns array of settings matching input parameters.
6a488035 301 *
cf470720 302 * @param array $params
35823763 303 * Array of one or more valid property_name=>value pairs.
6a488035 304 *
a6c01b45 305 * @return array
72b3a70c 306 * Array of matching settings
6a488035
TO
307 */
308function civicrm_api3_setting_get($params) {
309 $domains = _civicrm_api3_setting_getDomainArray($params);
35671d00 310 $result = $result = CRM_Core_BAO_Setting::getItems($params, $domains, CRM_Utils_Array::value('return', $params, array()));
244bbdd8 311 return civicrm_api3_create_success($result, $params, 'Setting', 'get');
6a488035 312}
11e09c59 313/**
1747ab99 314 * Metadata for setting create function.
11e09c59 315 *
cf470720
TO
316 * @param array $params
317 * Parameters as passed to the API.
11e09c59 318 */
f60289e9 319function _civicrm_api3_setting_get_spec(&$params) {
6a488035 320 $params['domain_id'] = array(
5a159702
EM
321 'api.default' => 'current_domain',
322 'title' => 'Setting Domain',
21dfd5f5 323 'description' => 'if you do not pass in a domain id this will default to the current domain',
6a488035
TO
324 );
325 $params['group'] = array(
5a159702 326 'title' => 'Setting Group',
21dfd5f5 327 'description' => 'if you know the group defining it will make the api more efficient',
35671d00 328 );
6a488035
TO
329}
330/**
1747ab99
EM
331 * Returns value for specific parameter.
332 *
333 * Function requires more fields than 'get' but is intended for
6a488035
TO
334 * runtime usage & should be quicker
335 *
cf470720 336 * @param array $params
1747ab99 337 * Array of one or more valid.
6a488035
TO
338 * property_name=>value pairs.
339 *
a6c01b45 340 * @return array
1747ab99 341 * API result array.
6a488035
TO
342 */
343function civicrm_api3_setting_getvalue($params) {
86bfd7a8
TO
344 //$config = CRM_Core_Config::singleton();
345 //if (isset($config->$params['name'])) {
346 // return $config->$params['name'];
347 //}
6a488035
TO
348 return CRM_Core_BAO_Setting::getItem(
349 $params['group'],
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)
355 );
356}
357
11e09c59 358/**
1747ab99 359 * Metadata for setting create function.
11e09c59 360 *
cf470720
TO
361 * @param array $params
362 * Parameters as passed to the API.
11e09c59 363 */
f60289e9 364function _civicrm_api3_setting_getvalue_spec(&$params) {
6a488035
TO
365
366 $params['group'] = array(
e7483cbe
J
367 'title' => 'Settings Group',
368 'api.required' => TRUE,
6a488035
TO
369 );
370 $params['name'] = array(
e7483cbe
J
371 'title' => 'Setting Name',
372 'api.aliases' => array('return'),
6a488035
TO
373 );
374 $params['default_value'] = array(
e7483cbe 375 'title' => 'Default Value',
6a488035
TO
376 );
377 $params['component_id'] = array(
e7483cbe 378 'title' => 'Component Id',
6a488035
TO
379 );
380 $params['contact_id'] = array(
e7483cbe 381 'title' => 'Contact Id',
6a488035
TO
382 );
383 $params['domain_id'] = array(
5a159702 384 'title' => 'Setting Domain',
21dfd5f5 385 'description' => 'if you do not pass in a domain id this will default to the current domain',
6a488035
TO
386 );
387}
11e09c59
TO
388
389/**
1747ab99
EM
390 * Converts domain input into an array.
391 *
392 * If an array is passed in this is used, if 'all' is passed
6a488035 393 * in this is converted to 'all arrays'
f60289e9 394 *
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
1747ab99
EM
397 * we did talk about id being a pseudonym for domain_id in this api so applying it here.
398 *
d0997921 399 * @param array $params
1747ab99 400 *
645ee340
EM
401 * @return array
402 * @throws \Exception
6a488035 403 */
9b873358
TO
404function _civicrm_api3_setting_getDomainArray(&$params) {
405 if (empty($params['domain_id']) && isset($params['id'])) {
f60289e9 406 $params['domain_id'] = $params['id'];
407 }
408
9b873358 409 if ($params['domain_id'] == 'current_domain') {
6a488035
TO
410 $params['domain_id'] = CRM_Core_Config::domainID();
411 }
412
9b873358 413 if ($params['domain_id'] == 'all') {
35671d00 414 $domainAPIResult = civicrm_api('domain', 'get', array('version' => 3, 'return' => 'id'));
6a488035
TO
415 if (isset($domainAPIResult['values'])) {
416 $params['domain_id'] = array_keys($domainAPIResult['values']);
417 }
92e4c2a5 418 else {
6a488035
TO
419 throw new Exception('All domains not retrieved - problem with Domain Get api call ' . $domainAPIResult['error_message']);
420 }
421 }
9b873358 422 if (is_array($params['domain_id'])) {
6a488035
TO
423 $domains = $params['domain_id'];
424 }
92e4c2a5 425 else {
6a488035
TO
426 $domains = array($params['domain_id']);
427 }
428 return $domains;
429}