| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | namespace Civi\Core; |
| 13 | |
| 14 | use Civi\Core\Event\SystemInstallEvent; |
| 15 | |
| 16 | /** |
| 17 | * Class LocalizationInitializer |
| 18 | * @package Civi\Core |
| 19 | */ |
| 20 | class LocalizationInitializer { |
| 21 | |
| 22 | /** |
| 23 | * Load the locale settings based on the installation language |
| 24 | * |
| 25 | * @param \Civi\Core\Event\SystemInstallEvent $event |
| 26 | * @throws \CRM_Core_Exception |
| 27 | */ |
| 28 | public static function initialize(SystemInstallEvent $event) { |
| 29 | |
| 30 | // get the current installation language |
| 31 | global $tsLocale; |
| 32 | $seedLanguage = $tsLocale; |
| 33 | if (!$seedLanguage) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // get the corresponding settings file if any |
| 38 | $localeDir = \CRM_Core_I18n::getResourceDir(); |
| 39 | $fileName = $localeDir . $seedLanguage . DIRECTORY_SEPARATOR . 'settings.default.json'; |
| 40 | |
| 41 | // initalization |
| 42 | $settingsParams = []; |
| 43 | |
| 44 | if (file_exists($fileName)) { |
| 45 | |
| 46 | // load the file and parse it |
| 47 | $json = file_get_contents($fileName); |
| 48 | $settings = json_decode($json, TRUE); |
| 49 | |
| 50 | if (!empty($settings)) { |
| 51 | // get all valid settings |
| 52 | $results = civicrm_api3('Setting', 'getfields', []); |
| 53 | $validSettings = array_keys($results['values']); |
| 54 | // add valid settings to params to send to api |
| 55 | foreach ($settings as $setting => $value) { |
| 56 | if (in_array($setting, $validSettings)) { |
| 57 | $settingsParams[$setting] = $value; |
| 58 | } |
| 59 | |
| 60 | } |
| 61 | |
| 62 | // ensure we don't mess with multilingual |
| 63 | unset($settingsParams['languageLimit']); |
| 64 | |
| 65 | // support for enabled languages (option group) |
| 66 | if (isset($settings['languagesOption']) && count($settings['languagesOption']) > 0) { |
| 67 | \CRM_Core_BAO_OptionGroup::setActiveValues('languages', $settings['languagesOption']); |
| 68 | } |
| 69 | |
| 70 | // set default currency in currencies_enabled (option group) |
| 71 | if (isset($settings['defaultCurrency'])) { |
| 72 | \CRM_Admin_Form_Setting_Localization::updateEnabledCurrencies([$settings['defaultCurrency']], $settings['defaultCurrency']); |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | |
| 77 | } |
| 78 | |
| 79 | // in any case, enforce the seedLanguage as the default language |
| 80 | $settingsParams['lcMessages'] = $seedLanguage; |
| 81 | |
| 82 | // apply the config |
| 83 | civicrm_api3('Setting', 'create', $settingsParams); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | } |