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