sync
[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($step, $opts=array())
34     {
35         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
36         $this->opts = $opts;
37         
38         switch($step) {
39             case 'scanProjectBJS':
40             case 'scanPmanBJS':
41             case 'scanPmanTemplates':
42             case 'scanTables':
43             case 'syncLanguage':
44                   $this->{$step}();
45                   $this->jok("DONE - " . $step);
46             default:
47                 $this->jerr("invalid step");
48         }
49         
50          
51     }
52     
53     function updateData() // this does everything?? not used?
54     
55     {   
56         $this->scanProjectBJS();
57         $this->scanPmanBJS();
58         $this->scanPmanTemplates();
59
60         $this->scanTables();
61         $this->syncLanguage();
62         
63         DB_DataObject::factory('core_template')->query(
64             "update core_template set is_deleted =  1 where filetype = ''"
65         );
66         
67         $this->jok('OK');
68         
69     }
70     
71     function scanProjectBJS()  // this is probably not used (replace by the CMS scanner)
72     {
73         $ff = HTML_FlexyFramework::get();
74         
75         if (empty($ff->Pman_Core)) {
76             $this->jerr("config[Pman_Core] is not set");
77         }
78         if (!empty($ff->Pman_Core['project_name'])) {
79             $base  = $ff->Pman_Core['project_name'];
80             
81             $dh = opendir($base);
82             
83             $ret = array();
84             
85             if(!$dh){
86                 $this->jerr("could not open dir: config[Pman_Core] = " . $base);
87                 return $ret; // something went wrong!?
88             }
89              
90             while (($fn = readdir($dh)) !== false) {
91                 
92                 if(empty($fn) || $fn[0] == '.' || !preg_match('/\.bjs$/', $fn)){
93                     continue;
94                 }
95                 
96                 if($this->cli){
97                     echo "Processing {$fn} \n";
98                 }
99                 
100                 $template = DB_DataObject::factory('core_template');
101                 $template->setFrom(array(
102                     'template' => $fn,
103                     'lang' => 'en',
104                     'view_name' => $base
105                 ));
106                 
107                 $o = false;
108                 
109                 if($template->find(true)){
110                     $o = clone ($template);
111                 }
112                 $template->filetype = 'bjs';
113                 
114                 $template->updated = $template->sqlValue("NOW()");
115                 
116                 (empty($o)) ? $template->insert() : $template->update($o);
117                 
118                 $data = json_decode(file_get_contents($base . '/' . $fn), true);
119                 
120                 $template->words = empty($data['strings']) ? array() : $data['strings'];
121                 
122                 $x = DB_DataObject::Factory('core_templatestr');
123                 $x->syncTemplateWords($template, false);
124             }
125         }
126          
127     }
128     
129     function scanPmanBJS()
130     {
131         $ids = array();
132         foreach ($this->modules() as $m){
133             $view_name = "Pman.$m";
134             
135             $dh = opendir("Pman/$m");
136         
137             $ret = array();
138
139             if(!$dh){
140                 continue;
141             }
142            
143             while (($fn = readdir($dh)) !== false) {
144             
145                 if(empty($fn) || $fn[0] == '.' || !preg_match('/\.bjs$/', $fn)){
146                     continue;
147                 }
148
149                 if($this->cli){
150                     echo "Processing {$fn} \n";
151                 }
152
153                 $template = DB_DataObject::factory('core_template');
154                 $template->setFrom(array(
155                     'template' => $fn,
156                     'lang' => 'en',
157                     'view_name' => $view_name,
158                     
159                 ));
160                 
161                 
162
163                 $o = false;
164
165                 if($template->find(true)){
166                     $o = clone ($template);
167                 }
168                 $updated = empty($template->updated) ? '1970-01-01' : $template->updated ;
169                 
170                 $template->is_deleted = 0;
171
172                 $template->filetype = 'bjs';
173                 $template->updated = $template->sqlValue("NOW()");
174
175                 (empty($o)) ? $template->insert() : $template->update($o);
176
177                 if (strtotime($updated) >= filemtime('Pman' . '/' . $m . '/' . $fn)) {
178                     continue;
179                 }
180                 
181                 $ids[] = $template->id;
182                 $data = json_decode(file_get_contents('Pman' . '/' . $m . '/' . $fn), true);
183
184                 $template->words = empty($data['strings']) ? array() : $data['strings'];
185
186                 $x = DB_DataObject::Factory('core_templatestr');
187                 $x->syncTemplateWords($template, false);
188             }
189         }
190         
191         $del = DB_DataObject::factory('core_template');
192         $del->whereAddIn('!id', $ids, 'int');
193         $del->whereAddIn('view_name', $this->modules(), 'string');
194         $del->filetype = 'bjs';
195         $delids = $del->fetchAll('id');
196         if ($delids) {
197             DB_DataObject::factory('core_template')->query(
198                 'update core_template set is_deleted =  1 where id in('. implode(',', $delids). ')'
199             );
200         }
201         
202         
203         
204         
205         
206     }
207     
208     function scanTables()
209     {
210         $ff = HTML_FlexyFramework::get();
211         
212         if (empty($ff->Pman_Core)) {
213             $this->jerr("config[Pman_Core] is not set");
214         }
215         
216         if(isset($ff->Pman_Core['DataObjects_Core_templatestr']['tables'])){
217             $cts = DB_DataObject::factory('core_templatestr');
218             
219             if($this->cli){
220                 echo "Sync tables.....\n";
221             }
222
223             // deactivate all table translation
224             $t = DB_DataObject::factory('core_templatestr');
225             $t->query(
226                 "UPDATE core_templatestr
227                 SET active = 0 
228                 WHERE on_table != ''"
229             );
230             
231             // activate the used table translation
232             foreach($ff->Pman_Core['DataObjects_Core_templatestr']['tables'] as $table=>$cols){
233                 $t = DB_DataObject::factory($table);
234                 foreach($t->fetchAll() as $d) {
235                     $cts->onTableChange($this, $d, 'update');
236                 }
237             }
238         }
239     }
240     
241     function syncLanguage()
242     {
243         $ff = HTML_FlexyFramework::get();
244         
245         if (empty($ff->Pman_Core)) {
246             $this->jerr("config[Pman_Core] is not set");
247         }
248          $ff = HTML_FlexyFramework::get();
249      
250         $opt_lang = empty($ff->Pman_Admin['languages']) ? array( 'l' => array()) : $ff->Pman_Admin['languages'];
251         
252         if($this->cli){
253             echo "Sync the Languages template.....\n";
254         }
255         
256         foreach($opt_lang as $l) {
257             if($this->cli){
258                 echo "Sync $l Language.....\n";
259             }
260
261             $tps = DB_DataObject::factory('core_templatestr');
262             $tps->syncLang($l); /// this should be configured somewhere..
263         }
264     }
265     
266     function scanPmanTemplates()
267     {
268         // the CMS stuff scanned the PHP code looking for references to templates..
269         $tp = DB_DAtaObject::Factory('core_template');
270         
271         foreach ($this->modules() as $m){
272             //var_dump($m);
273             // templates...
274             $ar = $this->scanDir(array(
275                  'tdir' => "Pman/$m/templates",
276                 'subdir' => '',
277                 'match' => '/\.(html|txt|abw)$/',
278                 'skipdir' => array('images','css','js'),
279                 
280             ));
281            //  print_r($ar);
282             
283             foreach($ar as $pg) {
284                  
285                 $temp = $tp->syncTemplatePage(array(
286                     'base' =>'Pman.'.$m, 
287                     'template_dir' =>  "Pman/$m/templates",
288                     'template' => $pg
289                 ));
290                 if ($temp) {
291                     $ids[] = $temp->id;
292                 }
293             }
294             // should clean up old templates..
295             // php files..
296             $ar = $this->scanDir(array(
297                 'tdir' => "Pman/$m",
298                 'subdir' => '',
299                 'match' => '/\.(php)$/',
300                 'skipdir' => array('templates'),
301                 
302             ));
303             
304             
305             foreach($ar as $pg) {
306                 
307                 $temp = $tp->syncPhpGetText(array(
308                     'base' =>'Pman.'.$m, 
309                     'template_dir' =>  "Pman/$m",
310                     'template' => $pg
311                 ));
312                 if ($temp) {
313                     $ids[] = $temp->id;
314                 }
315                 
316             }
317             
318             
319             
320             
321             
322             //$tp->syncTemplatePage($pg);
323         }
324         $del = DB_DataObject::factory('core_template');
325         $del->whereAddIn('!id', $ids, 'int');
326         $del->whereAddIn('view_name', $this->modules(), 'string');
327         $del->whereAddIn('filetype' , array( 'php', 'html' ), 'string');
328         $delids = $del->fetchAll('id');
329         if ($delids) {
330             DB_DataObject::factory('core_template')->query(
331                 'update core_template set is_deleted =  1 where id in('. implode(',', $delids). ')'
332             );
333         }
334          
335     }
336     
337     
338     
339     function scanDir($cfg) //$view, $tdir , $subdir='', $match)
340     {
341         //echo "TSCAN base= $base subdir =$subdir\n ";
342         $ff = HTML_FlexyFramework::get();
343          
344          $subdir = $cfg['subdir'];
345         $scandir = $cfg['tdir']. (empty($subdir) ? '' : '/') . $subdir;
346        
347         if (in_array($subdir, $cfg['skipdir'])) {
348             return array();
349         }
350         // skip dom_templates
351           
352         if (!file_exists($scandir)) {
353             return array();
354         }
355         $dh = opendir($scandir);
356         if(!$dh){
357             return array(); // something went wrong!?
358         }
359         $ret = array();
360             
361         while (($fn = readdir($dh)) !== false) {
362             // do we care that it will try and parse the template directory??? - not really..
363             // as we are only looking for php files..
364             if(empty($fn) || $fn[0] == '.'){
365                 continue;
366             }
367             
368             $fullpath = $scandir.(empty($scandir) ? '' : "/").$fn;
369             // echo "filename:  $fullpath \n";
370            
371             if (is_link($fullpath)) {
372                 continue;
373             }
374             
375             if(is_dir($fullpath)){
376                 // then recursively call self...
377                 $cfg['subdir'] = $subdir . (empty($subdir) ? '' : '/') . $fn;
378                 $children = $this->scanDir($cfg);
379                 if (count($children)) {
380                     $ret = array_merge($ret, $children);
381                     
382                 }
383                 continue;
384                  
385             }
386             
387             if (!preg_match($cfg['match'], $fn) || !is_file($fullpath)) {
388                 continue;
389             }
390             
391           
392             
393             $ret[] =  $subdir . (empty($subdir) ? '' : '/').  $fn;  /// this used to be strtolower?? why???
394
395             
396             
397         }
398 //        print_r($ret);
399         
400         return $ret;
401             
402         
403         
404          
405     }
406     
407 }