UpdateBjsTemplates.php
[Pman.Cms] / OutputTranslations.php
1 <?php
2
3 require_once 'Pman.php'; /// needed as we might not be included from pman..
4
5 class Pman_Cms_OutputTranslations extends Pman
6 {
7     var $lang = 'en';
8     
9     function get($lang = '',  $opts = array())
10     {
11         if(!empty($lang)){
12             $this->lang = $lang;
13         }
14         $projects = array();
15         
16         foreach ($this->modules() as $m){
17             $projects[] = "Pman.{$m}";
18         }
19         
20         $this->getTranslationsCache($projects);
21         exit;
22     }
23     
24     function getTranslations($projects = array())
25     {
26         $ret = array();
27         
28         $template = DB_DataObject::factory('cms_template');
29         $template->whereAddIn('view_name', $projects, 'string');
30         $template = $template->fetchAll('id');
31         
32         $base = DB_DataObject::factory('cms_templatestr');
33         $base->setFrom(array(
34             'lang' => '',
35             'active' => 1
36         ));
37         $base->whereAddIn('template_id', $template, 'int');
38         
39         $base = $base->fetchAll('id', 'template_id');
40         
41         $translation = DB_DataObject::factory('cms_templatestr');
42         $translation->autoJoin();
43         $translation->setFrom(array(
44             'template_id' => array_values($base),
45             'lang' => $this->lang,
46             'active' => 1
47         ));
48         $translation->whereAddIn('cms_templatestr.src_id', array_keys($base), 'int');
49         
50         $translation->selectAdd();
51         $translation->selectAdd("
52             cms_templatestr.txt AS txt,
53             join_template_id_id.template AS template_id_template,
54             join_src_id_id.mdsum AS src_id_mdsum,
55             join_src_id_id.txt AS src_id_txt
56         ");
57         
58         foreach ($translation->fetchAll() as $t){
59             if(!isset($ret[$t->template_id_template])){
60                 $ret[$t->template_id_template] = array();
61             }
62
63             $ret[$t->template_id_template]['_strings'][$t->src_id_mdsum] = (empty($t->txt)) ? $t->src_id_txt : $t->txt;
64             $ret[$t->template_id_template]['_strings_map'][$t->src_id_txt] = $t->src_id_mdsum;
65         }
66          
67         return $ret;
68     }
69     
70     function getTranslationsCache($projects = array())
71     {
72         if(empty($projects)){
73             return;
74         }
75         
76         $this->sessionState(0);
77         
78         $ff = HTML_FlexyFramework::get();
79         
80         $ui = posix_getpwuid(posix_geteuid());
81         
82         $this->cachePath = session_save_path() . '/' .
83                 $ui['name'] . '-' . $ff->project . '-templatestr/' . 
84                 $ff->project . '-' . $ff->version . '-' . $this->lang . '.fulloutput.json';
85         
86         $is_new = false;
87         
88         if(!$this->is_cached() ){
89             $is_new =true;
90             $translations = $this->getTranslations($projects);
91         
92             $output = "";
93
94             foreach ($translations as $file => $trans){
95                 $t = json_encode($trans['_strings']);
96                 $tmap = json_encode($trans['_strings_map']);
97
98                 $cls = str_replace('.bjs', '', $file);
99                 $output .= "try { ";
100                 $output .= "Roo.apply({$cls}._strings, {$t});";
101                 $output .= "Roo.apply({$cls}._strings_map, {$tmap});";
102                 $output .= " } ";
103                 $output .= "catch(e){}; ";
104             }
105
106             file_put_contents($this->cachePath, $output);
107         }
108         
109         $last_modified_time = filemtime($this->cachePath);
110         
111         if (
112             !$is_new
113             &&
114             (
115                 
116                 (
117                     isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
118                     strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
119                 )
120                 ||
121                 (
122                     isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
123                     trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($this->cachePath)
124                 )
125             )
126         ) { 
127             
128             header("HTTP/1.1 304 Not Modified");
129             exit;
130         }
131         
132         header('Content-Type: text/javascript');
133         
134         // dont do the 'cachy thing' on dev servers...
135         if (!$this->is_dev()) { 
136             header("Pragma: public");
137             header('Content-Length: '. filesize($this->cachePath));
138             
139             header('Cache-Control: max-age=2592000, public');
140             header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
141             header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
142             header('Etag: '. md5($this->cachePath));
143         }
144         
145         $fh = fopen($this->cachePath,'r');
146         fpassthru($fh);
147         fclose($fh);
148     }
149     
150     function is_dev()
151     {
152         if (!empty($_SERVER['HTTP_HOST']) && preg_match('/^dev/', $_SERVER['HTTP_HOST'])) {
153             return true;
154         }
155         return false;
156     }
157     
158     function is_cached()
159     {
160         
161         if (!empty($_GET['no_cache'])) {
162             return false;
163         }
164         if ($this->is_dev()) {
165             return false;
166         }
167         
168         
169         $dest = dirname($this->cachePath);
170         
171         if (!file_exists($dest)) {
172             mkdir($dest, 0700, true);
173         }
174         
175         if(!file_exists($this->cachePath)){
176             return false;
177         }
178       
179         $cms_templatestr = DB_DataObject::factory('cms_templatestr');
180         $cms_templatestr->active = 1;
181         $cms_templatestr->selectAdd();
182         $cms_templatestr->selectAdd("MAX(updated) AS latest");
183         $cms_templatestr->find(true);
184         
185       
186         
187         if(filemtime($this->cachePath) < strtotime($cms_templatestr->latest)){
188             return false;
189         }
190         
191         return true;
192     }
193 }