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