Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * BAO object for civicrm_setting table. This table is used to store civicrm settings that are not used | |
38 | * very frequently (i.e. not on every page load) | |
39 | * | |
40 | * The group column is used for grouping together all settings that logically belong to the same set. | |
41 | * Thus all settings in the same group are retrieved with one DB call and then cached for future needs. | |
42 | * | |
43 | */ | |
44 | class CRM_Core_BAO_Setting extends CRM_Core_DAO_Setting { | |
45 | ||
46 | /** | |
fe482240 | 47 | * Various predefined settings that have been migrated to the setting table. |
6a488035 | 48 | */ |
7da04cde | 49 | const |
6a488035 TO |
50 | ADDRESS_STANDARDIZATION_PREFERENCES_NAME = 'Address Standardization Preferences', |
51 | CAMPAIGN_PREFERENCES_NAME = 'Campaign Preferences', | |
5d0140d9 | 52 | DEVELOPER_PREFERENCES_NAME = 'Developer Preferences', |
6a488035 TO |
53 | DIRECTORY_PREFERENCES_NAME = 'Directory Preferences', |
54 | EVENT_PREFERENCES_NAME = 'Event Preferences', | |
55 | MAILING_PREFERENCES_NAME = 'Mailing Preferences', | |
56 | CONTRIBUTE_PREFERENCES_NAME = 'Contribute Preferences', | |
57 | MEMBER_PREFERENCES_NAME = 'Member Preferences', | |
58 | MULTISITE_PREFERENCES_NAME = 'Multi Site Preferences', | |
59 | PERSONAL_PREFERENCES_NAME = 'Personal Preferences', | |
60 | SYSTEM_PREFERENCES_NAME = 'CiviCRM Preferences', | |
61 | URL_PREFERENCES_NAME = 'URL Preferences', | |
62 | LOCALIZATION_PREFERENCES_NAME = 'Localization Preferences', | |
63 | SEARCH_PREFERENCES_NAME = 'Search Preferences'; | |
64 | static $_cache = NULL; | |
65 | ||
66 | /** | |
67 | * Checks whether an item is present in the in-memory cache table | |
68 | * | |
6a0b768e TO |
69 | * @param string $group |
70 | * (required) The group name of the item. | |
71 | * @param string $name | |
72 | * (required) The name of the setting. | |
73 | * @param int $componentID | |
74 | * The optional component ID (so components can share the same name space). | |
75 | * @param int $contactID | |
76 | * If set, this is a contactID specific setting, else its a global setting. | |
77b97be7 EM |
77 | * @param bool|int $load if true, load from local cache (typically memcache) |
78 | * | |
100fef9d | 79 | * @param int $domainID |
77b97be7 | 80 | * @param bool $force |
6a488035 | 81 | * |
5c766a0b | 82 | * @return bool |
a6c01b45 | 83 | * true if item is already in cache |
6a488035 | 84 | */ |
2da40d21 | 85 | public static function inCache( |
eb40b5a4 | 86 | $group, |
6a488035 TO |
87 | $name, |
88 | $componentID = NULL, | |
242bd179 TO |
89 | $contactID = NULL, |
90 | $load = FALSE, | |
eb40b5a4 | 91 | $domainID = NULL, |
92 | $force = FALSE | |
6a488035 TO |
93 | ) { |
94 | if (!isset(self::$_cache)) { | |
95 | self::$_cache = array(); | |
96 | } | |
97 | ||
98 | $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}"; | |
eb40b5a4 | 99 | |
56cb3188 DL |
100 | if ( |
101 | $load && | |
eb40b5a4 | 102 | ($force || !isset(self::$_cache[$cacheKey])) |
6a488035 | 103 | ) { |
eb40b5a4 | 104 | |
6a488035 TO |
105 | // check in civi cache if present (typically memcache) |
106 | $globalCache = CRM_Utils_Cache::singleton(); | |
107 | $result = $globalCache->get($cacheKey); | |
108 | if ($result) { | |
eb40b5a4 | 109 | |
6a488035 TO |
110 | self::$_cache[$cacheKey] = $result; |
111 | } | |
112 | } | |
113 | ||
114 | return isset(self::$_cache[$cacheKey]) ? $cacheKey : NULL; | |
115 | } | |
353ffa53 | 116 | |
eb40b5a4 | 117 | /** |
fe482240 | 118 | * Allow key o be cleared. |
353ffa53 TO |
119 | * @param string $cacheKey |
120 | */ | |
9b873358 | 121 | public static function flushCache($cacheKey) { |
eb40b5a4 | 122 | unset(self::$_cache[$cacheKey]); |
123 | $globalCache = CRM_Utils_Cache::singleton(); | |
124 | $globalCache->delete($cacheKey); | |
125 | } | |
6a488035 | 126 | |
b5c2afd0 EM |
127 | /** |
128 | * @param $values | |
129 | * @param $group | |
100fef9d CW |
130 | * @param int $componentID |
131 | * @param int $contactID | |
132 | * @param int $domainID | |
b5c2afd0 EM |
133 | * |
134 | * @return string | |
135 | */ | |
2da40d21 | 136 | public static function setCache( |
f9f40af3 | 137 | $values, |
6a488035 TO |
138 | $group, |
139 | $componentID = NULL, | |
140 | $contactID = NULL, | |
141 | $domainID = NULL | |
142 | ) { | |
143 | if (!isset(self::$_cache)) { | |
144 | self::$_cache = array(); | |
145 | } | |
146 | ||
147 | $cacheKey = "CRM_Setting_{$group}_{$componentID}_{$contactID}_{$domainID}"; | |
148 | ||
149 | self::$_cache[$cacheKey] = $values; | |
150 | ||
151 | $globalCache = CRM_Utils_Cache::singleton(); | |
152 | $result = $globalCache->set($cacheKey, $values); | |
153 | ||
154 | return $cacheKey; | |
155 | } | |
156 | ||
b5c2afd0 EM |
157 | /** |
158 | * @param $group | |
159 | * @param null $name | |
100fef9d CW |
160 | * @param int $componentID |
161 | * @param int $contactID | |
162 | * @param int $domainID | |
b5c2afd0 EM |
163 | * |
164 | * @return CRM_Core_DAO_Domain|CRM_Core_DAO_Setting | |
165 | */ | |
2da40d21 | 166 | public static function dao( |
f9f40af3 | 167 | $group, |
242bd179 | 168 | $name = NULL, |
6a488035 | 169 | $componentID = NULL, |
242bd179 | 170 | $contactID = NULL, |
6a488035 TO |
171 | $domainID = NULL |
172 | ) { | |
2c7039ef | 173 | if (self::isUpgradeFromPreFourOneAlpha1()) { |
03d8f8f3 | 174 | // civicrm_setting table is not going to be present. For now we'll just |
2c7039ef DS |
175 | // return a dummy object |
176 | $dao = new CRM_Core_DAO_Domain(); | |
177 | $dao->id = -1; // so ->find() doesn't fetch any data later on | |
178 | return $dao; | |
179 | } | |
6a488035 TO |
180 | $dao = new CRM_Core_DAO_Setting(); |
181 | ||
242bd179 TO |
182 | $dao->group_name = $group; |
183 | $dao->name = $name; | |
6a488035 TO |
184 | $dao->component_id = $componentID; |
185 | if (empty($domainID)) { | |
353ffa53 | 186 | $dao->domain_id = CRM_Core_Config::domainID(); |
6a488035 TO |
187 | } |
188 | else { | |
189 | $dao->domain_id = $domainID; | |
190 | } | |
191 | ||
192 | if ($contactID) { | |
193 | $dao->contact_id = $contactID; | |
194 | $dao->is_domain = 0; | |
195 | } | |
196 | else { | |
197 | $dao->is_domain = 1; | |
198 | } | |
199 | ||
200 | return $dao; | |
201 | } | |
202 | ||
203 | /** | |
fe482240 | 204 | * Retrieve the value of a setting from the DB table. |
6a488035 | 205 | * |
6a0b768e TO |
206 | * @param string $group |
207 | * (required) The group name of the item. | |
208 | * @param string $name | |
209 | * (required) The name under which this item is stored. | |
210 | * @param int $componentID | |
211 | * The optional component ID (so componenets can share the same name space). | |
212 | * @param string $defaultValue | |
213 | * The default value to return for this setting if not present in DB. | |
214 | * @param int $contactID | |
215 | * If set, this is a contactID specific setting, else its a global setting. | |
da6b46f4 | 216 | * |
100fef9d | 217 | * @param int $domainID |
6a488035 | 218 | * |
72b3a70c CW |
219 | * @return mixed |
220 | * The data if present in the setting table, else null | |
6a488035 | 221 | */ |
2da40d21 | 222 | public static function getItem( |
56cb3188 | 223 | $group, |
242bd179 TO |
224 | $name = NULL, |
225 | $componentID = NULL, | |
6a488035 | 226 | $defaultValue = NULL, |
242bd179 TO |
227 | $contactID = NULL, |
228 | $domainID = NULL | |
6a488035 TO |
229 | ) { |
230 | ||
231 | if (NULL !== ($override = self::getOverride($group, $name, NULL))) { | |
232 | return $override; | |
233 | } | |
234 | ||
235 | if (empty($domainID)) { | |
236 | $domainID = CRM_Core_Config::domainID(); | |
237 | } | |
238 | $cacheKey = self::inCache($group, $name, $componentID, $contactID, TRUE, $domainID); | |
239 | if (!$cacheKey) { | |
240 | $dao = self::dao($group, NULL, $componentID, $contactID, $domainID); | |
241 | $dao->find(); | |
242 | ||
243 | $values = array(); | |
244 | while ($dao->fetch()) { | |
245 | if (NULL !== ($override = self::getOverride($group, $dao->name, NULL))) { | |
246 | $values[$dao->name] = $override; | |
247 | } | |
248 | elseif ($dao->value) { | |
249 | $values[$dao->name] = unserialize($dao->value); | |
250 | } | |
251 | else { | |
252 | $values[$dao->name] = NULL; | |
253 | } | |
254 | } | |
255 | $dao->free(); | |
256 | ||
257 | $cacheKey = self::setCache($values, $group, $componentID, $contactID, $domainID); | |
258 | } | |
6a488035 TO |
259 | return $name ? CRM_Utils_Array::value($name, self::$_cache[$cacheKey], $defaultValue) : self::$_cache[$cacheKey]; |
260 | } | |
261 | ||
262 | /** | |
35823763 | 263 | * Store multiple items in the setting table. |
6a488035 | 264 | * |
6a0b768e TO |
265 | * @param array $params |
266 | * (required) An api formatted array of keys and values. | |
35823763 | 267 | * @param array $domains Array of domains to get settings for. Default is the current domain |
dd244018 EM |
268 | * @param $settingsToReturn |
269 | * | |
35823763 | 270 | * @return array |
6a488035 | 271 | */ |
00be9182 | 272 | public static function getItems(&$params, $domains = NULL, $settingsToReturn) { |
0e04f44e | 273 | $originalDomain = CRM_Core_Config::domainID(); |
6a488035 | 274 | if (empty($domains)) { |
0e04f44e | 275 | $domains[] = $originalDomain; |
6a488035 TO |
276 | } |
277 | if (!empty($settingsToReturn) && !is_array($settingsToReturn)) { | |
278 | $settingsToReturn = array($settingsToReturn); | |
279 | } | |
0e04f44e | 280 | $reloadConfig = FALSE; |
281 | ||
6a488035 TO |
282 | $fields = $result = array(); |
283 | $fieldsToGet = self::validateSettingsInput(array_flip($settingsToReturn), $fields, FALSE); | |
7583c3f3 | 284 | foreach ($domains as $domainID) { |
9b873358 | 285 | if ($domainID != CRM_Core_Config::domainID()) { |
0e04f44e | 286 | $reloadConfig = TRUE; |
7583c3f3 | 287 | CRM_Core_BAO_Domain::setDomain($domainID); |
0e04f44e | 288 | } |
289 | $config = CRM_Core_Config::singleton($reloadConfig, $reloadConfig); | |
7583c3f3 | 290 | $result[$domainID] = array(); |
6a488035 | 291 | foreach ($fieldsToGet as $name => $value) { |
9b873358 | 292 | if (!empty($fields['values'][$name]['prefetch'])) { |
22e263ad | 293 | if (isset($params['filters']) && isset($params['filters']['prefetch']) |
353ffa53 TO |
294 | && $params['filters']['prefetch'] == 0 |
295 | ) { | |
eb40b5a4 | 296 | // we are filtering out the prefetches from the return array |
297 | // so we will skip | |
298 | continue; | |
299 | } | |
353ffa53 | 300 | $configKey = CRM_Utils_Array::value('config_key', $fields['values'][$name], $name); |
9b873358 | 301 | if (isset($config->$configKey)) { |
6a488035 TO |
302 | $setting = $config->$configKey; |
303 | } | |
304 | } | |
305 | else { | |
6c552737 TO |
306 | $setting = CRM_Core_BAO_Setting::getItem( |
307 | $fields['values'][$name]['group_name'], | |
308 | $name, | |
309 | CRM_Utils_Array::value('component_id', $params), | |
310 | NULL, | |
311 | CRM_Utils_Array::value('contact_id', $params), | |
312 | $domainID | |
313 | ); | |
6a488035 TO |
314 | } |
315 | if (!is_null($setting)) { | |
316 | // we won't return if not set - helps in return all scenario - otherwise we can't indentify the missing ones | |
317 | // e.g for revert of fill actions | |
7583c3f3 | 318 | $result[$domainID][$name] = $setting; |
6a488035 TO |
319 | } |
320 | } | |
7583c3f3 | 321 | CRM_Core_BAO_Domain::resetDomain(); |
6a488035 TO |
322 | } |
323 | return $result; | |
324 | } | |
325 | ||
326 | /** | |
fe482240 | 327 | * Store an item in the setting table. |
6a488035 | 328 | * |
a57707d3 TO |
329 | * _setItem() is the common logic shared by setItem() and setItems(). |
330 | * | |
6a0b768e TO |
331 | * @param object $value |
332 | * (required) The value that will be serialized and stored. | |
333 | * @param string $group | |
334 | * (required) The group name of the item. | |
335 | * @param string $name | |
336 | * (required) The name of the setting. | |
337 | * @param int $componentID | |
338 | * The optional component ID (so componenets can share the same name space). | |
100fef9d | 339 | * @param int $contactID |
6a0b768e TO |
340 | * @param int $createdID |
341 | * An optional ID to assign the creator to. If not set, retrieved from session. | |
fd31fa4c | 342 | * |
100fef9d | 343 | * @param int $domainID |
6a488035 TO |
344 | * |
345 | * @return void | |
6a488035 | 346 | */ |
2da40d21 | 347 | public static function setItem( |
6a488035 TO |
348 | $value, |
349 | $group, | |
350 | $name, | |
351 | $componentID = NULL, | |
242bd179 TO |
352 | $contactID = NULL, |
353 | $createdID = NULL, | |
354 | $domainID = NULL | |
a57707d3 TO |
355 | ) { |
356 | $fields = array(); | |
357 | $fieldsToSet = self::validateSettingsInput(array($name => $value), $fields); | |
358 | //We haven't traditionally validated inputs to setItem, so this breaks things. | |
359 | //foreach ($fieldsToSet as $settingField => &$settingValue) { | |
360 | // self::validateSetting($settingValue, $fields['values'][$settingField]); | |
361 | //} | |
362 | ||
363 | return self::_setItem($fields['values'][$name], $value, $group, $name, $componentID, $contactID, $createdID, $domainID); | |
364 | } | |
365 | ||
366 | /** | |
fe482240 | 367 | * Store an item in a setting table. |
a57707d3 TO |
368 | * |
369 | * _setItem() is the common logic shared by setItem() and setItems(). | |
370 | * | |
6a0b768e TO |
371 | * @param array $metadata |
372 | * Metadata describing this field. | |
a57707d3 TO |
373 | * @param $value |
374 | * @param $group | |
100fef9d CW |
375 | * @param string $name |
376 | * @param int $componentID | |
377 | * @param int $contactID | |
378 | * @param int $createdID | |
379 | * @param int $domainID | |
a57707d3 | 380 | */ |
2da40d21 | 381 | public static function _setItem( |
a57707d3 TO |
382 | $metadata, |
383 | $value, | |
384 | $group, | |
385 | $name, | |
386 | $componentID = NULL, | |
242bd179 TO |
387 | $contactID = NULL, |
388 | $createdID = NULL, | |
389 | $domainID = NULL | |
6a488035 | 390 | ) { |
6a488035 TO |
391 | if (empty($domainID)) { |
392 | $domainID = CRM_Core_Config::domainID(); | |
393 | } | |
394 | ||
395 | $dao = self::dao($group, $name, $componentID, $contactID, $domainID); | |
396 | $dao->find(TRUE); | |
397 | ||
a57707d3 TO |
398 | if (isset($metadata['on_change'])) { |
399 | foreach ($metadata['on_change'] as $callback) { | |
c8074a93 TO |
400 | call_user_func( |
401 | Civi\Core\Resolver::singleton()->get($callback), | |
402 | unserialize($dao->value), | |
403 | $value, | |
404 | $metadata | |
405 | ); | |
a57707d3 TO |
406 | } |
407 | } | |
408 | ||
6a488035 TO |
409 | if (CRM_Utils_System::isNull($value)) { |
410 | $dao->value = 'null'; | |
411 | } | |
412 | else { | |
413 | $dao->value = serialize($value); | |
414 | } | |
415 | ||
416 | $dao->created_date = date('Ymdhis'); | |
417 | ||
418 | if ($createdID) { | |
419 | $dao->created_id = $createdID; | |
420 | } | |
421 | else { | |
422 | $session = CRM_Core_Session::singleton(); | |
423 | $createdID = $session->get('userID'); | |
424 | ||
425 | if ($createdID) { | |
426 | // ensure that this is a valid contact id (for session inconsistency rules) | |
427 | $cid = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
428 | $createdID, | |
429 | 'id', | |
430 | 'id' | |
431 | ); | |
432 | if ($cid) { | |
433 | $dao->created_id = $session->get('userID'); | |
434 | } | |
435 | } | |
436 | } | |
437 | ||
438 | $dao->save(); | |
439 | $dao->free(); | |
440 | ||
441 | // also save in cache if needed | |
442 | $cacheKey = self::inCache($group, $name, $componentID, $contactID, FALSE, $domainID); | |
443 | if ($cacheKey) { | |
444 | self::$_cache[$cacheKey][$name] = $value; | |
445 | } | |
446 | } | |
447 | ||
448 | /** | |
449 | * Store multiple items in the setting table. Note that this will also store config keys | |
450 | * the storage is determined by the metdata and is affected by | |
451 | * 'name' setting's name | |
452 | * 'prefetch' = store in config | |
453 | * 'config_only' = don't store in settings | |
454 | * 'config_key' = the config key is different to the settings key - e.g. debug where there was a conflict | |
455 | * 'legacy_key' = rename from config or setting with this name | |
456 | * | |
a57707d3 TO |
457 | * _setItem() is the common logic shared by setItem() and setItems(). |
458 | * | |
6a0b768e TO |
459 | * @param array $params |
460 | * (required) An api formatted array of keys and values. | |
2a6da8d7 EM |
461 | * @param null $domains |
462 | * | |
463 | * @throws api_Exception | |
6a488035 | 464 | * @domains array an array of domains to get settings for. Default is the current domain |
3d0d359e | 465 | * @return array |
6a488035 | 466 | */ |
00be9182 | 467 | public static function setItems(&$params, $domains = NULL) { |
7583c3f3 | 468 | $originalDomain = CRM_Core_Config::domainID(); |
6a488035 | 469 | if (empty($domains)) { |
7583c3f3 | 470 | $domains[] = $originalDomain; |
6a488035 | 471 | } |
7583c3f3 | 472 | $reloadConfig = FALSE; |
6a488035 TO |
473 | $fields = $config_keys = array(); |
474 | $fieldsToSet = self::validateSettingsInput($params, $fields); | |
475 | ||
476 | foreach ($fieldsToSet as $settingField => &$settingValue) { | |
477 | self::validateSetting($settingValue, $fields['values'][$settingField]); | |
478 | } | |
479 | ||
7583c3f3 | 480 | foreach ($domains as $domainID) { |
9b873358 | 481 | if ($domainID != CRM_Core_Config::domainID()) { |
7583c3f3 | 482 | $reloadConfig = TRUE; |
483 | CRM_Core_BAO_Domain::setDomain($domainID); | |
484 | } | |
485 | $result[$domainID] = array(); | |
6a488035 | 486 | foreach ($fieldsToSet as $name => $value) { |
9b873358 | 487 | if (empty($fields['values'][$name]['config_only'])) { |
a57707d3 TO |
488 | CRM_Core_BAO_Setting::_setItem( |
489 | $fields['values'][$name], | |
56cb3188 DL |
490 | $value, |
491 | $fields['values'][$name]['group_name'], | |
492 | $name, | |
493 | CRM_Utils_Array::value('component_id', $params), | |
494 | CRM_Utils_Array::value('contact_id', $params), | |
495 | CRM_Utils_Array::value('created_id', $params), | |
496 | $domainID | |
497 | ); | |
6a488035 | 498 | } |
9b873358 TO |
499 | if (!empty($fields['values'][$name]['prefetch'])) { |
500 | if (!empty($fields['values'][$name]['config_key'])) { | |
6a488035 TO |
501 | $name = $fields['values'][$name]['config_key']; |
502 | } | |
503 | $config_keys[$name] = $value; | |
504 | } | |
7583c3f3 | 505 | $result[$domainID][$name] = $value; |
506 | } | |
9b873358 | 507 | if ($reloadConfig) { |
7583c3f3 | 508 | CRM_Core_Config::singleton($reloadConfig, $reloadConfig); |
509 | } | |
510 | ||
9b873358 | 511 | if (!empty($config_keys)) { |
7583c3f3 | 512 | CRM_Core_BAO_ConfigSetting::create($config_keys); |
513 | } | |
9b873358 | 514 | if ($reloadConfig) { |
7583c3f3 | 515 | CRM_Core_BAO_Domain::resetDomain(); |
6a488035 | 516 | } |
6a488035 TO |
517 | } |
518 | ||
519 | return $result; | |
520 | } | |
521 | ||
522 | /** | |
100fef9d | 523 | * Gets metadata about the settings fields (from getfields) based on the fields being passed in |
6a488035 TO |
524 | * |
525 | * This function filters on the fields like 'version' & 'debug' that are not settings | |
77b97be7 | 526 | * |
6a0b768e TO |
527 | * @param array $params |
528 | * Parameters as passed into API. | |
529 | * @param array $fields | |
530 | * Empty array to be populated with fields metadata. | |
6a488035 TO |
531 | * @param bool $createMode |
532 | * | |
77b97be7 | 533 | * @throws api_Exception |
a6c01b45 CW |
534 | * @return array |
535 | * name => value array of the fields to be set (with extraneous removed) | |
6a488035 | 536 | */ |
00be9182 | 537 | public static function validateSettingsInput($params, &$fields, $createMode = TRUE) { |
6a488035 TO |
538 | $group = CRM_Utils_Array::value('group', $params); |
539 | ||
540 | $ignoredParams = array( | |
541 | 'version', | |
542 | 'id', | |
543 | 'domain_id', | |
544 | 'debug', | |
545 | 'created_id', | |
546 | 'component_id', | |
547 | 'contact_id', | |
548 | 'filters', | |
549 | 'entity_id', | |
550 | 'entity_table', | |
551 | 'sequential', | |
552 | 'api.has_parent', | |
f704dce7 | 553 | 'IDS_request_uri', |
554 | 'IDS_user_agent', | |
555 | 'check_permissions', | |
80fbde47 | 556 | 'options', |
e56fd67f | 557 | 'prettyprint', |
6a488035 TO |
558 | ); |
559 | $settingParams = array_diff_key($params, array_fill_keys($ignoredParams, TRUE)); | |
560 | $getFieldsParams = array('version' => 3); | |
d3e86119 | 561 | if (count($settingParams) == 1) { |
6a488035 TO |
562 | // ie we are only setting one field - we'll pass it into getfields for efficiency |
563 | list($name) = array_keys($settingParams); | |
564 | $getFieldsParams['name'] = $name; | |
565 | } | |
353ffa53 | 566 | $fields = civicrm_api3('setting', 'getfields', $getFieldsParams); |
6a488035 TO |
567 | $invalidParams = (array_diff_key($settingParams, $fields['values'])); |
568 | if (!empty($invalidParams)) { | |
e56fd67f | 569 | throw new api_Exception(implode(',', array_keys($invalidParams)) . " not valid settings"); |
6a488035 TO |
570 | } |
571 | if (!empty($settingParams)) { | |
572 | $filteredFields = array_intersect_key($settingParams, $fields['values']); | |
573 | } | |
574 | else { | |
575 | // no filters so we are interested in all for get mode. In create mode this means nothing to set | |
576 | $filteredFields = $createMode ? array() : $fields['values']; | |
577 | } | |
578 | return $filteredFields; | |
579 | } | |
580 | ||
581 | /** | |
582 | * Validate & convert settings input | |
583 | * | |
584 | * @value mixed value of the setting to be set | |
585 | * @fieldSpec array Metadata for given field (drawn from the xml) | |
586 | */ | |
00be9182 | 587 | public static function validateSetting(&$value, $fieldSpec) { |
9b873358 | 588 | if ($fieldSpec['type'] == 'String' && is_array($value)) { |
353ffa53 | 589 | $value = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $value) . CRM_Core_DAO::VALUE_SEPARATOR; |
6a488035 TO |
590 | } |
591 | if (empty($fieldSpec['validate_callback'])) { | |
607cb45e | 592 | return TRUE; |
6a488035 TO |
593 | } |
594 | else { | |
c8074a93 TO |
595 | $cb = Civi\Core\Resolver::singleton()->get($fieldSpec['validate_callback']); |
596 | if (!call_user_func_array($cb, array(&$value, $fieldSpec))) { | |
ee7b49c4 | 597 | throw new api_Exception("validation failed for {$fieldSpec['name']} = $value based on callback {$fieldSpec['validate_callback']}"); |
6a488035 TO |
598 | } |
599 | } | |
600 | } | |
601 | ||
602 | /** | |
603 | * Validate & convert settings input - translate True False to 0 or 1 | |
604 | * | |
605 | * @value mixed value of the setting to be set | |
606 | * @fieldSpec array Metadata for given field (drawn from the xml) | |
607 | */ | |
00be9182 | 608 | public static function validateBoolSetting(&$value, $fieldSpec) { |
6a488035 | 609 | if (!CRM_Utils_Rule::boolean($value)) { |
ee7b49c4 | 610 | throw new api_Exception("Boolean value required for {$fieldSpec['name']}"); |
6a488035 TO |
611 | } |
612 | if (!$value) { | |
613 | $value = 0; | |
614 | } | |
615 | else { | |
616 | $value = 1; | |
617 | } | |
618 | return TRUE; | |
619 | } | |
620 | ||
6a488035 TO |
621 | /** |
622 | * This provides information about the setting - similar to the fields concept for DAO information. | |
623 | * As the setting is serialized code creating validation setting input needs to know the data type | |
624 | * This also helps move information out of the form layer into the data layer where people can interact with | |
625 | * it via the API or other mechanisms. In order to keep this consistent it is important the form layer | |
626 | * also leverages it. | |
627 | * | |
628 | * Note that this function should never be called when using the runtime getvalue function. Caching works | |
629 | * around the expectation it will be called during setting administration | |
630 | * | |
631 | * Function is intended for configuration rather than runtime access to settings | |
632 | * | |
633 | * The following params will filter the result. If none are passed all settings will be returns | |
634 | * | |
6a0b768e TO |
635 | * @param int $componentID |
636 | * Id of relevant component. | |
2a6da8d7 | 637 | * @param array $filters |
c490a46a | 638 | * @param int $domainID |
2a6da8d7 EM |
639 | * @param null $profile |
640 | * | |
a6c01b45 CW |
641 | * @return array |
642 | * the following information as appropriate for each setting | |
5c766a0b TO |
643 | * - name |
644 | * - type | |
645 | * - default | |
646 | * - add (CiviCRM version added) | |
647 | * - is_domain | |
648 | * - is_contact | |
649 | * - description | |
650 | * - help_text | |
6a488035 | 651 | */ |
2da40d21 | 652 | public static function getSettingSpecification( |
607cb45e | 653 | $componentID = NULL, |
b597d0b1 | 654 | $filters = array(), |
607cb45e TO |
655 | $domainID = NULL, |
656 | $profile = NULL | |
b597d0b1 | 657 | ) { |
6a488035 TO |
658 | $cacheString = 'settingsMetadata_' . $domainID . '_' . $profile; |
659 | foreach ($filters as $filterField => $filterString) { | |
660 | $cacheString .= "_{$filterField}_{$filterString}"; | |
661 | } | |
662 | $cached = 1; | |
ee7b49c4 E |
663 | // the caching into 'All' seems to be a duplicate of caching to |
664 | // settingsMetadata__ - I think the reason was to cache all settings as defined & then those altered by a hook | |
6a488035 TO |
665 | $settingsMetadata = CRM_Core_BAO_Cache::getItem('CiviCRM setting Specs', $cacheString, $componentID); |
666 | if ($settingsMetadata === NULL) { | |
667 | $settingsMetadata = CRM_Core_BAO_Cache::getItem('CiviCRM setting Spec', 'All', $componentID); | |
668 | if (empty($settingsMetadata)) { | |
6a488035 | 669 | global $civicrm_root; |
86bfa4f6 | 670 | $metaDataFolders = array($civicrm_root . '/settings'); |
6a488035 | 671 | CRM_Utils_Hook::alterSettingsFolders($metaDataFolders); |
3b998fcc | 672 | $settingsMetadata = self::loadSettingsMetaDataFolders($metaDataFolders); |
353ffa53 | 673 | CRM_Core_BAO_Cache::setItem($settingsMetadata, 'CiviCRM setting Spec', 'All', $componentID); |
6a488035 TO |
674 | } |
675 | $cached = 0; | |
676 | } | |
677 | ||
03d8f8f3 | 678 | CRM_Utils_Hook::alterSettingsMetaData($settingsMetadata, $domainID, $profile); |
6a488035 | 679 | self::_filterSettingsSpecification($filters, $settingsMetadata); |
03d8f8f3 DL |
680 | |
681 | if (!$cached) { | |
682 | // this is a bit 'heavy' if you are using hooks but this function | |
683 | // is expected to only be called during setting administration | |
6a488035 | 684 | // it should not be called by 'getvalue' or 'getitem |
03d8f8f3 DL |
685 | CRM_Core_BAO_Cache::setItem( |
686 | $settingsMetadata, | |
687 | 'CiviCRM setting Specs', | |
688 | $cacheString, | |
689 | $componentID | |
690 | ); | |
6a488035 TO |
691 | } |
692 | return $settingsMetadata; | |
693 | ||
694 | } | |
695 | ||
3b998fcc | 696 | /** |
fe482240 | 697 | * Load the settings files defined in a series of folders. |
6a0b768e TO |
698 | * @param array $metaDataFolders |
699 | * List of folder paths. | |
3b998fcc TO |
700 | * @return array |
701 | */ | |
702 | public static function loadSettingsMetaDataFolders($metaDataFolders) { | |
703 | $settingsMetadata = array(); | |
704 | $loadedFolders = array(); | |
705 | foreach ($metaDataFolders as $metaDataFolder) { | |
706 | $realFolder = realpath($metaDataFolder); | |
707 | if (is_dir($realFolder) && !isset($loadedFolders[$realFolder])) { | |
708 | $loadedFolders[$realFolder] = TRUE; | |
709 | $settingsMetadata = $settingsMetadata + self::loadSettingsMetaData($metaDataFolder); | |
710 | } | |
711 | } | |
712 | return $settingsMetadata; | |
713 | } | |
714 | ||
6a488035 | 715 | /** |
fe482240 | 716 | * Load up settings metadata from files. |
6a488035 | 717 | */ |
00be9182 | 718 | public static function loadSettingsMetadata($metaDataFolder) { |
6a488035 TO |
719 | $settingMetaData = array(); |
720 | $settingsFiles = CRM_Utils_File::findFiles($metaDataFolder, '*.setting.php'); | |
721 | foreach ($settingsFiles as $file) { | |
722 | $settings = include $file; | |
723 | $settingMetaData = array_merge($settingMetaData, $settings); | |
724 | } | |
353ffa53 | 725 | CRM_Core_BAO_Cache::setItem($settingMetaData, 'CiviCRM setting Spec', 'All'); |
6a488035 TO |
726 | return $settingMetaData; |
727 | } | |
728 | ||
729 | /** | |
730 | * Filter the settings metadata according to filters passed in. This is a convenience filter | |
731 | * and allows selective reverting / filling of settings | |
732 | * | |
6a0b768e TO |
733 | * @param array $filters |
734 | * Filters to match against data. | |
735 | * @param array $settingSpec | |
736 | * Metadata to filter. | |
6a488035 | 737 | */ |
00be9182 | 738 | public static function _filterSettingsSpecification($filters, &$settingSpec) { |
6a488035 TO |
739 | if (empty($filters)) { |
740 | return; | |
741 | } | |
4c9b6178 | 742 | elseif (array_keys($filters) == array('name')) { |
9caace17 | 743 | $settingSpec = array($filters['name'] => CRM_Utils_Array::value($filters['name'], $settingSpec, '')); |
6a488035 TO |
744 | return; |
745 | } | |
746 | else { | |
747 | foreach ($settingSpec as $field => $fieldValues) { | |
748 | if (array_intersect_assoc($fieldValues, $filters) != $filters) { | |
749 | unset($settingSpec[$field]); | |
750 | } | |
751 | } | |
752 | return; | |
753 | } | |
754 | } | |
755 | ||
756 | /** | |
0880a9d0 | 757 | * Look for any missing settings and convert them from config or load default as appropriate. |
6a488035 TO |
758 | * This should be run from GenCode & also from upgrades to add any new defaults. |
759 | * | |
760 | * Multisites have often been overlooked in upgrade scripts so can be expected to be missing | |
761 | * a number of settings | |
762 | */ | |
00be9182 | 763 | public static function updateSettingsFromMetaData() { |
6a488035 TO |
764 | $apiParams = array( |
765 | 'version' => 3, | |
766 | 'domain_id' => 'all', | |
eb40b5a4 | 767 | 'filters' => array('prefetch' => 0), |
6a488035 TO |
768 | ); |
769 | $existing = civicrm_api('setting', 'get', $apiParams); | |
eb40b5a4 | 770 | |
6a488035 TO |
771 | if (!empty($existing['values'])) { |
772 | $allSettings = civicrm_api('setting', 'getfields', array('version' => 3)); | |
773 | foreach ($existing['values'] as $domainID => $domainSettings) { | |
774 | CRM_Core_BAO_Domain::setDomain($domainID); | |
775 | $missing = array_diff_key($allSettings['values'], $domainSettings); | |
776 | foreach ($missing as $name => $settings) { | |
777 | self::convertConfigToSetting($name, $domainID); | |
778 | } | |
779 | CRM_Core_BAO_Domain::resetDomain(); | |
780 | } | |
781 | } | |
782 | } | |
783 | ||
784 | /** | |
100fef9d | 785 | * Move an item from being in the config array to being stored as a setting |
6a488035 TO |
786 | * remove from config - as appropriate based on metadata |
787 | * | |
788 | * Note that where the key name is being changed the 'legacy_key' will give us the old name | |
789 | */ | |
00be9182 | 790 | public static function convertConfigToSetting($name, $domainID = NULL) { |
eb40b5a4 | 791 | // we have to force this here in case more than one domain is in play. |
792 | // whenever there is a possibility of more than one domain we must force it | |
6a488035 TO |
793 | $config = CRM_Core_Config::singleton(); |
794 | if (empty($domainID)) { | |
d3e86119 | 795 | $domainID = CRM_Core_Config::domainID(); |
6a488035 TO |
796 | } |
797 | $domain = new CRM_Core_DAO_Domain(); | |
798 | $domain->id = $domainID; | |
799 | $domain->find(TRUE); | |
800 | if ($domain->config_backend) { | |
801 | $values = unserialize($domain->config_backend); | |
0db6c3e1 TO |
802 | } |
803 | else { | |
6a488035 TO |
804 | $values = array(); |
805 | } | |
607cb45e | 806 | $spec = self::getSettingSpecification(NULL, array('name' => $name), $domainID); |
6a488035 TO |
807 | $configKey = CRM_Utils_Array::value('config_key', $spec[$name], CRM_Utils_Array::value('legacy_key', $spec[$name], $name)); |
808 | //if the key is set to config_only we don't need to do anything | |
9b873358 | 809 | if (empty($spec[$name]['config_only'])) { |
6a488035 TO |
810 | if (!empty($values[$configKey])) { |
811 | civicrm_api('setting', 'create', array('version' => 3, $name => $values[$configKey], 'domain_id' => $domainID)); | |
812 | } | |
813 | else { | |
814 | civicrm_api('setting', 'fill', array('version' => 3, 'name' => $name, 'domain_id' => $domainID)); | |
815 | } | |
816 | ||
817 | if (empty($spec[$name]['prefetch']) && !empty($values[$configKey])) { | |
818 | unset($values[$configKey]); | |
819 | $domain->config_backend = serialize($values); | |
820 | $domain->save(); | |
821 | unset($config->$configKey); | |
822 | } | |
823 | } | |
824 | } | |
825 | ||
b5c2afd0 EM |
826 | /** |
827 | * @param $group | |
100fef9d | 828 | * @param string $name |
b5c2afd0 | 829 | * @param bool $system |
100fef9d | 830 | * @param int $userID |
b5c2afd0 EM |
831 | * @param bool $localize |
832 | * @param string $returnField | |
833 | * @param bool $returnNameANDLabels | |
834 | * @param null $condition | |
835 | * | |
836 | * @return array | |
837 | */ | |
2da40d21 | 838 | public static function valueOptions( |
f9f40af3 | 839 | $group, |
6a488035 | 840 | $name, |
242bd179 TO |
841 | $system = TRUE, |
842 | $userID = NULL, | |
843 | $localize = FALSE, | |
844 | $returnField = 'name', | |
6a488035 | 845 | $returnNameANDLabels = FALSE, |
242bd179 | 846 | $condition = NULL |
6a488035 TO |
847 | ) { |
848 | $optionValue = self::getItem($group, $name); | |
849 | ||
850 | $groupValues = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, $returnField); | |
851 | ||
852 | //enabled name => label require for new contact edit form, CRM-4605 | |
853 | if ($returnNameANDLabels) { | |
854 | $names = $labels = $nameAndLabels = array(); | |
855 | if ($returnField == 'name') { | |
856 | $names = $groupValues; | |
857 | $labels = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, 'label'); | |
858 | } | |
859 | else { | |
860 | $labels = $groupValues; | |
861 | $names = CRM_Core_OptionGroup::values($name, FALSE, FALSE, $localize, $condition, 'name'); | |
862 | } | |
863 | } | |
864 | ||
865 | $returnValues = array(); | |
866 | foreach ($groupValues as $gn => $gv) { | |
867 | $returnValues[$gv] = 0; | |
868 | } | |
869 | ||
870 | if ($optionValue && !empty($groupValues)) { | |
871 | $dbValues = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
872 | substr($optionValue, 1, -1) | |
873 | ); | |
874 | ||
875 | if (!empty($dbValues)) { | |
876 | foreach ($groupValues as $key => $val) { | |
877 | if (in_array($key, $dbValues)) { | |
878 | $returnValues[$val] = 1; | |
879 | if ($returnNameANDLabels) { | |
880 | $nameAndLabels[$names[$key]] = $labels[$key]; | |
881 | } | |
882 | } | |
883 | } | |
884 | } | |
885 | } | |
886 | return ($returnNameANDLabels) ? $nameAndLabels : $returnValues; | |
887 | } | |
888 | ||
b5c2afd0 EM |
889 | /** |
890 | * @param $group | |
100fef9d | 891 | * @param string $name |
b5c2afd0 EM |
892 | * @param $value |
893 | * @param bool $system | |
100fef9d | 894 | * @param int $userID |
b5c2afd0 EM |
895 | * @param string $keyField |
896 | */ | |
2da40d21 | 897 | public static function setValueOption( |
f9f40af3 | 898 | $group, |
6a488035 TO |
899 | $name, |
900 | $value, | |
242bd179 TO |
901 | $system = TRUE, |
902 | $userID = NULL, | |
6a488035 TO |
903 | $keyField = 'name' |
904 | ) { | |
905 | if (empty($value)) { | |
906 | $optionValue = NULL; | |
907 | } | |
908 | elseif (is_array($value)) { | |
909 | $groupValues = CRM_Core_OptionGroup::values($name, FALSE, FALSE, FALSE, NULL, $keyField); | |
910 | ||
911 | $cbValues = array(); | |
912 | foreach ($groupValues as $key => $val) { | |
a7488080 | 913 | if (!empty($value[$val])) { |
6a488035 TO |
914 | $cbValues[$key] = 1; |
915 | } | |
916 | } | |
917 | ||
918 | if (!empty($cbValues)) { | |
919 | $optionValue = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, | |
353ffa53 TO |
920 | array_keys($cbValues) |
921 | ) . CRM_Core_DAO::VALUE_SEPARATOR; | |
6a488035 TO |
922 | } |
923 | else { | |
924 | $optionValue = NULL; | |
925 | } | |
926 | } | |
927 | else { | |
928 | $optionValue = $value; | |
929 | } | |
930 | ||
931 | self::setItem($optionValue, $group, $name); | |
932 | } | |
933 | ||
b5c2afd0 | 934 | /** |
c490a46a | 935 | * @param array $params |
100fef9d | 936 | * @param int $domainID |
b5c2afd0 | 937 | */ |
00be9182 | 938 | public static function fixAndStoreDirAndURL(&$params, $domainID = NULL) { |
2c7039ef DS |
939 | if (self::isUpgradeFromPreFourOneAlpha1()) { |
940 | return; | |
941 | } | |
942 | ||
6a488035 TO |
943 | if (empty($domainID)) { |
944 | $domainID = CRM_Core_Config::domainID(); | |
945 | } | |
946 | $sql = " | |
947 | SELECT name, group_name | |
948 | FROM civicrm_setting | |
949 | WHERE domain_id = %1 | |
950 | AND ( group_name = %2 | |
951 | OR group_name = %3 ) | |
952 | "; | |
953 | $sqlParams = array( | |
954 | 1 => array($domainID, 'Integer'), | |
955 | 2 => array(self::DIRECTORY_PREFERENCES_NAME, 'String'), | |
956 | 3 => array(self::URL_PREFERENCES_NAME, 'String'), | |
957 | ); | |
958 | ||
959 | $dirParams = array(); | |
960 | $urlParams = array(); | |
961 | ||
962 | $dao = CRM_Core_DAO::executeQuery($sql, | |
963 | $sqlParams, | |
964 | TRUE, | |
965 | NULL, | |
966 | FALSE, | |
967 | TRUE, | |
968 | // trap exceptions as error | |
969 | TRUE | |
970 | ); | |
971 | ||
972 | if (is_a($dao, 'DB_Error')) { | |
973 | if (CRM_Core_Config::isUpgradeMode()) { | |
974 | // seems like this is a 4.0 -> 4.1 upgrade, so we suppress this error and continue | |
975 | return; | |
976 | } | |
977 | else { | |
978 | echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n"; | |
979 | exit(); | |
980 | } | |
981 | } | |
982 | ||
983 | while ($dao->fetch()) { | |
984 | if (!isset($params[$dao->name])) { | |
985 | continue; | |
986 | } | |
987 | if ($dao->group_name == self::DIRECTORY_PREFERENCES_NAME) { | |
988 | $dirParams[$dao->name] = CRM_Utils_Array::value($dao->name, $params, ''); | |
989 | } | |
990 | else { | |
991 | $urlParams[$dao->name] = CRM_Utils_Array::value($dao->name, $params, ''); | |
992 | } | |
993 | unset($params[$dao->name]); | |
994 | } | |
995 | ||
996 | if (!empty($dirParams)) { | |
997 | self::storeDirectoryOrURLPreferences($dirParams, | |
998 | self::DIRECTORY_PREFERENCES_NAME | |
999 | ); | |
1000 | } | |
1001 | ||
1002 | if (!empty($urlParams)) { | |
1003 | self::storeDirectoryOrURLPreferences($urlParams, | |
1004 | self::URL_PREFERENCES_NAME | |
1005 | ); | |
1006 | } | |
1007 | } | |
1008 | ||
b5c2afd0 | 1009 | /** |
c490a46a | 1010 | * @param array $params |
b5c2afd0 EM |
1011 | * @param $group |
1012 | */ | |
00be9182 | 1013 | public static function storeDirectoryOrURLPreferences(&$params, $group) { |
6a488035 TO |
1014 | foreach ($params as $name => $value) { |
1015 | // always try to store relative directory or url from CMS root | |
1016 | $value = ($group == self::DIRECTORY_PREFERENCES_NAME) ? CRM_Utils_File::relativeDirectory($value) : CRM_Utils_System::relativeURL($value); | |
1017 | ||
1018 | self::setItem($value, $group, $name); | |
1019 | } | |
1020 | } | |
1021 | ||
b5c2afd0 | 1022 | /** |
c490a46a | 1023 | * @param array $params |
b5c2afd0 EM |
1024 | * @param bool $setInConfig |
1025 | */ | |
00be9182 | 1026 | public static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) { |
2c7039ef DS |
1027 | if (CRM_Core_Config::isUpgradeMode()) { |
1028 | $isJoomla = (defined('CIVICRM_UF') && CIVICRM_UF == 'Joomla') ? TRUE : FALSE; | |
1029 | // hack to set the resource base url so that js/ css etc is loaded correctly | |
1030 | if ($isJoomla) { | |
1031 | $params['userFrameworkResourceURL'] = CRM_Utils_File::addTrailingSlash(CIVICRM_UF_BASEURL, '/') . str_replace('administrator', '', CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', 'userFrameworkResourceURL', 'value', 'name')); | |
1032 | } | |
1033 | if (self::isUpgradeFromPreFourOneAlpha1()) { | |
1034 | return; | |
1035 | } | |
1036 | } | |
1037 | ||
6a488035 TO |
1038 | if ($setInConfig) { |
1039 | $config = CRM_Core_Config::singleton(); | |
1040 | } | |
1041 | ||
6a488035 TO |
1042 | $sql = " |
1043 | SELECT name, group_name, value | |
1044 | FROM civicrm_setting | |
1045 | WHERE ( group_name = %1 | |
1046 | OR group_name = %2 ) | |
1047 | AND domain_id = %3 | |
1048 | "; | |
6ea503d4 TO |
1049 | $sqlParams = array( |
1050 | 1 => array(self::DIRECTORY_PREFERENCES_NAME, 'String'), | |
6a488035 TO |
1051 | 2 => array(self::URL_PREFERENCES_NAME, 'String'), |
1052 | 3 => array(CRM_Core_Config::domainID(), 'Integer'), | |
1053 | ); | |
1054 | ||
1055 | $dao = CRM_Core_DAO::executeQuery($sql, | |
1056 | $sqlParams, | |
1057 | TRUE, | |
1058 | NULL, | |
1059 | FALSE, | |
1060 | TRUE, | |
1061 | // trap exceptions as error | |
1062 | TRUE | |
1063 | ); | |
1064 | ||
1065 | if (is_a($dao, 'DB_Error')) { | |
2c7039ef DS |
1066 | echo "Fatal DB error, exiting, seems like your schema does not have civicrm_setting table\n"; |
1067 | exit(); | |
6a488035 TO |
1068 | } |
1069 | ||
1070 | while ($dao->fetch()) { | |
1071 | $value = self::getOverride($dao->group_name, $dao->name, NULL); | |
1072 | if ($value === NULL && $dao->value) { | |
1073 | $value = unserialize($dao->value); | |
1074 | if ($dao->group_name == self::DIRECTORY_PREFERENCES_NAME) { | |
1075 | $value = CRM_Utils_File::absoluteDirectory($value); | |
1076 | } | |
1077 | else { | |
1078 | // CRM-7622: we need to remove the language part | |
1079 | $value = CRM_Utils_System::absoluteURL($value, TRUE); | |
1080 | } | |
1081 | } | |
1082 | // CRM-10931, If DB doesn't have any value, carry on with any default value thats already available | |
8cc574cf | 1083 | if (!isset($value) && !empty($params[$dao->name])) { |
6a488035 TO |
1084 | $value = $params[$dao->name]; |
1085 | } | |
1086 | $params[$dao->name] = $value; | |
1087 | ||
1088 | if ($setInConfig) { | |
1089 | $config->{$dao->name} = $value; | |
1090 | } | |
1091 | } | |
1092 | } | |
1093 | ||
1094 | /** | |
1095 | * Determine what, if any, overrides have been provided | |
1096 | * for a setting. | |
1097 | * | |
dd244018 | 1098 | * @param $group |
100fef9d | 1099 | * @param string $name |
dd244018 EM |
1100 | * @param $default |
1101 | * | |
6a488035 TO |
1102 | * @return mixed, NULL or an overriden value |
1103 | */ | |
1104 | protected static function getOverride($group, $name, $default) { | |
1105 | global $civicrm_setting; | |
1106 | if ($group && $name && isset($civicrm_setting[$group][$name])) { | |
1107 | return $civicrm_setting[$group][$name]; | |
1108 | } | |
1109 | else { | |
1110 | return $default; | |
1111 | } | |
1112 | } | |
6a488035 | 1113 | |
2c7039ef | 1114 | /** |
100fef9d | 1115 | * Civicrm_setting didn't exist before 4.1.alpha1 and this function helps taking decisions during upgrade |
2c7039ef | 1116 | * |
5c766a0b | 1117 | * @return bool |
2c7039ef | 1118 | */ |
00be9182 | 1119 | public static function isUpgradeFromPreFourOneAlpha1() { |
2c7039ef DS |
1120 | if (CRM_Core_Config::isUpgradeMode()) { |
1121 | $currentVer = CRM_Core_BAO_Domain::version(); | |
1122 | if (version_compare($currentVer, '4.1.alpha1') < 0) { | |
1123 | return TRUE; | |
1124 | } | |
1125 | } | |
1126 | return FALSE; | |
1127 | } | |
96025800 | 1128 | |
2c7039ef | 1129 | } |