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
39 function civicrm_api3_setting_getfields($params) {
40 if (!empty($params['action']) && strtolower($params['action']) == 'getvalue') {
43 'title' => 'name of setting field',
45 'type' => CRM_Utils_Type
::T_STRING
),
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
),
52 return civicrm_api3_create_success($result, $params, 'setting', 'getfields');
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
57 $params['filters']['name'] = $params['name'];
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)
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);
72 return civicrm_api3_create_success($result, $params, 'setting', 'getfields');
76 * Alter metadata for getfields functions
77 * @param array $params
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');
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
91 * @throws \CiviCRM_API3_Exception
94 function civicrm_api3_setting_getdefaults(&$params) {
95 $settings = civicrm_api3('setting', 'getfields', $params);
96 $domains = _civicrm_api3_setting_getDomainArray($params);
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'];
106 $noDefaults[$setting] = 1;
109 if (!empty($params['debug'])) {
110 // we are only tracking 'noDefaults' to help us check the xml
111 print_r($noDefaults);
114 return civicrm_api3_create_success($defaults, $params, 'setting', 'getfields');
117 * Metadata for setting create function
119 * @param array $params
120 * Parameters as passed to the API.
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',
132 * Revert settings to defaults
133 * @param array $params
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);
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));
153 return civicrm_api3_create_success($result, $params, 'setting', 'revert');
157 * Alter metadata for getfields functions
158 * @param array $params
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',
172 * Revert settings to defaults
173 * @param array $params
175 * @throws \CiviCRM_API3_Exception
178 function civicrm_api3_setting_fill(&$params) {
179 $defaults = civicrm_api3('setting', 'getdefaults', $params);
180 $domains = _civicrm_api3_setting_getDomainArray($params);
182 foreach ($domains as $domainID) {
184 'version' => $params['version'],
185 'domain_id' => $domainID,
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));
193 return civicrm_api3_create_success($result, $params, 'setting', 'fill');
197 * Alter metadata for getfields functions
198 * @param array $params
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',
212 * Create or update a setting
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
221 * {@getfields setting_create}
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');
230 * Metadata for setting create function
232 * @param array $params
233 * Parameters as passed to the API.
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',
242 $params['group'] = array(
243 'title' => 'Setting Group',
244 'description' => 'if you know the group defining it will make the api more efficient',
249 * Returns array of settings matching input parameters
251 * @param array $params
252 * Array of one or more valid.
253 * property_name=>value pairs.
256 * Array of matching settings
257 * {@getfields setting_get}
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');
265 * Metadata for setting create function
267 * @param array $params
268 * Parameters as passed to the API.
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',
276 $params['group'] = array(
277 'title' => 'Setting Group',
278 'description' => 'if you know the group defining it will make the api more efficient',
282 * Returns value for specific parameter. Function requires more fields than 'get' but is intended for
283 * runtime usage & should be quicker
285 * @param array $params
286 * (reference) Array of one or more valid.
287 * property_name=>value pairs.
290 * Array of matching settings
291 * {@getfields setting_get}
293 function civicrm_api3_setting_getvalue($params) {
294 $config = CRM_Core_Config
::singleton();
295 if (isset($config->$params['name'])) {
296 return $config->$params['name'];
298 return CRM_Core_BAO_Setting
::getItem(
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)
309 * Metadata for setting create function
311 * @param array $params
312 * Parameters as passed to the API.
314 function _civicrm_api3_setting_getvalue_spec(&$params) {
316 $params['group'] = array(
317 'title' => 'Settings Group',
318 'api.required' => TRUE,
320 $params['name'] = array(
321 'title' => 'Setting Name',
322 'api.aliases' => array('return'),
324 $params['default_value'] = array(
325 'title' => 'Default Value',
327 $params['component_id'] = array(
328 'title' => 'Component Id',
330 $params['contact_id'] = array(
331 'title' => 'Contact Id',
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',
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'
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
350 function _civicrm_api3_setting_getDomainArray(&$params) {
351 if (empty($params['domain_id']) && isset($params['id'])) {
352 $params['domain_id'] = $params['id'];
355 if ($params['domain_id'] == 'current_domain') {
356 $params['domain_id'] = CRM_Core_Config
::domainID();
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']);
365 throw new Exception('All domains not retrieved - problem with Domain Get api call ' . $domainAPIResult['error_message']);
368 if (is_array($params['domain_id'])) {
369 $domains = $params['domain_id'];
372 $domains = array($params['domain_id']);