SugarCRM源码分析之本地化货币


        本篇主要分析SugarCRM中本地化货币的源码,大体配置如下

 'default_currency_iso4217' => 'RMB',
  'default_currency_name' => 'RMB',
  'default_currency_show_preferred' => false,
  'default_currency_significant_digits' => 2,
  'default_currency_symbol' => '¥',

        详见代码分析

./include/entryPoint.php
$locale = Localization::getObject();

./include/Localization/Localization.php
public static function getObject()
{

    // 看看是否有定制化的本地化类
    $class = __CLASS__;
    if (SugarAutoLoader::load('custom/include/Localization/Localization.php')) {
        $class = SugarAutoLoader::customClass($class);
    }

    return new $class();
}

// 解析方法,相当于 __construct方法,类实例化时自动执行
function Localization() {
    global $sugar_config;
    $this->localeNameFormatDefault = empty($sugar_config['locale_name_format_default']) ? 's f l' : $sugar_config['default_name_format'];
    $this->loadCurrencies();
}

function loadCurrencies() {
    // trying to use DBManagerFactory here fails in install.php,
    // so leaving this as global $db.
    // $db = DBManagerFactory::getInstance();
    // 在数据库连接的那一篇已经分析过了,数据库实例存放在db变量内
    // 这种用法在SugarCRM中大量运用
    global $db;
    global $sugar_config;

    if(empty($db)) {
        return array();
    }

    // 在缓存分析那篇,sugar_cache_retrieve是用来获取存入缓存的数据的
    // 没有的话变想数据库中查询,再存放到缓存中
    $load = sugar_cache_retrieve('currency_list');
    if ( !is_array($load) ) {
        // load default from config.php
        $this->currencies['-99'] = array(
            'name'        => $sugar_config['default_currency_name'],
            'symbol'    => $sugar_config['default_currency_symbol'],
            'conversion_rate' => 1
        );

        $q = "SELECT id, name, symbol, conversion_rate FROM currencies WHERE status = 'Active' and deleted = 0";
        $r = $db->query($q);

        while($a = $db->fetchByAssoc($r)) {
            $load = array();
            $load['name'] = $a['name'];
            $load['symbol'] = $a['symbol'];
            $load['conversion_rate'] = $a['conversion_rate'];

            $this->currencies[$a['id']] = $load;
        }
        sugar_cache_put('currency_list',$this->currencies);
    } else {
        $this->currencies = $load;
    }
}


你可能感兴趣的:(SugarCRM源码分析之本地化货币)