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