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