Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | 41 | function 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 | 82 | function _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 | 110 | function 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 | 138 | function _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 | |
70599df6 | 147 | /** |
148 | * Get options for settings. | |
149 | * | |
150 | * @param array $params | |
151 | * | |
152 | * @return array | |
153 | * @throws \API_Exception | |
154 | */ | |
76068c6a TO |
155 | function civicrm_api3_setting_getoptions($params) { |
156 | $specs = CRM_Core_BAO_Setting::getSettingSpecification(); | |
157 | ||
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."); | |
160 | } | |
161 | ||
162 | $pseudoconstant = $specs[$params['field']]['pseudoconstant']; | |
163 | ||
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'); | |
170 | } | |
171 | ||
172 | throw new API_Exception("The field '" . $params['field'] . "' uses an unsupported option list."); | |
173 | } | |
174 | ||
11e09c59 | 175 | /** |
9d32e6f7 EM |
176 | * Revert settings to defaults. |
177 | * | |
d0997921 | 178 | * @param array $params |
9d32e6f7 | 179 | * |
645ee340 EM |
180 | * @return array |
181 | * @throws \Exception | |
6a488035 | 182 | */ |
9b873358 | 183 | function civicrm_api3_setting_revert(&$params) { |
244bbdd8 CW |
184 | $defaults = civicrm_api('Setting', 'getdefaults', $params); |
185 | $fields = civicrm_api('Setting', 'getfields', $params); | |
6a488035 TO |
186 | $fields = $fields['values']; |
187 | $domains = _civicrm_api3_setting_getDomainArray($params); | |
188 | $result = array(); | |
9b873358 | 189 | foreach ($domains as $domainID) { |
6a488035 | 190 | $valuesToRevert = array_intersect_key($defaults['values'][$domainID], $fields); |
9b873358 | 191 | if (!empty($valuesToRevert)) { |
6a488035 TO |
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 | |
244bbdd8 | 195 | $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToRevert)); |
6a488035 TO |
196 | } |
197 | } | |
198 | ||
244bbdd8 | 199 | return civicrm_api3_create_success($result, $params, 'Setting', 'revert'); |
6a488035 | 200 | } |
11e09c59 TO |
201 | |
202 | /** | |
dc64d047 EM |
203 | * Alter metadata for getfields functions. |
204 | * | |
d0997921 | 205 | * @param array $params |
11e09c59 | 206 | */ |
f60289e9 | 207 | function _civicrm_api3_setting_revert_spec(&$params) { |
b2ed8e73 CW |
208 | $params['name'] = array( |
209 | 'title' => 'Name', | |
210 | 'description' => 'Setting Name belongs to', | |
211 | ); | |
212 | $params['component_id'] = array( | |
213 | 'title' => 'Component ID', | |
214 | 'description' => 'ID of relevant component', | |
215 | ); | |
6a488035 TO |
216 | $params['domain_id'] = array( |
217 | 'api.default' => 'current_domain', | |
dc64d047 EM |
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', | |
5a159702 | 220 | 'title' => 'Setting Domain', |
6a488035 TO |
221 | ); |
222 | } | |
223 | ||
11e09c59 | 224 | /** |
1747ab99 EM |
225 | * Revert settings to defaults. |
226 | * | |
d0997921 | 227 | * @param array $params |
1747ab99 | 228 | * |
645ee340 EM |
229 | * @return array |
230 | * @throws \CiviCRM_API3_Exception | |
231 | * @throws \Exception | |
11e09c59 | 232 | */ |
9b873358 | 233 | function civicrm_api3_setting_fill(&$params) { |
244bbdd8 | 234 | $defaults = civicrm_api3('Setting', 'getdefaults', $params); |
6a488035 TO |
235 | $domains = _civicrm_api3_setting_getDomainArray($params); |
236 | $result = array(); | |
9b873358 | 237 | foreach ($domains as $domainID) { |
6a488035 TO |
238 | $apiArray = array( |
239 | 'version' => $params['version'], | |
21dfd5f5 | 240 | 'domain_id' => $domainID, |
6a488035 | 241 | ); |
244bbdd8 | 242 | $existing = civicrm_api3('Setting', 'get', $apiArray); |
6a488035 | 243 | $valuesToFill = array_diff_key($defaults['values'][$domainID], $existing['values'][$domainID]); |
9b873358 | 244 | if (!empty($valuesToFill)) { |
244bbdd8 | 245 | $result = array_merge($result, civicrm_api('Setting', 'create', $valuesToFill + $apiArray)); |
6a488035 TO |
246 | } |
247 | } | |
244bbdd8 | 248 | return civicrm_api3_create_success($result, $params, 'Setting', 'fill'); |
6a488035 | 249 | } |
11e09c59 TO |
250 | |
251 | /** | |
9d32e6f7 EM |
252 | * Alter metadata for getfields functions. |
253 | * | |
d0997921 | 254 | * @param array $params |
11e09c59 | 255 | */ |
f60289e9 | 256 | function _civicrm_api3_setting_fill_spec(&$params) { |
b2ed8e73 CW |
257 | $params['name'] = array( |
258 | 'title' => 'Name', | |
259 | 'description' => 'Setting Name belongs to', | |
260 | ); | |
261 | $params['component_id'] = array( | |
262 | 'title' => 'Component ID', | |
263 | 'description' => 'ID of relevant component', | |
264 | ); | |
6a488035 | 265 | $params['domain_id'] = array( |
5a159702 EM |
266 | 'api.default' => 'current_domain', |
267 | 'title' => 'Setting Domain', | |
1747ab99 | 268 | 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the ' |
dc64d047 | 269 | . 'current domain, an array or "all" are acceptable values for multiple domains', |
6a488035 TO |
270 | ); |
271 | } | |
272 | ||
273 | /** | |
35823763 | 274 | * Create or update a setting. |
6a488035 | 275 | * |
cf470720 | 276 | * @param array $params |
35823763 | 277 | * Parameters as per getfields. |
6a488035 | 278 | * |
a6c01b45 | 279 | * @return array |
72b3a70c | 280 | * api result array |
6a488035 TO |
281 | */ |
282 | function civicrm_api3_setting_create($params) { | |
283 | $domains = _civicrm_api3_setting_getDomainArray($params); | |
284 | $result = CRM_Core_BAO_Setting::setItems($params, $domains); | |
244bbdd8 | 285 | return civicrm_api3_create_success($result, $params, 'Setting', 'create'); |
6a488035 | 286 | } |
11e09c59 TO |
287 | |
288 | /** | |
1747ab99 | 289 | * Metadata for setting create function. |
6a488035 | 290 | * |
cf470720 TO |
291 | * @param array $params |
292 | * Parameters as passed to the API. | |
6a488035 | 293 | */ |
f60289e9 | 294 | function _civicrm_api3_setting_create_spec(&$params) { |
6a488035 TO |
295 | $params['domain_id'] = array( |
296 | 'api.default' => 'current_domain', | |
5a159702 | 297 | 'title' => 'Setting Domain', |
6a488035 | 298 | 'description' => 'if you do not pass in a domain id this will default to the current domain |
21dfd5f5 | 299 | an array or "all" are acceptable values for multiple domains', |
e7483cbe | 300 | ); |
35671d00 | 301 | $params['group'] = array( |
e7483cbe J |
302 | 'title' => 'Setting Group', |
303 | 'description' => 'if you know the group defining it will make the api more efficient', | |
304 | ); | |
6a488035 TO |
305 | } |
306 | ||
307 | /** | |
35823763 | 308 | * Returns array of settings matching input parameters. |
6a488035 | 309 | * |
cf470720 | 310 | * @param array $params |
35823763 | 311 | * Array of one or more valid property_name=>value pairs. |
6a488035 | 312 | * |
a6c01b45 | 313 | * @return array |
72b3a70c | 314 | * Array of matching settings |
6a488035 TO |
315 | */ |
316 | function civicrm_api3_setting_get($params) { | |
317 | $domains = _civicrm_api3_setting_getDomainArray($params); | |
35671d00 | 318 | $result = $result = CRM_Core_BAO_Setting::getItems($params, $domains, CRM_Utils_Array::value('return', $params, array())); |
244bbdd8 | 319 | return civicrm_api3_create_success($result, $params, 'Setting', 'get'); |
6a488035 | 320 | } |
11e09c59 | 321 | /** |
1747ab99 | 322 | * Metadata for setting create function. |
11e09c59 | 323 | * |
cf470720 TO |
324 | * @param array $params |
325 | * Parameters as passed to the API. | |
11e09c59 | 326 | */ |
f60289e9 | 327 | function _civicrm_api3_setting_get_spec(&$params) { |
6a488035 | 328 | $params['domain_id'] = array( |
5a159702 EM |
329 | 'api.default' => 'current_domain', |
330 | 'title' => 'Setting Domain', | |
21dfd5f5 | 331 | 'description' => 'if you do not pass in a domain id this will default to the current domain', |
6a488035 TO |
332 | ); |
333 | $params['group'] = array( | |
5a159702 | 334 | 'title' => 'Setting Group', |
21dfd5f5 | 335 | 'description' => 'if you know the group defining it will make the api more efficient', |
35671d00 | 336 | ); |
6a488035 TO |
337 | } |
338 | /** | |
1747ab99 EM |
339 | * Returns value for specific parameter. |
340 | * | |
341 | * Function requires more fields than 'get' but is intended for | |
6a488035 TO |
342 | * runtime usage & should be quicker |
343 | * | |
cf470720 | 344 | * @param array $params |
1747ab99 | 345 | * Array of one or more valid. |
6a488035 TO |
346 | * property_name=>value pairs. |
347 | * | |
a6c01b45 | 348 | * @return array |
1747ab99 | 349 | * API result array. |
6a488035 TO |
350 | */ |
351 | function civicrm_api3_setting_getvalue($params) { | |
86bfd7a8 TO |
352 | //$config = CRM_Core_Config::singleton(); |
353 | //if (isset($config->$params['name'])) { | |
354 | // return $config->$params['name']; | |
355 | //} | |
6a488035 TO |
356 | return CRM_Core_BAO_Setting::getItem( |
357 | $params['group'], | |
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) | |
363 | ); | |
364 | } | |
365 | ||
11e09c59 | 366 | /** |
1747ab99 | 367 | * Metadata for setting create function. |
11e09c59 | 368 | * |
cf470720 TO |
369 | * @param array $params |
370 | * Parameters as passed to the API. | |
11e09c59 | 371 | */ |
f60289e9 | 372 | function _civicrm_api3_setting_getvalue_spec(&$params) { |
6a488035 TO |
373 | |
374 | $params['group'] = array( | |
e7483cbe J |
375 | 'title' => 'Settings Group', |
376 | 'api.required' => TRUE, | |
6a488035 TO |
377 | ); |
378 | $params['name'] = array( | |
e7483cbe J |
379 | 'title' => 'Setting Name', |
380 | 'api.aliases' => array('return'), | |
6a488035 TO |
381 | ); |
382 | $params['default_value'] = array( | |
e7483cbe | 383 | 'title' => 'Default Value', |
6a488035 TO |
384 | ); |
385 | $params['component_id'] = array( | |
e7483cbe | 386 | 'title' => 'Component Id', |
6a488035 TO |
387 | ); |
388 | $params['contact_id'] = array( | |
e7483cbe | 389 | 'title' => 'Contact Id', |
6a488035 TO |
390 | ); |
391 | $params['domain_id'] = array( | |
5a159702 | 392 | 'title' => 'Setting Domain', |
21dfd5f5 | 393 | 'description' => 'if you do not pass in a domain id this will default to the current domain', |
6a488035 TO |
394 | ); |
395 | } | |
11e09c59 TO |
396 | |
397 | /** | |
1747ab99 EM |
398 | * Converts domain input into an array. |
399 | * | |
400 | * If an array is passed in this is used, if 'all' is passed | |
6a488035 | 401 | * in this is converted to 'all arrays' |
f60289e9 | 402 | * |
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 | |
1747ab99 EM |
405 | * we did talk about id being a pseudonym for domain_id in this api so applying it here. |
406 | * | |
d0997921 | 407 | * @param array $params |
1747ab99 | 408 | * |
645ee340 EM |
409 | * @return array |
410 | * @throws \Exception | |
6a488035 | 411 | */ |
9b873358 TO |
412 | function _civicrm_api3_setting_getDomainArray(&$params) { |
413 | if (empty($params['domain_id']) && isset($params['id'])) { | |
f60289e9 | 414 | $params['domain_id'] = $params['id']; |
415 | } | |
416 | ||
9b873358 | 417 | if ($params['domain_id'] == 'current_domain') { |
6a488035 TO |
418 | $params['domain_id'] = CRM_Core_Config::domainID(); |
419 | } | |
420 | ||
9b873358 | 421 | if ($params['domain_id'] == 'all') { |
35671d00 | 422 | $domainAPIResult = civicrm_api('domain', 'get', array('version' => 3, 'return' => 'id')); |
6a488035 TO |
423 | if (isset($domainAPIResult['values'])) { |
424 | $params['domain_id'] = array_keys($domainAPIResult['values']); | |
425 | } | |
92e4c2a5 | 426 | else { |
6a488035 TO |
427 | throw new Exception('All domains not retrieved - problem with Domain Get api call ' . $domainAPIResult['error_message']); |
428 | } | |
429 | } | |
9b873358 | 430 | if (is_array($params['domain_id'])) { |
6a488035 TO |
431 | $domains = $params['domain_id']; |
432 | } | |
92e4c2a5 | 433 | else { |
6a488035 TO |
434 | $domains = array($params['domain_id']); |
435 | } | |
436 | return $domains; | |
437 | } |