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