Commit | Line | Data |
---|---|---|
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 |
d0997921 | 35 | * @param array $params |
645ee340 | 36 | * @return array |
6a488035 TO |
37 | */ |
38 | ||
39 | function civicrm_api3_setting_getfields($params) { | |
9b873358 | 40 | if (!empty($params['action']) && strtolower($params['action']) == 'getvalue') { |
6a488035 TO |
41 | $result = array( |
42 | 'name' => array( | |
43 | 'title' => 'name of setting field', | |
44 | 'api.required' => 1, | |
45 | 'type' => CRM_Utils_Type::T_STRING), | |
c14d6dd1 | 46 | 'group' => array( |
6a488035 | 47 | 'api.required' => 0, |
1fdb479f | 48 | 'title' => 'Setting Group', |
6a488035 | 49 | 'description' => 'Settings Group. This is required if the setting is not stored in config', |
21dfd5f5 | 50 | 'type' => CRM_Utils_Type::T_STRING), |
6a488035 TO |
51 | ); |
52 | return civicrm_api3_create_success($result, $params, 'setting', 'getfields'); | |
53 | } | |
9b873358 | 54 | if (!empty($params['name'])) { |
6a488035 TO |
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( | |
35671d00 | 60 | CRM_Utils_Array::value('component_id', $params), |
6a488035 | 61 | CRM_Utils_Array::value('filters', $params, array()), |
35671d00 TO |
62 | CRM_Utils_Array::value('domain_id', $params, NULL), |
63 | CRM_Utils_Array::value('profile', $params, NULL) | |
6a488035 TO |
64 | ); |
65 | // find any supplemental information | |
9b873358 | 66 | if (!empty($params['action'])) { |
f60289e9 | 67 | $specFunction = '_civicrm_api3_setting_' . strtolower($params['action']) . '_spec'; |
6a488035 TO |
68 | if (function_exists($specFunction)) { |
69 | $specFunction($result); | |
70 | } | |
71 | } | |
72 | return civicrm_api3_create_success($result, $params, 'setting', 'getfields'); | |
73 | } | |
11e09c59 TO |
74 | |
75 | /** | |
6a488035 | 76 | * Alter metadata for getfields functions |
d0997921 | 77 | * @param array $params |
6a488035 | 78 | */ |
f60289e9 | 79 | function _civicrm_api3_setting_getfields_spec(&$params) { |
6a488035 TO |
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 | } | |
11e09c59 TO |
84 | |
85 | /** | |
6a488035 TO |
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 | |
d0997921 | 89 | * @param array $params |
645ee340 EM |
90 | * @return array |
91 | * @throws \CiviCRM_API3_Exception | |
92 | * @throws \Exception | |
6a488035 | 93 | */ |
9b873358 | 94 | function civicrm_api3_setting_getdefaults(&$params) { |
35671d00 | 95 | $settings = civicrm_api3('setting', 'getfields', $params); |
6a488035 TO |
96 | $domains = _civicrm_api3_setting_getDomainArray($params); |
97 | $defaults = array(); | |
9b873358 | 98 | foreach ($domains as $domainID) { |
6a488035 TO |
99 | $defaults[$domainID] = array(); |
100 | $noDefaults = array(); | |
9b873358 TO |
101 | foreach ($settings['values'] as $setting => $spec) { |
102 | if (array_key_exists('default', $spec) && !is_null($spec['default'])) { | |
6a488035 TO |
103 | $defaults[$domainID][$setting] = $spec['default']; |
104 | } | |
92e4c2a5 | 105 | else { |
6a488035 TO |
106 | $noDefaults[$setting] = 1; |
107 | } | |
108 | } | |
9b873358 | 109 | if (!empty($params['debug'])) { |
6a488035 TO |
110 | // we are only tracking 'noDefaults' to help us check the xml |
111 | print_r($noDefaults); | |
112 | } | |
113 | } | |
35671d00 | 114 | return civicrm_api3_create_success($defaults, $params, 'setting', 'getfields'); |
6a488035 | 115 | } |
11e09c59 TO |
116 | /** |
117 | * Metadata for setting create function | |
118 | * | |
cf470720 TO |
119 | * @param array $params |
120 | * Parameters as passed to the API. | |
11e09c59 | 121 | */ |
f60289e9 | 122 | function _civicrm_api3_setting_getdefaults_spec(&$params) { |
6a488035 | 123 | $params['domain_id'] = array( |
5a159702 EM |
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', | |
6a488035 TO |
128 | ); |
129 | } | |
11e09c59 TO |
130 | |
131 | /** | |
6a488035 | 132 | * Revert settings to defaults |
d0997921 | 133 | * @param array $params |
645ee340 EM |
134 | * @return array |
135 | * @throws \Exception | |
6a488035 | 136 | */ |
9b873358 | 137 | function civicrm_api3_setting_revert(&$params) { |
35671d00 TO |
138 | $defaults = civicrm_api('setting', 'getdefaults', $params); |
139 | $fields = civicrm_api('setting', 'getfields', $params); | |
6a488035 TO |
140 | $fields = $fields['values']; |
141 | $domains = _civicrm_api3_setting_getDomainArray($params); | |
142 | $result = array(); | |
9b873358 | 143 | foreach ($domains as $domainID) { |
6a488035 | 144 | $valuesToRevert = array_intersect_key($defaults['values'][$domainID], $fields); |
9b873358 | 145 | if (!empty($valuesToRevert)) { |
6a488035 TO |
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 | } | |
11e09c59 TO |
155 | |
156 | /** | |
6a488035 | 157 | * Alter metadata for getfields functions |
d0997921 | 158 | * @param array $params |
11e09c59 | 159 | */ |
f60289e9 | 160 | function _civicrm_api3_setting_revert_spec(&$params) { |
6a488035 TO |
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 | |
5a159702 EM |
166 | an array or "all" are acceptable values for multiple domains', |
167 | 'title' => 'Setting Domain', | |
6a488035 TO |
168 | ); |
169 | } | |
170 | ||
11e09c59 | 171 | /** |
6a488035 | 172 | * Revert settings to defaults |
d0997921 | 173 | * @param array $params |
645ee340 EM |
174 | * @return array |
175 | * @throws \CiviCRM_API3_Exception | |
176 | * @throws \Exception | |
11e09c59 | 177 | */ |
9b873358 | 178 | function civicrm_api3_setting_fill(&$params) { |
35671d00 | 179 | $defaults = civicrm_api3('setting', 'getdefaults', $params); |
6a488035 TO |
180 | $domains = _civicrm_api3_setting_getDomainArray($params); |
181 | $result = array(); | |
9b873358 | 182 | foreach ($domains as $domainID) { |
6a488035 TO |
183 | $apiArray = array( |
184 | 'version' => $params['version'], | |
21dfd5f5 | 185 | 'domain_id' => $domainID, |
6a488035 | 186 | ); |
35671d00 | 187 | $existing = civicrm_api3('setting', 'get', $apiArray); |
6a488035 | 188 | $valuesToFill = array_diff_key($defaults['values'][$domainID], $existing['values'][$domainID]); |
9b873358 | 189 | if (!empty($valuesToFill)) { |
6a488035 TO |
190 | $result = array_merge($result, civicrm_api('setting', 'create', $valuesToFill + $apiArray)); |
191 | } | |
192 | } | |
193 | return civicrm_api3_create_success($result, $params, 'setting', 'fill'); | |
194 | } | |
11e09c59 TO |
195 | |
196 | /** | |
6a488035 | 197 | * Alter metadata for getfields functions |
d0997921 | 198 | * @param array $params |
11e09c59 | 199 | */ |
f60289e9 | 200 | function _civicrm_api3_setting_fill_spec(&$params) { |
6a488035 TO |
201 | $params['name'] = array('title' => 'Setting Name belongs to'); |
202 | $params['component_id'] = array('title' => 'id of relevant component'); | |
203 | $params['domain_id'] = array( | |
5a159702 EM |
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 | |
21dfd5f5 | 207 | an array or "all" are acceptable values for multiple domains', |
6a488035 TO |
208 | ); |
209 | } | |
210 | ||
211 | /** | |
212 | * Create or update a setting | |
213 | * | |
cf470720 TO |
214 | * @param array $params |
215 | * Associative array of setting. | |
6a488035 TO |
216 | * name/value pairs + other vars as applicable - see getfields for more |
217 | * @example SettingCreate.php Std Create example | |
218 | * | |
a6c01b45 | 219 | * @return array |
72b3a70c | 220 | * api result array |
6a488035 | 221 | * {@getfields setting_create} |
6a488035 TO |
222 | */ |
223 | function civicrm_api3_setting_create($params) { | |
224 | $domains = _civicrm_api3_setting_getDomainArray($params); | |
225 | $result = CRM_Core_BAO_Setting::setItems($params, $domains); | |
35671d00 | 226 | return civicrm_api3_create_success($result, $params, 'setting', 'create'); |
6a488035 | 227 | } |
11e09c59 TO |
228 | |
229 | /** | |
6a488035 TO |
230 | * Metadata for setting create function |
231 | * | |
cf470720 TO |
232 | * @param array $params |
233 | * Parameters as passed to the API. | |
6a488035 | 234 | */ |
f60289e9 | 235 | function _civicrm_api3_setting_create_spec(&$params) { |
6a488035 TO |
236 | $params['domain_id'] = array( |
237 | 'api.default' => 'current_domain', | |
5a159702 | 238 | 'title' => 'Setting Domain', |
6a488035 | 239 | 'description' => 'if you do not pass in a domain id this will default to the current domain |
21dfd5f5 | 240 | an array or "all" are acceptable values for multiple domains', |
6a488035 | 241 | ); |
35671d00 | 242 | $params['group'] = array( |
5a159702 | 243 | 'title' => 'Setting Group', |
21dfd5f5 | 244 | 'description' => 'if you know the group defining it will make the api more efficient', |
35671d00 | 245 | ); |
6a488035 TO |
246 | } |
247 | ||
248 | /** | |
249 | * Returns array of settings matching input parameters | |
250 | * | |
cf470720 TO |
251 | * @param array $params |
252 | * Array of one or more valid. | |
6a488035 TO |
253 | * property_name=>value pairs. |
254 | * | |
a6c01b45 | 255 | * @return array |
72b3a70c | 256 | * Array of matching settings |
6a488035 | 257 | * {@getfields setting_get} |
6a488035 TO |
258 | */ |
259 | function civicrm_api3_setting_get($params) { | |
260 | $domains = _civicrm_api3_setting_getDomainArray($params); | |
35671d00 TO |
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'); | |
6a488035 | 263 | } |
11e09c59 | 264 | /** |
6a488035 | 265 | * Metadata for setting create function |
11e09c59 | 266 | * |
cf470720 TO |
267 | * @param array $params |
268 | * Parameters as passed to the API. | |
11e09c59 | 269 | */ |
f60289e9 | 270 | function _civicrm_api3_setting_get_spec(&$params) { |
6a488035 | 271 | $params['domain_id'] = array( |
5a159702 EM |
272 | 'api.default' => 'current_domain', |
273 | 'title' => 'Setting Domain', | |
21dfd5f5 | 274 | 'description' => 'if you do not pass in a domain id this will default to the current domain', |
6a488035 TO |
275 | ); |
276 | $params['group'] = array( | |
5a159702 | 277 | 'title' => 'Setting Group', |
21dfd5f5 | 278 | 'description' => 'if you know the group defining it will make the api more efficient', |
35671d00 | 279 | ); |
6a488035 TO |
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 | * | |
cf470720 TO |
285 | * @param array $params |
286 | * (reference) Array of one or more valid. | |
6a488035 TO |
287 | * property_name=>value pairs. |
288 | * | |
a6c01b45 | 289 | * @return array |
72b3a70c | 290 | * Array of matching settings |
6a488035 | 291 | * {@getfields setting_get} |
6a488035 TO |
292 | */ |
293 | function civicrm_api3_setting_getvalue($params) { | |
294 | $config = CRM_Core_Config::singleton(); | |
9b873358 | 295 | if (isset($config->$params['name'])) { |
6a488035 TO |
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 | ||
11e09c59 | 308 | /** |
6a488035 | 309 | * Metadata for setting create function |
11e09c59 | 310 | * |
cf470720 TO |
311 | * @param array $params |
312 | * Parameters as passed to the API. | |
11e09c59 | 313 | */ |
f60289e9 | 314 | function _civicrm_api3_setting_getvalue_spec(&$params) { |
6a488035 TO |
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( | |
5a159702 | 334 | 'title' => 'Setting Domain', |
21dfd5f5 | 335 | 'description' => 'if you do not pass in a domain id this will default to the current domain', |
6a488035 TO |
336 | ); |
337 | } | |
11e09c59 TO |
338 | |
339 | /** | |
6a488035 TO |
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' | |
f60289e9 | 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 | |
d0997921 | 346 | * @param array $params |
645ee340 EM |
347 | * @return array |
348 | * @throws \Exception | |
6a488035 | 349 | */ |
9b873358 TO |
350 | function _civicrm_api3_setting_getDomainArray(&$params) { |
351 | if (empty($params['domain_id']) && isset($params['id'])) { | |
f60289e9 | 352 | $params['domain_id'] = $params['id']; |
353 | } | |
354 | ||
9b873358 | 355 | if ($params['domain_id'] == 'current_domain') { |
6a488035 TO |
356 | $params['domain_id'] = CRM_Core_Config::domainID(); |
357 | } | |
358 | ||
9b873358 | 359 | if ($params['domain_id'] == 'all') { |
35671d00 | 360 | $domainAPIResult = civicrm_api('domain', 'get', array('version' => 3, 'return' => 'id')); |
6a488035 TO |
361 | if (isset($domainAPIResult['values'])) { |
362 | $params['domain_id'] = array_keys($domainAPIResult['values']); | |
363 | } | |
92e4c2a5 | 364 | else { |
6a488035 TO |
365 | throw new Exception('All domains not retrieved - problem with Domain Get api call ' . $domainAPIResult['error_message']); |
366 | } | |
367 | } | |
9b873358 | 368 | if (is_array($params['domain_id'])) { |
6a488035 TO |
369 | $domains = $params['domain_id']; |
370 | } | |
92e4c2a5 | 371 | else { |
6a488035 TO |
372 | $domains = array($params['domain_id']); |
373 | } | |
374 | return $domains; | |
375 | } |