fix #8131 - chinese translations
[Pman.Core] / DataObjects / Core_template.php
index 03f9b97..e64c9a8 100644 (file)
@@ -58,10 +58,45 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
         
     }
     
+    function beforeUpdate($old, $q, $roo)
+    {
+        if (!empty($q['_rescan'])){
+            if ($this->filetype != 'html') {
+                $roo->jerr("can not update a php source file currently - TDOD");
+            }
+            $pg = HTML_FlexyFramework::get()->page;
+            
+            $this->syncTemplatePage(array(
+                'template_dir' => $pg->rootDir . '/'. str_replace('.', '/', $this->view_name). '/templates',
+                'template' => $this->template,
+                'base' => $this->view_name,
+                'force' => true
+            ));
+            // update the different langage versions of this page.
+            $x = DB_Dataobject::Factory('core_templatestr');
+            $x->selectAdd();
+            $x->selectAdd('distinct(lang) as lang');
+            $x->whereAdd("lang != ''");
+            $langs  = $x->fetchAll('lang');
+            foreach($langs as $l) {
+                $x = DB_Dataobject::Factory('core_templatestr');
+                $x->syncLang($l, $this->id);
+            }
+           
+            
+            $roo->jok("updated -" .  $this->template);
+        }
+    }
+   
+    
+    
+    
     /*
+     * 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";
@@ -153,15 +188,17 @@ 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)
      *  @param base  << view name (module ? + templates?)
+     *  @param force << optional - forces even if database is newer.
      *  
      *  
      */
+    
     function syncTemplatePage($pgdata)
     {
         //print_r($pgdata);
@@ -172,7 +209,7 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
         ini_set('memory_limit', '512M');
         
         //var_dump($n);
-        $n= $pgdata['template'];  // remove trailing slash..
+        $n = $pgdata['template'];  // remove trailing slash..
         
         $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
         $opts = HTML_FlexyFramework::get()->Pman_Core;
@@ -207,27 +244,31 @@ 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;
                     $tmpl->filetype = 'html';
                     $tmpl->update($oo);
                 }
-                
-                
-                
-                return $tmpl;
+                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;
+                }
             }
         }
         
-        
+        //die("got here");
         
         try {
             $r = $flexy->compile($pgdata['template']);
+           
+            
         } 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';
@@ -238,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;
         }
           
@@ -262,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';
@@ -307,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'];
@@ -315,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(
@@ -400,7 +478,7 @@ WHERE (
     
     function genGetText($clsname, $lang=false)
     {
-        static $done = false;
+        static $done = array();
         $clsname = strtolower($clsname);
 
         textdomain($clsname);
@@ -411,12 +489,12 @@ WHERE (
         
 
         if (!empty($done[$clsname.':'.$lang])) {
-            return; // already sent headers and everything.
+            return true; // already sent headers and everything.
         }
         
         putenv("LANGUAGE=$lang");
-        if ($lang != 'en' && !setlocale(LC_ALL, $lang)) {
-            if (!setlocale(LC_ALL, $lang.'.UTF8')) {
+        if ($lang != 'en') {
+            if (!setlocale(LC_ALL, $lang.'.UTF-8')) {
                 $ff->page->jerr("Language is not available {$lang}");
             }
         }
@@ -435,7 +513,7 @@ WHERE (
         $d->filetype = 'php';
         if (! $d->find(true) ){
             $done[$clsname.':'.$lang] = true;
-            return;
+            return false;
         }
         $user = 'www-data'; // ?? do we need other ones
         $compileDir = ini_get('session.save_path') .'/' . 
@@ -464,24 +542,25 @@ WHERE (
         $done[$clsname.':'.$lang] = 1;
         
         // do we need to compile the file..
-        $ts = DB_DataObject::Factory('core_templatestr');
-        $ts->selectAdd('MAX(updated) as updated');
+        $ts = $this->factoryStr();
+        $ts->selectAdd('COALESCE(MAX(updated), "1000-01-01") as updated');
         $ts->lang = $lang;
         $ts->template_id = $d->id;
         if (!$ts->find(true)) {
             // then in theory there are no translations
-            return;
+            return false;
         }
         if (file_exists($fname) && strtotime($ts->updated) < filemtime($fname)) {
-            return; // file exists and is newer than our updated line.
+            return $fname; // file exists and is newer than our updated line.
         }
         //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({$ts->tableName()}.txt) > 0");
         $words = $ts->fetchAll('src_id_txt', 'txt' );
                
         if (!file_exists($fdir)) {
@@ -518,7 +597,7 @@ WHERE (
         
          
         
-        return;
+        return $fname;
         
         require_once 'File/Gettext.php';
         $gt = File_Gettext::factory('MO', $fname);