Api docblock cleanup.
[civicrm-core.git] / api / v3 / Setting.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 );
6a488035
TO
54 return civicrm_api3_create_success($result, $params, 'setting', 'getfields');
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 }
74 return civicrm_api3_create_success($result, $params, 'setting', 'getfields');
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) {
6a488035
TO
83 $params['filters'] = array('title' => 'Fields you wish to filter by e.g. array("group_name" => "CiviCRM Preferences")');
84 $params['component_id'] = array('title' => 'id of relevant component');
85 $params['profile'] = array('title' => 'profile is passed through to hooks & added to cachestring');
86}
11e09c59
TO
87
88/**
1747ab99
EM
89 * Return default values for settings.
90 *
91 * We will domain key this as it could vary by domain (ie. urls)
6a488035 92 * as we will be creating the option for a function rather than an value to be in the defaults
1747ab99
EM
93 * Note that is not in place as yet.
94 *
d0997921 95 * @param array $params
1747ab99 96 *
645ee340
EM
97 * @return array
98 * @throws \CiviCRM_API3_Exception
99 * @throws \Exception
6a488035 100 */
9b873358 101function civicrm_api3_setting_getdefaults(&$params) {
35671d00 102 $settings = civicrm_api3('setting', 'getfields', $params);
6a488035
TO
103 $domains = _civicrm_api3_setting_getDomainArray($params);
104 $defaults = array();
9b873358 105 foreach ($domains as $domainID) {
6a488035
TO
106 $defaults[$domainID] = array();
107 $noDefaults = array();
9b873358
TO
108 foreach ($settings['values'] as $setting => $spec) {
109 if (array_key_exists('default', $spec) && !is_null($spec['default'])) {
6a488035
TO
110 $defaults[$domainID][$setting] = $spec['default'];
111 }
92e4c2a5 112 else {
6a488035
TO
113 $noDefaults[$setting] = 1;
114 }
115 }
9b873358 116 if (!empty($params['debug'])) {
6a488035
TO
117 // we are only tracking 'noDefaults' to help us check the xml
118 print_r($noDefaults);
119 }
120 }
35671d00 121 return civicrm_api3_create_success($defaults, $params, 'setting', 'getfields');
6a488035 122}
11e09c59 123/**
9d32e6f7 124 * Metadata for setting create function.
11e09c59 125 *
cf470720
TO
126 * @param array $params
127 * Parameters as passed to the API.
11e09c59 128 */
f60289e9 129function _civicrm_api3_setting_getdefaults_spec(&$params) {
6a488035 130 $params['domain_id'] = array(
5a159702
EM
131 'api.default' => 'current_domain',
132 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the current domain
133 an array or "all" are acceptable values for multiple domains',
134 'title' => 'Setting Domain',
6a488035
TO
135 );
136}
11e09c59
TO
137
138/**
9d32e6f7
EM
139 * Revert settings to defaults.
140 *
d0997921 141 * @param array $params
9d32e6f7 142 *
645ee340
EM
143 * @return array
144 * @throws \Exception
6a488035 145 */
9b873358 146function civicrm_api3_setting_revert(&$params) {
35671d00
TO
147 $defaults = civicrm_api('setting', 'getdefaults', $params);
148 $fields = civicrm_api('setting', 'getfields', $params);
6a488035
TO
149 $fields = $fields['values'];
150 $domains = _civicrm_api3_setting_getDomainArray($params);
151 $result = array();
9b873358 152 foreach ($domains as $domainID) {
6a488035 153 $valuesToRevert = array_intersect_key($defaults['values'][$domainID], $fields);
9b873358 154 if (!empty($valuesToRevert)) {
6a488035
TO
155 $valuesToRevert['version'] = $params['version'];
156 $valuesToRevert['domain_id'] = $domainID;
157 // note that I haven't looked at how the result would appear with multiple domains in play
158 $result = array_merge($result, civicrm_api('setting', 'create', $valuesToRevert));
159 }
160 }
161
162 return civicrm_api3_create_success($result, $params, 'setting', 'revert');
163}
11e09c59
TO
164
165/**
dc64d047
EM
166 * Alter metadata for getfields functions.
167 *
d0997921 168 * @param array $params
11e09c59 169 */
f60289e9 170function _civicrm_api3_setting_revert_spec(&$params) {
6a488035
TO
171 $params['name'] = array('title' => 'Setting Name belongs to');
172 $params['component_id'] = array('title' => 'id of relevant component');
173 $params['domain_id'] = array(
174 'api.default' => 'current_domain',
dc64d047
EM
175 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the current domain'
176 . ' an array or "all" are acceptable values for multiple domains',
5a159702 177 'title' => 'Setting Domain',
6a488035
TO
178 );
179}
180
11e09c59 181/**
1747ab99
EM
182 * Revert settings to defaults.
183 *
d0997921 184 * @param array $params
1747ab99 185 *
645ee340
EM
186 * @return array
187 * @throws \CiviCRM_API3_Exception
188 * @throws \Exception
11e09c59 189 */
9b873358 190function civicrm_api3_setting_fill(&$params) {
35671d00 191 $defaults = civicrm_api3('setting', 'getdefaults', $params);
6a488035
TO
192 $domains = _civicrm_api3_setting_getDomainArray($params);
193 $result = array();
9b873358 194 foreach ($domains as $domainID) {
6a488035
TO
195 $apiArray = array(
196 'version' => $params['version'],
21dfd5f5 197 'domain_id' => $domainID,
6a488035 198 );
35671d00 199 $existing = civicrm_api3('setting', 'get', $apiArray);
6a488035 200 $valuesToFill = array_diff_key($defaults['values'][$domainID], $existing['values'][$domainID]);
9b873358 201 if (!empty($valuesToFill)) {
6a488035
TO
202 $result = array_merge($result, civicrm_api('setting', 'create', $valuesToFill + $apiArray));
203 }
204 }
205 return civicrm_api3_create_success($result, $params, 'setting', 'fill');
206}
11e09c59
TO
207
208/**
9d32e6f7
EM
209 * Alter metadata for getfields functions.
210 *
d0997921 211 * @param array $params
11e09c59 212 */
f60289e9 213function _civicrm_api3_setting_fill_spec(&$params) {
6a488035
TO
214 $params['name'] = array('title' => 'Setting Name belongs to');
215 $params['component_id'] = array('title' => 'id of relevant component');
216 $params['domain_id'] = array(
5a159702
EM
217 'api.default' => 'current_domain',
218 'title' => 'Setting Domain',
1747ab99 219 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the '
dc64d047 220 . 'current domain, an array or "all" are acceptable values for multiple domains',
6a488035
TO
221 );
222}
223
224/**
35823763 225 * Create or update a setting.
6a488035 226 *
cf470720 227 * @param array $params
35823763 228 * Parameters as per getfields.
6a488035 229 *
a6c01b45 230 * @return array
72b3a70c 231 * api result array
6a488035
TO
232 */
233function civicrm_api3_setting_create($params) {
234 $domains = _civicrm_api3_setting_getDomainArray($params);
235 $result = CRM_Core_BAO_Setting::setItems($params, $domains);
35671d00 236 return civicrm_api3_create_success($result, $params, 'setting', 'create');
6a488035 237}
11e09c59
TO
238
239/**
1747ab99 240 * Metadata for setting create function.
6a488035 241 *
cf470720
TO
242 * @param array $params
243 * Parameters as passed to the API.
6a488035 244 */
f60289e9 245function _civicrm_api3_setting_create_spec(&$params) {
6a488035
TO
246 $params['domain_id'] = array(
247 'api.default' => 'current_domain',
5a159702 248 'title' => 'Setting Domain',
6a488035 249 'description' => 'if you do not pass in a domain id this will default to the current domain
21dfd5f5 250 an array or "all" are acceptable values for multiple domains',
e7483cbe 251 );
35671d00 252 $params['group'] = array(
e7483cbe
J
253 'title' => 'Setting Group',
254 'description' => 'if you know the group defining it will make the api more efficient',
255 );
6a488035
TO
256}
257
258/**
35823763 259 * Returns array of settings matching input parameters.
6a488035 260 *
cf470720 261 * @param array $params
35823763 262 * Array of one or more valid property_name=>value pairs.
6a488035 263 *
a6c01b45 264 * @return array
72b3a70c 265 * Array of matching settings
6a488035
TO
266 */
267function civicrm_api3_setting_get($params) {
268 $domains = _civicrm_api3_setting_getDomainArray($params);
35671d00
TO
269 $result = $result = CRM_Core_BAO_Setting::getItems($params, $domains, CRM_Utils_Array::value('return', $params, array()));
270 return civicrm_api3_create_success($result, $params, 'setting', 'get');
6a488035 271}
11e09c59 272/**
1747ab99 273 * Metadata for setting create function.
11e09c59 274 *
cf470720
TO
275 * @param array $params
276 * Parameters as passed to the API.
11e09c59 277 */
f60289e9 278function _civicrm_api3_setting_get_spec(&$params) {
6a488035 279 $params['domain_id'] = array(
5a159702
EM
280 'api.default' => 'current_domain',
281 'title' => 'Setting Domain',
21dfd5f5 282 'description' => 'if you do not pass in a domain id this will default to the current domain',
6a488035
TO
283 );
284 $params['group'] = array(
5a159702 285 'title' => 'Setting Group',
21dfd5f5 286 'description' => 'if you know the group defining it will make the api more efficient',
35671d00 287 );
6a488035
TO
288}
289/**
1747ab99
EM
290 * Returns value for specific parameter.
291 *
292 * Function requires more fields than 'get' but is intended for
6a488035
TO
293 * runtime usage & should be quicker
294 *
cf470720 295 * @param array $params
1747ab99 296 * Array of one or more valid.
6a488035
TO
297 * property_name=>value pairs.
298 *
a6c01b45 299 * @return array
1747ab99 300 * API result array.
6a488035
TO
301 */
302function civicrm_api3_setting_getvalue($params) {
303 $config = CRM_Core_Config::singleton();
9b873358 304 if (isset($config->$params['name'])) {
6a488035
TO
305 return $config->$params['name'];
306 }
307 return CRM_Core_BAO_Setting::getItem(
308 $params['group'],
309 CRM_Utils_Array::value('name', $params),
310 CRM_Utils_Array::value('component_id', $params),
311 CRM_Utils_Array::value('default_value', $params),
312 CRM_Utils_Array::value('contact_id', $params),
313 CRM_Utils_Array::value('domain_id', $params)
314 );
315}
316
11e09c59 317/**
1747ab99 318 * Metadata for setting create function.
11e09c59 319 *
cf470720
TO
320 * @param array $params
321 * Parameters as passed to the API.
11e09c59 322 */
f60289e9 323function _civicrm_api3_setting_getvalue_spec(&$params) {
6a488035
TO
324
325 $params['group'] = array(
e7483cbe
J
326 'title' => 'Settings Group',
327 'api.required' => TRUE,
6a488035
TO
328 );
329 $params['name'] = array(
e7483cbe
J
330 'title' => 'Setting Name',
331 'api.aliases' => array('return'),
6a488035
TO
332 );
333 $params['default_value'] = array(
e7483cbe 334 'title' => 'Default Value',
6a488035
TO
335 );
336 $params['component_id'] = array(
e7483cbe 337 'title' => 'Component Id',
6a488035
TO
338 );
339 $params['contact_id'] = array(
e7483cbe 340 'title' => 'Contact Id',
6a488035
TO
341 );
342 $params['domain_id'] = array(
5a159702 343 'title' => 'Setting Domain',
21dfd5f5 344 'description' => 'if you do not pass in a domain id this will default to the current domain',
6a488035
TO
345 );
346}
11e09c59
TO
347
348/**
1747ab99
EM
349 * Converts domain input into an array.
350 *
351 * If an array is passed in this is used, if 'all' is passed
6a488035 352 * in this is converted to 'all arrays'
f60289e9 353 *
354 * Really domain_id should always be set but doing an empty check because at the moment
355 * using crm-editable will pass an id & default won't be applied
1747ab99
EM
356 * we did talk about id being a pseudonym for domain_id in this api so applying it here.
357 *
d0997921 358 * @param array $params
1747ab99 359 *
645ee340
EM
360 * @return array
361 * @throws \Exception
6a488035 362 */
9b873358
TO
363function _civicrm_api3_setting_getDomainArray(&$params) {
364 if (empty($params['domain_id']) && isset($params['id'])) {
f60289e9 365 $params['domain_id'] = $params['id'];
366 }
367
9b873358 368 if ($params['domain_id'] == 'current_domain') {
6a488035
TO
369 $params['domain_id'] = CRM_Core_Config::domainID();
370 }
371
9b873358 372 if ($params['domain_id'] == 'all') {
35671d00 373 $domainAPIResult = civicrm_api('domain', 'get', array('version' => 3, 'return' => 'id'));
6a488035
TO
374 if (isset($domainAPIResult['values'])) {
375 $params['domain_id'] = array_keys($domainAPIResult['values']);
376 }
92e4c2a5 377 else {
6a488035
TO
378 throw new Exception('All domains not retrieved - problem with Domain Get api call ' . $domainAPIResult['error_message']);
379 }
380 }
9b873358 381 if (is_array($params['domain_id'])) {
6a488035
TO
382 $domains = $params['domain_id'];
383 }
92e4c2a5 384 else {
6a488035
TO
385 $domains = array($params['domain_id']);
386 }
387 return $domains;
388}