Pman.Tab.AdminCompanies.bjs
[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($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          
107         
108         if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $this->modulesList())) {
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($_REQUEST['module']);
117         
118         $data = $this->loadTranslateDB($lang,$_REQUEST['module']);
119         
120         $data[$id] = $_REQUEST['txt'];
121         
122         if (!isset($this->originalKeys[$id])) {
123             
124             
125             $this->jerr("invalid key ?");
126         }
127         
128         $this->saveTranslateDB($lang,$_REQUEST['module'],$this->originalKeys[$id], $id, $_REQUEST['txt']);
129         
130         
131         
132         $this->writeTransMod($lang,$_REQUEST['module'], $data);
133         // write merged file..
134         //$this->prefix = '_T["'.$lang .'"]=';
135         //file_put_contents($this->fn, $this->prefix. $j->encode($this->data).';');
136         $this->jok("OK");
137     }
138     
139     /**
140      * load strings that need translating..
141      */
142     
143     function loadOriginalStrings($module)
144     {
145         // since this can handle errors better.!!?
146         $info = $this->moduleJavascriptFilesInfo($module);
147         //print_r($info);
148         $tfile =$info->basedir . '/'. $info->translation_data;
149          //var_dump($tfile);
150         if (empty($tfile) || !file_exists($tfile)) {
151             return array();
152         }
153         
154         
155         require_once 'Services/JSON.php';
156         $j = new Services_JSON();
157         $this->original = (array) $j->decode('{'. file_get_contents($tfile).'}');
158         ksort($this->original);
159
160         $this->originalKeys = array();
161         
162         // 
163         foreach($this->original as $k=>$ar) {
164             foreach($ar as $tr=>$trv) {
165                 $key = md5($k.'-'.$tr);
166                 $this->originalKeys[$key] = $k;
167             }
168         }
169         
170     }
171     
172      
173     
174     
175    
176     /***
177      *
178      * loadTranslateDB -
179      *
180      *
181      * @return key=>value list of translation_id=>tranlation.
182      *
183      *
184      */
185     
186     function loadTranslateDB($lang, $module)
187     {
188         
189         //DB_DataObject::debugLevel(1);
190         $d = DB_DataObject::factory('translations');
191         $d->module = $module;
192         $d->tlang = $lang;
193         
194         $ret = array();
195         
196         if ($d->count()) {
197             // since key includes file 
198             $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
199         }
200         // no data is contained in the database, we should initialize it, if we can
201         $info  = $this->moduleJavascriptFilesInfo($module);
202         $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
203         
204        
205         if (!file_exists($fn)) {
206             ///Die($fn ." does not exist?");
207             return $ret;
208         }
209         
210         
211         $default = (array) json_decode(file_get_contents($fn));
212         //echo '<PRE>';print_r($default); print_r($this->originalKeys);exit;
213         
214         
215         
216         foreach($default as $k=>$v) {
217             if (isset($ret[$k])) {
218                 continue; // skip database already holds a version of this translation.
219             }
220             // is it relivant anymore..
221             if (!isset($this->originalKeys[$k])) {
222                 continue;
223             }
224             // it's current..
225             $this->saveTranslateDB($lang, $module, $this->originalKeys[$k], $k, $v);
226             $ret[$k] = $v;
227             
228             
229         }
230         return $ret;
231         
232          
233     }
234     
235     function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
236     {
237         $d = DB_DataObject::factory('translations');
238         $d->module = $module;
239         $d->tlang = $lang;
240         $d->tfile = $tfile;
241         $d->tkey = $tkey;
242         if ($d->find(true)) {
243             $d->tval = $tval;
244             $d->update();
245         }
246         $d->tval = $tval;
247         $d->insert();
248         
249     }
250     
251     
252     
253     
254     function getTransFilename($lang, $module)
255     {
256         
257         $ff = HTML_FlexyFramework::get();
258         $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
259         if (!file_exists(dirname($fn))) {
260             
261             /// create the direct
262             $oldumask = umask(0);
263             mkdir(dirname($fn), 0770, true);
264             umask($oldumask);  
265             clearstatcache();
266             if (!file_exists(dirname($fn))) {
267                 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
268             }
269             
270         }
271         return $fn;
272     }
273     
274      
275     /**
276      * Writes a file MODULE.js inside of _translations_
277      *
278      * this should contain all contents from all the language directroris for this
279      * module
280      *
281      */
282     
283     function writeTransMod($lang, $module, $data)
284     {
285         //print_R($data);
286         
287         
288         $fn = $this->getTransFilename($lang, $module);
289         require_once 'Services/JSON.php';
290         $j = new Services_JSON();
291         
292         file_put_contents($fn, $j->stringify($data, null, 4));
293         
294         $ff = HTML_FlexyFramework::get();
295         $base = $ff->rootDir.'/_translations_' ;
296         $out = '';
297         foreach(scandir($base) as $l) {
298              
299             if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
300                 continue;
301             }
302             // decode as our temp files contain spaces..
303             $jdata = json_decode(file_get_contents("$base/$l/$module.json") );
304             $out .= "_T.$l= Roo.apply( _T.$l || { }, " . json_encode($jdata) . ");\n";
305         }
306         //var_dump($out);
307         if (strlen($out)) {
308             file_put_contents("$base/$module.js", $out);
309         }
310         //$this->writeTrans($lang);
311     }
312      
313     
314     
315 }