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