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