fix #8131 - chinese translations
[Pman.Core] / DataObjects / Core_template.php
index 963f4a1..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,33 +244,50 @@ 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']))) {
-                return $tmpl;
+            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);
+                }
+                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';
                 $tmpl->update($tmpl);
             } else {
+                $tmpl->is_deleted = 0;
+                $tmpl->filetype = 'html';
                 $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;
         }
           
@@ -249,9 +303,10 @@ 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';
             $tmpl->template = $pgdata['template'];
             $tmpl->lang = 'en'; /// ??? hard coded??
             $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
@@ -267,7 +322,8 @@ class Pman_Core_DataObjects_Core_template  extends DB_DataObject
                 //echo "FIX LANG?";exit;
                 $tmpl->lang = 'en'; /// ??? hard coded??
             }
-            
+            $tmpl->filetype = 'html';
+            $tmpl->is_deleted = 0;
             $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
             $tmpl->update($xx);
         }
@@ -292,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'];
@@ -300,62 +363,258 @@ 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 != $filetype) {
+                    $oo = clone($tmpl);
+                    $tmpl->is_deleted = 0;
+                    $tmpl->filetype = $filetype;
+                    $tmpl->update($oo);
+                }
                 return $tmpl;
             }
         }
+
         $words = array();
+
+        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;
+        }
+
+        $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->update($oo);
+        } else {
+            $tmpl->is_deleted = 0;
+            $tmpl->filetype = $filetype;
+            $tmpl->lang = 'en';
+            $tmpl->updated = date('Y-m-d H:i:s', filemtime($tmpl->currentTemplate));
+            $tmpl->insert();
+        }
+
+        $tmpl->words = $words;
+
+        $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(
+REPLACE(view_name, '.','_'),
+'_',
+REPLACE(template,'/','_')
+)
+)
+FROM core_template
+
+WHERE (
+ = 'release_pressrelease_distributionreportnew_journalistdistribution.php'
+)
+*/
+
+    
+    function genGetText($clsname, $lang=false)
+    {
+        static $done = array();
+        $clsname = strtolower($clsname);
+
+        textdomain($clsname);
+     
+
+        $ff = HTML_FlexyFramework::get();
+        $lang = $lang ? $lang : (isset($ff->locale) ? $ff->locale : 'en');
         
-        $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;
+
+        if (!empty($done[$clsname.':'.$lang])) {
+            return true; // already sent headers and everything.
+        }
+        
+        putenv("LANGUAGE=$lang");
+        if ($lang != 'en') {
+            if (!setlocale(LC_ALL, $lang.'.UTF-8')) {
+                $ff->page->jerr("Language is not available {$lang}");
             }
-            $ct = $tok[1][0];
-            $words[] =  str_replace('\\'. $ct, $ct, trim($tok[1] , $ct));
-            
         }
-        // create the template...
         
         
-         if (!$tmpl->id) {
-            
-            $tmpl->template = $pgdata['template'];
-            $tmpl->lang = 'en'; /// ??? hard coded??
-            $tmpl->updated = date('Y-m-d H:i:s', filemtime($tmpl->currentTemplate));
-            $tmpl->insert();
-        } else {
-            $xx =clone($tmpl);
-            
-            $tmpl->lang = 'en'; /// ??? hard coded??
-            $tmpl->updated = date('Y-m-d H:i:s', filemtime($tmpl->currentTemplate));
-            $tmpl->update($xx);
+        $d = DB_DataObject::factory($this->tableName());
+        $d->whereAdd("
+            LOWER(
+                CONCAT(
+                      REPLACE(view_name, '.','_'),
+                    '_',
+                    REPLACE(template,'/','_')
+                )
+            ) = '{$clsname}.php'
+       ");
+        $d->filetype = 'php';
+        if (! $d->find(true) ){
+            $done[$clsname.':'.$lang] = true;
+            return false;
         }
-      
-        $words = array_unique($words);
+        $user = 'www-data'; // ?? do we need other ones
+        $compileDir = ini_get('session.save_path') .'/' . 
+            $user . '_gettext_' . $ff->project;
         
-        if (!count($words)) {
-            return;
+        if ($ff->appNameShort) {
+            $compileDir .= '_' . $ff->appNameShort;
+        }
+        if ($ff->version) {
+            $compileDir .= '.' . $ff->version;
         }
+        $lang = $lang ? $lang : $ff->locale;
+        $fdir = "{$compileDir}/{$lang}/LC_MESSAGES";
+        $fname = "{$fdir}/{$clsname}.mo";
         
-             
-        $tmpl->words = $words;
-            
-        $x = DB_DataObject::Factory('core_templatestr');
-        $x->syncTemplateWords($tmpl);    
          
+        //exit;
+        bindtextdomain($clsname, $compileDir) ;
+        bind_textdomain_codeset($clsname, 'UTF-8');
+
+        textdomain($clsname);
+
         
-        return $tmpl;
+        //textdomain($clsname);
         
+        $done[$clsname.':'.$lang] = 1;
         
+        // do we need to compile the file..
+        $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 false;
+        }
+        if (file_exists($fname) && strtotime($ts->updated) < filemtime($fname)) {
+            return $fname; // file exists and is newer than our updated line.
+        }
+        //DB_DataObject::debugLevel(1);
+
+        $ts = $this->factoryStr();
+        $ts->autoJoin();
+        $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)) {
+            //var_dump($fdir);
+            mkdir($fdir, 0700, true);
+        }
+        
+        require_once 'File/Gettext.php';
+        $gt = File_Gettext::factory('PO', preg_replace('/\.mo$/', '.po', $fname));
+        $gt->fromArray(
+            
+            array(
+                'meta' => array(
+                    "Language" =>  $lang,
+                    'Content-Type'      => 'text/plain; charset=UTF-8',
+                    'Content-Transfer-Encoding'      => ' 8bit',
+                     'PO-Revision-Date'  => date('Y-m-d H:iO'),
+                 ),
+                'strings' => $words
+            )
+            
+        );
+        $gt->save();
+        
+        // mo DOESNT WORK!!
+        require_once 'System.php';
+        $poname = preg_replace('/\.mo$/', '.po', $fname);
+        $msgfmt = System::which('msgfmt');
+        $cmd = "{$msgfmt} {$poname}  -o {$fname}";
+        //echo $cmd;
+        
+        `$cmd`;
+        
+        
+         
+        
+        return $fname;
+        
+        require_once 'File/Gettext.php';
+        $gt = File_Gettext::factory('MO', $fname);
+        $gt->fromArray(
+            
+            array(
+                'meta' => array(
+                     "Language" =>  $lang,
+                    'Content-Type'      => 'text/plain; charset=UTF-8',
+                    'Content-Transfer-Encoding'      => ' 8bit',
+                     'PO-Revision-Date'  => date('Y-m-d H:iO'),
+                ),
+                'strings' => $words
+            )
+            
+        );
+        $gt->save(); 
         
     }
 }
\ No newline at end of file