DataObjects/Core_event_audit.php
[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 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     );
56     /**
57      * initalizie the cfg aray
58      *
59      */
60     function cfg()
61     {
62         static $loaded  = false;
63         if ($loaded) {
64             return self::$cfg;
65         }
66         $loaded =true;
67         $ff= HTML_FlexyFramework::get();
68          
69         // since our opts array changed alot..
70         $opts = empty($ff->Pman_Core_I18N) ? (empty($ff->Pman_I18N) ? array() : $ff->Pman_I18N)  : $ff->Pman_Core_I18N;
71         
72         $i = DB_DataObject::Factory('I18n');
73         // load the cofiguration
74         foreach($opts as $k=>$v) {
75             
76             if ($v == '*') { // everything..
77                 self::$cfg[$k] = $i->availableCodes($k);
78                 continue;
79             }
80             self::$cfg[$k] = is_array($v) ? $v  : explode(',', $v);
81         }
82         return self::$cfg;
83         
84         
85     }
86     
87     
88       // the default configuration.
89     
90     function applyFilters($q, $au)
91     {
92         
93         //DB_DataObject::debugLevel(1);
94         if (!empty($q['query']['_with_en'])) {
95             
96             $this->buildDB(); // ensure we have the full database...
97             
98             $this->selectAdd("
99                 i18n_translate(ltype, lkey, 'en') as lval_en
100                 
101             ");
102         }
103     }
104     
105     
106     function translate($inlang,$ltype,$kval)
107     {
108         
109         $x = DB_DataObject::factory('i18n');
110         $x->ltype = $ltype;
111         $x->inlang= $inlang;
112         $x->kval = $kval;
113         $x->limit(1);
114         $x->find(true);
115         return $x->lval;
116         
117     }
118     
119     
120     
121     function toTransList($ltype, $inlang)
122     {
123         
124         $x = DB_DataObject::factory('i18n');
125         $x->ltype = $ltype;
126         $x->inlang= $inlang;
127         $x->selectAdd();
128         $x->selectAdd('lkey as code, lval as title');
129         $x->find();
130         $ret = array();
131         while ($x->fetch()) {
132             $ret[] = array(
133                 'code' => $x->code,
134                 'title' =>$x->title);
135         }
136         return $ret;
137     }
138      
139     
140     
141     
142     // -------------- code to handle importing into database..
143     
144     
145     
146     
147     // returns a list of all countries/languages etc.. (with '*')
148     function availableCodes($t)
149     {
150         $ret = array();
151         switch ($t) {
152             case 'c':
153                 require_once 'I18Nv2/Country.php';
154                 
155                 $c = new I18Nv2_Country('en');
156                 $ret =  array_keys($c->codes);
157                 $ret[] = '**';
158                 break;
159             case 'l':
160                 require_once 'I18Nv2/Language.php';
161                 $c = new I18Nv2_Language('en');
162                 $ret =  array_keys($c->codes);
163                 $ret[] = '**';
164                 break;
165             case 'm':
166                 require_once 'I18Nv2/Currency.php';
167                 $c = new I18Nv2_Currency('en');
168                 $ret =  array_keys($c->codes);
169                 $ret[] = '**';
170                 break;
171         }
172         
173         foreach ($ret as $k=>$v) {
174             $ret[$k] = strtoupper($v);
175         }
176         
177         
178         return $ret;
179     }
180     
181     
182     function buildDB($ltype= false, $inlang= false )
183     {
184         $this->cfg();
185         if ($ltype === false) {
186             // trigger all builds.
187             //DB_DataObject::debugLevel(1);
188             $this->buildDB('c');
189             $this->buildDB('l');
190             $this->buildDB('m');
191             return;
192         }
193         if ($inlang == '**') {
194             return; // dont bother building generic..
195         }
196         
197         
198         if ($inlang === false) {
199             // do we want to add our 'configured ones..'
200             // We only build translatiosn for our configured ones..
201             //foreach( $this->availableCodes('l') as $l) {
202                 
203             foreach( self::$cfg['t'] as $l) {
204                 $this->buildDB($ltype, $l);
205             }
206             return;
207         }
208         
209         
210         //DB_DataObject::debugLevel(1);
211         $x = DB_DataObject::factory('i18n');
212         $x->inlang= $inlang;
213         $x->ltype = $ltype;
214         
215         $complete = $x->fetchAll('lkey');
216         
217         $list =  $this->availableCodes($ltype);
218         
219         
220         foreach($list as $lkey) {
221             // skip ones we know we have done...
222             if (in_array($lkey, $complete)) {
223                 continue;
224             }
225             $x = DB_DataObject::factory('i18n');
226             $x->ltype = $ltype;
227             $x->lkey = $lkey;
228             $x->inlang= $inlang;
229             if ($x->find(true)) {
230                 $xx= clone($x);
231                 $x->lval = $this->defaultTranslate($inlang, $ltype, $lkey);
232                 $x->update($xx);
233                 continue;
234             }
235             $x->lval = $this->defaultTranslate($inlang, $ltype, $lkey);
236             $x->insert();
237             
238         }
239          
240         
241     }
242     
243     /**
244      * default translate  - use i18n classes to provide a value.
245      *
246      * 
247      */
248      
249     function defaultTranslate($lang, $type, $k) 
250     {
251       
252         static $cache;
253         
254         if (empty($k)) {
255             return '??';
256         }
257
258         $lbits = explode('_', strtoupper($lang));
259         $lang = $lbits[0];
260         
261         if (!isset($cache[$lang])) {
262             require_once 'I18Nv2/Country.php';
263             require_once 'I18Nv2/Language.php';
264             require_once 'I18Nv2/Currency.php';
265             $cache[$lang] = array(
266                 'l' =>  new I18Nv2_Language($lang, 'UTF-8'),
267                 'c' => new I18Nv2_Country($lang, 'UTF-8'),
268                 'm' => new I18Nv2_Currency($lang, 'UTF-8')
269             );
270             //echo '<PRE>';print_r(array($lang, $cache[$lang]['c']));
271         }
272         
273         if ($k == '**') {
274             return 'Other / Unknown';
275         }
276     
277         
278         if ($type == 'l') {
279             $tolang = explode('_', $k);
280          
281             $ret = $cache[$lang][$type]->getName($tolang[0]);
282             if (count($tolang) > 1) {
283                 $ret.= '('.$tolang[1].')'; 
284             }
285             return $ret;
286         }
287         $ret = $cache[$lang][$type]->getName($k);
288         //print_r(array($k, $ret));
289         return $ret;
290         
291         
292     }
293     
294     
295 }