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