c1d6b9086a3c00f9908e586138915b85a61743d0
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * File for CiviCRM APIv3 settings
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
38 function civicrm_api3_setting_getfields($params) {
39 if (!empty($params['action']) && strtolower($params['action']) == 'getvalue') {
42 'title' => 'name of setting field',
44 'type' => CRM_Utils_Type
::T_STRING
),
47 'title' => 'Setting Group',
48 'description' => 'Settings Group. This is required if the setting is not stored in config',
49 'type' => CRM_Utils_Type
::T_STRING
),
51 return civicrm_api3_create_success($result, $params, 'setting', 'getfields');
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
56 $params['filters']['name'] = $params['name'];
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)
64 // find any supplemental information
65 if (!empty($params['action'])) {
66 $specFunction = '_civicrm_api3_setting_' . strtolower($params['action']) . '_spec';
67 if (function_exists($specFunction)) {
68 $specFunction($result);
71 return civicrm_api3_create_success($result, $params, 'setting', 'getfields');
75 * Alter metadata for getfields functions
76 * @param array $params
78 function _civicrm_api3_setting_getfields_spec(&$params) {
79 $params['filters'] = array('title' => 'Fields you wish to filter by e.g. array("group_name" => "CiviCRM Preferences")');
80 $params['component_id'] = array('title' => 'id of relevant component');
81 $params['profile'] = array('title' => 'profile is passed through to hooks & added to cachestring');
85 * Return default values for settings. We will domain key this as it could vary by domain (ie. urls)
86 * as we will be creating the option for a function rather than an value to be in the defaults
87 * Note that is not in place as yet
88 * @param array $params
90 * @throws \CiviCRM_API3_Exception
93 function civicrm_api3_setting_getdefaults(&$params) {
94 $settings = civicrm_api3('setting', 'getfields', $params);
95 $domains = _civicrm_api3_setting_getDomainArray($params);
97 foreach ($domains as $domainID) {
98 $defaults[$domainID] = array();
99 $noDefaults = array();
100 foreach ($settings['values'] as $setting => $spec) {
101 if (array_key_exists('default', $spec) && !is_null($spec['default'])) {
102 $defaults[$domainID][$setting] = $spec['default'];
105 $noDefaults[$setting] = 1;
108 if (!empty($params['debug'])) {
109 // we are only tracking 'noDefaults' to help us check the xml
110 print_r($noDefaults);
113 return civicrm_api3_create_success($defaults, $params, 'setting', 'getfields');
116 * Metadata for setting create function
118 * @param array $params
119 * Parameters as passed to the API.
121 function _civicrm_api3_setting_getdefaults_spec(&$params) {
122 $params['domain_id'] = array(
123 'api.default' => 'current_domain',
124 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the current domain
125 an array or "all" are acceptable values for multiple domains',
126 'title' => 'Setting Domain',
131 * Revert settings to defaults
132 * @param array $params
136 function civicrm_api3_setting_revert(&$params) {
137 $defaults = civicrm_api('setting', 'getdefaults', $params);
138 $fields = civicrm_api('setting', 'getfields', $params);
139 $fields = $fields['values'];
140 $domains = _civicrm_api3_setting_getDomainArray($params);
142 foreach ($domains as $domainID) {
143 $valuesToRevert = array_intersect_key($defaults['values'][$domainID], $fields);
144 if (!empty($valuesToRevert)) {
145 $valuesToRevert['version'] = $params['version'];
146 $valuesToRevert['domain_id'] = $domainID;
147 // note that I haven't looked at how the result would appear with multiple domains in play
148 $result = array_merge($result, civicrm_api('setting', 'create', $valuesToRevert));
152 return civicrm_api3_create_success($result, $params, 'setting', 'revert');
156 * Alter metadata for getfields functions
157 * @param array $params
159 function _civicrm_api3_setting_revert_spec(&$params) {
160 $params['name'] = array('title' => 'Setting Name belongs to');
161 $params['component_id'] = array('title' => 'id of relevant component');
162 $params['domain_id'] = array(
163 'api.default' => 'current_domain',
164 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the current domain
165 an array or "all" are acceptable values for multiple domains',
166 'title' => 'Setting Domain',
171 * Revert settings to defaults
172 * @param array $params
174 * @throws \CiviCRM_API3_Exception
177 function civicrm_api3_setting_fill(&$params) {
178 $defaults = civicrm_api3('setting', 'getdefaults', $params);
179 $domains = _civicrm_api3_setting_getDomainArray($params);
181 foreach ($domains as $domainID) {
183 'version' => $params['version'],
184 'domain_id' => $domainID,
186 $existing = civicrm_api3('setting', 'get', $apiArray);
187 $valuesToFill = array_diff_key($defaults['values'][$domainID], $existing['values'][$domainID]);
188 if (!empty($valuesToFill)) {
189 $result = array_merge($result, civicrm_api('setting', 'create', $valuesToFill +
$apiArray));
192 return civicrm_api3_create_success($result, $params, 'setting', 'fill');
196 * Alter metadata for getfields functions
197 * @param array $params
199 function _civicrm_api3_setting_fill_spec(&$params) {
200 $params['name'] = array('title' => 'Setting Name belongs to');
201 $params['component_id'] = array('title' => 'id of relevant component');
202 $params['domain_id'] = array(
203 'api.default' => 'current_domain',
204 'title' => 'Setting Domain',
205 'description' => 'Defaults may differ by domain - if you do not pass in a domain id this will default to the current domain
206 an array or "all" are acceptable values for multiple domains',
211 * Create or update a setting
213 * @param array $params
214 * Associative array of setting.
215 * name/value pairs + other vars as applicable - see getfields for more
216 * @example SettingCreate.php Std Create example
220 * {@getfields setting_create}
222 function civicrm_api3_setting_create($params) {
223 $domains = _civicrm_api3_setting_getDomainArray($params);
224 $result = CRM_Core_BAO_Setting
::setItems($params, $domains);
225 return civicrm_api3_create_success($result, $params, 'setting', 'create');
229 * Metadata for setting create function
231 * @param array $params
232 * Parameters as passed to the API.
234 function _civicrm_api3_setting_create_spec(&$params) {
235 $params['domain_id'] = array(
236 'api.default' => 'current_domain',
237 'title' => 'Setting Domain',
238 'description' => 'if you do not pass in a domain id this will default to the current domain
239 an array or "all" are acceptable values for multiple domains',
241 $params['group'] = array(
242 'title' => 'Setting Group',
243 'description' => 'if you know the group defining it will make the api more efficient',
248 * Returns array of settings matching input parameters
250 * @param array $params
251 * Array of one or more valid.
252 * property_name=>value pairs.
255 * Array of matching settings
256 * {@getfields setting_get}
258 function civicrm_api3_setting_get($params) {
259 $domains = _civicrm_api3_setting_getDomainArray($params);
260 $result = $result = CRM_Core_BAO_Setting
::getItems($params, $domains, CRM_Utils_Array
::value('return', $params, array()));
261 return civicrm_api3_create_success($result, $params, 'setting', 'get');
264 * Metadata for setting create function
266 * @param array $params
267 * Parameters as passed to the API.
269 function _civicrm_api3_setting_get_spec(&$params) {
270 $params['domain_id'] = array(
271 'api.default' => 'current_domain',
272 'title' => 'Setting Domain',
273 'description' => 'if you do not pass in a domain id this will default to the current domain',
275 $params['group'] = array(
276 'title' => 'Setting Group',
277 'description' => 'if you know the group defining it will make the api more efficient',
281 * Returns value for specific parameter. Function requires more fields than 'get' but is intended for
282 * runtime usage & should be quicker
284 * @param array $params
285 * (reference) Array of one or more valid.
286 * property_name=>value pairs.
289 * Array of matching settings
290 * {@getfields setting_get}
292 function civicrm_api3_setting_getvalue($params) {
293 $config = CRM_Core_Config
::singleton();
294 if (isset($config->$params['name'])) {
295 return $config->$params['name'];
297 return CRM_Core_BAO_Setting
::getItem(
299 CRM_Utils_Array
::value('name', $params),
300 CRM_Utils_Array
::value('component_id', $params),
301 CRM_Utils_Array
::value('default_value', $params),
302 CRM_Utils_Array
::value('contact_id', $params),
303 CRM_Utils_Array
::value('domain_id', $params)
308 * Metadata for setting create function
310 * @param array $params
311 * Parameters as passed to the API.
313 function _civicrm_api3_setting_getvalue_spec(&$params) {
315 $params['group'] = array(
316 'title' => 'Settings Group',
317 'api.required' => TRUE,
319 $params['name'] = array(
320 'title' => 'Setting Name',
321 'api.aliases' => array('return'),
323 $params['default_value'] = array(
324 'title' => 'Default Value',
326 $params['component_id'] = array(
327 'title' => 'Component Id',
329 $params['contact_id'] = array(
330 'title' => 'Contact Id',
332 $params['domain_id'] = array(
333 'title' => 'Setting Domain',
334 'description' => 'if you do not pass in a domain id this will default to the current domain',
339 * Converts domain input into an array. If an array is passed in this is used, if 'all' is passed
340 * in this is converted to 'all arrays'
342 * Really domain_id should always be set but doing an empty check because at the moment
343 * using crm-editable will pass an id & default won't be applied
344 * we did talk about id being a pseudonym for domain_id in this api so applying it here
345 * @param array $params
349 function _civicrm_api3_setting_getDomainArray(&$params) {
350 if (empty($params['domain_id']) && isset($params['id'])) {
351 $params['domain_id'] = $params['id'];
354 if ($params['domain_id'] == 'current_domain') {
355 $params['domain_id'] = CRM_Core_Config
::domainID();
358 if ($params['domain_id'] == 'all') {
359 $domainAPIResult = civicrm_api('domain', 'get', array('version' => 3, 'return' => 'id'));
360 if (isset($domainAPIResult['values'])) {
361 $params['domain_id'] = array_keys($domainAPIResult['values']);
364 throw new Exception('All domains not retrieved - problem with Domain Get api call ' . $domainAPIResult['error_message']);
367 if (is_array($params['domain_id'])) {
368 $domains = $params['domain_id'];
371 $domains = array($params['domain_id']);