Pman/I18N.php
[Pman.Base] / Pman / I18N.php
1 <?php
2
3 /// provide language data!!!
4 // THIS IS PUBLICALLY ACCESSABLE..
5 /**
6  * 
7  * Either we load this as a standard array at start??
8  * eg. after login...
9  * 
10  * or we treat it like a regular pulldown... - on demand...
11  * 
12  * big snag is that we can not include these until the user has specified their interface language.
13  * 
14  * If we do a global jS array?
15  * Pman.Lang.en = [ 'en' , 'English']
16  * 
17  * If we do a standard 'store' version..
18  * { total:  x, data [ { code: 'en', title: 'English' }] }
19  * 
20  */
21
22 require_once 'Pman.php';
23 class Pman_I18N extends Pman
24 {
25     
26     // these are the languages we support.
27     var $cfg = array(
28         'l' => array(
29             'en', 'zh_CN',   'zh_HK',  'zh_TW', 'th', 'ko', 'ja', 'ms', 
30             'id', // indonesian
31             'tl', // tagalog
32             'vi', //vietnamise
33             'hi', // hindi
34             'ta', // tamil
35             '**', // other
36         ),
37         'c' => array(
38              'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
39         ),
40         'm' => array(
41             'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
42         )
43     );
44     
45     
46      
47     
48     
49     function getAuth()
50     {
51         parent::getAuth(); // load company!
52         //return true;
53         $au = $this->getAuthUser();
54         //if (!$au) {
55         //    $this->jerr("Not authenticated", array('authFailure' => true));
56         //}
57         $this->authUser = $au;
58         
59         $opts = PEAR::getStaticProperty('Pman_I18N', 'options');
60         $opts = empty($opts)  ?  array() : $opts;
61         
62         
63         foreach($opts as $k=>$v) {
64             
65             if ($v == '*') {
66                 $this->cfg[$k] = $this->getDefaultCfg($k);
67                 continue;
68             }
69             $this->cfg[$k] = explode(',', $v);
70         }
71         
72         
73         
74         
75         return true;
76     }
77     // returns a list of all countries..
78     function getDefaultCfg($t) {
79         $ret = array();
80         switch ($t) {
81             case 'c':
82                 require_once 'I18Nv2/Country.php';
83                 
84                 $c = new I18Nv2_Country('en');
85                 $ret =  array_keys($c->codes);
86                 break;
87             case 'l':
88                 require_once 'I18Nv2/Language.php';
89                 $c = new I18Nv2_Language('en');
90                 $ret =  array_keys($c->codes);
91                 break;
92             case 'm':
93                 require_once 'I18Nv2/Currency.php';
94                 $c = new I18Nv2_Currency('en');
95                 $ret =  array_keys($c->codes);
96                 break;
97         }
98         foreach ($ret as $k=>$v) {
99             $ret[$k] = strtoupper($v);
100         }
101         
102         
103         return $ret;
104     }
105     
106     
107     
108     function setSession($au)
109     {
110         $this->authUser = $au;
111         $lbits = implode('_', $this->findLang());
112         if (empty($_SESSION['Pman_I18N'])) {
113             $_SESSION['Pman_I18N']  = array();
114         }
115         
116         $_SESSION['Pman_I18N'][$lbits] = array(
117             'l' => $this->getList('l', $lbits),
118             'c' => $this->getList('c', $lbits),
119             'm' => $this->getList('m', $lbits),
120         );
121         
122         
123     }
124       
125     function getList($type, $inlang,$fi=false)
126     {
127         //$l = new I18Nv2_Language($inlang);
128         //$c= new I18Nv2_Country($inlang);
129         $filter = !$fi  ? false :  $this->loadFilter($type); // project specific languages..
130        // print_r($filter);
131         
132         $ret = array();
133         
134         
135         
136         
137         foreach($this->cfg[$type] as $k) {
138             if (is_array($filter) && !in_array($k, $filter)) {
139                 continue;
140             }
141              
142             $ret[] = array(
143                 'code'=>$k , 
144                 'title' => $this->translate($inlang, $type, $k)
145             );
146             continue;
147             
148         }
149         // sort it??
150         return $ret;
151         
152     }
153      
154     
155     function findLang() {
156          
157         $lang = !$this->authUser || empty($this->authUser->lang ) ? 'en' : $this->authUser->lang;
158         $lbits = explode('_', strtoupper($lang));
159         $lbits[0] = strtolower($lbits[0]);
160         require_once 'I18Nv2/Country.php';
161         require_once 'I18Nv2/Language.php';
162         $langs = new I18Nv2_Language('en');
163         $countries = new I18Nv2_Country('en');
164       //  print_r($langs);
165         //print_R($lbits);
166         if (!isset($langs->codes[strtolower($lbits[0])])) {
167             $this->jerr('invalid lang');
168         }
169         if (!empty($lbits[1]) &&  !isset($countries->codes[$lbits[1]])) {  
170             $this->jerr('invalid lang Country component');
171             
172         }
173         return $lbits;
174     }
175     
176     function get($s)
177     {
178         if (empty($s)) {
179             die('no type');
180         }
181         
182         $lbits = $this->findLang();
183          
184         
185         
186         
187         switch($s) {
188             case 'Lang': 
189                 $ret = $this->getList('l', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
190                 break;
191
192             case 'Country':
193                 $ret = $this->getList('c', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
194                 break;
195                 
196              case 'Currency':
197                 $ret = $this->getList('m', $lbits[0],empty($_REQUEST['filter']) ? false : $_REQUEST['filter']);
198                 break;
199               
200             case 'BuildDB':
201             // by admin only?!?
202                 //DB_DataObject::debugLevel(1);
203                 $this->buildDb('l');
204                 $this->buildDb('c');
205                 $this->buildDb('m');
206                 die("DONE!");
207                 break;
208                   
209             default: 
210                 $this->jerr("ERROR");
211         }
212          
213         $this->jdata($ret);
214         exit;
215         
216     }
217     function loadFilter($type)
218     {
219         // this code only applies to Clipping module
220         if (!$this->authUser) {
221             return false;
222         }
223         
224         // this needs moving to it's own project
225         
226         if (!$this->hasModule('Clipping')) {
227             return false;
228         }
229         if ($type == 'm') {
230             return false;
231         }
232         
233         //DB_DataObject::debugLevel(1);
234         $q = DB_DataObject::factory('Projects');
235         
236         $c = DB_Dataobject::factory('Companies');
237         $c->get($this->authUser->company_id);
238         if ($c->comptype !='OWNER') {
239             $q->client_id = $this->authUser->company_id;
240         }
241         $q->selectAdd();
242         $col = ($type == 'l' ? 'languages' : 'countries');
243         $q->selectAdd('distinct(' . ($type == 'l' ? 'languages' : 'countries').') as dval');
244         $q->whereAdd("LENGTH($col) > 0");
245         $q->find();
246         $ret = array();
247         $ret['**'] = 1;
248         while ($q->fetch()) {
249             $bits = explode(',', $q->dval);
250             foreach($bits as $k) {
251                 $ret[$k] = true;
252             }
253         }
254         return array_keys($ret);
255         
256     }
257    
258      
259     function translateList($au, $type, $k)  
260     {
261         $ar = explode(',', $k);
262         $ret = array();
263         foreach($ar as $kk) {
264             $ret[] = $this->translate($au, $type, $kk);
265         }
266         return implode(', ', $ret);
267     }
268      /**
269      * translate
270      * usage :
271      * require_once 'Pman/I18N.php';
272      * $x = new Pman_I18N();
273      * $x->translate($this->authuser, 'c', 'US');
274      * @param au - auth User
275      * @param type = 'c' or 'l'
276      * @param k - key to translate
277      * 
278      */
279      
280     function translate($au, $type, $k) 
281     {
282       
283         static $cache;
284         if (empty($k)) {
285             return '??';
286         }
287         $lang = !$au || empty($au->lang ) ? 'en' : is_string($au) ? $au : $au->lang;
288         $lbits = explode('_', strtoupper($lang));
289         $lang = $lbits[0];
290         
291         if (!isset($cache[$lang])) {
292             require_once 'I18Nv2/Country.php';
293             require_once 'I18Nv2/Language.php';
294             require_once 'I18Nv2/Currency.php';
295             $cache[$lang] = array(
296                 'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
297                 'c' => new I18Nv2_Country($lang, 'UTF-8'),
298                 'm' => new I18Nv2_Currency($lang, 'UTF-8')
299             );
300             //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
301         }
302         if ($k == '**') {
303             return 'Other / Unknown';
304         }
305     
306         
307         if ($type == 'l') {
308             $tolang = explode('_', $k);
309          
310             $ret = $cache[$lang][$type]->getName($tolang[0]);
311             if (count($tolang) > 1) {
312                 $ret.= '('.$tolang[1].')'; 
313             }
314             return $ret;
315         }
316         $ret = $cache[$lang][$type]->getName($k);
317         //print_r(array($k, $ret));
318         return $ret;
319         
320         
321     }
322     
323     
324     
325     function buildDB($ltype= false, $inlang= false )
326     {
327         if ($ltype === false) {
328             
329             die("OOPS NO LTYPE");
330         }
331         
332         if ($inlang === false) {
333             foreach( $this->cfg['l'] as $l) {
334                 $this->buildDB($ltype, $l);
335             }
336             return;
337         }
338         
339         $list = $this->cfg[$ltype];
340         
341         foreach($list as $lkey) {
342             $x = DB_DataObject::factory('i18n');
343             $x->ltype = $ltype;
344             $x->lkey = $lkey;
345             $x->inlang= $inlang;
346             if ($x->find(true)) {
347                 $xx= clone($x);
348                 $x->lval = $this->translate($inlang, $ltype, $lkey);
349                 $x->update($xx);
350                 continue;
351             }
352             $x->lval = $this->translate($inlang, $ltype, $lkey);
353             $x->insert();
354             
355         }
356         
357         
358         
359         
360     }
361     
362 }