Skip to content

Commit

Permalink
CRM-16395 Adding generic correction for enabled currencies + fixing i…
Browse files Browse the repository at this point in the history
…nactive languages display in i18n settings form
  • Loading branch information
samuelsov committed Oct 13, 2016
1 parent 926e58e commit 5affa15
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 59 deletions.
100 changes: 73 additions & 27 deletions CRM/Admin/Form/Setting/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
13 changes: 12 additions & 1 deletion CRM/Core/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,30 @@ protected function setPhpGettextLocale($locale) {
* @return array
* Array of code/language name mappings
*/

public static function languages($justEnabled = FALSE) {
static $all = NULL;
static $enabled = NULL;

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())) {
while ($filename = readdir($dir)) {
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];
}
}
}
Expand All @@ -178,6 +187,8 @@ public static function languages($justEnabled = FALSE) {
unset($all[$code]);
}
}

ksort($all);
}

if ($enabled === NULL) {
Expand Down
32 changes: 1 addition & 31 deletions Civi/Core/LocalizationInitializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

}
Expand Down Expand Up @@ -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);

}

}
3 changes: 3 additions & 0 deletions settings/Localization.setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 5affa15

Please sign in to comment.