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