InterfaceTranslations.php
[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/Admin/Translations.php';
21
22 class Pman_Admin_Translations extends Pman_Admin_Translations 
23 {
24     
25     var $prefix = '';
26     var $fn = '';
27     var $data = array();
28     
29     var $original = array() ; // filename => array( orig_string > orig_string)
30     var $originalKeys = array() ; // md5(filename-orig_string) => filename
31     
32     function getAuth()
33     {
34         
35         return true;
36     }
37     
38     
39     function get($module)
40     {
41         
42         if (!empty($module)) {
43             $this->init();
44             //DB_DataObject::debugLevel(1);
45             require_once 'Services/JSON.php';
46             $d = DB_DataObject::factory('translations');
47             $d->module = $module;
48             $d->selectAdd();
49             $d->selectAdd('distinct(tlang) as tlang');
50             header('Content-type: text/javascript');
51             $langs= $d->fetchAll('tlang');
52             foreach($langs as $lang) {
53                 // output the translations strings file..
54                 
55                     
56                 $this->loadOriginalStrings($module);
57                 
58                 $data = $this->loadTranslateDB($lang,$module);
59                 
60                 $j = new Services_JSON();
61                 echo "_T.{$lang}= Roo.apply( _T.{$lang} || { }, " .  $j->stringify($data, null, 4) . ");\n";
62                 
63             }
64             exit;
65             
66         }
67         
68         // load and parse json file containing all translations...
69         if (isset($_REQUEST['id'])) {
70             return $this->post();
71         }
72         if (empty($_REQUEST['lang']) || !preg_match('/^[A-Z_]+$/i', $_REQUEST['lang'])) {
73             $this->jerr("NO LANG / INVALID LANG");
74         }
75          
76         $enable = $this->modulesList();
77         
78         if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
79             $this->jerr("NO MODULE / INVALID MODULE");
80         }
81         
82         
83         $lang = $_REQUEST['lang'];
84         $module = $_REQUEST['module'];
85         
86         
87          $this->loadOriginalStrings($module); // what needs translating..
88         
89         $ff = $this->bootLoader;
90         if (empty($ff->Pman['public_cache_dir'])) {
91             $this->jerr("public_cache_dir has not been set up");
92         }
93         
94         
95         
96         $translated_data = $this->loadTranslateDB($lang, $module); // the 'database!'
97         
98         
99        // echo '<PRE>';print_R($data);exit;
100         // covert data ready to send back..
101         
102         $ret = array();
103         foreach($this->original as $k=>$ar) {
104             foreach($ar as $tr=>$trv) {
105                 // $hint = isset($hints[$tr]) ? $hints[$tr] : '';
106                 $key = md5($k.'-'.$tr);
107                 $ret[] = array(
108                     'id' => $lang.'/'.$key,
109                     'msum' => $key,
110                     'colname' => $k,
111                     'origtxt' => $tr,
112                     'txt' => isset($translated_data[$key]) ? $translated_data[$key] : '',
113                    // 'suggest' => $hint
114                 );
115                 
116             }
117         }
118         
119         
120         $this->jdata($ret);
121         
122         
123         exit;
124         
125     }
126     
127     function post() 
128     {
129          
130          
131         
132         if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $this->modulesList())) {
133             $this->jerr("NO MODULE / INVALID MODULE");
134         }
135         
136         //id    zh_HK/54e1d44609e3abed11f6e1eb6ae54988
137         //txt   é \85ç\9b®
138         list($lang,$id) = explode('/', $_REQUEST['id']);
139         
140         $this->loadOriginalStrings($_REQUEST['module']);
141         
142         $data = $this->loadTranslateDB($lang,$_REQUEST['module']);
143         
144         $data[$id] = $_REQUEST['txt'];
145         
146         if (!isset($this->originalKeys[$id])) {
147             
148             
149             $this->jerr("invalid key ?");
150         }
151         
152         $this->saveTranslateDB($lang,$_REQUEST['module'],$this->originalKeys[$id], $id, $_REQUEST['txt']);
153         
154         
155         
156         $this->writeTransMod($lang,$_REQUEST['module'], $data);
157         // write merged file..
158         //$this->prefix = '_T["'.$lang .'"]=';
159         //file_put_contents($this->fn, $this->prefix. $j->encode($this->data).';');
160         $this->jok("OK");
161     }
162     
163     /**
164      * load strings that need translating..
165      */
166     
167     function loadOriginalStrings($module)
168     {
169         // since this can handle errors better.!!?
170         $info = $this->moduleJavascriptFilesInfo($module);
171         //print_r($info);
172         $tfile =$info->basedir . '/'. $info->translation_data;
173          //var_dump($tfile);
174         if (empty($tfile) || !file_exists($tfile)) {
175             return array();
176         }
177         
178         
179         require_once 'Services/JSON.php';
180         $j = new Services_JSON();
181         $this->original = (array) $j->decode('{'. file_get_contents($tfile).'}');
182         ksort($this->original);
183
184         $this->originalKeys = array();
185         
186         // 
187         foreach($this->original as $k=>$ar) {
188             foreach($ar as $tr=>$trv) {
189                 $key = md5($k.'-'.$tr);
190                 $this->originalKeys[$key] = $k;
191             }
192         }
193         
194     }
195     
196      
197     
198     
199    
200     /***
201      *
202      * loadTranslateDB -
203      *
204      *
205      * @return key=>value list of translation_id=>tranlation.
206      *
207      *
208      */
209     
210     function loadTranslateDB($lang, $module)
211     {
212         
213         //DB_DataObject::debugLevel(1);
214         $d = DB_DataObject::factory('translations');
215         $d->module = $module;
216         $d->tlang = $lang;
217         $d->whereAdd('LENGTH(tval) > 0');
218         $ret = array();
219         
220         if ($d->count()) {
221             // since key includes file 
222             $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
223         }
224         // no data is contained in the database, we should initialize it, if we can
225         $info  = $this->moduleJavascriptFilesInfo($module);
226         $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
227         
228        
229         if (!file_exists($fn)) {
230             ///Die($fn ." does not exist?");
231             return $ret;
232         }
233         
234         
235         $default = (array) json_decode(file_get_contents($fn));
236         //echo '<PRE>';print_r($default); print_r($this->originalKeys);exit;
237         
238         
239         
240         foreach($default as $k=>$v) {
241             if (isset($ret[$k])) {
242                 continue; // skip database already holds a version of this translation.
243             }
244             // is it relivant anymore..
245             if (!isset($this->originalKeys[$k])) {
246                 continue;
247             }
248             
249             // it's current..
250             $this->saveTranslateDB($lang, $module, $this->originalKeys[$k], $k, $v);
251             $ret[$k] = $v;
252             
253             
254         }
255         return $ret;
256         
257          
258     }
259     
260     function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
261     {
262         $d = DB_DataObject::factory('translations');
263         $d->module = $module;
264         $d->tlang = $lang;
265         $d->tfile = $tfile;
266         $d->tkey = $tkey;
267         if ($d->find(true)) {
268             $d->tval = $tval;
269             $d->update();
270         }
271         $d->tval = $tval;
272         $d->insert();
273         
274     }
275     
276     
277     
278     
279     function getTransFilename($lang, $module)
280     {
281         
282         $ff = HTML_FlexyFramework::get();
283         $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
284         if (!file_exists(dirname($fn))) {
285             
286             /// create the direct
287             $oldumask = umask(0);
288             mkdir(dirname($fn), 0770, true);
289             umask($oldumask);  
290             clearstatcache();
291             if (!file_exists(dirname($fn))) {
292                 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
293             }
294             
295         }
296         return $fn;
297     }
298     
299      
300     /**
301      * Writes a file MODULE.js inside of _translations_
302      *
303      * this should contain all contents from all the language directroris for this
304      * module
305      *
306      */
307     
308     function writeTransMod($lang, $module, $data)
309     {
310         //print_R($data);
311         
312         
313         $fn = $this->getTransFilename($lang, $module);
314         require_once 'Services/JSON.php';
315         $j = new Services_JSON();
316         
317         file_put_contents($fn, $j->stringify($data, null, 4));
318         
319         $ff = HTML_FlexyFramework::get();
320         $base = $ff->rootDir.'/_translations_' ;
321         $out = '';
322         foreach(scandir($base) as $l) {
323              
324             if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
325                 continue;
326             }
327             // decode as our temp files contain spaces..
328             $jdata = json_decode(file_get_contents("$base/$l/$module.json") );
329             $out .= "_T.$l= Roo.apply( _T.$l || { }, " . json_encode($jdata) . ");\n";
330         }
331         //var_dump($out);
332         if (strlen($out)) {
333             file_put_contents("$base/$module.js", $out);
334         }
335         //$this->writeTrans($lang);
336     }
337      
338      
339     
340 }