Fix #6494 - translations code for reports
[Pman.Core] / DataObjects / Core_template.php
1 <?php
2 /**
3  * Table Definition for core_template 
4  *
5  *
6  * The idea here is that it contains all the strings in the templates with a language '' (empty)
7  * , it then generates a matching set of strings
8  * 
9  */
10 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
11
12 class Pman_Core_DataObjects_Core_template  extends DB_DataObject 
13 {
14     ###START_AUTOCODE
15     /* the code below is auto generated do not remove the above tag */
16
17     public $__table = 'core_template';         // table name
18     public $id;                              // int(11)  not_null primary_key auto_increment
19     public $template;                           // string(64)  not_null
20
21     public $updated;                        // blob(65535)  blob
22     public $lang;    // text  NOT NULL;
23     public $view_name; // eg mobile or desktop
24     /* the code above is auto generated do not remove the tag below */
25     ###END_AUTOCODE
26     
27     
28     function applyFilters($q, $au, $roo)
29     {
30         //DB_DataObject::debugLEvel(1);
31 //        $x = DB_Dataobject::Factory($this->tableName());
32         
33         
34         // template scanning and syncing should be done by the UpdateDatabase Code.
35         //if (!$x->count() || !empty($q['_rescan'])) {
36             //DB_DataObject::debugLEvel(1);
37             //$tp = DB_DataObject::factory('core_template');
38             //$opts = HTML_FlexyFramework::get()->Pman_Core;
39             //$tp->syncTemplateDir(false, '', !empty($q['_rescan']));
40             //if (isset($q['lang'])) {
41             //    $this->syncLang($q['lang']);
42             //}
43         //} 
44 //        $this->whereAdd("
45 //                join_
46 //            ");
47         
48     }
49         
50     function toRooArray($req)
51     {
52         $ret = $this->toArray();
53         if (!empty($req['_clean_name']) ) {
54             $ret['template_clean'] = preg_replace('#\.html$#i', '', $this->template);
55             
56         }
57         return $ret;
58         
59     }
60     
61     /*
62      * @param base (should be full path to template directory)
63      * @param subdir = empty for top or subpath.
64      */
65     function syncTemplateDir($base = false,  $subdir = '', $force = false)
66     {
67         echo "syncTemplateDir: $base , $subdir, $force \n";
68         //print_r(func_get_args());
69         if (!$base) {
70             $ff = HTML_FlexyFramework::get();
71             if (!isset($ff->Pman_Core)) {
72                 echo "[ERROR] Not scanning template directory - no set in Pman_Core[templateDir]\n";
73                 return;
74             }
75             $opts = $ff->Pman_Core;
76             if (is_array($opts['templateDir'])) {
77                 foreach($opts['templateDir'] as $type=>$dir) {
78                     $this->syncTemplateDir($dir, '', $force);
79                 }
80                 return;
81             }
82             
83             $base = $opts['templateDir'];
84             
85             
86         }
87         if($force){
88             $tmpls = DB_DataObject::factory('core_template');
89             $this->tmpls = $tmpls->fetchAll('template','id'); // dupes??
90         }
91         
92         $tmp_dir = $base . (empty($subdir) ? '' : '/') . $subdir;
93         
94         if(!is_dir($tmp_dir)){
95             return;
96         }
97         
98         $dh = opendir($tmp_dir);
99         if(!$dh){
100             return; // something went wrong!?
101         }
102         
103         while (($fn = readdir($dh)) !== false) {
104             // do we care that it will try and parse the template directory??? - not really..
105             // as we are only looking for php files..
106             if(empty($fn) || $fn[0] == '.'){
107                 continue;
108             }
109
110             $fullpath = $tmp_dir."/".$fn;
111 //            echo "filename: ".$fullpath." \n";    
112              
113             $relpath = $subdir . (empty($subdir) ? '' : '/') . $fn;
114             
115             if(is_dir($fullpath)){
116                 // then recursively call self...
117 //                var_Dump($base);
118 //                var_Dump($subdir . (empty($subdir) ? '' : '/') . $fn);
119                 $this->syncTemplateDir($base, $subdir . (empty($subdir) ? '' : '/') . $fn );
120                 
121                 continue;
122             }
123             if (!preg_match('/(\.html|\.txt|\.abw)$/', $fn)) {
124                 continue;
125             }
126             
127              
128 //            var_dump($tmp);
129 //            var_dump($tmp_path);
130 //            $fn = basename($fn);
131             if (isset($this->tmpls[$relpath])) {
132                 unset($this->tmpls[$relpath]);
133             }
134             
135             
136             
137                
138             $template = $this->syncTemplate($relpath, true, false);
139 //            var_dump($template);
140             if (is_a($template, 'PEAR_Error')) {
141                 continue;
142             }
143         }
144         closedir($dh);
145 //        
146  
147         if($force){
148             foreach($this->tmpls as $id) {
149                 $x = DB_DataObject::factory($this->tableName());
150                 if ($x->get($id)) {
151                     $x->delete();
152                 }
153             }
154         }
155     }
156     
157     /* compile a html template
158      *  
159      *  @param template_dir  << the path to the template dir ... Pman/XXX/template ...
160      *  @param template   << name of template used by name field)
161      *  @param base  << view name (module ? + templates?)
162      *  
163      *  
164      */
165     function syncTemplatePage($pgdata)
166     {
167         //print_r($pgdata);
168         
169         $force = true;
170          //echo "compiling:"; print_r($pgdata);
171         // read the template and extract the translatable strings.
172         ini_set('memory_limit', '512M');
173         
174         //var_dump($n);
175         $n= $pgdata['template'];  // remove trailing slash..
176         
177         $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
178         $opts = HTML_FlexyFramework::get()->Pman_Core;
179         //print_R($opts);
180         //$dir = $opts['templateDir'] . '/' . $node;
181         $oo = array(
182             'fatalError'    => PEAR_ERROR_EXCEPTION,  
183             'disableTranslate' => false,
184             'templateDir' => $pgdata['template_dir'],
185             'compileDir' => $fopts['compileDir'] . '_translation_files',
186             'forceCompile' => true, //?? only for force above???
187         );
188          
189         // non-html templates - treat as such..
190         // abiword - treat as html?
191         if (!preg_match('/\.(html|abw)$/i', $pgdata['template'])) {
192             $oo['nonHTML'] = true;
193         }
194         
195         //print_r(array($oo, $n));
196         
197         
198         $flexy = new HTML_Template_Flexy( $oo );
199           
200         if (!$flexy->resolvePath ($pgdata['template'])) {
201             //echo "SKIP - could not resolve path?\n"; print_r($oo);
202             return false;
203         }
204         
205         // attempt to find the template... record.
206         $tmpl = DB_DataObject::Factory($this->tableName());
207        
208         $tmpl->view_name = $pgdata['base'];
209         if ($tmpl->get('template',  $pgdata['template'])) {
210             if (strtotime($tmpl->updated) >= filemtime($flexy->resolvePath ($pgdata['template']))) {
211                 return $tmpl;
212             }
213         }
214         
215         
216         
217         try {
218             $r = $flexy->compile($pgdata['template']);
219         } catch(Exception $e) {
220             $old = clone($tmpl);
221             $tmpl->updated   = date('Y-m-d H:i:s',filemtime($flexy->resolvePath ($pgdata['template'])));
222             if ($tmpl->id) {
223                 $tmpl->update($tmpl);
224             } else {
225                 $tmpl->lang = 'en';
226                 $tmpl->insert();
227             }
228             
229             
230             return false;
231         }
232        
233       
234         if (is_a($r,'PEAR_Error')) {
235             
236            // echo $r->toString(). "\n";
237             return $r;
238         }
239           
240         $tmpl = DB_DataObject::Factory($this->tableName());
241         $tmpl->words = file_exists($flexy->getTextStringsFile) ?
242                 unserialize(file_get_contents($flexy->getTextStringsFile)) :
243                 array();
244         
245         $tmpl->contentStrings   = $flexy->compiler->contentStrings;
246         //var_dump(file_exists($flexy->getTextStringsFile));
247         //print_r($tmpl->words);
248         $tmpl->currentTemplate  = $flexy->currentTemplate;
249         
250         $tmpl->view_name = $pgdata['base'];
251         
252         
253         if (!$tmpl->get('template',  $pgdata['template'])) {
254             
255             $tmpl->template = $pgdata['template'];
256             $tmpl->lang = 'en'; /// ??? hard coded??
257             $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
258             $tmpl->insert();
259         } else {
260             $xx =clone($tmpl);
261             // has it been cahnged...
262             //if (!$force && filemtime($flexy->currentTemplate) == strtotime($tmpl->updated)) {
263                 // nothing changed..
264             //    return $tmpl;
265             //}
266             if (empty($tmpl->lang))  {
267                 //echo "FIX LANG?";exit;
268                 $tmpl->lang = 'en'; /// ??? hard coded??
269             }
270             
271             $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
272             $tmpl->update($xx);
273         }
274       
275         
276         $x = DB_DataObject::Factory('core_templatestr');
277         $x->syncTemplateWords($tmpl);
278         
279         // if file_exists ( template/path/name.php << eg. a matching view..)
280         // then create a system page for this. 
281         
282         
283         
284         $x = DB_DataObject::Factory('core_template_element');
285         $tmpl->elements = $x->syncTemplateElement($tmpl,
286                 file_get_contents($flexy->currentTemplate),
287                 $flexy->compiler->contentStrings,
288                 false);
289         
290         
291         
292         return clone($tmpl);
293     
294     }
295     function syncPhpGetText($pgdata)
296     {
297         $tmpl = DB_DataObject::Factory($this->tableName());
298         $tmpl->view_name = $pgdata['base'];
299         $tmpl->currentTemplate = $pgdata['template_dir'] . '/'. $pgdata['template'];
300         
301         if ($tmpl->get('template',  $pgdata['template'])) {
302             if (strtotime($tmpl->updated) >= filemtime( $tmpl->currentTemplate )) {
303                 //return $tmpl;
304             }
305         }
306         $words = array();
307         
308         $ar = token_get_all(file_get_contents( $tmpl->currentTemplate  ));
309         foreach( $ar as $i=> $tok) {
310             if (!is_array($tok) || $tok[0] != T_CONSTANT_ENCAPSED_STRING) {
311                 continue;
312             }
313             if ($i < 2) {
314                 continue;
315             }
316             if (is_array($ar[$i-1]) || $ar[$i-1] != '(') {
317                 continue;
318             }
319             if (!is_array($ar[$i-2]) || $ar[$i-2][1] != '_') {
320                 continue;
321             }
322             $ct = $tok[1][0];
323             $words[] =  str_replace('\\'. $ct, $ct, trim($tok[1] , $ct));
324             
325         }
326         // create the template...
327         
328         
329          if (!$tmpl->id) {
330             
331             $tmpl->template = $pgdata['template'];
332             $tmpl->lang = 'en'; /// ??? hard coded??
333             $tmpl->updated = date('Y-m-d H:i:s', filemtime($tmpl->currentTemplate));
334             $tmpl->insert();
335         } else {
336             $xx =clone($tmpl);
337             
338             $tmpl->lang = 'en'; /// ??? hard coded??
339             $tmpl->updated = date('Y-m-d H:i:s', filemtime($tmpl->currentTemplate));
340             $tmpl->update($xx);
341         }
342       
343         $words = array_unique($words);
344         
345         if (!count($words)) {
346             return;
347         }
348         
349              
350         $tmpl->words = $words;
351             
352         $x = DB_DataObject::Factory('core_templatestr');
353         $x->syncTemplateWords($tmpl);    
354          
355         
356         return $tmpl;
357         
358         
359         
360     }
361 }