fix #8131 - chinese translations
[Pman.Core] / DataObjects / Core_templatestr.php
index c1aadbf..a800750 100644 (file)
@@ -84,7 +84,6 @@ class Pman_Core_DataObjects_Core_templatestr extends DB_DataObject
      */
     function onTableChange($roo, $obj, $chg)
     {
-        
         $ff = HTML_FlexyFramework::get()->Pman_Core;
             
         if(empty($ff['DataObjects_Core_templatestr']['tables'])){
@@ -95,18 +94,42 @@ class Pman_Core_DataObjects_Core_templatestr extends DB_DataObject
             return;
         }
         $cols = $ff['DataObjects_Core_templatestr']['tables'][$tn];
-        
-        
+
+        $used = array();
         foreach($cols as $c) {
+            
+            if(strpos($c, ',') !== false) {
+                $arr = explode(',', $c);
+                $c = $arr[0];
+                $cond = $arr[1];
+
+                $ar = explode('=', $cond);
+                $key = $ar[0];
+
+                // skip if condition not fulfilled
+                if($obj->{$ar[0]} != $ar[1]) {
+                    continue;
+                }
+            }
+
+            // skip if empty string
+            if(empty($obj->$c)) {
+                continue;
+            }
+
             $x = $this->factory($this->tableName());
             $x->on_id = $obj->pid();
             $x->on_table = $tn;
             $x->on_col = $c;
             $x->lang = ''; /// eg. base language..
             $up = $x->find(true);
-            if ($up && $x->txt == $obj->$c) {
-                continue; // update an no change..
+
+            // skip when no change
+            if($up && $x->txt == $obj->$c) {
+                $used[] = $x->id;
+                continue;
             }
+
             $x->active = 1;
             $x->src_id = 0;
             $x->txt = $obj->$c;
@@ -115,6 +138,23 @@ class Pman_Core_DataObjects_Core_templatestr extends DB_DataObject
             $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
             $up ? $x->update() : $x->insert();
         }
+
+        // make sure the used one are active
+        if(count($used)) {
+            $t = DB_DataObject::factory($this->tableName());
+            // activate the aprent data
+            $t->query("UPDATE core_templatestr
+                SET active = 1 WHERE id in (" . implode(',' ,$used) . ")
+            ");
+            // activate the child data
+            $t->query("UPDATE  core_templatestr 
+            SET active = 1
+              WHERE
+                 src_id IN (". implode(',' ,$used) . ")
+                AND
+                lang != ''
+            ");
+        }
         
         
     }
@@ -289,6 +329,11 @@ class Pman_Core_DataObjects_Core_templatestr extends DB_DataObject
             
             
             $v = trim($v);
+
+            // skip empty words
+            if(empty($v)) {
+                continue;
+            }
             
             $md = $keyvalue ? $k : md5($v);
             
@@ -315,7 +360,8 @@ class Pman_Core_DataObjects_Core_templatestr extends DB_DataObject
             $active[] = $cur[$md];
             
             // we have it already? - 
-            $tt->query("UPDATE {$this->tableName()}
+            $t = DB_DataObject::factory($this->tableName());
+            $t->query("UPDATE {$this->tableName()}
                         SET active= 1
                         WHERE
                         id = ".$cur[$md]);
@@ -442,18 +488,23 @@ class Pman_Core_DataObjects_Core_templatestr extends DB_DataObject
         
         // find all the id's from lang that have not been generated..
         
-        //find the origanal 
-        $t = DB_DataObject::factory($tn);
-        $t->whereAdd("lang = ''");
-        $t->active = 1;
         
-        //old code, this did not support the on_table
-//        $id_tmp = $t->fetchAll('id','template_id');
-//        $ids = array_keys($id_tmp);
-        $id_tmp = array();
-        //new code for support the sync tables 
-        foreach($t->fetchAll() as $ori){
-            $id_tmp[$ori->id] = $ori;
+        static $id_tmp = false;
+        
+        if ($id_tmp == false) {
+            //find the origanal 
+            $t = DB_DataObject::factory($tn);
+            $t->whereAdd("lang = ''");
+            $t->active = 1;
+            
+            //old code, this did not support the on_table
+    //        $id_tmp = $t->fetchAll('id','template_id');
+    //        $ids = array_keys($id_tmp);
+            $id_tmp = array();
+            //new code for support the sync tables 
+            foreach($t->fetchAll() as $ori){
+                $id_tmp[$ori->id] = $ori;
+            }
         }
         $ids = array_keys($id_tmp);
         
@@ -464,25 +515,33 @@ class Pman_Core_DataObjects_Core_templatestr extends DB_DataObject
         //$t->active = 1;
         $got = $t->fetchAll('src_id');
         $missing = array_diff($ids, $got);
-        foreach($missing as $id) {
-            
-            $t = DB_DataObject::factory($tn);
-            $t->setFrom(array(
-                'src_id' => $id,
-                'txt' =>  '',
-                'lang' => $lang,
-                'updated' => date('Y-m-d H:i:s', strtotime("NOW")),
-                'template_id'=> $id_tmp[$id]->template_id,
-                'on_table' => $id_tmp[$id]->on_table,
-                'on_id' => $id_tmp[$id]->on_id,
-                'on_col' => $id_tmp[$id]->on_col,
-                'active' => 1,
-                // no md5um
-            ));
-            $t->insert();
-        }
-        
         
+        if (empty($missing)) {
+            return;
+        }
+        $t = DB_DataObject::factory($tn);
+        $q = "CREATE TEMPORARY TABLE core_templatestr_insert SELECT
+            id as src_id,
+            '' as txt,
+            '$lang' as lang,
+            NOW() as updated,
+            template_id,
+            on_table,
+            on_id,
+            on_col,
+            1 as active
+            FROM core_templatestr
+            WHERE
+            id IN (". implode(',', $missing) . ")
+        ";
+        //echo $q; exit;
+        DB_DataObject::factory($tn)->query($q);
+        $q = "INSERT INTO $tn (src_id, txt, lang, updated, template_id, on_table,on_id, on_col, active) SELECT * FROM core_templatestr_insert";
+        DB_DataObject::factory($tn)->query($q);
+        $q = "DROP TEMPORARY TABLE core_templatestr_insert";
+        DB_DataObject::factory($tn)->query($q);
+            
+      
         
     }
     
@@ -692,4 +751,55 @@ class Pman_Core_DataObjects_Core_templatestr extends DB_DataObject
         
     }
     
+    function toRooArray($req) {
+        $ret = $this->toArray();
+
+        if (empty($req['csvCols'])) {
+            return $ret;
+        }
+
+        // for download
+
+        // translations for table columns
+        if(!empty($ret['on_table']) && !empty($ret['on_id']) && !empty($ret['on_col'])) {
+            $ret['template_id_view_name'] = 'database';
+            $ret['template_id_template'] = $ret['on_table'] . ':' . $ret['on_col'];
+        }
+
+        return $ret;
+    }
+
+    function postListFilter($ar, $au, $req)
+    {
+        if (empty($req['csvCols'])) {
+            return $ar;
+        }
+
+        // for download
+
+        $ret = array();
+
+        foreach($ar as $v) {
+            if(empty($v['on_table']) || empty($v['on_id']) || empty($v['on_col'])) {
+                $ret[] = $v;
+                continue;
+            }
+
+            // translations for table columns
+            // avoid duplicate (same src_id_mdsum, same on_table, same on_col, but different on_id)
+
+            $key = $v['on_table'] . ':' . $v['on_col'] . ':' . $v['src_id_mdsum'];
+
+            if(!empty($ret[$key])) {
+                continue;
+            }
+
+            $ret[$key] = $v;
+        }
+
+        $ret = array_values($ret);
+        
+        return $ret;
+
+    }
 }