sync
[Pman.Admin] / InterfaceTranslations.php
1 <?php
2
3
4 /**
5  * Pman_Admin_Translation:
6  * - the latest version of this....
7  *
8  * Files:
9  *   output / current state:    ROOT/_translations_/MODULE.js
10  *   input:       Pman::moduleJavascriptFilesInfo($MODULE)->translation_data
11  * 
12  * 
13  * see:
14  * Pman->modulesList()
15  *
16  * Note: at present the front end does not query this to get a list of modules..
17  * 
18  */
19
20 require_once 'Pman.php';
21
22 class Pman_Admin_InterfaceTranslations extends Pman 
23 {
24      var $prefix = '';
25     var $fn = '';
26     var $data = array();
27     
28     var $original = array() ; // filename => array( orig_string > orig_string)
29     var $originalKeys = array() ; // md5(filename-orig_string) => filename
30      
31     function getAuth()
32     {
33         
34         return true;
35     }
36     
37     
38     function get($module, $opts = Array())
39     {
40         
41         if (empty($module)) {
42             $this->jerr("no module selected");
43         }
44         $this->init();
45          //DB_DataObject::debugLevel(1);
46         require_once 'Services/JSON.php';
47         $d = DB_DataObject::factory('translations');
48         $d->module = $module;
49         $d->selectAdd();
50         $d->selectAdd('distinct(tlang) as tlang');
51         header('Content-type: text/javascript');
52         $langs= $d->fetchAll('tlang');
53         foreach($langs as $lang) {
54             // output the translations strings file..
55             
56                 
57             $this->loadOriginalStrings($module);
58             
59             $data = $this->loadTranslateDB($lang,$module);
60             
61             $j = new Services_JSON();
62             echo "_T = _T || {} ; _T.{$lang}= Roo.apply( _T.{$lang} || { }, " .  $j->stringify($data, null, 4) . ");\n";
63             
64         }
65         exit;
66                
67     }
68     
69     function post($v) 
70     {
71         $this->jerr("invalid url"); 
72      
73     }
74     
75      /**
76      * load strings that need translating..
77      */
78     
79     function loadOriginalStrings($module)
80     {
81         // since this can handle errors better.!!?
82         $info = $this->moduleJavascriptFilesInfo($module);
83         //print_r($info);
84         
85         
86         
87         $this->original  = array();
88         $tfile = $info->basedir . '/'. $info->translation_data;
89          //var_dump($tfile);
90         //if (empty($tfile) || !file_exists($tfile)) {
91             
92         foreach($info->filesmtime as $f =>$mt) {
93             $bjs = preg_replace('/\.js$/','.bjs', $f);
94             if (!file_exists($bjs)) {
95                 continue;
96             }
97             $jd = json_decode(file_get_contents($bjs));
98             if (empty($jd->strings)) {
99                 continue;
100             }
101             $this->original[str_replace('.bjs', '', basename($bjs)) ] = array_flip((array)$jd->strings);
102         }
103          
104         file_put_contents($tfile, json_encode($this->original));
105         
106             
107         //}
108         
109         
110         //print_R($this->original);exit;
111         
112         
113         $this->original = (array) json_decode( file_get_contents($tfile) );
114         ksort($this->original);
115
116         $this->originalKeys = array();
117         
118         // 
119         foreach($this->original as $k=>$ar) {
120             foreach($ar as $tr=>$trv) {
121                 $key = md5($k.'-'.$tr);
122                 $this->originalKeys[$key] = $k;
123             }
124         }
125         
126     }
127     
128     /***
129      *
130      * loadTranslateDB -
131      *
132      *
133      * @return key=>value list of translation_id=>tranlation.
134      *
135      *
136      */
137     
138     function loadTranslateDB($lang, $module)
139     {
140         
141         //DB_DataObject::debugLevel(1);
142         $d = DB_DataObject::factory('translations');
143         $d->module = $module;
144         $d->tlang = $lang;
145         $d->whereAdd('LENGTH(tval) > 0');
146         $ret = array();
147         
148         if ($d->count()) {
149             // since key includes file 
150             $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
151         }
152         // no data is contained in the database, we should initialize it, if we can
153         $info  = $this->moduleJavascriptFilesInfo($module);
154         $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
155         
156        
157         if (!file_exists($fn)) {
158             ///Die($fn ." does not exist?");
159             return $ret;
160         }
161         
162         
163         $default = (array) json_decode(file_get_contents($fn));
164         //echo '<PRE>';print_r($default); print_r($this->originalKeys);exit;
165         
166         
167         
168         foreach($default as $k=>$v) {
169             if (isset($ret[$k])) {
170                 continue; // skip database already holds a version of this translation.
171             }
172             // is it relivant anymore..
173             if (!isset($this->originalKeys[$k])) {
174                 continue;
175             }
176             
177             // it's current..
178             $this->saveTranslateDB($lang, $module, $this->originalKeys[$k], $k, $v);
179             $ret[$k] = $v;
180             
181             
182         }
183         return $ret;
184         
185          
186     }
187     
188     
189 }