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