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