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