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             return $d->fetchAll('tkey','tval');    
189         }
190         // no data is contained in the database, we should initialize it, if we can
191         if ()
192         
193         
194         
195         $d->find();
196         
197         
198         while ($d->fetch()) {
199             if (!isset($ret[$d->tfile])) {
200                 $ret[$d->tfile] = array( $d->tkey => $d->tval );
201                 continue;
202             }
203             $ret[$d->tfile][$d->tkey] = $d->tval;
204         }
205         return $ret;
206     }
207     
208     function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
209     {
210         $d = DB_DataObject('translations');
211         $d->module = $module;
212         $d->lang = $lang;
213         $d->tfile = $tfile;
214         $d->tkey = $tkey;
215         if ($d->find(true)) {
216             $d->tval = $tval;
217             $d->update();
218         }
219         $d->tval = $tval;
220         $d->insert();
221         
222     }
223     
224     
225     
226     
227     function getTransFilename($lang, $module)
228     {
229         
230         $ff = HTML_FlexyFramework::get();
231         $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
232         if (!file_exists(dirname($fn))) {
233             
234             /// create the direct
235             $oldumask = umask(0);
236             mkdir(dirname($fn), 0770, true);
237             umask($oldumask);  
238             clearstatcache();
239             if (!file_exists(dirname($fn))) {
240                 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
241             }
242             
243         }
244         return $fn;
245     }
246     
247      
248     /**
249      * Writes a file MODULE.js inside of _translations_
250      *
251      * this should contain all contents from all the language directroris for this
252      * module
253      *
254      */
255     
256     function writeTransMod($lang, $module, $data)
257     {
258         print_R($data);
259         
260         $fn = $this->getTransFilename($lang, $module);
261         file_put_contents($fn, json_encode($data));
262          $ff = HTML_FlexyFramework::get();
263         $base = $ff->rootDir.'/_translations_' ;
264         $out = '';
265         foreach(scandir($base) as $l) {
266              
267             if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
268                 continue;
269             }
270               
271             $out .= "_T.$l= Roo.apply( _T.$l || { }, " . file_get_contents("$base/$l/$module.json") . ");\n";
272         }
273         //var_dump($out);
274         if (strlen($out)) {
275             file_put_contents("$base/$module.js", $out);
276         }
277         //$this->writeTrans($lang);
278     }
279      
280     
281     
282 }