DataObjects/Cms_page.php
[Pman.Cms] / OutputTranslations.php
index 88949e7..7a48aea 100644 (file)
@@ -6,43 +6,182 @@ class Pman_Cms_OutputTranslations extends Pman
 {
     var $lang = 'en';
     
-    function get($lang)
+    function get($lang = '',  $opts = array())
     {
         if(!empty($lang)){
             $this->lang = $lang;
         }
+        $projects = array();
         
+        foreach ($this->modules() as $m){
+            $projects[] = "Pman.{$m}";
+        }
+        
+        $this->getTranslationsCache($projects);
+        exit;
+    }
+    
+    function getTranslations($projects = array())
+    {
         $ret = array();
+        
         $template = DB_DataObject::factory('cms_template');
+        $template->whereAddIn('view_name', $projects, 'string');
+        $template = $template->fetchAll('id');
+        
+        $base = DB_DataObject::factory('cms_templatestr');
+        $base->setFrom(array(
+            'lang' => '',
+            'active' => 1
+        ));
+        $base->whereAddIn('template_id', $template, 'int');
+        
+        $base = $base->fetchAll('id', 'template_id');
+        
+        $translation = DB_DataObject::factory('cms_templatestr');
+        $translation->autoJoin();
+        $translation->setFrom(array(
+            'template_id' => array_values($base),
+            'lang' => $this->lang,
+            'active' => 1
+        ));
+        $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();
+            }
 
-        foreach ($template->fetchAll() as $tmpl){
-            $base = DB_DataObject::factory('cms_templatestr');
-            $base->setFrom(array(
-                'template_id' => $template->id,
-                'lang' => '',
-                'active' => 1
-            ));
-            $base = $base->fetchAll('id');
+            $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);
+        }
+        
+        $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)
+                )
+            )
+        ) { 
             
-            $translation = DB_DataObject::factory('cms_templatestr');
-            $translation->autoJoin();
-            $translation->setFrom(array(
-                'template_id' => $template->id,
-                'lang' => $this->lang,
-                'active' => 1
-            ));
-            $translation->whereAddIn('cms_templatestr.src_id', $base, 'int');
+            header("HTTP/1.1 304 Not Modified");
+            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));
             
-            foreach ($translation->fetchAll() as $t){
-                if(!isset($ret[$tmpl->template])){
-                    $ret[$tmpl->template] = array();
-                }
-                
-                $ret[$tmpl->template][$t->src_id_mdsum] = (empty($t->txt)) ? $t->src_id_txt : $t->txt;
-            }
+            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));
         }
         
-        return $ret;
+        $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 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