From 930ef8a2d21905e93393a735a52c823b0b9d487c Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Mon, 6 Jun 2016 17:21:46 -0400 Subject: [PATCH 01/11] CRM-16395 - WIP with generic installer --- Civi/Core/Container.php | 1 + Civi/Core/LocalizationInitializer.php | 75 +++++++++++++++++++++++++++ settings.json | 37 +++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 Civi/Core/LocalizationInitializer.php create mode 100644 settings.json diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 4acec9fe67b8..9dab67b1ccc2 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -231,6 +231,7 @@ public function createEventDispatcher($container) { $dispatcher = new ContainerAwareEventDispatcher($container); $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\InstallationCanary', 'check')); $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\DatabaseInitializer', 'initialize')); + $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\LocalizationInitializer', 'initialize')); $dispatcher->addListener('hook_civicrm_post::Activity', array('\Civi\CCase\Events', 'fireCaseChange')); $dispatcher->addListener('hook_civicrm_post::Case', array('\Civi\CCase\Events', 'fireCaseChange')); $dispatcher->addListener('hook_civicrm_caseChange', array('\Civi\CCase\Events', 'delegateToXmlListeners')); diff --git a/Civi/Core/LocalizationInitializer.php b/Civi/Core/LocalizationInitializer.php new file mode 100644 index 000000000000..075d48827094 --- /dev/null +++ b/Civi/Core/LocalizationInitializer.php @@ -0,0 +1,75 @@ +userSystem->getCiviSourceStorage(); + + // get the corresponding settings file if any + $settingsParams = array(); + $fileName = $resourceDir . $seedLanguage . DIRECTORY_SEPARATOR . 'settings.json'; + if (file_exists($fileName)) { + $json = file_get_contents($fileName); + $settings = json_decode($json, TRUE); + if (!empty($settings)) { + // get all valid settings + $results = civicrm_api3('Setting', 'getfields', array()); + $validSettings = array_keys($results['values']); + // add valid settings to params to send to api + foreach ($settings as $setting => $value) { + if (in_array($setting, $validSettings)) { + $settingsParams[$setting] = $value; + } + // TODO: add support for currencies_enabled which is an OptionGroup + } + } + } + $settingsParams['domain_id'] = 'current_domain'; + //$settingsParams['lcMessages'] = $seedLanguage; +watchdog('debug', 'params -- ' . print_r($settingsParams,1)); + + // apply the config + civicrm_api3('Setting', 'create', $settingsParams); + } + +} diff --git a/settings.json b/settings.json new file mode 100644 index 000000000000..8cb864c31ec6 --- /dev/null +++ b/settings.json @@ -0,0 +1,37 @@ +{ + "address_format": "{contact.address_name} +{contact.street_address} +{contact.supplemental_address_1} +{contact.supplemental_address_2} +{contact.city}{, }{contact.state_province}{ }{contact.postal_code} +{contact.country}", + "mailing_format": "{contact.addressee} +{contact.street_address} +{contact.supplemental_address_1} +{contact.supplemental_address_2} +{contact.city}{, }{contact.state_province}{ }{contact.postal_code} +{contact.country}", + "display_name_format": "{contact.individual_prefix}{ }{contact.first_name}{ }{contact.last_name}{ }{contact.individual_suffix}", + "sort_name_format": "{contact.last_name}{, }{contact.first_name}", + "monetaryThousandSeparator": ",", + "monetaryDecimalPoint": ".", + "moneyformat": "%c %a", + "moneyvalueformat": "%!i", + "defaultCurrency": "USD", + "defaultContactCountry": "1039", + "defaultContactStateProvince": "1110", + "countryLimit": [1039], + "provinceLimit": [1039], + "dateformatDatetime": "%B %E%f, %Y %l:%M %P", + "dateformatFull": "%B %E%f, %Y", + "dateformatPartial": "%B %Y", + "dateformatTime": "%l:%M %P", + "dateformatYear": "%Y", + "dateformatFinancialBatch": "%m/%d/%Y", + "dateInputFormat": "mm/dd/yy", + "fiscalYearStart": "Array", + "languageLimit": "Array", + "lcMessages": "en_US", + "timeInputFormat": "1", + "weekBegins": "0" +} From 17cbdd5866c1ccbce1226756c04de1a3c07c1122 Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Wed, 8 Jun 2016 11:27:53 -0400 Subject: [PATCH 02/11] CRM-16395 doesn't seems to be possible to have the seedLanguage in Install event context --- CRM/Core/Config.php | 2 ++ Civi/Core/Container.php | 4 ++-- Civi/Core/LocalizationInitializer.php | 34 ++++++++++++++++++--------- install/index.php | 8 ++++++- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index 3d4d587a8f3b..c105d60bc959 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -496,6 +496,7 @@ public function free() { * - Upgrade from an old version (with first-run tracking) */ public function handleFirstRun() { + // Ordinarily, we prefetch settings en masse and find that the system is already installed. // No extra SQL queries required. if (Civi::settings()->get('installed')) { @@ -520,6 +521,7 @@ public function handleFirstRun() { return; } $dao = CRM_Core_DAO::executeQuery('SELECT version FROM civicrm_domain'); + while ($dao->fetch()) { if ($dao->version && version_compare($dao->version, CRM_Utils_System::version(), '<')) { return; diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 9dab67b1ccc2..00218dd30a44 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -229,8 +229,8 @@ public function createAngularManager() { */ public function createEventDispatcher($container) { $dispatcher = new ContainerAwareEventDispatcher($container); - $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\InstallationCanary', 'check')); - $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\DatabaseInitializer', 'initialize')); + //$dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\InstallationCanary', 'check')); + //$dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\DatabaseInitializer', 'initialize')); $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\LocalizationInitializer', 'initialize')); $dispatcher->addListener('hook_civicrm_post::Activity', array('\Civi\CCase\Events', 'fireCaseChange')); $dispatcher->addListener('hook_civicrm_post::Case', array('\Civi\CCase\Events', 'fireCaseChange')); diff --git a/Civi/Core/LocalizationInitializer.php b/Civi/Core/LocalizationInitializer.php index 075d48827094..3ff0b519ccae 100644 --- a/Civi/Core/LocalizationInitializer.php +++ b/Civi/Core/LocalizationInitializer.php @@ -27,6 +27,9 @@ namespace Civi\Core; +use Civi; +use Civi\Core\Event\SystemInstallEvent; + /** * Class LocalizationInitializer * @package Civi\Core @@ -40,17 +43,24 @@ class LocalizationInitializer { * @throws \CRM_Core_Exception */ public static function initialize(SystemInstallEvent $event) { + // get the current installation language - $seedLanguage = 'fr_CA'; - $resourceDir = \CRM_Core_I18n::getResourceDir(); - //\CRM_Core_Config::singleton()->userSystem->getCiviSourceStorage(); + global $tsLocale; + $seedLanguage = $tsLocale; + print $seedLanguage; die(); + if (!$seedLanguage) return; // get the corresponding settings file if any - $settingsParams = array(); - $fileName = $resourceDir . $seedLanguage . DIRECTORY_SEPARATOR . 'settings.json'; + $localeDir = \CRM_Core_I18n::getResourceDir(); + $fileName = $localeDir . $seedLanguage . DIRECTORY_SEPARATOR . 'settings.json'; + if (file_exists($fileName)) { + + // load the file and parse it + $settingsParams = array(); $json = file_get_contents($fileName); $settings = json_decode($json, TRUE); + if (!empty($settings)) { // get all valid settings $results = civicrm_api3('Setting', 'getfields', array()); @@ -63,13 +73,15 @@ public static function initialize(SystemInstallEvent $event) { // TODO: add support for currencies_enabled which is an OptionGroup } } - } - $settingsParams['domain_id'] = 'current_domain'; - //$settingsParams['lcMessages'] = $seedLanguage; -watchdog('debug', 'params -- ' . print_r($settingsParams,1)); - // apply the config - civicrm_api3('Setting', 'create', $settingsParams); + // TODO: add some validation ? or it should be in the API ? + + // enforce the seedLanguage as the default language + $settingsParams['lcMessages'] = $seedLanguage; + + // apply the config + civicrm_api3('Setting', 'create', $settingsParams); + } } } diff --git a/install/index.php b/install/index.php index 4b685a3e01df..9a1d60a55526 100644 --- a/install/index.php +++ b/install/index.php @@ -177,7 +177,7 @@ } } -// Set the locale (required by CRM_Core_Config) +// Set the CMS // This is mostly sympbolic, since nothing we do during the install // really requires CIVICRM_UF to be defined. $installTypeToUF = array( @@ -189,6 +189,7 @@ $uf = (isset($installTypeToUF[$installType]) ? $installTypeToUF[$installType] : 'Drupal'); define('CIVICRM_UF', $uf); +// Set the Locale (required by CRM_Core_Config) global $tsLocale; $tsLocale = 'en_US'; @@ -1487,6 +1488,11 @@ public function install($config) { // now enable civicrm module. module_enable(array('civicrm', 'civicrmtheme')); + CRM_Core_DAO::executeQuery('select * from civicrm_domain'); + + // SystemInstallEvent will be call from here with the first call of CiviCRM_Core_Settings + // FIXME: we need to pass the seedLanguage somehow but we don't have access to the db yet + // clear block, page, theme, and hook caches drupal_flush_all_caches(); From 2ed3bddd17acc8b0fa45e9548e063b15aafc23ea Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Wed, 8 Jun 2016 16:13:43 -0400 Subject: [PATCH 03/11] CRM-16395 it now works + extra languages and currencies initialization --- Civi/Core/Container.php | 4 +- Civi/Core/LocalizationInitializer.php | 76 +++++++++++++++++++++++---- install/index.php | 15 ++---- 3 files changed, 72 insertions(+), 23 deletions(-) diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 00218dd30a44..9dab67b1ccc2 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -229,8 +229,8 @@ public function createAngularManager() { */ public function createEventDispatcher($container) { $dispatcher = new ContainerAwareEventDispatcher($container); - //$dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\InstallationCanary', 'check')); - //$dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\DatabaseInitializer', 'initialize')); + $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\InstallationCanary', 'check')); + $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\DatabaseInitializer', 'initialize')); $dispatcher->addListener(SystemInstallEvent::EVENT_NAME, array('\Civi\Core\LocalizationInitializer', 'initialize')); $dispatcher->addListener('hook_civicrm_post::Activity', array('\Civi\CCase\Events', 'fireCaseChange')); $dispatcher->addListener('hook_civicrm_post::Case', array('\Civi\CCase\Events', 'fireCaseChange')); diff --git a/Civi/Core/LocalizationInitializer.php b/Civi/Core/LocalizationInitializer.php index 3ff0b519ccae..5766b6358193 100644 --- a/Civi/Core/LocalizationInitializer.php +++ b/Civi/Core/LocalizationInitializer.php @@ -47,17 +47,18 @@ public static function initialize(SystemInstallEvent $event) { // get the current installation language global $tsLocale; $seedLanguage = $tsLocale; - print $seedLanguage; die(); if (!$seedLanguage) return; // get the corresponding settings file if any $localeDir = \CRM_Core_I18n::getResourceDir(); - $fileName = $localeDir . $seedLanguage . DIRECTORY_SEPARATOR . 'settings.json'; + $fileName = $localeDir . $seedLanguage . DIRECTORY_SEPARATOR . 'settings.default.json'; + + // initalization + $settingsParams = array(); if (file_exists($fileName)) { // load the file and parse it - $settingsParams = array(); $json = file_get_contents($fileName); $settings = json_decode($json, TRUE); @@ -70,18 +71,75 @@ public static function initialize(SystemInstallEvent $event) { if (in_array($setting, $validSettings)) { $settingsParams[$setting] = $value; } - // TODO: add support for currencies_enabled which is an OptionGroup + + } + + // ensure we don't mess with multilingual + unset($settingsParams['languageLimit']); + + // support for enabled languages (option group) + if (isset($settings['languagesOption']) && count($settings['languagesOption']) > 0) { + self::updateLanguages($settings['languagesOption']); + } + + // set default currency in currencies_enabled (option group) + if (isset($settings['defaultCurrency'])) { + self::updateCurrencies(array($settings['defaultCurrency']), $settings['defaultCurrency']); } + } - // TODO: add some validation ? or it should be in the API ? + } + + // in any case, enforce the seedLanguage as the default language + $settingsParams['lcMessages'] = $seedLanguage; + + // apply the config + civicrm_api3('Setting', 'create', $settingsParams); - // enforce the seedLanguage as the default language - $settingsParams['lcMessages'] = $seedLanguage; + } - // apply the config - civicrm_api3('Setting', 'create', $settingsParams); + protected static function updateLanguages($languages) { + $result = civicrm_api3('OptionValue', 'get', array( + 'option_group_id' => "languages", + 'options' => array('limit' => 0), + )); + + foreach ($result['values'] as $id => $language) { + $params = $language; + if (in_array($language['name'], $languages)) { + $params['is_active'] = 1; + } else { + // disable every language that is not in the given list + $params['is_active'] = 0; + } + civicrm_api3('OptionValue', 'create', $params); } + + } + + protected static function updateCurrencies($currencies, $default) { + + // sort so that when we display drop down, weights have right value + sort($currencies); + + // get labels for all the currencies + $options = array(); + + $currencySymbols = \CRM_Admin_Form_Setting_Localization::getCurrencySymbols(); + for ($i = 0; $i < count($currencies); $i++) { + $options[] = array( + 'label' => $currencySymbols[$currencies[$i]], + 'value' => $currencies[$i], + 'weight' => $i + 1, + 'is_active' => 1, + 'is_default' => $currencies[$i] == $default, + ); + } + + $dontCare = NULL; + \CRM_Core_OptionGroup::createAssoc('currencies_enabled', $options, $dontCare); + } } diff --git a/install/index.php b/install/index.php index 9a1d60a55526..7b06440cadac 100644 --- a/install/index.php +++ b/install/index.php @@ -1488,10 +1488,10 @@ public function install($config) { // now enable civicrm module. module_enable(array('civicrm', 'civicrmtheme')); - CRM_Core_DAO::executeQuery('select * from civicrm_domain'); - // SystemInstallEvent will be call from here with the first call of CiviCRM_Core_Settings - // FIXME: we need to pass the seedLanguage somehow but we don't have access to the db yet + // we need to pass the seedLanguage before that + global $civicrm_setting; + $civicrm_setting['domain']['lcMessages'] = $config['seedLanguage']; // clear block, page, theme, and hook caches drupal_flush_all_caches(); @@ -1503,15 +1503,6 @@ public function install($config) { $GLOBALS['user'] = $original_user; drupal_save_session(TRUE); - //change the default language to one chosen - if (isset($config['seedLanguage']) && $config['seedLanguage'] != 'en_US') { - civicrm_api3('Setting', 'create', array( - 'domain_id' => 'current_domain', - 'lcMessages' => $config['seedLanguage'], - ) - ); - } - $output .= ''; $output .= ''; $output .= ''; From b9e491ecaf7513e325bea776adbcc09376974752 Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Wed, 8 Jun 2016 16:21:00 -0400 Subject: [PATCH 04/11] CRM-16395 remove settings.json - was there for dev purpose only --- settings.json | 37 ------------------------------------- 1 file changed, 37 deletions(-) delete mode 100644 settings.json diff --git a/settings.json b/settings.json deleted file mode 100644 index 8cb864c31ec6..000000000000 --- a/settings.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "address_format": "{contact.address_name} -{contact.street_address} -{contact.supplemental_address_1} -{contact.supplemental_address_2} -{contact.city}{, }{contact.state_province}{ }{contact.postal_code} -{contact.country}", - "mailing_format": "{contact.addressee} -{contact.street_address} -{contact.supplemental_address_1} -{contact.supplemental_address_2} -{contact.city}{, }{contact.state_province}{ }{contact.postal_code} -{contact.country}", - "display_name_format": "{contact.individual_prefix}{ }{contact.first_name}{ }{contact.last_name}{ }{contact.individual_suffix}", - "sort_name_format": "{contact.last_name}{, }{contact.first_name}", - "monetaryThousandSeparator": ",", - "monetaryDecimalPoint": ".", - "moneyformat": "%c %a", - "moneyvalueformat": "%!i", - "defaultCurrency": "USD", - "defaultContactCountry": "1039", - "defaultContactStateProvince": "1110", - "countryLimit": [1039], - "provinceLimit": [1039], - "dateformatDatetime": "%B %E%f, %Y %l:%M %P", - "dateformatFull": "%B %E%f, %Y", - "dateformatPartial": "%B %Y", - "dateformatTime": "%l:%M %P", - "dateformatYear": "%Y", - "dateformatFinancialBatch": "%m/%d/%Y", - "dateInputFormat": "mm/dd/yy", - "fiscalYearStart": "Array", - "languageLimit": "Array", - "lcMessages": "en_US", - "timeInputFormat": "1", - "weekBegins": "0" -} From b69560f7fb2db78cc5bb35cd8c241993d07b0600 Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Wed, 8 Jun 2016 16:30:03 -0400 Subject: [PATCH 05/11] CRM-16395 revert change from CRM/Core/Config.php --- CRM/Core/Config.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/CRM/Core/Config.php b/CRM/Core/Config.php index c105d60bc959..3d4d587a8f3b 100644 --- a/CRM/Core/Config.php +++ b/CRM/Core/Config.php @@ -496,7 +496,6 @@ public function free() { * - Upgrade from an old version (with first-run tracking) */ public function handleFirstRun() { - // Ordinarily, we prefetch settings en masse and find that the system is already installed. // No extra SQL queries required. if (Civi::settings()->get('installed')) { @@ -521,7 +520,6 @@ public function handleFirstRun() { return; } $dao = CRM_Core_DAO::executeQuery('SELECT version FROM civicrm_domain'); - while ($dao->fetch()) { if ($dao->version && version_compare($dao->version, CRM_Utils_System::version(), '<')) { return; From 926e58ecb4c63a60fcdba3e3c610d59db4be3594 Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Wed, 8 Jun 2016 16:48:48 -0400 Subject: [PATCH 06/11] Checkstyle and documentation --- Civi/Core/LocalizationInitializer.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Civi/Core/LocalizationInitializer.php b/Civi/Core/LocalizationInitializer.php index 5766b6358193..efdc57ad0ff0 100644 --- a/Civi/Core/LocalizationInitializer.php +++ b/Civi/Core/LocalizationInitializer.php @@ -36,7 +36,7 @@ */ class LocalizationInitializer { - /* + /** * Load the locale settings based on the installation language * * @param \Civi\Core\Event\SystemInstallEvent $event @@ -47,7 +47,9 @@ public static function initialize(SystemInstallEvent $event) { // get the current installation language global $tsLocale; $seedLanguage = $tsLocale; - if (!$seedLanguage) return; + if (!$seedLanguage) { + return; + } // get the corresponding settings file if any $localeDir = \CRM_Core_I18n::getResourceDir(); @@ -99,6 +101,12 @@ public static function initialize(SystemInstallEvent $event) { } + /** + * Enable provided languages and disable all others + * + * @param $languages array of languages (locale code) ['fr_CA', 'en_CA'] + * @throws \CiviCRM_API3_Exception + */ protected static function updateLanguages($languages) { $result = civicrm_api3('OptionValue', 'get', array( 'option_group_id' => "languages", @@ -109,7 +117,8 @@ protected static function updateLanguages($languages) { $params = $language; if (in_array($language['name'], $languages)) { $params['is_active'] = 1; - } else { + } + else { // disable every language that is not in the given list $params['is_active'] = 0; } @@ -118,6 +127,12 @@ protected static function updateLanguages($languages) { } + /** + * Replace available currencies by the ones provided + * + * @param $currencies array of currencies ['USD', 'CAD'] + * @param $default default currency + */ protected static function updateCurrencies($currencies, $default) { // sort so that when we display drop down, weights have right value From 5affa15765b6ab0fd099eba03d06a1563a56e527 Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Thu, 13 Oct 2016 16:00:29 -0400 Subject: [PATCH 07/11] CRM-16395 Adding generic correction for enabled currencies + fixing inactive languages display in i18n settings form --- CRM/Admin/Form/Setting/Localization.php | 100 +++++++++++++++++------- CRM/Core/I18n.php | 13 ++- Civi/Core/LocalizationInitializer.php | 32 +------- settings/Localization.setting.php | 3 + 4 files changed, 89 insertions(+), 59 deletions(-) diff --git a/CRM/Admin/Form/Setting/Localization.php b/CRM/Admin/Form/Setting/Localization.php index 5ad770b38ba9..020a6a8ef613 100644 --- a/CRM/Admin/Form/Setting/Localization.php +++ b/CRM/Admin/Form/Setting/Localization.php @@ -186,40 +186,16 @@ public function postProcess() { // we do this only to initialize monetary decimal point and thousand separator $config = CRM_Core_Config::singleton(); - // save enabled currencies and defaul currency in option group 'currencies_enabled' + // save enabled currencies and default currency in option group 'currencies_enabled' // CRM-1496 if (empty($values['currencyLimit'])) { $values['currencyLimit'] = array($values['defaultCurrency']); } - elseif (!in_array($values['defaultCurrency'], - $values['currencyLimit'] - ) - ) { + elseif (!in_array($values['defaultCurrency'], $values['currencyLimit'])) { $values['currencyLimit'][] = $values['defaultCurrency']; } - // sort so that when we display drop down, weights have right value - sort($values['currencyLimit']); - - // get labels for all the currencies - $options = array(); - - $currencySymbols = self::getCurrencySymbols(); - for ($i = 0; $i < count($values['currencyLimit']); $i++) { - $options[] = array( - 'label' => $currencySymbols[$values['currencyLimit'][$i]], - 'value' => $values['currencyLimit'][$i], - 'weight' => $i + 1, - 'is_active' => 1, - 'is_default' => $values['currencyLimit'][$i] == $values['defaultCurrency'], - ); - } - - $dontCare = NULL; - CRM_Core_OptionGroup::createAssoc('currencies_enabled', - $options, - $dontCare - ); + self::updateEnabledCurrencies($values['currencyLimit'], $values['defaultCurrency']); // unset currencyLimit so we dont store there unset($values['currencyLimit']); @@ -264,6 +240,38 @@ public function postProcess() { } } + + /** + * Replace available currencies by the ones provided + * + * @param $currencies array of currencies ['USD', 'CAD'] + * @param $default default currency + */ + protected static function updateEnabledCurrencies($currencies, $default) { + + // sort so that when we display drop down, weights have right value + sort($currencies); + + // get labels for all the currencies + $options = array(); + + $currencySymbols = \CRM_Admin_Form_Setting_Localization::getCurrencySymbols(); + for ($i = 0; $i < count($currencies); $i++) { + $options[] = array( + 'label' => $currencySymbols[$currencies[$i]], + 'value' => $currencies[$i], + 'weight' => $i + 1, + 'is_active' => 1, + 'is_default' => $currencies[$i] == $default, + ); + } + + $dontCare = NULL; + \CRM_Core_OptionGroup::createAssoc('currencies_enabled', $options, $dontCare); + + } + + /** * @return array */ @@ -336,6 +344,44 @@ public static function onChangeLcMessages($oldLocale, $newLocale, $metadata, $do } } + public static function onChangeDefaultCurrency($oldCurrency, $newCurrency, $metadata) { + if ($oldCurrency == $newCurrency) { + return; + } + + // ensure that default currency is always in the list of enabled currencies + $currencies = array_keys(CRM_Core_OptionGroup::values('currencies_enabled')); + if (!in_array($newCurrency, $currencies)) { + if (empty($currencies)) { + $currencies = array($values['defaultCurrency']); + } + else { + $currencies[] = $newCurrency; + } + + // sort so that when we display drop down, weights have right value + sort($currencies); + + // get labels for all the currencies + $options = array(); + + $currencySymbols = CRM_Admin_Form_Setting_Localization::getCurrencySymbols(); + for ($i = 0; $i < count($currencies); $i++) { + $options[] = array( + 'label' => $currencySymbols[$currencies[$i]], + 'value' => $currencies[$i], + 'weight' => $i + 1, + 'is_active' => 1, + 'is_default' => $currencies[$i] == $newCurrency, + ); + } + + $dontCare = NULL; + CRM_Core_OptionGroup::createAssoc('currencies_enabled', $options, $dontCare); + } + + } + /** * @return array */ diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index fef2bed8523d..670ed595a964 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -148,6 +148,7 @@ protected function setPhpGettextLocale($locale) { * @return array * Array of code/language name mappings */ + public static function languages($justEnabled = FALSE) { static $all = NULL; static $enabled = NULL; @@ -155,6 +156,14 @@ public static function languages($justEnabled = FALSE) { if (!$all) { $all = CRM_Contact_BAO_Contact::buildOptions('preferred_language'); + // get labels + $rows = array(); + $labels = array(); + CRM_Core_OptionValue::getValues(array('name' => 'languages'), $rows); + foreach ($rows as $id => $row) { + $labels[$row['name']] = $row['label']; + } + // check which ones are available; add them to $all if not there already $codes = array(); if (is_dir(CRM_Core_I18n::getResourceDir()) && $dir = opendir(CRM_Core_I18n::getResourceDir())) { @@ -162,7 +171,7 @@ public static function languages($justEnabled = FALSE) { if (preg_match('/^[a-z][a-z]_[A-Z][A-Z]$/', $filename)) { $codes[] = $filename; if (!isset($all[$filename])) { - $all[$filename] = $filename; + $all[$filename] = $labels[$filename]; } } } @@ -178,6 +187,8 @@ public static function languages($justEnabled = FALSE) { unset($all[$code]); } } + + ksort($all); } if ($enabled === NULL) { diff --git a/Civi/Core/LocalizationInitializer.php b/Civi/Core/LocalizationInitializer.php index efdc57ad0ff0..bf4a6ac6096b 100644 --- a/Civi/Core/LocalizationInitializer.php +++ b/Civi/Core/LocalizationInitializer.php @@ -86,7 +86,7 @@ public static function initialize(SystemInstallEvent $event) { // set default currency in currencies_enabled (option group) if (isset($settings['defaultCurrency'])) { - self::updateCurrencies(array($settings['defaultCurrency']), $settings['defaultCurrency']); + \CRM_Admin_Form_Setting_Localization::updateEnabledCurrencies(array($settings['defaultCurrency']), $settings['defaultCurrency']); } } @@ -127,34 +127,4 @@ protected static function updateLanguages($languages) { } - /** - * Replace available currencies by the ones provided - * - * @param $currencies array of currencies ['USD', 'CAD'] - * @param $default default currency - */ - protected static function updateCurrencies($currencies, $default) { - - // sort so that when we display drop down, weights have right value - sort($currencies); - - // get labels for all the currencies - $options = array(); - - $currencySymbols = \CRM_Admin_Form_Setting_Localization::getCurrencySymbols(); - for ($i = 0; $i < count($currencies); $i++) { - $options[] = array( - 'label' => $currencySymbols[$currencies[$i]], - 'value' => $currencies[$i], - 'weight' => $i + 1, - 'is_active' => 1, - 'is_default' => $currencies[$i] == $default, - ); - } - - $dontCare = NULL; - \CRM_Core_OptionGroup::createAssoc('currencies_enabled', $options, $dontCare); - - } - } diff --git a/settings/Localization.setting.php b/settings/Localization.setting.php index e375d92f3a9a..1a84c607b941 100644 --- a/settings/Localization.setting.php +++ b/settings/Localization.setting.php @@ -142,6 +142,9 @@ 'pseudoconstant' => array( 'callback' => 'CRM_Admin_Form_Setting_Localization::getCurrencySymbols', ), + 'on_change' => array( + 'CRM_Admin_Form_Setting_Localization::onChangeDefaultCurrency', + ), ), 'defaultContactCountry' => array( 'group_name' => 'Localization Preferences', From 4a851ea4675e95aff9937d29e72d2b0e96b22992 Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Thu, 13 Oct 2016 16:06:21 -0400 Subject: [PATCH 08/11] CRM-16395 civilint --- CRM/Core/I18n.php | 1 - 1 file changed, 1 deletion(-) diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index 670ed595a964..a31ca643277c 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -148,7 +148,6 @@ protected function setPhpGettextLocale($locale) { * @return array * Array of code/language name mappings */ - public static function languages($justEnabled = FALSE) { static $all = NULL; static $enabled = NULL; From 5ac92b2afbc8b77e7f59f6e732a6ceacd2ae2be6 Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Thu, 13 Oct 2016 21:50:53 -0400 Subject: [PATCH 09/11] CRM-16395 better performance for languages initialization --- CRM/Admin/Form/Setting/Localization.php | 2 +- CRM/Core/BAO/OptionGroup.php | 32 +++++++++++++++++++++++++ Civi/Core/LocalizationInitializer.php | 28 +--------------------- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/CRM/Admin/Form/Setting/Localization.php b/CRM/Admin/Form/Setting/Localization.php index 020a6a8ef613..eb89da2a3cd8 100644 --- a/CRM/Admin/Form/Setting/Localization.php +++ b/CRM/Admin/Form/Setting/Localization.php @@ -247,7 +247,7 @@ public function postProcess() { * @param $currencies array of currencies ['USD', 'CAD'] * @param $default default currency */ - protected static function updateEnabledCurrencies($currencies, $default) { + public static function updateEnabledCurrencies($currencies, $default) { // sort so that when we display drop down, weights have right value sort($currencies); diff --git a/CRM/Core/BAO/OptionGroup.php b/CRM/Core/BAO/OptionGroup.php index 3dedb3740c55..453777161ca0 100644 --- a/CRM/Core/BAO/OptionGroup.php +++ b/CRM/Core/BAO/OptionGroup.php @@ -183,4 +183,36 @@ public static function ensureOptionGroupExists($params) { } } + /** + * Set the given values to active, and set all other values to inactive. + * + * @param string $optionGroupName + * e.g "languages" + * @param array $activeValues + * e.g. array("en_CA","fr_CA") + */ + public static function setActiveValues($optionGroupName, $activeValues) { + $params = array( + 1 => array($optionGroupName, 'String'), + ); + + // convert activeValues into placeholders / params in the query + $placeholders = array(); + $i = count($params) + 1; + foreach ($activeValues as $value) { + $placeholders[] = "%{$i}"; + $params[$i] = array($value, 'String'); + $i++; + } + $placeholders = implode(', ', $placeholders); + + CRM_Core_DAO::executeQuery(" +UPDATE civicrm_option_value cov + LEFT JOIN civicrm_option_group cog ON cov.option_group_id = cog.id +SET cov.is_active = CASE WHEN cov.name IN ({$placeholders}) THEN 1 ELSE 0 END +WHERE cog.name = %1", + $params + ); + } + } diff --git a/Civi/Core/LocalizationInitializer.php b/Civi/Core/LocalizationInitializer.php index bf4a6ac6096b..6eb89355c9b4 100644 --- a/Civi/Core/LocalizationInitializer.php +++ b/Civi/Core/LocalizationInitializer.php @@ -81,7 +81,7 @@ public static function initialize(SystemInstallEvent $event) { // support for enabled languages (option group) if (isset($settings['languagesOption']) && count($settings['languagesOption']) > 0) { - self::updateLanguages($settings['languagesOption']); + \CRM_Core_BAO_OptionGroup::setActiveValues('languages', $settings['languagesOption']); } // set default currency in currencies_enabled (option group) @@ -101,30 +101,4 @@ public static function initialize(SystemInstallEvent $event) { } - /** - * Enable provided languages and disable all others - * - * @param $languages array of languages (locale code) ['fr_CA', 'en_CA'] - * @throws \CiviCRM_API3_Exception - */ - protected static function updateLanguages($languages) { - $result = civicrm_api3('OptionValue', 'get', array( - 'option_group_id' => "languages", - 'options' => array('limit' => 0), - )); - - foreach ($result['values'] as $id => $language) { - $params = $language; - if (in_array($language['name'], $languages)) { - $params['is_active'] = 1; - } - else { - // disable every language that is not in the given list - $params['is_active'] = 0; - } - civicrm_api3('OptionValue', 'create', $params); - } - - } - } From fb42a732f35bcb74ad3a008732154d1f15c7e570 Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Fri, 14 Oct 2016 09:48:13 -0400 Subject: [PATCH 10/11] CRM-16395 faulty copy/paste syntax --- CRM/Admin/Form/Setting/Localization.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CRM/Admin/Form/Setting/Localization.php b/CRM/Admin/Form/Setting/Localization.php index eb89da2a3cd8..7f84a3d7b139 100644 --- a/CRM/Admin/Form/Setting/Localization.php +++ b/CRM/Admin/Form/Setting/Localization.php @@ -255,7 +255,7 @@ public static function updateEnabledCurrencies($currencies, $default) { // get labels for all the currencies $options = array(); - $currencySymbols = \CRM_Admin_Form_Setting_Localization::getCurrencySymbols(); + $currencySymbols = CRM_Admin_Form_Setting_Localization::getCurrencySymbols(); for ($i = 0; $i < count($currencies); $i++) { $options[] = array( 'label' => $currencySymbols[$currencies[$i]], @@ -267,7 +267,7 @@ public static function updateEnabledCurrencies($currencies, $default) { } $dontCare = NULL; - \CRM_Core_OptionGroup::createAssoc('currencies_enabled', $options, $dontCare); + CRM_Core_OptionGroup::createAssoc('currencies_enabled', $options, $dontCare); } From 2d6dbdb038a90058ff0ed3154086110f9782a83d Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Fri, 14 Oct 2016 10:04:51 -0400 Subject: [PATCH 11/11] CRM-16395 remove duplicate code --- CRM/Admin/Form/Setting/Localization.php | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/CRM/Admin/Form/Setting/Localization.php b/CRM/Admin/Form/Setting/Localization.php index 7f84a3d7b139..1f1a0520945a 100644 --- a/CRM/Admin/Form/Setting/Localization.php +++ b/CRM/Admin/Form/Setting/Localization.php @@ -359,25 +359,7 @@ public static function onChangeDefaultCurrency($oldCurrency, $newCurrency, $meta $currencies[] = $newCurrency; } - // sort so that when we display drop down, weights have right value - sort($currencies); - - // get labels for all the currencies - $options = array(); - - $currencySymbols = CRM_Admin_Form_Setting_Localization::getCurrencySymbols(); - for ($i = 0; $i < count($currencies); $i++) { - $options[] = array( - 'label' => $currencySymbols[$currencies[$i]], - 'value' => $currencies[$i], - 'weight' => $i + 1, - 'is_active' => 1, - 'is_default' => $currencies[$i] == $newCurrency, - ); - } - - $dontCare = NULL; - CRM_Core_OptionGroup::createAssoc('currencies_enabled', $options, $dontCare); + CRM_Admin_Form_Setting_Localization::updateEnabledCurrencies($currencies, $newCurrency); } }