Pman.Dialog.CmsBlog.bjs
[Pman.Cms] / OutputTranslations.php
index 083b211..7a48aea 100644 (file)
@@ -4,63 +4,184 @@ require_once 'Pman.php'; /// needed as we might not be included from pman..
 
 class Pman_Cms_OutputTranslations extends Pman
 {
-    var $cms_template = '';
-    var $on_table = '';
-    var $on_id = '';
-    var $on_col = '';
-    var $lang = '';
+    var $lang = 'en';
     
-    function get()
+    function get($lang = '',  $opts = array())
     {
-        DB_DataObject::debugLevel(1);
-        
-        if(!empty($this->template)){
-            return $this->templateTranslations();
+        if(!empty($lang)){
+            $this->lang = $lang;
         }
+        $projects = array();
         
-        if(!empty($this->on_table) && !empty($this->on_id) && !empty($this->on_col)){
-            return $this->tableTranslations();
+        foreach ($this->modules() as $m){
+            $projects[] = "Pman.{$m}";
         }
         
-        return array();
-        
+        $this->getTranslationsCache($projects);
+        exit;
     }
     
-    function templateTranslations()
+    function getTranslations($projects = array())
     {
         $ret = array();
         
         $template = DB_DataObject::factory('cms_template');
-        if(!$template->get('template', $this->cms_template)){
-            return $ret;
-        }
+        $template->whereAddIn('view_name', $projects, 'string');
+        $template = $template->fetchAll('id');
         
         $base = DB_DataObject::factory('cms_templatestr');
         $base->setFrom(array(
-            'template_id' => $template->id,
             'lang' => '',
             'active' => 1
         ));
+        $base->whereAddIn('template_id', $template, 'int');
         
-        $base = $base->fetchAll('id', 'mdsum');
+        $base = $base->fetchAll('id', 'template_id');
         
         $translation = DB_DataObject::factory('cms_templatestr');
+        $translation->autoJoin();
         $translation->setFrom(array(
-            'template_id' => $template->id,
+            'template_id' => array_values($base),
             'lang' => $this->lang,
             'active' => 1
         ));
-        $translation->whereAddIn('src_id', array_keys($base), 'int');
+        $translation->whereAddIn('cms_templatestr.src_id', array_keys($base), 'int');
+        
+        $translation->selectAdd();
+        $translation->selectAdd("
+            cms_templatestr.txt AS txt,
+            join_template_id_id.template AS template_id_template,
+            join_src_id_id.mdsum AS src_id_mdsum,
+            join_src_id_id.txt AS src_id_txt
+        ");
+        
+        foreach ($translation->fetchAll() as $t){
+            if(!isset($ret[$t->template_id_template])){
+                $ret[$t->template_id_template] = array();
+            }
+
+            $ret[$t->template_id_template][$t->src_id_mdsum] = (empty($t->txt)) ? $t->src_id_txt : $t->txt;
+        }
+        
+        return $ret;
+    }
+    
+    function getTranslationsCache($projects = array())
+    {
+        if(empty($projects)){
+            return;
+        }
+        
+        $this->sessionState(0);
+        
+        $ff = HTML_FlexyFramework::get();
+        
+        $ui = posix_getpwuid(posix_geteuid());
+        
+        $this->cachePath = session_save_path() . '/' .
+                $ui['name'] . '-' . $ff->project . '-templatestr/' . 
+                $ff->project . '-' . $ff->version . '-' . $this->lang . '.fulloutput.json';
+        
+        $is_new = false;
+        
+        if(!$this->is_cached() ){
+            $is_new =true;
+            $translations = $this->getTranslations($projects);
+        
+            $output = "";
+
+            foreach ($translations as $file => $trans){
+                $t = json_encode($trans);
+
+                $cls = str_replace('.bjs', '', $file);
+                $output .= "try { ";
+                $output .= "Roo.apply({$cls}._strings, {$t});";
+                $output .= " } ";
+                $output .= "catch(e){}; ";
+            }
+
+            file_put_contents($this->cachePath, $output);
+        }
         
-        $translation = $translation->fetchAll('id', 'txt');
+        $last_modified_time = filemtime($this->cachePath);
+        
+        if (
+            !$is_new
+            &&
+            (
+                
+                (
+                    isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
+                    strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
+                )
+                ||
+                (
+                    isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
+                    trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($this->cachePath)
+                )
+            )
+        ) { 
+            
+            header("HTTP/1.1 304 Not Modified");
+            exit;
+        }
         
-        print_R($translation);exit;
+        header('Content-Type: text/javascript');
         
+        // dont do the 'cachy thing' on dev servers...
+        if (!$this->is_dev()) { 
+            header("Pragma: public");
+            header('Content-Length: '. filesize($this->cachePath));
+            
+            header('Cache-Control: max-age=2592000, public');
+            header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
+            header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
+            header('Etag: '. md5($this->cachePath));
+        }
         
+        $fh = fopen($this->cachePath,'r');
+        fpassthru($fh);
+        fclose($fh);
+    }
+    
+    function is_dev()
+    {
+        if (!empty($_SERVER['HTTP_HOST']) && preg_match('/^dev/', $_SERVER['HTTP_HOST'])) {
+            return true;
+        }
+        return false;
     }
     
-    function tableTranslations()
+    function is_cached()
     {
+        if (!empty($_GET['no_cache'])) {
+            return false;
+        }
+        if ($this->is_dev()) {
+            return false;
+        }
+        
+        
+        $dest = dirname($this->cachePath);
+        
+        if (!file_exists($dest)) {
+            mkdir($dest, 0700, true);
+        }
+        
+        if(!file_exists($this->cachePath)){
+            return false;
+        }
+      
+        $cms_templatestr = DB_DataObject::factory('cms_templatestr');
+        $cms_templatestr->active = 1;
+        $cms_templatestr->selectAdd();
+        $cms_templatestr->selectAdd("MAX(updated) AS latest");
+        $cms_templatestr->find(true);
+        
+        if(filemtime($this->cachePath) < strtotime($cms_templatestr->latest)){
+            return false;
+        }
         
+        return true;
     }
 }
\ No newline at end of file