Fix #5881 - language list and names
[Pman.Core] / DataObjects / I18n.php
1 <?php
2 /**
3  * Table Definition for i18n
4  *
5  * This is heavily related to the Pman_I18n implementation..
6  *
7  * It should eventually replace most of that..
8  * 
9  */
10 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
11
12 class Pman_Core_DataObjects_I18n extends DB_DataObject 
13 {
14     ###START_AUTOCODE
15     /* the code below is auto generated do not remove the above tag */
16
17     public $__table = 'i18n';                            // table name
18     public $id;                              // int(11)  not_null primary_key auto_increment
19     public $ltype;                           // string(1)  not_null multiple_key
20     public $lkey;                            // string(8)  not_null
21     public $inlang;                          // string(8)  not_null
22     public $lval;                            // string(64)  not_null
23
24     
25     /* the code above is auto generated do not remove the tag below */
26     ###END_AUTOCODE
27     
28     // we have a small default set of languages available..
29     // you can modify this by making this setting in the index.php loader.
30     // Pman_Core_i18n = array( 'c' => *, 'l' => '*', 'm' => '*')
31     
32     static $cfg = array(
33         // translated versions availalable
34         
35         't' => array(
36             'en', 'zh_CN',   'zh_HK', 
37         ),
38         // languages available
39         'l' => array(
40             
41             'en', 'zh_CN',   'zh_HK',  'zh_TW', //'th', 'ko', 'ja', 'ms', 
42             //'id', // indonesian
43            // 'tl', // tagalog
44            // 'vi', //vietnamise
45           //  'hi', // hindi
46           //  'ta', // tamil
47           //  '**', // other
48         ),
49         'c' => '*', // array(
50              //'AU', 'CN', 'HK', 'IN', 'ID', 'JP', 'MY', 'NZ', 'TW', 'SG', 'TH', 'KR', 'US', 'PH', 'VN','**'
51         //),
52         'm' => array(
53             'USD', 'HKD', 'GBP', 'CNY', 'SGD', 'JPY'
54         ),
55         'p' => '*',
56         'add_l'=> array(), // key -> value additional languages... 
57         'add_c'=> array(), // additional countries...(eg. '-R' => 'Regional' )
58         'add_m'=> array(), // additional currencies...
59         'add_p'=> array(), // additional currencies...
60
61         
62     );
63     /**
64      * initalizie the cfg aray
65      *
66      */
67     function cfg()
68     {
69         static $loaded  = false;
70         if ($loaded) {
71             return self::$cfg;
72         }
73          
74         $loaded =true;
75         $ff= HTML_FlexyFramework::get();
76         
77         // BC compatible.. if any of these are set, then we use them as the settings..
78         $opts = array();
79         foreach(array('Pman_Core_I18n', 'Pman_Core_I18N', 'Pman_I18N','Pman_I18n') as $pk) {
80             if (isset($ff->$pk)) {
81                 //var_dump($pk);
82                 $opts =  $ff->$pk;
83                 break;
84             }
85         }
86         //echo '<PRE>';print_R($opts);//exit;
87         
88          
89         // var_dump($opts);exit;
90         
91         $i = DB_DataObject::Factory('I18n');
92         // load the cofiguration
93         foreach($opts as $k=>$v) {
94             
95             if ($v == '*') { // everything..
96                // self::$cfg[$k] = $i->availableCodes($k, false);
97                 continue;
98             }
99              
100             self::$cfg[$k] = is_array($v) ? $v  : explode(',', $v);
101         }
102         // available codes recursively calls this... -- so the above has to be set first..
103         foreach($opts as $k=>$v) {
104             
105             if ($v == '*') { // everything..
106                 self::$cfg[$k] = '*'; //$i->availableCodes($k, false);
107                 continue;
108             }
109            
110         }
111         
112         
113         return self::$cfg;
114         
115         
116     }
117     
118     
119       // the default configuration.
120     
121     function applyFilters($q, $au)
122     {
123         $this->buildDB();
124         //DB_DataObject::debugLevel(1);
125         if (!empty($q['query']['_with_en'])) {
126             
127             $this->buildDB(); // ensure we have the full database...
128             
129             $this->selectAdd("
130                 id as id,
131                 i18n_translate(ltype, lkey, 'en') as lval_en
132             ");
133         }
134         
135         if (!empty($q['_as_code_and_title'])) {
136             $tn = $this->tableName();
137             
138             $this->selectAdd();
139             $this->selectAdd("
140                     {$tn}.lval as title,
141                     {$tn}.lkey as code
142             ");
143             $this->is_active = 1;
144             if (!empty($q['_title'])) {
145                 $this->whereAdd("{$tn}.lval like '{$this->escape($_REQUEST['_title'])}%'");
146             }
147         }
148          
149         if (!empty($q['!code'])) {
150             $this->whereAddIn('!lkey', explode(',', $q['!code']), 'string'); 
151         }
152         if (!empty($q['query']['name'])) {
153             //DB_DAtaObject::debugLevel(1);
154             $v = strtoupper($this->escape($q['query']['name']));
155             $this->whereAdd("upper(lval) LIKE '%{$v}%'");
156         }
157         
158          if (!empty($q['query']['name_starts'])) {
159             $this->whereAdd("lval LIKE '". $this->escape($q['query']['name_starts']). "%'");
160         }
161         
162         if (!empty($q['_filtered']) && !empty($this->ltype)) {
163             $cfg = $this->cfg();
164             $filter = $cfg[$this->ltype];
165             if(is_array($filter)){
166                 $this->whereAddIn('lkey', $filter, 'string'); 
167             }
168             
169         }
170         
171         if(!empty($q['_with_geoip_count'])) {
172             
173             $this->selectAdd("
174                 (
175                     SELECT
176                             COUNT(geoip_division.id)
177                     FROM
178                             geoip_division
179                     WHERE
180                             geoip_division.country = i18n.lkey
181                 ) AS no_of_division,
182                 (
183                     SELECT
184                             COUNT(geoip_city.id)
185                     FROM
186                             geoip_city
187                     WHERE
188                             geoip_city.country = i18n.lkey
189                 ) AS no_of_city
190             ");
191         }
192         
193         if(!empty($q['_hide_unused'])) {
194             $this->whereAdd("
195                 (
196                     SELECT
197                             COUNT(geoip_division.id)
198                     FROM
199                             geoip_division
200                     WHERE
201                             geoip_division.country = i18n.lkey
202                 ) > 0
203             ");
204         }
205     }
206     
207     function lookupCode($inlang,$ltype,$name)
208     {
209         $x = DB_DataObject::factory('i18n');
210         $x->ltype = $ltype;
211         $x->lval = $name;
212         $x->inlang= $inlang;
213         
214         $x->limit(1);
215         if ($x->find(true) && !empty($x->lkey)) {
216             return $x->lkey;
217         }
218         return '';
219         
220         
221     }
222     
223     
224     function translate($inlang,$ltype,$kval)
225     {
226         
227         $x = DB_DataObject::factory('i18n');
228         $x->ltype = $ltype;
229         $x->lkey = $kval;
230         $x->inlang= $inlang;
231         $fallback = clone($x);
232         
233         $x->limit(1);
234         if ($x->find(true) && !empty($x->lval)) {
235             return $x->lval;
236         }
237         $fallback->inlang = 'en';
238         if ($fallback->find(true) && !empty($fallback->lval)) {
239            return $fallback->lval;
240         }
241         return $kval;
242     }
243     
244     
245     
246     function toTransList($ltype, $inlang)
247     {
248         
249         
250         $this->ltype = $ltype;
251         $this->inlang= $inlang;
252         $this->selectAdd();
253         $this->selectAdd('lkey as code, lval as title');
254         
255         $this->find();
256         $ret = array();
257         while ($this->fetch()) {
258             $ret[] = array(
259                 'code'  => $this->code,
260                 'title' => $this->title
261             );
262         }
263         return $ret;
264     }
265      
266     
267     
268     
269     // -------------- code to handle importing into database..
270     
271     
272     
273     
274     // returns a list of all countries/languages etc.. (with '*')
275     function availableCodes($t, $filtered = true)
276     {
277         $ret = array();
278         $cfg = $this->cfg();
279         //echo '<PRE>';print_r($cfg);
280         switch ($t) {
281             case 'c':
282                 require_once 'I18Nv2/Country.php';
283                 
284                 $c = new I18Nv2_Country('en');
285                 $ret =  array_keys($c->codes);
286                 if (!empty($cfg['add_c'])) {
287                     $ret = array_merge($ret, array_keys($cfg['add_c']));
288                 }
289                 
290                 
291                  
292                 
293                 $ret[] = '**';
294                 //echo '<PRE>';print_R($cfg); print_r($ret); exit;
295                 break;
296             
297             case 'l':
298                 require_once 'I18Nv2/Language.php';
299                 $c = new I18Nv2_Language('en');
300                 $ret =  array_keys($c->codes); // we need to make sure these are lowercase!!!
301                 
302                 
303                 foreach ($cfg['add_l'] as $k=>$v){
304                     // make sure that add_l is formated correctly.. (lower_UPPER?)
305                     $tolang = explode('_', $k);
306                     $tolang[0] = strtolower($tolang[0]);
307                     $tolang = implode('_', $tolang);
308                     
309                     unset($cfg['add_l'][$k]); // if they match..unset first.. then set
310                     
311                     $cfg['add_l'][$tolang] = $v;
312                 }
313                 if (!empty($cfg['add_l'])) {
314                     $ret = array_merge($ret, array_keys($cfg['add_l']));
315                 }
316
317                 $ret[] = '**';
318                 break;
319             case 'm':
320                 require_once 'I18Nv2/Currency.php';
321                 $c = new I18Nv2_Currency('en');
322                 $ret =  array_keys($c->codes);
323                 if (!empty($cfg['add_m'])) {
324                     $ret = array_merge($ret, array_keys($cfg['add_m']));
325                 }
326                 $ret[] = '**';
327                 break;
328             case 'p':
329                 require_once 'I18Nv2/PhonePrefix.php';
330                 $c = new I18Nv2_PhonePrefix('en');
331                 $ret =  array_keys($c->codes);
332                 if (!empty($cfg['add_p'])) {
333                     $ret = array_merge($ret, array_keys($cfg['add_p']));
334                 }
335                 $ret[] = '**';
336                 break;
337         }
338         
339         
340         
341         if ($filtered  && !empty($cfg[$t]) && is_array($cfg[$t])) {
342             // then there is a filter. - we should include all of them, even if they are not relivatn??
343             return $cfg[$t]; //array_intersect($cfg[$t], $ret);
344             
345         }
346         
347         // why upper case everyting?!?!?
348         
349         //foreach ($ret as $k=>$v) {
350         //    $ret[$k] = ($t=='l') ? $ret[$k] : strtoupper($v);
351         //}
352
353         return $ret;
354     }
355     
356     
357     function buildDB($ltype= false, $inlang= false )
358     {
359         $cfg = $this->cfg();
360         
361         if ($ltype === false) {
362             // trigger all builds.
363             //DB_DataObject::debugLevel(1);
364             $this->buildDB('c');
365             $this->buildDB('l');
366             $this->buildDB('m');
367             $this->buildDB('p', 'en');
368             return;
369         }
370         
371         if ($inlang == '**') {
372             return; // dont bother building generic..
373         }
374         
375         
376         if ($inlang === false) {
377             // do we want to add our 'configured ones..'
378             // We only build translatiosn for our configured ones..
379             //foreach( $this->availableCodes('l') as $l) {
380                 
381             foreach( $cfg['t'] as $l) {
382                 $this->buildDB($ltype, $l);
383             }
384             return;
385         }
386         
387         
388         //DB_DataObject::debugLevel(1);
389         $x = DB_DataObject::factory('I18n');
390         $x->inlang= $inlang;
391         $x->ltype = $ltype;
392         
393         $complete = $x->fetchAll('lkey');
394         
395         $list =  $this->availableCodes($ltype);
396         
397         foreach($list as $lkey) {
398             // skip ones we know we have done...
399             if (in_array($lkey, $complete)) {
400                 continue;
401             }
402             if (empty($lkey)) { // not sure why we get empty values here.
403                 continue;
404             }
405             $x = DB_DataObject::factory('I18n');
406             $x->ltype = $ltype;
407             $x->lkey = $lkey;  
408             $x->inlang= $inlang;
409             if ($x->find(true)) {
410                 $xx= clone($x);
411                 $x->lval = $this->defaultTranslate($inlang, $ltype, $lkey);
412                 $x->update($xx);
413                 continue;
414             }
415             $x->lval = $this->defaultTranslate($inlang, $ltype, $lkey);
416             $x->insert();
417             
418         }
419          
420         
421     }
422     
423     /**
424      * default translate  - use i18n classes to provide a value.
425      *
426      * 
427      */
428      
429     function defaultTranslate($lang, $type, $k) 
430     {
431       
432         static $cache;
433         
434         
435         $cfg = $this->cfg();
436         if (empty($k)) {
437             return '??';
438         }
439         
440         //$lbits = explode('_', strtoupper($lang));
441         $lbits = explode('_', $lang);
442         $orig_lang = $lang;
443         $lang = $lbits[0];
444         
445         if (!isset($cache[$lang])) {
446             require_once 'I18Nv2/Country.php';
447             require_once 'I18Nv2/Language.php';
448             require_once 'I18Nv2/Currency.php';
449             require_once 'I18Nv2/PhonePrefix.php';
450             $cache[$lang] = array(
451                 'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
452                 'c' => new I18Nv2_Country($lang, 'UTF-8'),
453                 'm' => new I18Nv2_Currency($lang, 'UTF-8'),
454                 'p' => new I18Nv2_PhonePrefix($lang, 'UTF-8')
455             );
456             //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
457         }
458         
459         if ($k == '**') {
460             return 'Other / Unknown';
461         }
462         
463         
464         // for languages if we get zh_HK then we write out Chinese ( HK )
465         
466         
467         if ($type == 'l' && strpos($k, '_') > -1) {
468             $tolang = explode('_', $k);
469             $ret = $cache[$lang][$type]->getName(strtolower($tolang[0])) .  '('.$tolang[1].')'; 
470             
471         } else {
472             $ret = $cache[$lang][$type]->getName($k);
473         }
474         
475         if ($orig_lang == 'zh_HK' || $orig_lang == 'zh_TW' ) {
476             // then translation is by default in simplified.
477             //print_r($ret);
478             $ret = @iconv("UTF-8", "GB2312//IGNORE", $ret);
479             //print_r($ret);
480             $ret = @iconv("GB2312", "BIG5//IGNORE", $ret);
481             //print_r($ret);
482             
483             $ret = @iconv("BIG5", "UTF-8//IGNORE", $ret);
484             //print_r($ret);
485          }
486         
487         
488         
489         // our wierd countries/langs etc..
490         if (isset($cfg['add_' . $type][$k])) {
491             return $cfg['add_' . $type][$k];
492             
493         }
494         
495         return $ret;
496         
497         
498     }
499     
500     
501 }