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