fix #8131 - chinese translations
[Pman.Core] / DataObjects / Core_template.php
index f19deaf..e64c9a8 100644 (file)
@@ -92,9 +92,11 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
     
     
     /*
+     * USED? - this is in updateBJSTemplate ?
      * @param base (should be full path to template directory)
      * @param subdir = empty for top or subpath.
      */
+    /*
     function syncTemplateDir($base = false,  $subdir = '', $force = false)
     {
         echo "syncTemplateDir: $base , $subdir, $force \n";
@@ -186,8 +188,8 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
             }
         }
     }
-    
-    /* compile a html template
+    */
+    /* compile a html template  - called by UpdateBjsTemplates - scan Pman Templates
      *  
      *  @param template_dir  << the path to the template dir ... Pman/XXX/template ...
      *  @param template   << name of template used by name field)
@@ -196,6 +198,7 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
      *  
      *  
      */
+    
     function syncTemplatePage($pgdata)
     {
         //print_r($pgdata);
@@ -241,7 +244,8 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
        
         $tmpl->view_name = $pgdata['base'];
         if ($tmpl->get('template',  $pgdata['template'])) {
-            if (strtotime($tmpl->updated) >= filemtime($flexy->resolvePath ($pgdata['template']))) {
+            clearstatcache();
+            if (strtotime($tmpl->updated) >= filemtime($flexy->resolvePath ($pgdata['template']) . '/'. $pgdata['template'])) {
                 if ($tmpl->is_deleted != 0 ||  $tmpl->filetype != 'html') {
                     $oo = clone($tmpl);
                     $tmpl->is_deleted = 0;
@@ -249,6 +253,8 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
                     $tmpl->update($oo);
                 }
                 if (empty($pgdata['force'])) {
+                  //  echo "SKIP NO UPDATE: " . $pgdata['template'] ."\n";
+                   // echo $flexy->resolvePath ($pgdata['template']).  ':'. $tmpl->updated  . ">=" .  date('Y-m-d H:i:s',filemtime($flexy->resolvePath ($pgdata['template']))) . "\n";
                     return $tmpl;
                 }
             }
@@ -262,7 +268,7 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
             
         } catch(Exception $e) {
             $old = clone($tmpl);
-            $tmpl->updated   = date('Y-m-d H:i:s',filemtime($flexy->resolvePath ($pgdata['template'])));
+            $tmpl->updated   = date('Y-m-d H:i:s',filemtime($flexy->resolvePath ($pgdata['template']) . '/'. $pgdata['template']));
             if ($tmpl->id) {
                 $tmpl->is_deleted = 0;
                 $tmpl->filetype = 'html';
@@ -273,15 +279,15 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
                 $tmpl->lang = 'en';
                 $tmpl->insert();
             }
-            
-            
+            //echo "SKIP: " . $pgdata['template'] ."\n";
+           //   echo "SKIP - exception\n"; print_r($e);
             return false;
         }
        
       
         if (is_a($r,'PEAR_Error')) {
-            
-           // echo $r->toString(). "\n";
+            //echo "SKIP: " . $pgdata['template'] ."\n";
+            //echo $r->toString(). "\n";
             return $r;
         }
           
@@ -297,7 +303,7 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
         
         $tmpl->view_name = $pgdata['base'];
         
-        
+        //echo $pgdata['template'] ."\n";
         if (!$tmpl->get('template',  $pgdata['template'])) {
             $tmpl->is_deleted = 0;
             $tmpl->filetype = 'html';
@@ -342,7 +348,14 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
         return clone($tmpl);
     
     }
-    function syncPhpGetText($pgdata)
+    
+    // allow reuse in cms templatstr
+    function factoryStr()
+    {
+        return DB_DataObject::factory('core_templatestr');
+    }
+
+    function syncFileWord($pgdata, $filetype)
     {
         $tmpl = DB_DataObject::Factory($this->tableName());
         $tmpl->view_name = $pgdata['base'];
@@ -350,73 +363,103 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
         
         if ($tmpl->get('template',  $pgdata['template'])) {
             if (strtotime($tmpl->updated) >= filemtime( $tmpl->currentTemplate )) {
-                if ($tmpl->is_deleted != 0 ||  $tmpl->filetype != 'html') {
+                if ($tmpl->is_deleted != 0 ||  $tmpl->filetype != $filetype) {
                     $oo = clone($tmpl);
                     $tmpl->is_deleted = 0;
-                    $tmpl->filetype = 'php';
+                    $tmpl->filetype = $filetype;
                     $tmpl->update($oo);
                 }
                 return $tmpl;
             }
         }
+
         $words = array();
-        
-        $ar = token_get_all(file_get_contents( $tmpl->currentTemplate  ));
-        foreach( $ar as $i=> $tok) {
-            if (!is_array($tok) || $tok[0] != T_CONSTANT_ENCAPSED_STRING) {
-                continue;
-            }
-            if ($i < 2) {
-                continue;
-            }
-            if (is_array($ar[$i-1]) || $ar[$i-1] != '(') {
-                continue;
-            }
-            if (!is_array($ar[$i-2]) || $ar[$i-2][1] != '_') {
-                continue;
-            }
-            $ct = $tok[1][0];
-            $words[] =  str_replace('\\'. $ct, $ct, trim($tok[1] , $ct));
-            
+
+        switch($filetype) {
+            case "php":
+                $ar = token_get_all(file_get_contents( $tmpl->currentTemplate  ));
+                foreach( $ar as $i=> $tok) {
+                    if (!is_array($tok) || $tok[0] != T_CONSTANT_ENCAPSED_STRING) {
+                        continue;
+                    }
+                    if ($i < 2) {
+                        continue;
+                    }
+                    if (is_array($ar[$i-1]) || $ar[$i-1] != '(') {
+                        continue;
+                    }
+                    if (!is_array($ar[$i-2]) || $ar[$i-2][1] != '_') {
+                        continue;
+                    }
+                    $ct = $tok[1][0];
+                    $words[] =  str_replace('\\'. $ct, $ct, trim($tok[1] , $ct));
+                    
+                }
+                break;
+            case "js":
+                $fc = file_get_contents( $tmpl->currentTemplate );
+        
+                preg_match_all('/\._\("([^"]+)"\)/', $fc, $outd);
+                $words = $outd[1];
+                 
+                preg_match_all('/\._\(\'([^\']+)\'\)/', $fc, $outs);
+                
+                // ?? seriously adding two arrays?
+                $words =  array_diff(array_merge($words, $outs[1]), array_intersect($words, $outs[1]));
+                break;
+            case "xml":
+                $words = $pgdata['words'];
+                break;
         }
-        // create the template...
-        
-        
-         if (!$tmpl->id) {
-            
-            $tmpl->template = $pgdata['template'];
-            $tmpl->lang = 'en'; /// ??? hard coded??
-            $tmpl->filetype = 'php';
+
+        $words = array_unique($words);
+
+        if(empty($words)) {
+            return;
+        }
+
+        if ($tmpl->id) {
+            $oo = clone($tmpl);
             $tmpl->is_deleted = 0;
+            $tmpl->filetype = $filetype;
             $tmpl->updated = date('Y-m-d H:i:s', filemtime($tmpl->currentTemplate));
-            $tmpl->insert();
+            $tmpl->update($oo);
         } else {
-            $xx =clone($tmpl);
-            $tmpl->filetype = 'php';
             $tmpl->is_deleted = 0;
-            $tmpl->lang = 'en'; /// ??? hard coded??
+            $tmpl->filetype = $filetype;
+            $tmpl->lang = 'en';
             $tmpl->updated = date('Y-m-d H:i:s', filemtime($tmpl->currentTemplate));
-            $tmpl->update($xx);
-        }
-      
-        $words = array_unique($words);
-        
-        if (!count($words)) {
-            return;
+            $tmpl->insert();
         }
-        
-             
+
         $tmpl->words = $words;
-            
-        $x = DB_DataObject::Factory('core_templatestr');
-        $x->syncTemplateWords($tmpl);    
-         
-        
+
+        $this->factoryStr()->syncTemplateWords($tmpl);
+
         return $tmpl;
-        
-        
-        
     }
+
+    function syncPhpGetText($pgdata)
+    {
+        return $this->syncFileWord($pgdata, 'php'); 
+    }
+
+    /**
+     * plain JS files use ._(....) to flag 
+     * it does not support quoted strings or anything really
+     * very simple strings only
+     */ 
+    
+    function syncJsWords($pgdata)
+    {
+        return $this->syncFileWord($pgdata, 'js');   
+    }
+
+    function syncPowerpointXMLText($pgdata) 
+    {
+        return $this->syncFileWord($pgdata, 'xml');
+    }
+    
     /*
     SELECT LOWER(
 CONCAT(
@@ -499,7 +542,7 @@ WHERE (
         $done[$clsname.':'.$lang] = 1;
         
         // do we need to compile the file..
-        $ts = DB_DataObject::Factory('core_templatestr');
+        $ts = $this->factoryStr();
         $ts->selectAdd('COALESCE(MAX(updated), "1000-01-01") as updated');
         $ts->lang = $lang;
         $ts->template_id = $d->id;
@@ -512,12 +555,12 @@ WHERE (
         }
         //DB_DataObject::debugLevel(1);
 
-        $ts = DB_DataObject::Factory('core_templatestr');
+        $ts = $this->factoryStr();
         $ts->autoJoin();
-        $ts->selectAdd('join_src_id_id.txt as src_id_txt, core_templatestr.txt as txt');
+        $ts->selectAdd("join_src_id_id.txt as src_id_txt, {$ts->tableName()}.txt as txt");
         $ts->lang = $lang;
         $ts->template_id = $d->id;
-        $ts->whereAdd("LENGTH(join_src_id_id.txt) > 0 AND LENGTH(core_templatestr.txt) > 0");
+        $ts->whereAdd("LENGTH(join_src_id_id.txt) > 0 AND LENGTH({$ts->tableName()}.txt) > 0");
         $words = $ts->fetchAll('src_id_txt', 'txt' );
                
         if (!file_exists($fdir)) {