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