Fix #6494 - translations code for reports
authorAlan Knowles <alan@roojs.com>
Wed, 2 Dec 2020 06:22:39 +0000 (14:22 +0800)
committerAlan Knowles <alan@roojs.com>
Wed, 2 Dec 2020 06:22:39 +0000 (14:22 +0800)
DataObjects/Core_template.php [new file with mode: 0644]
DataObjects/Core_template_element.php [new file with mode: 0644]
DataObjects/Core_templatestr.php [new file with mode: 0644]
DataObjects/Translations.php
DataObjects/pman.links.ini
sql/core_template.sql [new file with mode: 0644]
sql/core_template_element.sql [new file with mode: 0644]
sql/core_templatestr.sql [new file with mode: 0644]
sql/translations.sql

diff --git a/DataObjects/Core_template.php b/DataObjects/Core_template.php
new file mode 100644 (file)
index 0000000..b4c176e
--- /dev/null
@@ -0,0 +1,361 @@
+<?php
+/**
+ * Table Definition for core_template 
+ *
+ *
+ * The idea here is that it contains all the strings in the templates with a language '' (empty)
+ * , it then generates a matching set of strings
+ * 
+ */
+class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
+
+class Pman_Core_DataObjects_Core_template  extends DB_DataObject 
+{
+    ###START_AUTOCODE
+    /* the code below is auto generated do not remove the above tag */
+
+    public $__table = 'core_template';         // table name
+    public $id;                              // int(11)  not_null primary_key auto_increment
+    public $template;                           // string(64)  not_null
+
+    public $updated;                        // blob(65535)  blob
+    public $lang;    // text  NOT NULL;
+    public $view_name; // eg mobile or desktop
+    /* the code above is auto generated do not remove the tag below */
+    ###END_AUTOCODE
+    
+    
+    function applyFilters($q, $au, $roo)
+    {
+        //DB_DataObject::debugLEvel(1);
+//        $x = DB_Dataobject::Factory($this->tableName());
+        
+        
+        // template scanning and syncing should be done by the UpdateDatabase Code.
+        //if (!$x->count() || !empty($q['_rescan'])) {
+            //DB_DataObject::debugLEvel(1);
+            //$tp = DB_DataObject::factory('core_template');
+            //$opts = HTML_FlexyFramework::get()->Pman_Core;
+            //$tp->syncTemplateDir(false, '', !empty($q['_rescan']));
+            //if (isset($q['lang'])) {
+            //    $this->syncLang($q['lang']);
+            //}
+        //} 
+//        $this->whereAdd("
+//                join_
+//            ");
+        
+    }
+        
+    function toRooArray($req)
+    {
+        $ret = $this->toArray();
+        if (!empty($req['_clean_name']) ) {
+            $ret['template_clean'] = preg_replace('#\.html$#i', '', $this->template);
+            
+        }
+        return $ret;
+        
+    }
+    
+    /*
+     * @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";
+        //print_r(func_get_args());
+        if (!$base) {
+            $ff = HTML_FlexyFramework::get();
+            if (!isset($ff->Pman_Core)) {
+                echo "[ERROR] Not scanning template directory - no set in Pman_Core[templateDir]\n";
+                return;
+            }
+            $opts = $ff->Pman_Core;
+            if (is_array($opts['templateDir'])) {
+                foreach($opts['templateDir'] as $type=>$dir) {
+                    $this->syncTemplateDir($dir, '', $force);
+                }
+                return;
+            }
+            
+            $base = $opts['templateDir'];
+            
+            
+        }
+        if($force){
+            $tmpls = DB_DataObject::factory('core_template');
+            $this->tmpls = $tmpls->fetchAll('template','id'); // dupes??
+        }
+        
+        $tmp_dir = $base . (empty($subdir) ? '' : '/') . $subdir;
+        
+        if(!is_dir($tmp_dir)){
+            return;
+        }
+        
+        $dh = opendir($tmp_dir);
+        if(!$dh){
+            return; // something went wrong!?
+        }
+        
+        while (($fn = readdir($dh)) !== false) {
+            // do we care that it will try and parse the template directory??? - not really..
+            // as we are only looking for php files..
+            if(empty($fn) || $fn[0] == '.'){
+                continue;
+            }
+
+            $fullpath = $tmp_dir."/".$fn;
+//            echo "filename: ".$fullpath." \n";    
+             
+            $relpath = $subdir . (empty($subdir) ? '' : '/') . $fn;
+            
+            if(is_dir($fullpath)){
+                // then recursively call self...
+//                var_Dump($base);
+//                var_Dump($subdir . (empty($subdir) ? '' : '/') . $fn);
+                $this->syncTemplateDir($base, $subdir . (empty($subdir) ? '' : '/') . $fn );
+                
+                continue;
+            }
+            if (!preg_match('/(\.html|\.txt|\.abw)$/', $fn)) {
+                continue;
+            }
+            
+             
+//            var_dump($tmp);
+//            var_dump($tmp_path);
+//            $fn = basename($fn);
+            if (isset($this->tmpls[$relpath])) {
+                unset($this->tmpls[$relpath]);
+            }
+            
+            
+            
+               
+            $template = $this->syncTemplate($relpath, true, false);
+//            var_dump($template);
+            if (is_a($template, 'PEAR_Error')) {
+                continue;
+            }
+        }
+        closedir($dh);
+//        
+        if($force){
+            foreach($this->tmpls as $id) {
+                $x = DB_DataObject::factory($this->tableName());
+                if ($x->get($id)) {
+                    $x->delete();
+                }
+            }
+        }
+    }
+    
+    /* compile a html template
+     *  
+     *  @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?)
+     *  
+     *  
+     */
+    function syncTemplatePage($pgdata)
+    {
+        //print_r($pgdata);
+        
+        $force = true;
+         //echo "compiling:"; print_r($pgdata);
+        // read the template and extract the translatable strings.
+        ini_set('memory_limit', '512M');
+        
+        //var_dump($n);
+        $n= $pgdata['template'];  // remove trailing slash..
+        
+        $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
+        $opts = HTML_FlexyFramework::get()->Pman_Core;
+        //print_R($opts);
+        //$dir = $opts['templateDir'] . '/' . $node;
+        $oo = array(
+            'fatalError'    => PEAR_ERROR_EXCEPTION,  
+            'disableTranslate' => false,
+            'templateDir' => $pgdata['template_dir'],
+            'compileDir' => $fopts['compileDir'] . '_translation_files',
+            'forceCompile' => true, //?? only for force above???
+        );
+         
+        // non-html templates - treat as such..
+        // abiword - treat as html?
+        if (!preg_match('/\.(html|abw)$/i', $pgdata['template'])) {
+            $oo['nonHTML'] = true;
+        }
+        
+        //print_r(array($oo, $n));
+        
+        
+        $flexy = new HTML_Template_Flexy( $oo );
+          
+        if (!$flexy->resolvePath ($pgdata['template'])) {
+            //echo "SKIP - could not resolve path?\n"; print_r($oo);
+            return false;
+        }
+        
+        // attempt to find the template... record.
+        $tmpl = DB_DataObject::Factory($this->tableName());
+       
+        $tmpl->view_name = $pgdata['base'];
+        if ($tmpl->get('template',  $pgdata['template'])) {
+            if (strtotime($tmpl->updated) >= filemtime($flexy->resolvePath ($pgdata['template']))) {
+                return $tmpl;
+            }
+        }
+        
+        
+        
+        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'])));
+            if ($tmpl->id) {
+                $tmpl->update($tmpl);
+            } else {
+                $tmpl->lang = 'en';
+                $tmpl->insert();
+            }
+            
+            
+            return false;
+        }
+       
+      
+        if (is_a($r,'PEAR_Error')) {
+            
+           // echo $r->toString(). "\n";
+            return $r;
+        }
+          
+        $tmpl = DB_DataObject::Factory($this->tableName());
+        $tmpl->words = file_exists($flexy->getTextStringsFile) ?
+                unserialize(file_get_contents($flexy->getTextStringsFile)) :
+                array();
+        
+        $tmpl->contentStrings   = $flexy->compiler->contentStrings;
+        //var_dump(file_exists($flexy->getTextStringsFile));
+        //print_r($tmpl->words);
+        $tmpl->currentTemplate  = $flexy->currentTemplate;
+        
+        $tmpl->view_name = $pgdata['base'];
+        
+        
+        if (!$tmpl->get('template',  $pgdata['template'])) {
+            
+            $tmpl->template = $pgdata['template'];
+            $tmpl->lang = 'en'; /// ??? hard coded??
+            $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
+            $tmpl->insert();
+        } else {
+            $xx =clone($tmpl);
+            // has it been cahnged...
+            //if (!$force && filemtime($flexy->currentTemplate) == strtotime($tmpl->updated)) {
+                // nothing changed..
+            //    return $tmpl;
+            //}
+            if (empty($tmpl->lang))  {
+                //echo "FIX LANG?";exit;
+                $tmpl->lang = 'en'; /// ??? hard coded??
+            }
+            
+            $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
+            $tmpl->update($xx);
+        }
+      
+        
+        $x = DB_DataObject::Factory('core_templatestr');
+        $x->syncTemplateWords($tmpl);
+        
+        // if file_exists ( template/path/name.php << eg. a matching view..)
+        // then create a system page for this. 
+        
+        
+        
+        $x = DB_DataObject::Factory('core_template_element');
+        $tmpl->elements = $x->syncTemplateElement($tmpl,
+                file_get_contents($flexy->currentTemplate),
+                $flexy->compiler->contentStrings,
+                false);
+        
+        
+        
+        return clone($tmpl);
+    
+    }
+    function syncPhpGetText($pgdata)
+    {
+        $tmpl = DB_DataObject::Factory($this->tableName());
+        $tmpl->view_name = $pgdata['base'];
+        $tmpl->currentTemplate = $pgdata['template_dir'] . '/'. $pgdata['template'];
+        
+        if ($tmpl->get('template',  $pgdata['template'])) {
+            if (strtotime($tmpl->updated) >= filemtime( $tmpl->currentTemplate )) {
+                //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));
+            
+        }
+        // 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);
+        }
+      
+        $words = array_unique($words);
+        
+        if (!count($words)) {
+            return;
+        }
+        
+             
+        $tmpl->words = $words;
+            
+        $x = DB_DataObject::Factory('core_templatestr');
+        $x->syncTemplateWords($tmpl);    
+         
+        
+        return $tmpl;
+        
+        
+        
+    }
+}
\ No newline at end of file
diff --git a/DataObjects/Core_template_element.php b/DataObjects/Core_template_element.php
new file mode 100644 (file)
index 0000000..ad0e3b3
--- /dev/null
@@ -0,0 +1,240 @@
+<?php
+/**
+ * Table Definition for core_template 
+ *
+ *
+ * The idea here is that it contains all the strings in the templates with a language '' (empty)
+ * , it then generates a matching set of strings
+ * 
+ */
+class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
+
+class Pman_Core_DataObjects_Core_template_element  extends DB_DataObject 
+{
+    ###START_AUTOCODE
+    /* the code below is auto generated do not remove the above tag */
+
+    public $__table = 'core_template_element';         // table name
+    public $id;                              // int(11)  not_null primary_key auto_increment
+    public $template_id;                           // string(64)  not_null
+    public $name;
+    /* the code above is auto generated do not remove the tag below */
+    ###END_AUTOCODE
+    
+    
+    
+    function applyFilters($q, $au, $roo)
+    {
+        if (isset($q['_core_page_id'] )) {
+            if (empty($q['_core_page_id'] )) {
+                $this->whereAdd('1=0');
+                return;
+            }
+            $p = DB_DataObject::factory('core_page');
+            $p->get($q['_core_page_id']);
+            $this->template_id = $p->template_id;
+            
+            
+        }
+        
+        
+    }
+    /**
+     *
+     *  the purpose of this is to find all the block[xxxx]
+     *  and make a row for then...
+     *
+     * it should return all the 
+     */
+    
+    
+    function syncTemplateElement($template)
+    {
+        $tn = $this->tableName();
+        
+        $contents = file_get_contents($template->currentTemplate);
+        $contentStrings = $template->contentStrings;
+        
+        //print_R($template->contentStrings);
+        
+        // this should also look for <flexy:use\s+content="\{block\[([A-Za-z0-9_]+)\]^+"\s" (default_content) </flexy:use>
+        // if it finds this
+        
+        // -> make sure there is a reference to element in core_template_element...
+        // -> System PAGE and elements???? with that text as the contents..
+        $core_page_strings = array();
+        
+        $matches = array();
+        if (preg_match_all('/flexy\:content=\"(\{block\[([A-Za-z0-9_-]+)\]\.([A-Za-z0-9_]+)([^"]*))"/',
+            $contents,
+            $matches
+        )) {
+            // print_R($matches);
+            // remove the full match..
+            // so that the match string matches the old format.
+            // the match[0] will be the one them matches contentStrings
+            array_shift($matches);
+            foreach($matches[0] as $i=>$k) {
+                if (!isset($contentStrings[$k])) {
+                    continue;
+                }
+                //print_R($matches);
+                if (!isset($core_page_strings[ $matches[1][$i] ])) {
+                    $core_page_strings[ $matches[1][$i] ] = array();
+                }
+                $core_page_strings[ $matches[1][$i] ][ $matches[2][$i] ] = trim($contentStrings[$k]);
+//                print_r(trim($contentStrings[$k]));
+            }
+        }
+        
+        
+        
+        $old_matches = array();
+        if (preg_match_all('#\{block\[([A-Za-z0-9_-]+)\]\.#',
+                $contents,
+                $old_matches
+        )) {
+            
+            // pushes old matches onto new ones..
+            foreach($old_matches[0] as $i =>$v) {
+                $matches[0][] = $v;
+                $matches[1][] = $old_matches[1][$i];
+                
+                if (!isset($core_page_strings[ $matches[1][$i] ])) {
+                    $core_page_strings[ $matches[1][$i] ] = array(
+                        'title' => trim($matches[1][$i]),
+                        'body' => 'Fill in text here'
+                        
+                    );
+                    
+                }
+//                print_r(trim($matches[1][$i]));
+            }
+        }
+        //print_r($core_page_strings);
+        // why delete the template???
+        
+        // if (empty($matches[0]) && empty($old_matches[0])) {
+        //     $this->query("
+        //         DELETE FROM {$tn} WHERE template_id = {$template->id}
+        //     ");
+        //     return;
+        // }
+         
+         
+          
+        /// ---- USE THE SAME CODE - 
+        
+        
+        $elements = array_unique($matches[1]);
+        //print_r($elements);
+        
+        $ret = array();
+        
+        $t = DB_DataObject::Factory($tn);
+        $t->template_id = $template->id;
+        $base = clone($t);
+        $old = $t->fetchAll('name', 'id');
+        
+        foreach($elements as $el) {
+            
+            if (!isset($old[$el])) {
+                $t = clone($base);
+                $t->name = $el;
+                $t->content_strings = isset($core_page_strings[$el]) ? $core_page_strings[$el] : array();
+                
+                $t->insert();
+                $ret[] = clone($t);
+            } else {
+                $t =DB_DataObject::Factory($tn);
+                $t->get($old[$el]);
+                $t->content_strings = isset($core_page_strings[$el]) ? $core_page_strings[$el] : array();
+                unset($old[$el]);
+                $ret[] = clone($t);
+                // got element already.. ignore it..
+                
+            }
+            // add
+            
+        }
+        
+        // delete elements, and  pages pointing to this element.. --- sounds about right..
+        
+        foreach($old as $n=>$id) {
+            $t = DB_DataObject::Factory($tn);
+            $t->get($id);
+            $t->delete();
+            // de'reference the core_pages that refered to it..
+            $core = DB_DataObject::factory('core_page');
+            $core->query("UPDATE core_page set is_system_page = 0, element_id= 0 WHERE element_id = {$t->id}");
+            
+        }
+        
+        return $ret;
+        
+    }
+    
+    function syncTemplateFromPage($pgdata)
+    {
+        //print_r($pgdata);
+        if (empty($pgdata['page'])) {
+            return false;
+        }
+        $element_id = DB_DataObject::factory('core_enum')->lookup('core_page_type', 'element'); 
+
+        $core = DB_DataObject::factory('core_page');
+        //DB_DataObject::DebugLevel(1);
+        $core->setFrom(array(
+                'page_type_id'  => $element_id,
+                'page_link' => $this->name,
+                'parent_id' => $pgdata['page']->id,
+                'is_element' => 1,
+                'element_id' => $this->id,
+                'language' => 'en',
+                'is_system_page' => 1
+        ));
+        if ($core->count()) {
+            $core->fetch();
+            $this->page = clone($core);
+            return;
+        }
+        
+        
+        $core = DB_DataObject::factory('core_page');
+        $core->title = $this->name; /// placeholder..
+         
+       // from parsing earlier..
+        
+        //print_R($this->content_strings);
+        
+        // allow for bodyToDisplayHTML... etc..
+        foreach( array('title', 'body', 'extended') as $prop) {
+            if (isset($this->content_strings[$prop])) {
+                continue;
+            }
+            foreach($this->content_strings as $k=>$v) {
+                if (substr($k,0,strlen($prop)) == $prop) {
+                    $this->content_strings[$prop] = $v;
+                }
+                
+            }
+        }
+        $core->setFrom($this->content_strings); 
+        
+        $core->setFrom(array(
+                'page_type_id'  => $element_id,
+                'page_link' => $this->name,
+                'parent_id' => $pgdata['page']->id,
+                'is_element' => 1,
+                'element_id' => $this->id,
+                'language' => 'en',
+                'is_system_page' => 1
+        ));
+        $core->insert();    
+        
+        $this->page = clone($core);
+    }
+    
+    
+}
+    
\ No newline at end of file
diff --git a/DataObjects/Core_templatestr.php b/DataObjects/Core_templatestr.php
new file mode 100644 (file)
index 0000000..60d4e6f
--- /dev/null
@@ -0,0 +1,637 @@
+<?php
+/**
+ * Table Definition for core_templatestr
+ *
+ *
+ * The idea here is that it contains all the strings in the templates with a language '' (empty)
+ * , it then generates a matching set of strings
+ * 
+ */
+class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
+
+class Pman_Core_DataObjects_Core_templatestr extends DB_DataObject 
+{
+    ###START_AUTOCODE
+    /* the code below is auto generated do not remove the above tag */
+
+    public $__table = 'core_templatestr';         // table name
+    public $id;                              // int(11)  not_null primary_key auto_increment
+    public $template_id;                           // string(64)  not_null
+    public $txt;                    // datetime(19)  multiple_key binary
+    public $updated;                        // blob(65535)  blob
+    public $src_id;                          // int(11)  not_null
+    public $lang;    // text  NOT NULL;
+    public $mdsum;    // text  NOT NULL;
+    public $active;
+    public $on_table;
+    public $on_id;
+    public $on_col;
+    /* the code above is auto generated do not remove the tag below */
+    ###END_AUTOCODE
+    
+    function beforeInsert($q,$roo)
+    {
+        if(!empty($q['_rescan'])){
+            $this->syncLang($q['_rescan']);
+            $roo->jok('OK');
+        }
+    }
+    
+    
+    function applyFilters($q, $au, $roo)
+    {
+        if (!empty($q['_tree'])) {
+            $this->applyFiltersTree($q,$roo);
+        }
+        
+        if(!empty($q['on_table']) && !is_numeric($q['template_id'])){
+            $this->template_id = 0;
+        }
+        if (!empty($q['_search_txt'])) {
+            $str = $this->escape($q['_search_txt']);
+            $this->whereAdd("core_templatestr.txt like '%{$str}%' OR join_src_id_id.txt like '%{$str}%'");
+            
+        }
+    }
+    function translateTableCol($obj, $col, $lang)
+    {
+        $cts = DB_DataObject::factory('core_templatestr');
+        $cts->lang = $lang;
+        $cts->on_table = $obj->tableName();
+        $cts->on_id = $obj->pid();
+        $cc = clone($cts);
+        if(!$cts->find(true)){
+            return $obj->$col;
+        }
+        
+        
+        if(empty($cts->txt)){
+            return $obj->$col;
+        }
+            
+        return $cts->txt;
+    }
+    /**
+     *
+     * insert the origanal table text
+     * 
+     * @param type $roo
+     * @param type $obj
+     * @param type $chg
+     * @return type 
+     */
+    function onTableChange($roo, $obj, $chg)
+    {
+        
+        $ff = HTML_FlexyFramework::get()->Pman_Core;
+            
+        if(empty($ff['DataObjects_Core_templatestr']['tables'])){
+            return;
+        }
+        $tn = $obj->tableName();
+        if(empty($ff['DataObjects_Core_templatestr']['tables'][$tn])){
+            return;
+        }
+        $cols = $ff['DataObjects_Core_templatestr']['tables'][$tn];
+        
+        
+        foreach($cols as $c) {
+            $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..
+            }
+            $x->active = 1;
+            $x->src_id = 0;
+            $x->txt = $obj->$c;
+            $x->mdsum = md5($obj->$c);
+            $x->template_id = 0;
+            $x->updated = date('Y-m-d H:i:s', strtotime("NOW"));
+            $up ? $x->update() : $x->insert();
+        }
+        
+        
+    }
+    
+    
+    function applyFiltersTree($q,$roo)
+    {
+        if (empty($q['node'])) {
+            $roo->jerr("invalid node");
+        }
+        switch(true) {
+            
+            case ($q['node'] == 'transtree'):
+               // DB_DataObject::debugLevel(1);
+                $x = DB_Dataobject::Factory($this->tableName());
+                $x->selectAdd();
+                $x->selectAdd('distinct(lang) as lang');
+                $x->whereAdd("lang != ''");
+                $ret= array();
+                foreach( $x->fetchAll('lang') as $l) {
+                    $ret[] = array(
+                        'text'=>$l,
+                        'id' => 'lang:'.$l,
+                        'language' => true
+                    );
+                }
+                if (empty($ret)) {
+                    $ret[] = array(
+                        'text'=>'en',
+                        'id' => 'lang:en',
+                        'language' => true
+                    );
+                }
+                $roo->jdata($ret);
+            
+            case  preg_match('/^lang:/', $q['node']):
+                
+                $lang = preg_replace('/^lang:/', '', $q['node']);
+                $ret= array();  
+                $x = DB_DataObject::factory('core_templatestr');
+                $x->autoJoin();
+                $x->selectAdd();
+                $x->selectAdd('distinct(view_name) as view_name');
+                $x->lang = $lang;
+                $x->orderBy('view_name DESC');
+                $x->find();
+                while($x->fetch()) {
+                    $ret[] = array( 
+                        'text'=> $x->view_name,
+                        'id' => 'view:'. $lang .':'.  $x->view_name,
+                        'leaf' => false
+                    );
+                }
+                
+                
+                $roo->jdata($ret);
+                
+                   break;
+                
+                
+            case  preg_match('/^view:/', $q['node']):
+                
+               
+                $bits= explode(":",preg_replace('/^view:/', '', $q['node']));
+                
+                 $x = DB_DataObject::factory($this->tableName());
+                $x->autoJoin();
+                $x->selectAdd();
+                $x->selectAdd('distinct(core_templatestr.template_id) as template_id');
+                $x->whereAdd("join_template_id_id.view_name = '{$x->escape($bits[1])}'");
+                $x->lang = $bits[0];
+                $ids = $x->fetchAll('template_id');
+                
+                $ret= array();
+                //add the table type lists
+                
+                $ff = HTML_FlexyFramework::get()->Pman_Core;
+                
+                if(!empty($ff['DataObjects_Core_templatestr']['tables'])){
+                    foreach($ff['DataObjects_Core_templatestr']['tables'] as $table=>$v){
+                        $ret[] = array(
+                            'text'=> $table,
+                            'on_table' => $table,
+                            'id' => 0,
+                            'leaf' => true
+                        );
+                    }
+                }
+                
+//                $x->orderBy('template ASC');
+//                $x->whereAdd("lang != ''");
+                
+                //below are old code
+                $xx = DB_Dataobject::Factory('core_template');
+                $xx->whereAddIn('id', $ids, 'int');
+                $xx->selectAdd();
+                $xx->selectAdd("
+                               
+                    id, concat(  template) as template_name
+                ");
+                $xx->orderBy('template_name ASC');
+                
+                foreach( $xx->fetchAll('id', 'template_name') as $l =>$n) {
+                    $ret[] = array(
+                        'text'=> $n,
+                        'id' => $l,
+                        'leaf' => true
+                    );
+                }
+                
+                $roo->jdata($ret);
+                 break;
+        }
+                
+        
+        
+    }
+    
+    
+    /**
+     *
+     * 
+     * @param object $tmpl core_template data object
+     * @param array $words array of words 
+     */ 
+    function syncTemplateWords($tmpl, $keyvalue = false)
+    {
+        
+        $words = $tmpl->words;
+        // mapping for template : 
+        //tablename => $n (templatename) 
+        //tableid => $k (key value)
+        //colname => $n (templatename) 
+        // mdsum => md5(sum)
+        //
+        //print_r($words);exit;
+        // grab original
+        $tt = DB_DataObject::factory($this->tableName());
+
+
+        $t = DB_DataObject::factory($this->tableName());
+        $t->template_id = $tmpl->id;
+        $t->whereAdd("lang = ''");
+        
+        
+        // we have a situation where old md sums where created..
+        
+        
+        
+        $cur = $t->fetchAll('mdsum', 'id'); 
+        
+        
+        
+        
+        
+        
+        
+        // now loop through current..
+        $cwords = array();// not in used??
+        $active = array();
+//        echo "sync Template Words... \n";
+//        print_r($words);
+        
+        foreach($words as $k=>$v) {
+            
+            
+            $v = trim($v);
+            
+            $md = $keyvalue ? $k : md5($v);
+            
+            // check to see if there are more that one versions of that md5
+            
+            
+            if (!isset($cur[$md])) {
+                // create a record for it..
+                $t = DB_DataObject::factory($this->tableName());
+                $t->setFrom(array(
+                    'txt' => $v,
+                    'lang' => '',// by default should a english 
+                    'updated' => date('Y-m-d H:i:s', strtotime("YESTERDAY")),
+                    'template_id'=>$tmpl->id,
+                    'mdsum' => $md,
+                    'src_id' => 0,
+                    'active' => 1,
+                ));
+                $active[] =  $t->insert();
+                continue;
+            }  
+            $cur[$md] = $this->checkDupes($tmpl->id, '',  $cur[$md] , $md);
+            
+            $active[] = $cur[$md];
+            
+            // we have it already? - 
+            $tt->query("UPDATE {$this->tableName()}
+                        SET active= 1
+                        WHERE
+                        id = ".$cur[$md]);
+            unset($cur[$md]);
+            
+        }
+        // delete unused.
+        
+        
+        
+
+        $deactive = array();
+        if (count(array_values($cur))) {// de-active unused
+
+            $t = DB_DataObject::factory($this->tableName());
+//            echo "de-active current?? \n";
+//            print_r($cur);
+//            echo "\n";
+            $deactive = array_values($cur);
+            $t->query("UPDATE core_templatestr
+                      SET active = 0 WHERE id in (" . implode(',' ,$deactive) . ")
+                     ");
+        }
+        
+        // delete all the items that are not relivant.
+        // clear orphaned chidren - it just blanks out the src id, so they can be used as suggestions..?
+        // this does not help - as it just puts random strings in there.. - with no reference to the original text..
+        $t = DB_DataObject::factory($this->tableName());
+    
+        // this will active the child data
+        if (empty($active)) {// set the active array to empty
+            $active = array(-1);
+        }
+        $t->query("UPDATE  core_templatestr 
+                SET active = 1
+                  WHERE
+                     src_id IN (". implode(',' ,$active) . ")
+                    AND
+                    template_id = {$tmpl->id}
+        ");
+        //deactive the child data
+        if (empty($deactive)) {
+            $deactive = array(-1);
+        }
+        $t->query("UPDATE  core_templatestr 
+                SET active = 0
+                  WHERE
+                    src_id IN (". implode(',' ,$deactive) . ")
+                    AND
+                    template_id = {$tmpl->id}
+                    AND
+                    lang != ''
+        ");
+    }
+    
+    function checkDupes($tid, $lang, $id, $mdsum) {
+        
+        $t = DB_DataObject::factory($this->tableName());
+        $t->template_id = $tid;
+        $t->mdsum = $mdsum;
+        $t->whereAdd("lang = '{$lang}'");
+        if ($t->count() == 1) {
+            return $id; // only got one ... no issues..
+        }
+        
+        //echo "GOT DUPES : $id, $lang, $id , $mdsum\n";
+        
+        //DB_DataObject::debugLevel(1);
+        // find out if any of them have got translations.
+        $ids = $t->fetchAll('id');
+        
+        
+        $t = DB_DataObject::factory($this->tableName());
+        $t->whereAddIn('src_id', $ids, 'int');
+        $t->whereAdd("txt != ''");
+        $t->orderBy('updated DESC');
+        if ($t->count()) {
+            $t->limit(1);
+            // do any translations exist?
+            $t->find(true);
+            $id = $t->src_id;
+        }
+        
+        
+        // delete all the others...
+        $t = DB_DataObject::factory($this->tableName());
+        $t->whereAddIn('src_id', $ids, 'int');
+        $t->whereAdd("src_id != $id");
+        $t->find();
+        while($t->fetch()) {
+            $tt = clone($t);
+            $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
+            $tt->update($t);
+        }
+        $t = DB_DataObject::factory($this->tableName());
+        $t->whereAddIn('id', $ids, 'int');
+        $t->whereAdd("id != $id");
+        $t->find();
+        while($t->fetch()) {
+            $tt = clone($t);
+            $tt->mdsum = $t->mdsum . '-bad-'. $t->id;
+            $tt->update($t);
+        }
+        // this is done by calling code 
+        //$t = DB_DataObject::factory($this->tableName());
+        //$t->query("update core_templatestr set active= 1 where src_id = $id");
+        
+        
+        
+       //exit;
+        return $id;
+    
+               
+         
+    }
+    
+    function syncLang($lang)
+    {
+        // bugs with our old code...
+//        die('in?');
+        $tn = $this->tableName();
+        $t = DB_DataObject::factory($tn);
+        $t->query("DELETE FROM {$tn} WHERE lang !='' AND src_id = 0 AND on_table  = ''");
+        
+        // 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;
+        }
+        $ids = array_keys($id_tmp);
+        
+        // matching by language:
+        $t = DB_DataObject::factory($tn);
+        $t->whereAddIn('src_id', $ids , 'int');
+        $t->lang = $lang;
+        //$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();
+        }
+        
+        
+        
+    }
+    
+    // called from flexy to translate a string.
+    
+    
+    function translateFlexyString($flexy, $string)
+    {
+        //var_dump($string);
+        $debug = false;;
+        //if (!empty($_REQUEST['_debug'])) { $debug= true; }
+        
+        // using $flexy->currentTemplate -> find the template we are looking at..
+        // then find the string for $flexy->options['locale']
+        //DB_DataObject::debugLevel(1);
+        if ($debug) { var_dump($string); }
+        
+        static $cache = array(); // cache of templates..
+        
+        $ff = HTML_FlexyFramework::get();
+        $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
+        
+        if ($debug) { var_dump(array('view_name'=> $view_name)); }
+        
+        $tempdir = '';
+        foreach($flexy->options['templateDir'] as $td) {
+            if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
+                $tempdir = $td;
+                break;
+            }
+        }
+        
+        
+        $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
+        
+        if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
+            if ($debug) { echo "from cache no match - $string\n"; }
+            return $string;
+        }
+        
+        if (!isset($cache[$tmpname])) {
+                
+            
+            
+            $tmpl = DB_DataObject::factory('core_template');
+            if ($view_name !== false) {
+                $tmpl->view_name = $view_name;
+            }
+            if(!$tmpl->get('template', $tmpname)){
+                // strip of site prefix if set...
+                 
+                $tmpl = DB_DataObject::factory('core_template');
+                if(!$tmpl->get('template', $tmpname)){
+                    //var_dump("no template? {$tmpname} or {$relpath}" );
+                    $cache[$tmpname] = false;
+                    if ($debug) { echo "no template found - no match - $string\n"; }
+                    return $string;
+                }
+            }
+            $cache[$tmpname] = $tmpl;
+        } else {
+            $tmpl = $cache[$tmpname] ;
+        }
+         
+    
+        //get original template id 
+        $orig = DB_DataObject::factory($this->tableName());
+        $orig->lang = '';
+        $orig->template_id = $tmpl->id;
+        $orig->active = 1;
+        if(!$orig->get( 'mdsum' , md5(trim($string)))){
+             //var_dump('no text? '. $string);
+            if ($debug) { echo "no original string found tplid: {$tmpl->id}\n"; }
+            return false;
+        }
+         
+        //find out the text by language
+        $x = DB_DataObject::factory($this->tableName());
+        $x->lang = $flexy->options['locale'];
+        $x->template_id = $tmpl->id;
+        if(!$x->get('src_id', $orig->id)){
+            //var_dump('no trans found' . $orig->id);
+            if ($debug) { echo "no translation found\n"; }
+            return false;
+        }
+        if ($debug) { echo "returning $x->txt\n"; }
+        //var_Dump($x->txt);
+        return empty($x->txt) ? $string : $x->txt;
+    }
+    
+    // determine if a complied template need recompling
+    
+    function translateChanged($flexy)
+    {
+        //return true;
+        // var_dump('check changed?');
+        //DB_DataObject::debugLevel(1);
+        //var_Dump(array($flexy->options['templateDir'][0], $flexy->currentTemplate));
+        
+        //var_dump($flexy->compiledTemplate);
+        $utime = file_exists($flexy->compiledTemplate) ?  filemtime( $flexy->compiledTemplate) : 0;
+        
+        
+        static $cache = array(); // cache of templates..
+        
+        $ff = HTML_FlexyFramework::get();
+        $view_name = isset($ff->Pman_Core['view_name']) ? $ff->Pman_Core['view_name'] : false;
+        
+        $tempdir = '';
+        foreach($flexy->options['templateDir'] as $td) {
+            if (substr($flexy->currentTemplate, 0, strlen($td)) == $td) {
+                $tempdir = $td;
+                break;
+            }
+        }
+        
+        
+        $tmpname = substr($flexy->currentTemplate, strlen($td) +1);
+        
+        if (isset($cache[$tmpname]) && $cache[$tmpname] === false) {
+            return false;
+        }
+        
+        if (!isset($cache[$tmpname])) {
+                
+            
+            
+            $tmpl = DB_DataObject::factory('core_template');
+            if ($view_name !== false) {
+                $tmpl->view_name = $view_name;
+            }
+            if(!$tmpl->get('template', $tmpname)){
+                // strip of site prefix if set...
+                 
+                $tmpl = DB_DataObject::factory('core_template');
+                if(!$tmpl->get('template', $tmpname)){
+                    //var_dump("no template? {$tmpname} or {$relpath}" );
+                    $cache[$tmpname] = false;
+                    return false;
+                }
+            }
+            $cache[$tmpname] = $tmpl;
+        } else {
+            $tmpl = $cache[$tmpname] ;
+        }
+          
+         
+        
+        $x = DB_DataObject::factory($this->tableName());
+        $x->lang = $flexy->options['locale'];
+        $x->active = 1;
+        $x->template_id = $tmpl->id;
+        $x->whereAdd("updated > '". date('Y-m-d H:i:s', $utime)."'");
+        
+        return $x->count() ? true : false;
+        
+        
+    }
+    
+}
index 291dad3..d3d0c03 100644 (file)
@@ -20,4 +20,13 @@ class Pman_Core_DataObjects_Translations extends DB_DataObject
     
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
+    
+    function loadFromModule($modinfo)
+    {
+        
+        
+        
+        
+    }
+    
 }
index 2aeba9b..6e1c70d 100644 (file)
@@ -83,6 +83,15 @@ updated_by = core_person:id
 [core_person_settings]
 person_id = core_person:id
 
+
+
+[core_templatestr]
+src_id = core_templatestr:id
+template_id = core_template:id
+
+[core_template_element]
+template_id = core_template:id
+
 [database__render]
 core_project = name
 core_company= name
diff --git a/sql/core_template.sql b/sql/core_template.sql
new file mode 100644 (file)
index 0000000..62c8a04
--- /dev/null
@@ -0,0 +1,11 @@
+-- template .
+CREATE TABLE  core_template (
+  id int(11)  NOT NULL AUTO_INCREMENT,
+  template varchar(254)  NOT NULL,
+  updated datetime  NOT NULL,
+  lang varchar(6)  NOT NULL,
+  PRIMARY KEY (id),
+  INDEX lookup(template, lang)
+);
+
+ALTER TABLE core_template ADD COLUMN view_name varchar(32) NOT NULL default '';
diff --git a/sql/core_template_element.sql b/sql/core_template_element.sql
new file mode 100644 (file)
index 0000000..e10b61a
--- /dev/null
@@ -0,0 +1,10 @@
+
+
+-- templat eelement {element[xxxx].*}
+CREATE TABLE  core_template_element (
+  id int(11)  NOT NULL AUTO_INCREMENT,
+  name varchar(254)  NOT NULL,
+  PRIMARY KEY (id)
+);
+ALTER TABLE core_template_element ADD COLUMN template_id INT(11) NOT NULL DEFAULT 0;
diff --git a/sql/core_templatestr.sql b/sql/core_templatestr.sql
new file mode 100644 (file)
index 0000000..15bf3c3
--- /dev/null
@@ -0,0 +1,25 @@
+
+CREATE TABLE  core_templatestr (
+  id int(11)  NOT NULL AUTO_INCREMENT,
+  template varchar(254)  NOT NULL,
+  txt text  NOT NULL,
+  updated datetime  NOT NULL,
+  src_id int(11)  NOT NULL,
+  lang varchar(6)  NOT NULL,
+  PRIMARY KEY (id),
+  INDEX lookup(template, lang)
+);
+
+ALTER TABLE core_templatestr ADD COLUMN active INT(4) NOT NULL  DEFAULT 0;
+
+ALTER TABLE core_templatestr ADD COLUMN mdsum VARCHAR(64) NOT NULL DEFAULT '';
+ALTER TABLE core_templatestr ADD COLUMN template_id INT(11) NOT NULL DEFAULT 0;
+ALTER TABLE core_templatestr DROP COLUMN template;
+
+ALTER TABLE core_templatestr ADD COLUMN on_table VARCHAR(64) DEFAULT '';
+ALTER TABLE core_templatestr ADD COLUMN on_id INT(11) DEFAULT 0;
+ALTER TABLE core_templatestr ADD COLUMN on_col VARCHAR(64) DEFAULT '';
+
+ALTER TABLE core_templatestr ADD INDEX lookup_a(active, lang, on_table, on_id, on_col);
+
index 5b46560..aa879e7 100644 (file)
@@ -1,4 +1,7 @@
 
+
+-- depricated use core_template templatestr etc..
+
 CREATE TABLE  translations (
   id int(11)  NOT NULL AUTO_INCREMENT,
   PRIMARY KEY (id)