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