EventView.php
[Pman.Admin] / Translations.php
index 0700af5..a9ce759 100644 (file)
@@ -41,8 +41,35 @@ class Pman_Admin_Translations extends Pman
     }
     
     
-    function get()
+    function get($module, $opts = Array())
     {
+        
+        if (!empty($module)) {
+            $this->init();
+            //DB_DataObject::debugLevel(1);
+            require_once 'Services/JSON.php';
+            $d = DB_DataObject::factory('translations');
+            $d->module = $module;
+            $d->selectAdd();
+            $d->selectAdd('distinct(tlang) as tlang');
+            header('Content-type: text/javascript');
+            $langs= $d->fetchAll('tlang');
+            foreach($langs as $lang) {
+                // output the translations strings file..
+                
+                    
+                $this->loadOriginalStrings($module);
+                
+                $data = $this->loadTranslateDB($lang,$module);
+                
+                $j = new Services_JSON();
+                echo "_T.{$lang}= Roo.apply( _T.{$lang} || { }, " .  $j->stringify($data, null, 4) . ");\n";
+                
+            }
+            exit;
+            
+        }
+        
         // load and parse json file containing all translations...
         if (isset($_REQUEST['id'])) {
             return $this->post();
@@ -62,10 +89,12 @@ class Pman_Admin_Translations extends Pman
         $module = $_REQUEST['module'];
         
         
-         $this->loadOriginalStrings($lang,$module); // what needs translating..
-        
-        
+         $this->loadOriginalStrings($module); // what needs translating..
         
+        $ff = $this->bootLoader;
+        if (empty($ff->Pman['public_cache_dir'])) {
+            $this->jerr("public_cache_dir has not been set up");
+        }
         
         
         
@@ -100,12 +129,12 @@ class Pman_Admin_Translations extends Pman
         
     }
     
-    function post() 
+    function post($v
     {
          
-        $fm = HTML_FlexyFramework::get();
-        $enable = explode(',',   $fm->enable);
-        if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $enable)) {
+         
+        
+        if (empty($_REQUEST['module']) || !in_array($_REQUEST['module'], $this->modulesList())) {
             $this->jerr("NO MODULE / INVALID MODULE");
         }
         
@@ -113,11 +142,21 @@ class Pman_Admin_Translations extends Pman
         //txt  é \85ç\9b®
         list($lang,$id) = explode('/', $_REQUEST['id']);
         
+        $this->loadOriginalStrings($_REQUEST['module']);
         
-        $data = $this->loadTranslate($lang,$_REQUEST['module']);
+        $data = $this->loadTranslateDB($lang,$_REQUEST['module']);
         
         $data[$id] = $_REQUEST['txt'];
         
+        if (!isset($this->originalKeys[$id])) {
+            
+            
+            $this->jerr("invalid key ?");
+        }
+        
+        $this->saveTranslateDB($lang,$_REQUEST['module'],$this->originalKeys[$id], $id, $_REQUEST['txt']);
+        
+        
         
         $this->writeTransMod($lang,$_REQUEST['module'], $data);
         // write merged file..
@@ -130,7 +169,7 @@ class Pman_Admin_Translations extends Pman
      * load strings that need translating..
      */
     
-    function loadOriginalStrings($lang, $module)
+    function loadOriginalStrings($module)
     {
         // since this can handle errors better.!!?
         $info = $this->moduleJavascriptFilesInfo($module);
@@ -144,7 +183,9 @@ class Pman_Admin_Translations extends Pman
         
         require_once 'Services/JSON.php';
         $j = new Services_JSON();
-        $this->original = $j->decode('{'. file_get_contents($tfile).'}');
+        $this->original = (array) $j->decode('{'. file_get_contents($tfile).'}');
+        ksort($this->original);
+
         $this->originalKeys = array();
         
         // 
@@ -160,28 +201,7 @@ class Pman_Admin_Translations extends Pman
      
     
     
-    /**
-     * 
-     * Load the user translated strings.
-     * {root}/_translation_/{lang}/{module}.js
-     *
-     * 
-     *
-     * 
-     */
-    function loadTranslate($lang, $module)
-    {
-         
-        $fn = $this->getTransFilename($lang,$module);
-        
-         
-        if (!file_exists($fn)) {
-            return array();
-        }
-        
-        return (array) json_decode(file_get_contents($fn));
-        //$this->data = (array) $j->decode(substr(file_get_contents($this->fn), strlen($this->prefix), -1));
-    }
+   
     /***
      *
      * loadTranslateDB -
@@ -194,22 +214,34 @@ class Pman_Admin_Translations extends Pman
     
     function loadTranslateDB($lang, $module)
     {
-        $d = DB_DataObject('translations');
-        $d->module = $module;
-        $d->lang = $lang;
         
+        //DB_DataObject::debugLevel(1);
+        $d = DB_DataObject::factory('translations');
+        $d->module = $module;
+        $d->tlang = $lang;
+        $d->whereAdd('LENGTH(tval) > 0');
+        $ret = array();
         
         if ($d->count()) {
             // since key includes file 
             $ret = $d->fetchAll('tkey','tval'); /// shoudl we include updates
         }
         // no data is contained in the database, we should initialize it, if we can
-        $info  = moduleJavascriptFilesInfo($module);
+        $info  = $this->moduleJavascriptFilesInfo($module);
         $fn = $info->module_dir.'/_translations_/'.$lang.'.js';
+        
+       
         if (!file_exists($fn)) {
+            ///Die($fn ." does not exist?");
             return $ret;
         }
+        
+        
         $default = (array) json_decode(file_get_contents($fn));
+        //echo '<PRE>';print_r($default); print_r($this->originalKeys);exit;
+        
+        
+        
         foreach($default as $k=>$v) {
             if (isset($ret[$k])) {
                 continue; // skip database already holds a version of this translation.
@@ -218,13 +250,13 @@ class Pman_Admin_Translations extends Pman
             if (!isset($this->originalKeys[$k])) {
                 continue;
             }
+            
             // it's current..
             $this->saveTranslateDB($lang, $module, $this->originalKeys[$k], $k, $v);
             $ret[$k] = $v;
             
             
         }
-        
         return $ret;
         
          
@@ -232,9 +264,9 @@ class Pman_Admin_Translations extends Pman
     
     function saveTranslateDB($lang, $module, $tfile, $tkey, $tval)
     {
-        $d = DB_DataObject('translations');
+        $d = DB_DataObject::factory('translations');
         $d->module = $module;
-        $d->lang = $lang;
+        $d->tlang = $lang;
         $d->tfile = $tfile;
         $d->tkey = $tkey;
         if ($d->find(true)) {
@@ -280,11 +312,16 @@ class Pman_Admin_Translations extends Pman
     
     function writeTransMod($lang, $module, $data)
     {
-        print_R($data);
+        //print_R($data);
+        
         
         $fn = $this->getTransFilename($lang, $module);
-        file_put_contents($fn, json_encode($data));
-         $ff = HTML_FlexyFramework::get();
+        require_once 'Services/JSON.php';
+        $j = new Services_JSON();
+        
+        file_put_contents($fn, $j->stringify($data, null, 4));
+        
+        $ff = HTML_FlexyFramework::get();
         $base = $ff->rootDir.'/_translations_' ;
         $out = '';
         foreach(scandir($base) as $l) {
@@ -292,8 +329,9 @@ class Pman_Admin_Translations extends Pman
             if (!strlen($l) || $l[0] == '.' || !is_dir("$base/$l") || !file_exists("$base/$l/$module.json")) {
                 continue;
             }
-              
-            $out .= "_T.$l= Roo.apply( _T.$l || { }, " . file_get_contents("$base/$l/$module.json") . ");\n";
+            // decode as our temp files contain spaces..
+            $jdata = json_decode(file_get_contents("$base/$l/$module.json") );
+            $out .= "_T.$l= Roo.apply( _T.$l || { }, " . json_encode($jdata) . ");\n";
         }
         //var_dump($out);
         if (strlen($out)) {
@@ -302,6 +340,6 @@ class Pman_Admin_Translations extends Pman
         //$this->writeTrans($lang);
     }
      
-    
+     
     
 }
\ No newline at end of file