DataObjects/pman.links.ini
[Pman.Core] / I18n.php
index 2300418..afd8950 100644 (file)
--- a/I18n.php
+++ b/I18n.php
@@ -9,19 +9,27 @@
  * By default it returns
  * 
  * Pman.I18n.Data = {
-      en : [ { code : 'end', title : 'English' }, .... ],
+      
+      en : {
+          l :  [ { code : 'en', title : 'English' }, .... ],
+          c :  [ { code : 'UK', title : 'United Kingdom' }, .... ],
+          m :  [ { code : 'USD', title : 'US Dollars' }, .... ],
       fr : ....
    }
  * 
  * 
- * usage:
+ * other usage:
  * 
  * index.php/Pman/I18N/BuildDB -- buildes the database..
+ * .. other formats are depreciated, but are supported by the old code....
+ * 
  * 
+ * Database language translation should be done using the database table.
+ * So sorting can be done correctly..
  * 
- * Config in index.php..
+ * Configuration in index.php..
  * 
- *  'Pman_I18N' => array(
+ *  'Pman_Core_I18N' => array(
       'l' => array(
             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
             'id', // indonesian
@@ -31,7 +39,7 @@
             'ta', // tamil
             '**', // other
         ), 
-       'c' => '*',
+       'c' => '*', // eg. all languages..
        'm' => array( 'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY' )
     ), 
 
@@ -41,16 +49,20 @@ require_once 'Pman.php';
 
 class Pman_Core_i18N extends Pman
 {
-    function getAuth()
-    {
-        // anyone can downlaod this list - as it's needed prior to the login box.
-        return true;
-    }
     
-    
-    // these are the languages we support.
+    // 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
@@ -81,10 +93,13 @@ class Pman_Core_i18N extends Pman
         //}
         $this->authUser = $au;
         
-        $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
-        $opts = empty($opts)  ?  array() : $opts;
+        $ff= HTML_FlexyFramework::get();
+        
         
+        $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 == '*') {
@@ -123,6 +138,7 @@ class Pman_Core_i18N extends Pman
                 $ret[] = '**';
                 break;
         }
+        
         foreach ($ret as $k=>$v) {
             $ret[$k] = strtoupper($v);
         }
@@ -131,100 +147,14 @@ class Pman_Core_i18N extends Pman
         return $ret;
     }
     
-    
-    
-    function setSession($au)
-    {
-        $this->authUser = $au;
-        $lbits = implode('_', $this->findLang());
-        if (empty($_SESSION['Pman_I18N'])) {
-            $_SESSION['Pman_I18N']  = array();
-        }
-        
-        $_SESSION['Pman_I18N'][$lbits] = array(
-            'l' => $this->getList('l', $lbits),
-            'c' => $this->getList('c', $lbits),
-            'm' => $this->getList('m', $lbits),
-        );
-        
-        
-    }
-      
-    function getList($type, $inlang,$fi=false)
-    {
-        //$l = new I18Nv2_Language($inlang);
-        //$c= new I18Nv2_Country($inlang);
-        $filter = !$fi  ? false :  $this->loadFilter($type); // project specific languages..
-       // print_r($filter);
-        
-        $ret = array();
-        
-        
-        
-        
-        foreach($this->cfg[$type] as $k) {
-            if (is_array($filter) && !in_array($k, $filter)) {
-                continue;
-            }
-             
-            $ret[] = array(
-                'code'=>$k , 
-                'title' => $this->translate($inlang, $type, $k)
-            );
-            continue;
-            
-        }
-        // sort it??
-        return $ret;
-        
-    }
      
-    
-    function findLang() {
-         
-        $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
-        $lbits = explode('_', strtoupper($lang));
-        $lbits[0] = strtolower($lbits[0]);
-        require_once 'I18Nv2/Country.php';
-        require_once 'I18Nv2/Language.php';
-        $langs = new I18Nv2_Language('en');
-        $countries = new I18Nv2_Country('en');
-      //  print_r($langs);
-        //print_R($lbits);
-        if (!isset($langs->codes[strtolower($lbits[0])])) {
-            $this->jerr('invalid lang');
-        }
-        if (!empty($lbits[1]) &&  !isset($countries->codes[$lbits[1]])) {  
-            $this->jerr('invalid lang Country component');
-            
-        }
-        return $lbits;
-    }
-    
-    function get($s)
+    function get($s ='')
     {
-        if (empty($s)) {
-            die('no type');
-        }
-        
-        $lbits = $this->findLang();
-         
         
         
-        
-        switch($s) {
-            case 'Lang': 
-                $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
-                break;
-
-            case 'Country':
-                $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
-                break;
-                
-             case 'Currency':
-                $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
-                break;
-              
+        switch ($s)
+        {
+            
             case 'BuildDB':
             // by admin only?!?
                 //DB_DataObject::debugLevel(1);
@@ -235,6 +165,8 @@ class Pman_Core_i18N extends Pman
                 break;
                   
             default: 
+                $this->outputJavascript();
+                // output javascript..
                 $this->jerr("ERROR");
         }
          
@@ -242,62 +174,66 @@ class Pman_Core_i18N extends Pman
         exit;
         
     }
-    function loadFilter($type)
+    
+    function outputJavascript()
     {
-        // this code only applies to Clipping module
-        if (!$this->authUser) {
-            return false;
-        }
-        
-        // this needs moving to it's own project
         
-        if (!$this->hasModule('Clipping')) {
-            return false;
-        }
-        if ($type == 'm') {
-            return false;
+        require_once 'I18Nv2/Country.php';
+        require_once 'I18Nv2/Language.php';
+        require_once 'I18Nv2/Currency.php';
+        
+        $langs = $this->cfg['t'];
+       // var_dump($langs);exit;
+        $ar = array();
+        foreach($langs as $lang)
+        {
+            $rlang = array_shift(explode('_', strtoupper($lang)));
+            
+            $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'))
+            );
         }
+        //echo '<PRE>';print_r($ar);
+        header('Content-type: text/javascript');
+        echo 'Pman.I18n.Data = ' .  json_encode($ar);
+        exit;
         
-        //DB_DataObject::debugLevel(1);
-        $q = DB_DataObject::factory('Projects');
         
-        $c = DB_Dataobject::factory('Companies');
-        $c->get($this->authUser->company_id);
-        if ($c->comptype !='OWNER') {
-            $q->client_id = $this->authUser->company_id;
-        }
-        $q->selectAdd();
-        $col = ($type == 'l' ? 'languages' : 'countries');
-        $q->selectAdd('distinct(' . ($type == 'l' ? 'languages' : 'countries').') as dval');
-        $q->whereAdd("LENGTH($col) > 0");
-        $q->find();
-        $ret = array();
-        $ret['**'] = 1;
-        while ($q->fetch()) {
-            $bits = explode(',', $q->dval);
-            foreach($bits as $k) {
-                $ret[$k] = true;
-            }
-        }
-        return array_keys($ret);
         
     }
-   
-     
-    function translateList($au, $type, $k)  
-    {
-        $ar = explode(',', $k);
+    function objToList($type, $obj) {
         $ret = array();
-        foreach($ar as $kk) {
-            $ret[] = $this->translate($au, $type, $kk);
+         
+         
+        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 implode(', ', $ret);
+        return $ret;
     }
+    
      /**
-     * translate
+     * translate (used by database building);
      * usage :
-     * require_once 'Pman/I18N.php';
-     * $x = new Pman_I18N();
+     * require_once 'Pman/Core/I18N.php';
+     * $x = new Pman_Core_I18N();
      * $x->translate($this->authuser, 'c', 'US');
      * @param au - auth User
      * @param type = 'c' or 'l'
@@ -360,7 +296,7 @@ class Pman_Core_i18N extends Pman
             return; // dont bother building generic..
         }
         if ($inlang === false) {
-            foreach( $this->cfg['l'] as $l) {
+            foreach( $this->cfg['t'] as $l) {
                 $this->buildDB($ltype, $l);
             }
             return;