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->loadTranslate($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      * 
151      */
152     function loadTranslate($lang, $module)
153     {
154          
155         $fn = $this->getTransFilename($lang,$module);
156         
157          
158         if (!file_exists($fn)) {
159             return array();
160         }
161         
162         return (array) json_decode(file_get_contents($fn));
163         //$this->data = (array) $j->decode(substr(file_get_contents($this->fn), strlen($this->prefix), -1));
164     }
165     
166     
167     function loadTranslateDB($lang, $module)
168     {
169         $d = DB_DataObject('translations');
170         $d->module = $module;
171         $d->lang = $lang;
172         $d->find();
173         while ($d->fetch()) {
174             if (!isset($ret[$d->tfile])) {
175                 $ret[$d->tfile] = array( $d->tkey => $d->tval );
176                 continue;
177             }
178             $ret[$d->tfile][$d->tkey] = $d->tval;
179         }
180         return $ret;
181     }
182     
183     function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
184     {
185         $d = DB_DataObject('translations');
186         $d->module = $module;
187         $d->lang = $lang;
188         $d->tfile = $tfile;
189         $d->tkey = $tkey;
190         if ($d->find(true)) {
191             $d->tval = $tval;
192             $d->update();
193         }
194         $d->tval = $tval;
195         $d->insert();
196         
197     }
198     
199     
200     
201     
202     function getTransFilename($lang, $module)
203     {
204         
205         $ff = HTML_FlexyFramework::get();
206         $fn = $ff->rootDir .'/_translations_/'. $lang . '/'. $module.'.json';
207         if (!file_exists(dirname($fn))) {
208             
209             /// create the direct
210             $oldumask = umask(0);
211             mkdir(dirname($fn), 0770, true);
212             umask($oldumask);  
213             clearstatcache();
214             if (!file_exists(dirname($fn))) {
215                 $this->jerr("_translations_ directory does not exist in Pman folder - it needs to be editable");
216             }
217             
218         }
219         return $fn;
220     }
221     
222      
223     /**
224      * Writes a file MODULE.js inside of _translations_
225      *
226      * this should contain all contents from all the language directroris for this
227      * module
228      *
229      */
230     
231     function writeTransMod($lang, $module, $data)
232     {
233         print_R($data);
234         
235         $fn = $this->getTransFilename($lang, $module);
236         file_put_contents($fn, json_encode($data));
237          $ff = HTML_FlexyFramework::get();
238         $base = $ff->rootDir.'/_translations_' ;
239         $out = '';
240         foreach(scandir($base) as $l) {
241              
242             if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
243                 continue;
244             }
245               
246             $out .= "_T.$l= Roo.apply( _T.$l || { }, " . file_get_contents("$base/$l/$module.json") . ");\n";
247         }
248         //var_dump($out);
249         if (strlen($out)) {
250             file_put_contents("$base/$module.js", $out);
251         }
252         //$this->writeTrans($lang);
253     }
254      
255     
256     
257 }