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