SimpleExcel.php
[Pman.Core] / I18n.php
index afd8950..8367964 100644 (file)
--- a/I18n.php
+++ b/I18n.php
  */
 require_once 'Pman.php';
 
-class Pman_Core_i18N extends Pman
+class Pman_Core_I18n extends Pman
 {
  
     
-    // these are the default languages we support.
-    // they will allways be overlaid with the current configuration (via getAuth)
-    // THESE WILL ALLWAYS BE UPPERCASE!!!
-    var $cfg = array(
-        // translated versions availalable
-        
-        't' => array(
-            'en', 'zh_CN',   'zh_HK', 
-        ),
-        // languages available
-        'l' => array(
-            
-            'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
-            'id', // indonesian
-            'tl', // tagalog
-            'vi', //vietnamise
-            'hi', // hindi
-            'ta', // tamil
-            '**', // other
-        ),
-        'c' => array(
-             'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
-        ),
-        'm' => array(
-            'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
-        )
-    );
-    
+     
     
      
     
@@ -94,145 +67,155 @@ class Pman_Core_i18N extends Pman
         $this->authUser = $au;
         
         $ff= HTML_FlexyFramework::get();
+         
         
+        $opts = empty($ff->Pman_Core_I18N) ?
+                (empty($ff->Pman_I18N) ? array() : $ff->Pman_I18N)
+                : $ff->Pman_Core_I18N;
         
-        $opts = empty($ff->Pman_Core_I18N) ? (empty($ff->Pman_I18N) ? array() : $ff->Pman_I18N)  : $ff->Pman_Core_I18N;
-        
-        
-        // load the cofiguration
-        foreach($opts as $k=>$v) {
-            
-            if ($v == '*') {
-                $this->cfg[$k] = $this->getDefaultCfg($k);
-                continue;
-            }
-            $this->cfg[$k] = is_array($v) ? $v  : explode(',', $v);
-        }
-        
+         
         
         
         
         return true;
     }
-    // returns a list of all countries..
-    function getDefaultCfg($t) {
-        $ret = array();
-        switch ($t) {
-            case 'c':
-                require_once 'I18Nv2/Country.php';
-                
-                $c = new I18Nv2_Country('en');
-                $ret =  array_keys($c->codes);
-                $ret[] = '**';
-                break;
-            case 'l':
-                require_once 'I18Nv2/Language.php';
-                $c = new I18Nv2_Language('en');
-                $ret =  array_keys($c->codes);
-                $ret[] = '**';
-                break;
-            case 'm':
-                require_once 'I18Nv2/Currency.php';
-                $c = new I18Nv2_Currency('en');
-                $ret =  array_keys($c->codes);
-                $ret[] = '**';
-                break;
-        }
+     
+    
+    function guessUsersLanguage()
+    {
+        $this->sessionState(0);
         
-        foreach ($ret as $k=>$v) {
-            $ret[$k] = strtoupper($v);
+        $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
+        
+        /// verify the selected language..
+        $i = DB_DataObject::Factory('I18n');
+        $i->ltype = 'l';                           // string(1)  not_null multiple_key
+        $i->lkey = $lang;                            // string(8)  not_null
+        if (!$i->count()) {    
+            $i = DB_DataObject::Factory('I18n');
+            $i->buildDb();
+            
+            $i = DB_DataObject::Factory('I18n');
+            $i->ltype = 'l';                           // string(1)  not_null multiple_key
+            $i->lkey = $lang;  
+            if (!$i->count()) { 
+                $this->jerr('invalid lang configured: ' . $lang);
+            }
         }
         
         
-        return $ret;
+        return explode('_', $lang);
     }
-    
      
-    function get($s ='')
+    function get($s ='', $opts=array())
     {
+     
+        $this->sessionState(0);
+        $lbits = $this->guessUsersLanguage();
+         
+        if ($this->authUser && !empty($_REQUEST['_debug'])) {
+            DB_DataObject::debugLevel(1);
+        }
         
         
-        switch ($s)
-        {
-            
-            case 'BuildDB':
-            // by admin only?!?
-                //DB_DataObject::debugLevel(1);
-                $this->buildDb('l');
-                $this->buildDb('c');
-                $this->buildDb('m');
-                die("DONE!");
+        
+        $i = DB_DataObject::Factory('I18n');
+        $i->is_active = 1;
+        switch($s) {
+            case 'Lang':
+                 
+                
+                $i->ltype = 'l';
+                $i->applyFilters($_REQUEST, $this->authUser, $this);
+                $this->jdata($i->toTransList('l',  implode('_',$lbits)));
                 break;
-                  
-            default: 
-                $this->outputJavascript();
-                // output javascript..
-                $this->jerr("ERROR");
+
+            case 'Country':
+                $i->ltype = 'c';
+                $i->applyFilters($_REQUEST, $this->authUser, $this);
+                $this->jdata($i->toTransList('c',  implode('_',$lbits)));
+               
+                break;
+                
+            case 'Currency':
+                $i->ltype = 'm';
+                $i->applyFilters($_REQUEST, $this->authUser, $this);
+                $this->jdata($i->toTransList('m',  implode('_',$lbits)));
+                break;
+            
+            case 'Timezone':
+                $ar = DateTimeZone::listAbbreviations();
+                $ret = array();
+                $tza = array();
+                foreach($ar as $tl => $sar) {
+                    foreach($sar as $tz) {
+                        $tza[]  = $tz['timezone_id'];
+                    
+                    }
+                }
+                $tza= array_unique($tza);
+                sort($tza);
+                foreach($tza as $tz) {
+                    //filtering..
+                    if (empty($_REQUEST['q']) ||
+                            0 === strcasecmp(
+                                    substr($tz,0, strlen($_REQUEST['q'])),
+                                    $_REQUEST['q'])
+                    ) {
+                        $ret[] = array('tz' => $tz);
+                    }
+                    
+                }
+                $this->jdata($ret);
+                
+                
+                
+             
+                
+        }
+        if (!empty($_REQUEST['debug'])) {
+            DB_DataObject::debugLevel(1);
         }
-         
-        $this->jdata($ret);
-        exit;
-        
-    }
-    
-    function outputJavascript()
-    {
-        
-        require_once 'I18Nv2/Country.php';
-        require_once 'I18Nv2/Language.php';
-        require_once 'I18Nv2/Currency.php';
         
-        $langs = $this->cfg['t'];
+        $i = DB_DataObject::Factory('I18n');
+        $i->buildDB();
+      
+       
+        $i = DB_DataObject::Factory('I18n');
+        $cfg = $i->cfg();
+        $langs = $cfg['t'];
        // var_dump($langs);exit;
         $ar = array();
         foreach($langs as $lang)
         {
-            $rlang = array_shift(explode('_', strtoupper($lang)));
+            //$rlang = array_shift(explode('_', strtoupper($lang)));
+            $ll = explode('_', $lang);
+            $rlang = array_shift($ll);
             
-            $ar[$lang] = array(
-                'l' => $this->objToList('l', new I18Nv2_Language($rlang, 'UTF-8')),
-                'c' => $this->objToList('c', new I18Nv2_Country($rlang, 'UTF-8')),
-                'm' => $this->objToList('m', new I18Nv2_Currency($rlang, 'UTF-8'))
-            );
+            $ar[$lang] = array();
+            $i = DB_DataObject::Factory('I18n');
+            $ar[$lang]['l'] = $i->toTransList('l',  $rlang);
+            $i = DB_DataObject::Factory('I18n');
+            $ar[$lang]['c'] =  $i->toTransList('c', $rlang);
+            $i = DB_DataObject::Factory('I18n');
+            $ar[$lang]['m'] = $i->toTransList('m', $rlang);
         }
         //echo '<PRE>';print_r($ar);
         header('Content-type: text/javascript');
+        echo "Roo.namespace('Pman.I18n');";
         echo 'Pman.I18n.Data = ' .  json_encode($ar);
         exit;
         
         
         
     }
-    function objToList($type, $obj) {
-        $ret = array();
-         
-         
-        foreach($this->cfg[$type] as $k) {
-            $sub = false;
-            
-            if (strpos($k, '_') !== false) {
-                $bits = explode('_', $k);
-                $k = array_shift($bits);
-                $sub = array_shift($bits);
-            }
-            $v = $k == '**' ? 'Other' : $obj->getName($k);
-            
-            if ($sub) {
-                $v .= ' ('.$sub.')';
-            }
-            
-            $ret[] = array(
-                'code'=>   ($type=='l' ? strtolower($k) : strtoupper($k)) . ($sub ? '_'.strtoupper($sub) : ''), 
-                'title' => $v
-            );
-        }
-        return $ret;
-    }
+    
     
      /**
      * translate (used by database building);
      * usage :
-     * require_once 'Pman/Core/I18N.php';
+     * require_once 'Pman/Core/I18n.php';
      * $x = new Pman_Core_I18N();
      * $x->translate($this->authuser, 'c', 'US');
      * @param au - auth User
@@ -249,82 +232,65 @@ class Pman_Core_i18N extends Pman
             return '??';
         }
         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
-        $lbits = explode('_', strtoupper($lang));
-        $lang = $lbits[0];
         
-        if (!isset($cache[$lang])) {
-            require_once 'I18Nv2/Country.php';
-            require_once 'I18Nv2/Language.php';
-            require_once 'I18Nv2/Currency.php';
-            $cache[$lang] = array(
-                'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
-                'c' => new I18Nv2_Country($lang, 'UTF-8'),
-                'm' => new I18Nv2_Currency($lang, 'UTF-8')
-            );
-            //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
-        }
-        if ($k == '**') {
-            return 'Other / Unknown';
-        }
-    
+        // does it need caching?
+        
+        $i = DB_DataObject::Factory('I18n');
+        return $i->translate($lang,$type,$k);
+        
         
-        if ($type == 'l') {
-            $tolang = explode('_', $k);
          
-            $ret = $cache[$lang][$type]->getName($tolang[0]);
-            if (count($tolang) > 1) {
-                $ret.= '('.$tolang[1].')'; 
-            }
-            return $ret;
-        }
-        $ret = $cache[$lang][$type]->getName($k);
-        //print_r(array($k, $ret));
-        return $ret;
         
         
     }
+    /**
+     * translate a list of items
+     * @param Pman_Core_DataObjects_Person $au Authenticated user
+     * @param String                      $type  c/l/m
+     * @param String                      $k     'comma' seperated list of keys to translate
+     */
     
+    function translateList($au, $type, $k)  
+    {
+        $ar = explode(',', $k);
+        $ret = array();
+        foreach($ar as $kk) {
+            $ret[] = $this->translate($au, $type, $kk);
+        }
+        return implode(', ', $ret);
+    }
+    /**
+     * DO NOT USE THIS -- see core_curr_rates dataobject.
+     */
     
     
-    function buildDB($ltype= false, $inlang= false )
+    function convertCurrency($val, $from, $to)
     {
-        if ($ltype === false) {
-            
-            die("OOPS NO LTYPE");
-        }
-        if ($inlang == '**') {
-            return; // dont bother building generic..
-        }
-        if ($inlang === false) {
-            foreach( $this->cfg['t'] as $l) {
-                $this->buildDB($ltype, $l);
-            }
-            return;
-        }
         
-        $list =  $this->getDefaultCfg($ltype);
-        
-        DB_DataObject::debugLevel(1);
+        return DB_DAtaObject::Factory('core_curr_rate')->convert($val,$from,$to);
+    
+    }
+    /**
+     * DO NOT USE THIS -- see core_curr_rates dataobject.
+     *
+     */
+    
+    
+    
+    function loadRates()
+    {   
+        static $rates = array();
         
-        foreach($list as $lkey) {
-            $x = DB_DataObject::factory('i18n');
-            $x->ltype = $ltype;
-            $x->lkey = $lkey;
-            $x->inlang= $inlang;
-            if ($x->find(true)) {
-                $xx= clone($x);
-                $x->lval = $this->translate($inlang, $ltype, $lkey);
-                $x->update($xx);
-                continue;
-            }
-            $x->lval = $this->translate($inlang, $ltype, $lkey);
-            $x->insert();
-            
+        if (!empty($rates)) {
+            $this->rates = $rates;
+            return;
         }
         
-        
-        
+        $this->rates = $rates = DB_DAtaObject::Factory('core_curr_rate')->currentRates();
         
     }
     
+    
+     
+    
 }
\ No newline at end of file