I18n.php
[Pman.Core] / I18n.php
index 8835199..9826c59 100644 (file)
--- a/I18n.php
+++ b/I18n.php
@@ -47,7 +47,7 @@
  */
 require_once 'Pman.php';
 
-class Pman_Core_I18N extends Pman
+class Pman_Core_I18n extends Pman
 {
  
     
@@ -69,7 +69,9 @@ class Pman_Core_I18N extends Pman
         $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;
         
          
         
@@ -87,8 +89,16 @@ class Pman_Core_I18N extends Pman
         $i = DB_DataObject::Factory('I18n');
         $i->ltype = 'l';                           // string(1)  not_null multiple_key
         $i->lkey = $lang;                            // string(8)  not_null
-        if (!$i->count()) {
-            $this->jerr('invalid lang configured: ' . $lang);
+        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);
+            }
         }
         
         
@@ -97,41 +107,38 @@ class Pman_Core_I18N extends Pman
      
     function get($s ='')
     {
-        $i = DB_DataObject::Factory('I18n');
-        $i->buildDb();
+     
      
         $lbits = $this->guessUsersLanguage();
          
         
-        
+        $i = DB_DataObject::Factory('I18n');
         
         switch($s) {
-            case 'Lang': 
-                $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
+            case 'Lang':
+                $i->ltype = 'l';
+                $i->applyFilters($_REQUEST, $this->authUser, $this);
+                $this->jdata($i->toTransList('l',  $lbits[0]));
                 break;
 
             case 'Country':
-                $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
+                $i->ltype = 'c';
+                $i->applyFilters($_REQUEST, $this->authUser, $this);
+                $this->jdata($i->toTransList('c',  $lbits[0]));
+               
                 break;
                 
              case 'Currency':
-                $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
+                $i->ltype = 'm';
+                $i->applyFilters($_REQUEST, $this->authUser, $this);
+                $this->jdata($i->toTransList('m',  $lbits[0]));
                 break;
-            // part of parent!!!!
-            /*
-            case 'BuildDB':
-            // by admin only?!?
-                //DB_DataObject::debugLevel(1);
-                $this->buildDb('l');
-                $this->buildDb('c');
-                $this->buildDb('m');
-                die("DONE!");
-                break;
-            */      
-            default: 
+             
                 
         }
-          
+        
+        $i = DB_DataObject::Factory('I18n');
+        $i->buildDb();
       
        
         $i = DB_DataObject::Factory('I18n');
@@ -143,11 +150,13 @@ class Pman_Core_I18N extends Pman
         {
             $rlang = array_shift(explode('_', strtoupper($lang)));
             
-            $ar[$lang] = array(
-                'l' => $i->toTransList('l',  $rlang),
-                'c' => $i->toTransList('c', $rlang),
-                'm' => $i->toTransList('m', $rlang),
-            );
+            $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');
@@ -191,6 +200,78 @@ class Pman_Core_I18N extends Pman
         
         
     }
+    /**
+     * 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);
+    }
+    /**
+     * convert rate:
+     * usage  : $i = new Pman_Core_I18n();
+     * $ret = $i->convertCurrency(100,"HKD","USD");
+     * if ($ret == false) {
+            /// something went wrong.
+     }
+     * 
+     * @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 convertCurrency($val, $from, $to)
+    {
+        $r = $this->loadRates();
+        if ($r === false) {
+            return false;
+        }
+        if (!isset($this->rates[$from]) || !isset($this->rates[$to]) ) {
+            return false;
+        }
+        //echo '<PRE>';print_R($this->rates);
+        $base = (1.0 / $this->rates[$from]) * $val;
+  
+        return $this->rates[$to] * $base;
+    
+    }
+    var $rates = array();
+    function loadRates()
+    {
+        if (!empty($this->rates)) {
+            return true;
+        }
+        $target = ini_get('session.save_path').'/eurofxref-daily.xml';
+        if (!file_exists($target) || filemtime($target) < (time() - 60*60*24)) {
+            $f = file_get_contents('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml');
+            if (!strlen($f)) {
+                return false;
+            }
+            file_put_contents($target,$f);
+        } 
+        $dom = simplexml_load_file($target);
+        $this->rates['EUR'] = 1.0;
+        $this->rates['TWD'] = 46.7008412;
+        $this->rates['VND'] = 26405.3;
+       
+        foreach($dom->Cube->Cube->Cube as $c) {
+           //echo '<PRE>';print_r($c );
+            $this->rates[(string)$c['currency']] = (string)$c['rate'];
+        }
+         $this->rates['RMB'] = $this->rates['CNY'] ;
+    }
+    
+    
      
     
 }
\ No newline at end of file