Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * BAO object for civicrm_setting table. This table is used to store civicrm settings that are not used | |
36 | * very frequently (i.e. not on every page load) | |
37 | * | |
38 | * The group column is used for grouping together all settings that logically belong to the same set. | |
39 | * Thus all settings in the same group are retrieved with one DB call and then cached for future needs. | |
6a488035 TO |
40 | */ |
41 | class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { | |
42 | ||
43 | /** | |
fe482240 | 44 | * Various predefined settings that have been migrated to the setting table. |
6a488035 | 45 | */ |
7da04cde | 46 | const |
6a488035 TO |
47 | ADDRESS_STANDARDIZATION_PREFERENCES_NAME = 'Address Standardization Preferences', |
48 | CAMPAIGN_PREFERENCES_NAME = 'Campaign Preferences', | |
5d0140d9 | 49 | DEVELOPER_PREFERENCES_NAME = 'Developer Preferences', |
6a488035 TO |
50 | DIRECTORY_PREFERENCES_NAME = 'Directory Preferences', |
51 | EVENT_PREFERENCES_NAME = 'Event Preferences', | |
52 | MAILING_PREFERENCES_NAME = 'Mailing Preferences', | |
1ec598f3 | 53 | MAP_PREFERENCES_NAME = 'Map Preferences', |
6a488035 TO |
54 | CONTRIBUTE_PREFERENCES_NAME = 'Contribute Preferences', |
55 | MEMBER_PREFERENCES_NAME = 'Member Preferences', | |
56 | MULTISITE_PREFERENCES_NAME = 'Multi Site Preferences', | |
57 | PERSONAL_PREFERENCES_NAME = 'Personal Preferences', | |
58 | SYSTEM_PREFERENCES_NAME = 'CiviCRM Preferences', | |
59 | URL_PREFERENCES_NAME = 'URL Preferences', | |
60 | LOCALIZATION_PREFERENCES_NAME = 'Localization Preferences', | |
61 | SEARCH_PREFERENCES_NAME = 'Search Preferences'; | |
6a488035 | 62 | |
6a488035 | 63 | /** |
fe482240 | 64 | * Retrieve the value of a setting from the DB table. |
6a488035 | 65 | * |
6a0b768e | 66 | * @param string $group |
73152f09 | 67 | * The group name of the item (deprecated). |
6a0b768e TO |
68 | * @param string $name |
69 | * (required) The name under which this item is stored. | |
70 | * @param int $componentID | |
71 | * The optional component ID (so componenets can share the same name space). | |
72 | * @param string $defaultValue | |
73 | * The default value to return for this setting if not present in DB. | |
74 | * @param int $contactID | |
75 | * If set, this is a contactID specific setting, else its a global setting. | |
da6b46f4 | 76 | * |
100fef9d | 77 | * @param int $domainID |
6a488035 | 78 | * |
72b3a70c CW |
79 | * @return mixed |
80 | * The data if present in the setting table, else null | |
6a488035 | 81 | */ |
2da40d21 | 82 | public static function getItem( |
56cb3188 | 83 | $group, |
242bd179 TO |
84 | $name = NULL, |
85 | $componentID = NULL, | |
6a488035 | 86 | $defaultValue = NULL, |
242bd179 TO |
87 | $contactID = NULL, |
88 | $domainID = NULL | |
6a488035 | 89 | ) { |
3a84c0ab TO |
90 | /** @var \Civi\Core\SettingsManager $manager */ |
91 | $manager = \Civi::service('settings_manager'); | |
92 | $settings = ($contactID === NULL) ? $manager->getBagByDomain($domainID) : $manager->getBagByContact($domainID, $contactID); | |
73152f09 JK |
93 | if ($name === NULL) { |
94 | CRM_Core_Error::debug_log_message("Deprecated: Group='$group'. Name should be provided.\n"); | |
95 | } | |
96 | if ($componentID !== NULL) { | |
97 | CRM_Core_Error::debug_log_message("Deprecated: Group='$group'. Name='$name'. Component should be omitted\n"); | |
98 | } | |
99 | if ($defaultValue !== NULL) { | |
100 | CRM_Core_Error::debug_log_message("Deprecated: Group='$group'. Name='$name'. Defaults should come from metadata\n"); | |
6a488035 | 101 | } |
3a84c0ab | 102 | return $name ? $settings->get($name) : $settings->all(); |
6a488035 TO |
103 | } |
104 | ||
105 | /** | |
35823763 | 106 | * Store multiple items in the setting table. |
6a488035 | 107 | * |
6a0b768e TO |
108 | * @param array $params |
109 | * (required) An api formatted array of keys and values. | |
35823763 | 110 | * @param array $domains Array of domains to get settings for. Default is the current domain |
dd244018 EM |
111 | * @param $settingsToReturn |
112 | * | |
35823763 | 113 | * @return array |
6a488035 | 114 | */ |
00be9182 | 115 | public static function getItems(&$params, $domains = NULL, $settingsToReturn) { |
0e04f44e | 116 | $originalDomain = CRM_Core_Config::domainID(); |
6a488035 | 117 | if (empty($domains)) { |
0e04f44e | 118 | $domains[] = $originalDomain; |
6a488035 TO |
119 | } |
120 | if (!empty($settingsToReturn) && !is_array($settingsToReturn)) { | |
121 | $settingsToReturn = array($settingsToReturn); | |
122 | } | |
0e04f44e | 123 | |
6a488035 TO |
124 | $fields = $result = array(); |
125 | $fieldsToGet = self::validateSettingsInput(array_flip($settingsToReturn), $fields, FALSE); | |
7583c3f3 | 126 | foreach ($domains as $domainID) { |
7583c3f3 | 127 | $result[$domainID] = array(); |
6a488035 | 128 | foreach ($fieldsToGet as $name => $value) { |
76bd16ab TO |
129 | $contactID = CRM_Utils_Array::value('contact_id', $params); |
130 | $setting = CRM_Core_BAO_Setting::getItem(NULL, $name, NULL, NULL, $contactID, $domainID); | |
6a488035 TO |
131 | if (!is_null($setting)) { |
132 | // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones | |
133 | // e.g for revert of fill actions | |
7583c3f3 | 134 | $result[$domainID][$name] = $setting; |
6a488035 TO |
135 | } |
136 | } | |
137 | } | |
138 | return $result; | |
139 | } | |
140 | ||
141 | /** | |
fe482240 | 142 | * Store an item in the setting table. |
6a488035 | 143 | * |
a57707d3 TO |
144 | * _setItem() is the common logic shared by setItem() and setItems(). |
145 | * | |
6a0b768e TO |
146 | * @param object $value |
147 | * (required) The value that will be serialized and stored. | |
148 | * @param string $group | |
73152f09 | 149 | * The group name of the item (deprecated). |
6a0b768e TO |
150 | * @param string $name |
151 | * (required) The name of the setting. | |
152 | * @param int $componentID | |
153 | * The optional component ID (so componenets can share the same name space). | |
100fef9d | 154 | * @param int $contactID |
6a0b768e TO |
155 | * @param int $createdID |
156 | * An optional ID to assign the creator to. If not set, retrieved from session. | |
fd31fa4c | 157 | * |
100fef9d | 158 | * @param int $domainID |
6a488035 | 159 | */ |
2da40d21 | 160 | public static function setItem( |
6a488035 TO |
161 | $value, |
162 | $group, | |
163 | $name, | |
164 | $componentID = NULL, | |
242bd179 TO |
165 | $contactID = NULL, |
166 | $createdID = NULL, | |
167 | $domainID = NULL | |
a57707d3 | 168 | ) { |
3a84c0ab TO |
169 | /** @var \Civi\Core\SettingsManager $manager */ |
170 | $manager = \Civi::service('settings_manager'); | |
171 | $settings = ($contactID === NULL) ? $manager->getBagByDomain($domainID) : $manager->getBagByContact($domainID, $contactID); | |
172 | $settings->set($name, $value); | |
a57707d3 TO |
173 | } |
174 | ||
6a488035 TO |
175 | /** |
176 | * Store multiple items in the setting table. Note that this will also store config keys | |
177 | * the storage is determined by the metdata and is affected by | |
178 | * 'name' setting's name | |
6a488035 TO |
179 | * 'config_key' = the config key is different to the settings key - e.g. debug where there was a conflict |
180 | * 'legacy_key' = rename from config or setting with this name | |
181 | * | |
a57707d3 TO |
182 | * _setItem() is the common logic shared by setItem() and setItems(). |
183 | * | |
6a0b768e TO |
184 | * @param array $params |
185 | * (required) An api formatted array of keys and values. | |
2a6da8d7 EM |
186 | * @param null $domains |
187 | * | |
188 | * @throws api_Exception | |
6a488035 | 189 | * @domains array an array of domains to get settings for. Default is the current domain |
3d0d359e | 190 | * @return array |
6a488035 | 191 | */ |
00be9182 | 192 | public static function setItems(&$params, $domains = NULL) { |
3a84c0ab TO |
193 | $domains = empty($domains) ? array(CRM_Core_Config::domainID()) : $domains; |
194 | ||
195 | // FIXME: redundant validation | |
196 | // FIXME: this whole thing should just be a loop to call $settings->add() on each domain. | |
197 | ||
23bb9c85 | 198 | $fields = array(); |
6a488035 TO |
199 | $fieldsToSet = self::validateSettingsInput($params, $fields); |
200 | ||
201 | foreach ($fieldsToSet as $settingField => &$settingValue) { | |
202 | self::validateSetting($settingValue, $fields['values'][$settingField]); | |
203 | } | |
204 | ||
7583c3f3 | 205 | foreach ($domains as $domainID) { |
23bb9c85 TO |
206 | Civi::settings($domainID)->add($fieldsToSet); |
207 | $result[$domainID] = $fieldsToSet; | |
6a488035 TO |
208 | } |
209 | ||
210 | return $result; | |
211 | } | |
212 | ||
213 | /** | |
100fef9d | 214 | * Gets metadata about the settings fields (from getfields) based on the fields being passed in |
6a488035 TO |
215 | * |
216 | * This function filters on the fields like 'version' & 'debug' that are not settings | |
77b97be7 | 217 | * |
6a0b768e TO |
218 | * @param array $params |
219 | * Parameters as passed into API. | |
220 | * @param array $fields | |
221 | * Empty array to be populated with fields metadata. | |
6a488035 TO |
222 | * @param bool $createMode |
223 | * | |
77b97be7 | 224 | * @throws api_Exception |
a6c01b45 CW |
225 | * @return array |
226 | * name => value array of the fields to be set (with extraneous removed) | |
6a488035 | 227 | */ |
00be9182 | 228 | public static function validateSettingsInput($params, &$fields, $createMode = TRUE) { |
6a488035 TO |
229 | $ignoredParams = array( |
230 | 'version', | |
231 | 'id', | |
232 | 'domain_id', | |
233 | 'debug', | |
234 | 'created_id', | |
235 | 'component_id', | |
236 | 'contact_id', | |
237 | 'filters', | |
238 | 'entity_id', | |
239 | 'entity_table', | |
240 | 'sequential', | |
241 | 'api.has_parent', | |
f704dce7 | 242 | 'IDS_request_uri', |
243 | 'IDS_user_agent', | |
244 | 'check_permissions', | |
80fbde47 | 245 | 'options', |
e56fd67f | 246 | 'prettyprint', |
2ed80443 FG |
247 | // CRM-18347: ignore params unintentionally passed by API explorer on WP |
248 | 'page', | |
249 | 'noheader', | |
250 | // CRM-18347: ignore params unintentionally passed by wp CLI tool | |
251 | '', | |
a3396545 FG |
252 | // CRM-19877: ignore params extraneously passed by Joomla |
253 | 'option', | |
254 | 'task', | |
6a488035 TO |
255 | ); |
256 | $settingParams = array_diff_key($params, array_fill_keys($ignoredParams, TRUE)); | |
257 | $getFieldsParams = array('version' => 3); | |
d3e86119 | 258 | if (count($settingParams) == 1) { |
6a488035 TO |
259 | // ie we are only setting one field - we'll pass it into getfields for efficiency |
260 | list($name) = array_keys($settingParams); | |
261 | $getFieldsParams['name'] = $name; | |
262 | } | |
353ffa53 | 263 | $fields = civicrm_api3('setting', 'getfields', $getFieldsParams); |
6a488035 TO |
264 | $invalidParams = (array_diff_key($settingParams, $fields['values'])); |
265 | if (!empty($invalidParams)) { | |
e56fd67f | 266 | throw new api_Exception(implode(',', array_keys($invalidParams)) . " not valid settings"); |
6a488035 TO |
267 | } |
268 | if (!empty($settingParams)) { | |
269 | $filteredFields = array_intersect_key($settingParams, $fields['values']); | |
270 | } | |
271 | else { | |
272 | // no filters so we are interested in all for get mode. In create mode this means nothing to set | |
273 | $filteredFields = $createMode ? array() : $fields['values']; | |
274 | } | |
275 | return $filteredFields; | |
276 | } | |
277 | ||
278 | /** | |
3bdf1f3a | 279 | * Validate & convert settings input. |
6a488035 | 280 | * |
3bdf1f3a | 281 | * @param mixed $value |
282 | * value of the setting to be set | |
283 | * @param array $fieldSpec | |
284 | * Metadata for given field (drawn from the xml) | |
285 | * | |
286 | * @return bool | |
287 | * @throws \api_Exception | |
6a488035 | 288 | */ |
00be9182 | 289 | public static function validateSetting(&$value, $fieldSpec) { |
9b873358 | 290 | if ($fieldSpec['type'] == 'String' && is_array($value)) { |
353ffa53 | 291 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR; |
6a488035 TO |
292 | } |
293 | if (empty($fieldSpec['validate_callback'])) { | |
607cb45e | 294 | return TRUE; |
6a488035 TO |
295 | } |
296 | else { | |
c8074a93 TO |
297 | $cb = Civi\Core\Resolver::singleton()->get($fieldSpec['validate_callback']); |
298 | if (!call_user_func_array($cb, array(&$value, $fieldSpec))) { | |
ee7b49c4 | 299 | throw new api_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}"); |
6a488035 TO |
300 | } |
301 | } | |
302 | } | |
303 | ||
304 | /** | |
ea3ddccf | 305 | * Validate & convert settings input - translate True False to 0 or 1. |
6a488035 | 306 | * |
ea3ddccf | 307 | * @param mixed $value value of the setting to be set |
308 | * @param array $fieldSpec Metadata for given field (drawn from the xml) | |
309 | * | |
310 | * @return bool | |
311 | * @throws \api_Exception | |
6a488035 | 312 | */ |
00be9182 | 313 | public static function validateBoolSetting(&$value, $fieldSpec) { |
6a488035 | 314 | if (!CRM_Utils_Rule::boolean($value)) { |
ee7b49c4 | 315 | throw new api_Exception("Boolean value required for {$fieldSpec['name']}"); |
6a488035 TO |
316 | } |
317 | if (!$value) { | |
318 | $value = 0; | |
319 | } | |
320 | else { | |
321 | $value = 1; | |
322 | } | |
323 | return TRUE; | |
324 | } | |
325 | ||
6a488035 TO |
326 | /** |
327 | * This provides information about the setting - similar to the fields concept for DAO information. | |
328 | * As the setting is serialized code creating validation setting input needs to know the data type | |
329 | * This also helps move information out of the form layer into the data layer where people can interact with | |
330 | * it via the API or other mechanisms. In order to keep this consistent it is important the form layer | |
331 | * also leverages it. | |
332 | * | |
333 | * Note that this function should never be called when using the runtime getvalue function. Caching works | |
334 | * around the expectation it will be called during setting administration | |
335 | * | |
336 | * Function is intended for configuration rather than runtime access to settings | |
337 | * | |
338 | * The following params will filter the result. If none are passed all settings will be returns | |
339 | * | |
6a0b768e TO |
340 | * @param int $componentID |
341 | * Id of relevant component. | |
2a6da8d7 | 342 | * @param array $filters |
c490a46a | 343 | * @param int $domainID |
2a6da8d7 EM |
344 | * @param null $profile |
345 | * | |
a6c01b45 CW |
346 | * @return array |
347 | * the following information as appropriate for each setting | |
5c766a0b TO |
348 | * - name |
349 | * - type | |
350 | * - default | |
351 | * - add (CiviCRM version added) | |
352 | * - is_domain | |
353 | * - is_contact | |
354 | * - description | |
355 | * - help_text | |
6a488035 | 356 | */ |
2da40d21 | 357 | public static function getSettingSpecification( |
607cb45e | 358 | $componentID = NULL, |
b597d0b1 | 359 | $filters = array(), |
607cb45e TO |
360 | $domainID = NULL, |
361 | $profile = NULL | |
b597d0b1 | 362 | ) { |
e1d39824 | 363 | return \Civi\Core\SettingsMetadata::getMetadata($filters, $domainID); |
6a488035 TO |
364 | } |
365 | ||
b5c2afd0 EM |
366 | /** |
367 | * @param $group | |
100fef9d | 368 | * @param string $name |
b5c2afd0 | 369 | * @param bool $system |
100fef9d | 370 | * @param int $userID |
b5c2afd0 EM |
371 | * @param bool $localize |
372 | * @param string $returnField | |
373 | * @param bool $returnNameANDLabels | |
374 | * @param null $condition | |
375 | * | |
376 | * @return array | |
377 | */ | |
2da40d21 | 378 | public static function valueOptions( |
f9f40af3 | 379 | $group, |
6a488035 | 380 | $name, |
242bd179 TO |
381 | $system = TRUE, |
382 | $userID = NULL, | |
383 | $localize = FALSE, | |
384 | $returnField = 'name', | |
6a488035 | 385 | $returnNameANDLabels = FALSE, |
242bd179 | 386 | $condition = NULL |
6a488035 TO |
387 | ) { |
388 | $optionValue = self::getItem($group, $name); | |
389 | ||
390 | $groupValues = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, $returnField); | |
391 | ||
392 | //enabled name => label require for new contact edit form, CRM-4605 | |
393 | if ($returnNameANDLabels) { | |
394 | $names = $labels = $nameAndLabels = array(); | |
395 | if ($returnField == 'name') { | |
396 | $names = $groupValues; | |
397 | $labels = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, 'label'); | |
398 | } | |
399 | else { | |
400 | $labels = $groupValues; | |
401 | $names = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, 'name'); | |
402 | } | |
403 | } | |
404 | ||
405 | $returnValues = array(); | |
406 | foreach ($groupValues as $gn => $gv) { | |
407 | $returnValues[$gv] = 0; | |
408 | } | |
409 | ||
410 | if ($optionValue && !empty($groupValues)) { | |
411 | $dbValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
412 | substr($optionValue, 1, -1) | |
413 | ); | |
414 | ||
415 | if (!empty($dbValues)) { | |
416 | foreach ($groupValues as $key => $val) { | |
417 | if (in_array($key, $dbValues)) { | |
418 | $returnValues[$val] = 1; | |
419 | if ($returnNameANDLabels) { | |
420 | $nameAndLabels[$names[$key]] = $labels[$key]; | |
421 | } | |
422 | } | |
423 | } | |
424 | } | |
425 | } | |
426 | return ($returnNameANDLabels) ? $nameAndLabels : $returnValues; | |
427 | } | |
428 | ||
b5c2afd0 | 429 | /** |
73152f09 | 430 | * @param $group (deprecated) |
100fef9d | 431 | * @param string $name |
b5c2afd0 EM |
432 | * @param $value |
433 | * @param bool $system | |
100fef9d | 434 | * @param int $userID |
b5c2afd0 EM |
435 | * @param string $keyField |
436 | */ | |
2da40d21 | 437 | public static function setValueOption( |
f9f40af3 | 438 | $group, |
6a488035 TO |
439 | $name, |
440 | $value, | |
242bd179 TO |
441 | $system = TRUE, |
442 | $userID = NULL, | |
6a488035 TO |
443 | $keyField = 'name' |
444 | ) { | |
445 | if (empty($value)) { | |
446 | $optionValue = NULL; | |
447 | } | |
448 | elseif (is_array($value)) { | |
449 | $groupValues = CRM_Core_OptionGroup::values($name, FALSE, FALSE, FALSE, NULL, $keyField); | |
450 | ||
451 | $cbValues = array(); | |
452 | foreach ($groupValues as $key => $val) { | |
a7488080 | 453 | if (!empty($value[$val])) { |
6a488035 TO |
454 | $cbValues[$key] = 1; |
455 | } | |
456 | } | |
457 | ||
458 | if (!empty($cbValues)) { | |
459 | $optionValue = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
460 | array_keys($cbValues) |
461 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
462 | } |
463 | else { | |
464 | $optionValue = NULL; | |
465 | } | |
466 | } | |
467 | else { | |
468 | $optionValue = $value; | |
469 | } | |
470 | ||
471 | self::setItem($optionValue, $group, $name); | |
472 | } | |
473 | ||
2c7039ef | 474 | /** |
100fef9d | 475 | * Civicrm_setting didn't exist before 4.1.alpha1 and this function helps taking decisions during upgrade |
2c7039ef | 476 | * |
5c766a0b | 477 | * @return bool |
2c7039ef | 478 | */ |
00be9182 | 479 | public static function isUpgradeFromPreFourOneAlpha1() { |
2c7039ef DS |
480 | if (CRM_Core_Config::isUpgradeMode()) { |
481 | $currentVer = CRM_Core_BAO_Domain::version(); | |
482 | if (version_compare($currentVer, '4.1.alpha1') < 0) { | |
483 | return TRUE; | |
484 | } | |
485 | } | |
486 | return FALSE; | |
487 | } | |
96025800 | 488 | |
2c7039ef | 489 | } |