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