Fix #6494 - translations code for reports
[Pman.Admin] / UpdateBjsTemplates.php
1 <?php
2
3 /**
4  *
5  
6  *
7  */
8
9 require_once 'Pman.php';
10 class Pman_Admin_UpdateBjsTemplates extends Pman
11 {
12     
13     static $cli_desc = "Update BJS Templates";
14  
15     static $cli_opts = array();
16     
17     var $cli = false;
18     
19     var $opts;
20     
21     function getAuth() {
22         
23         
24         $ff = HTML_FlexyFramework::get();
25         if (!empty($ff->cli)) {
26             $this->cli = true;
27             return true;
28         }
29         
30         return true;
31     }
32      
33     function get($tbl, $opts=array())
34     {
35         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
36         $this->opts = $opts;
37         $this->updateData();
38     }
39     
40     function updateData()
41     {   
42         $this->scanProjectBJS();
43         $this->scanPmanBJS();
44         $this->scanPmanTemplates();
45
46         $this->scanTables();
47         $this->syncLanguage();
48         
49         $this->jok('OK');
50         
51     }
52     
53     function scanProjectBJS()
54     {
55         $ff = HTML_FlexyFramework::get();
56         
57         if (empty($ff->Pman_Core)) {
58             $this->jerr("config[Pman_Core] is not set");
59         }
60         if (!empty($ff->Pman_Core['project_name'])) {
61             $base  = $ff->Pman_Core['project_name'];
62             
63             $dh = opendir($base);
64             
65             $ret = array();
66             
67             if(!$dh){
68                 $this->jerr("could not open dir: config[Pman_Core] = " . $base);
69                 return $ret; // something went wrong!?
70             }
71              
72             while (($fn = readdir($dh)) !== false) {
73                 
74                 if(empty($fn) || $fn[0] == '.' || !preg_match('/\.bjs$/', $fn)){
75                     continue;
76                 }
77                 
78                 if($this->cli){
79                     echo "Processing {$fn} \n";
80                 }
81                 
82                 $template = DB_DataObject::factory('core_template');
83                 $template->setFrom(array(
84                     'template' => $fn,
85                     'lang' => 'en',
86                     'view_name' => $base
87                 ));
88                 
89                 $o = false;
90                 
91                 if($template->find(true)){
92                     $o = clone ($template);
93                 }
94                 
95                 $template->updated = $template->sqlValue("NOW()");
96                 
97                 (empty($o)) ? $template->insert() : $template->update($o);
98                 
99                 $data = json_decode(file_get_contents($base . '/' . $fn), true);
100                 
101                 $template->words = empty($data['strings']) ? array() : $data['strings'];
102                 
103                 $x = DB_DataObject::Factory('core_templatestr');
104                 $x->syncTemplateWords($template, false);
105             }
106         }
107          
108     }
109     
110     function scanPmanBJS()
111     {
112         foreach ($this->modules() as $m){
113             $view_name = "Pman.$m";
114             
115             $dh = opendir("Pman/$m");
116         
117             $ret = array();
118
119             if(!$dh){
120                 continue;
121             }
122             
123             while (($fn = readdir($dh)) !== false) {
124             
125                 if(empty($fn) || $fn[0] == '.' || !preg_match('/\.bjs$/', $fn)){
126                     continue;
127                 }
128
129                 if($this->cli){
130                     echo "Processing {$fn} \n";
131                 }
132
133                 $template = DB_DataObject::factory('core_template');
134                 $template->setFrom(array(
135                     'template' => $fn,
136                     'lang' => 'en',
137                     'view_name' => $view_name
138                 ));
139
140                 $o = false;
141
142                 if($template->find(true)){
143                     $o = clone ($template);
144                 }
145
146                 $template->updated = $template->sqlValue("NOW()");
147
148                 (empty($o)) ? $template->insert() : $template->update($o);
149
150                 $data = json_decode(file_get_contents('Pman' . '/' . $m . '/' . $fn), true);
151
152                 $template->words = empty($data['strings']) ? array() : $data['strings'];
153
154                 $x = DB_DataObject::Factory('core_templatestr');
155                 $x->syncTemplateWords($template, false);
156             }
157         }
158         
159     }
160     
161     function scanTables()
162     {
163         $ff = HTML_FlexyFramework::get();
164         
165         if (empty($ff->Pman_Core)) {
166             $this->jerr("config[Pman_Core] is not set");
167         }
168         
169         if(isset($ff->Pman_Core['DataObjects_Core_templatestr']['tables'])){
170             $cts = DB_DataObject::factory('core_templatestr');
171             
172             if($this->cli){
173                 echo "Sync tables.....\n";
174             }
175             
176             foreach($ff->Pman_Core['DataObjects_Core_templatestr']['tables'] as $table=>$cols){
177                 $t = DB_DataObject::factory($table);
178                 foreach($t->fetchAll() as $d) {
179                     $cts->onTableChange($this, $d, 'update');
180                 }
181             }
182         }
183     }
184     
185     function syncLanguage()
186     {
187         $ff = HTML_FlexyFramework::get();
188         
189         if (empty($ff->Pman_Core)) {
190             $this->jerr("config[Pman_Core] is not set");
191         }
192         
193         $opt_lang = empty($ff->Pman_Core_I18n) ? array( 'l' => array()) : $ff->Pman_Core_I18n;
194         
195         if($this->cli){
196             echo "Sync the Languages template.....\n";
197         }
198         
199         foreach($opt_lang['l'] as $l) {
200             if($this->cli){
201                 echo "Sync $l Language.....\n";
202             }
203
204             $tps = DB_DataObject::factory('core_templatestr');
205             $tps->syncLang($l); /// this should be configured somewhere..
206         }
207     }
208     
209     function scanPmanTemplates()
210     {
211         // the CMS stuff scanned the PHP code looking for references to templates..
212         $tp = DB_DAtaObject::Factory('core_template');
213         
214         foreach ($this->modules() as $m){
215             
216             // templates...
217             $ar = $this->scanDir(array(
218                  'tdir' => "Pman/$m/templates",
219                 'subdir' => '',
220                 'match' => '/\.(html|txt|abw)$/',
221                 'skipdir' => array('images','css','js'),
222                 
223             ));
224             
225             
226             foreach($ar as $pg) {
227                 
228                  
229                 $tp->syncTemplatePage(array(
230                     'base' =>'Pman.'.$m, 
231                     'template_dir' =>  "Pman/$m/templates",
232                     'template' => $pg
233                 ));
234             }
235             // should clean up old templates..
236             // php files..
237             $ar = $this->scanDir(array(
238                 'tdir' => "Pman/$m",
239                 'subdir' => '',
240                 'match' => '/\.(php)$/',
241                 'skipdir' => array('templates'),
242                 
243             ));
244             
245             
246             foreach($ar as $pg) {
247                 
248                 $tp->syncPhpGetText(array(
249                     'base' =>'Pman.'.$m, 
250                     'template_dir' =>  "Pman/$m",
251                     'template' => $pg
252                 ));
253             }
254             
255             
256             
257             
258             
259             //$tp->syncTemplatePage($pg);
260         }
261         
262          
263     }
264     
265     
266     
267     function scanDir($cfg) //$view, $tdir , $subdir='', $match)
268     {
269         //echo "TSCAN base= $base subdir =$subdir\n ";
270         $ff = HTML_FlexyFramework::get();
271          
272          $subdir = $cfg['subdir'];
273         $scandir = $cfg['tdir']. (empty($subdir) ? '' : '/') . $subdir;
274        
275         if (in_array($subdir, $cfg['skipdir'])) {
276             return array();
277         }
278         // skip dom_templates
279           
280         if (!file_exists($scandir)) {
281             return array();
282         }
283         $dh = opendir($scandir);
284         if(!$dh){
285             return array(); // something went wrong!?
286         }
287         $ret = array();
288             
289         while (($fn = readdir($dh)) !== false) {
290             // do we care that it will try and parse the template directory??? - not really..
291             // as we are only looking for php files..
292             if(empty($fn) || $fn[0] == '.'){
293                 continue;
294             }
295             
296             $fullpath = $scandir.(empty($scandir) ? '' : "/").$fn;
297             // echo "filename:  $fullpath \n";
298            
299             if (is_link($fullpath)) {
300                 continue;
301             }
302             
303             if(is_dir($fullpath)){
304                 // then recursively call self...
305                 $cfg['subdir'] = $subdir . (empty($subdir) ? '' : '/') . $fn;
306                 $children = $this->scanDir($cfg);
307                 if (count($children)) {
308                     $ret = array_merge($ret, $children);
309                     
310                 }
311                 continue;
312                  
313             }
314             
315             if (!preg_match($cfg['match'], $fn) || !is_file($fullpath)) {
316                 continue;
317             }
318             
319           
320             
321             $ret[] =  $subdir . (empty($subdir) ? '' : '/').  $fn;  /// this used to be strtolower?? why???
322
323             
324             
325         }
326 //        print_r($ret);
327         
328         return $ret;
329             
330         
331         
332          
333     }
334     
335 }