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         
132         $ids = array();
133         foreach ($this->modules() as $m){
134             $view_name = "Pman.$m";
135             
136             $dh = opendir("Pman/$m");
137         
138             $ret = array();
139
140             if(!$dh){
141                 continue;
142             }
143            
144             while (($fn = readdir($dh)) !== false) {
145             
146                 if(empty($fn) || $fn[0] == '.' || !preg_match('/\.bjs$/', $fn)){
147                     continue;
148                 }
149
150                 if($this->cli){
151                     echo "Processing {$fn} \n";
152                 }
153
154                 $template = DB_DataObject::factory('core_template');
155                 $template->setFrom(array(
156                     'template' => $fn,
157                     'lang' => 'en',
158                     'view_name' => $view_name,
159                     
160                 ));
161                 
162                 
163
164                 $o = false;
165
166                 if($template->find(true)){
167                     $o = clone ($template);
168                 }
169                 $updated = empty($template->updated) ? '1970-01-01' : $template->updated ;
170                 
171                 $template->is_deleted = 0;
172
173                 $template->filetype = 'bjs';
174                 $template->updated = $template->sqlValue("NOW()");
175
176                 (empty($o)) ? $template->insert() : $template->update($o);
177                 
178                 if (strtotime($updated) >= filemtime('Pman' . '/' . $m . '/' . $fn)) {
179                     continue;
180                 }
181                 
182                 $ids[] = $template->id;
183                 $data = json_decode(file_get_contents('Pman' . '/' . $m . '/' . $fn), true);
184
185                 $template->words = empty($data['strings']) ? array() : $data['strings'];
186
187                 $x = DB_DataObject::Factory('core_templatestr');
188                 $x->syncTemplateWords($template, false);
189             }
190         }
191         
192         $del = DB_DataObject::factory('core_template');
193         $del->whereAddIn('!id', $ids, 'int');
194         $del->whereAddIn('view_name', $this->modules(), 'string');
195         $del->filetype = 'bjs';
196         $delids = $del->fetchAll('id');
197         if ($delids) {
198             DB_DataObject::factory('core_template')->query(
199                 'update core_template set is_deleted =  1 where id in('. implode(',', $delids). ')'
200             );
201         }
202         
203         
204         
205         
206         
207     }
208     
209     function scanTables()
210     {
211         $ff = HTML_FlexyFramework::get();
212         
213         if (empty($ff->Pman_Core)) {
214             $this->jerr("config[Pman_Core] is not set");
215         }
216         
217         if(isset($ff->Pman_Core['DataObjects_Core_templatestr']['tables'])){
218             $cts = DB_DataObject::factory('core_templatestr');
219             
220             if($this->cli){
221                 echo "Sync tables.....\n";
222             }
223             
224             foreach($ff->Pman_Core['DataObjects_Core_templatestr']['tables'] as $table=>$cols){
225                 $t = DB_DataObject::factory($table);
226                 foreach($t->fetchAll() as $d) {
227                     $cts->onTableChange($this, $d, 'update');
228                 }
229             }
230         }
231     }
232     
233     function syncLanguage()
234     {
235         $ff = HTML_FlexyFramework::get();
236         
237         if (empty($ff->Pman_Core)) {
238             $this->jerr("config[Pman_Core] is not set");
239         }
240          $ff = HTML_FlexyFramework::get();
241      
242         $opt_lang = empty($ff->Pman_Admin['languages']) ? array( 'l' => array()) : $ff->Pman_Admin['languages'];
243         
244         if($this->cli){
245             echo "Sync the Languages template.....\n";
246         }
247         
248         foreach($opt_lang as $l) {
249             if($this->cli){
250                 echo "Sync $l Language.....\n";
251             }
252
253             $tps = DB_DataObject::factory('core_templatestr');
254             $tps->syncLang($l); /// this should be configured somewhere..
255         }
256     }
257     
258     function scanPmanTemplates()
259     {
260         // the CMS stuff scanned the PHP code looking for references to templates..
261         $tp = DB_DAtaObject::Factory('core_template');
262         
263         foreach ($this->modules() as $m){
264             //var_dump($m);
265             // templates...
266             $ar = $this->scanDir(array(
267                  'tdir' => "Pman/$m/templates",
268                 'subdir' => '',
269                 'match' => '/\.(html|txt|abw)$/',
270                 'skipdir' => array('images','css','js'),
271                 
272             ));
273            //  print_r($ar);
274             
275             foreach($ar as $pg) {
276                  
277                 $temp = $tp->syncTemplatePage(array(
278                     'base' =>'Pman.'.$m, 
279                     'template_dir' =>  "Pman/$m/templates",
280                     'template' => $pg
281                 ));
282                 if ($temp) {
283                     $ids[] = $temp->id;
284                 }
285             }
286             // should clean up old templates..
287             // php files..
288             $ar = $this->scanDir(array(
289                 'tdir' => "Pman/$m",
290                 'subdir' => '',
291                 'match' => '/\.(php)$/',
292                 'skipdir' => array('templates'),
293                 
294             ));
295             
296             
297             foreach($ar as $pg) {
298                 
299                 $temp = $tp->syncPhpGetText(array(
300                     'base' =>'Pman.'.$m, 
301                     'template_dir' =>  "Pman/$m",
302                     'template' => $pg
303                 ));
304                 if ($temp) {
305                     $ids[] = $temp->id;
306                 }
307                 
308             }
309             
310             
311             
312             
313             
314             //$tp->syncTemplatePage($pg);
315         }
316         $del = DB_DataObject::factory('core_template');
317         $del->whereAddIn('!id', $ids, 'int');
318         $del->whereAddIn('view_name', $this->modules(), 'string');
319         $del->whereAddIn('filetype' , array( 'php', 'html' ), 'string');
320         $delids = $del->fetchAll('id');
321         if ($delids) {
322             DB_DataObject::factory('core_template')->query(
323                 'update core_template set is_deleted =  1 where id in('. implode(',', $delids). ')'
324             );
325         }
326          
327     }
328     
329     
330     
331     function scanDir($cfg) //$view, $tdir , $subdir='', $match)
332     {
333         //echo "TSCAN base= $base subdir =$subdir\n ";
334         $ff = HTML_FlexyFramework::get();
335          
336          $subdir = $cfg['subdir'];
337         $scandir = $cfg['tdir']. (empty($subdir) ? '' : '/') . $subdir;
338        
339         if (in_array($subdir, $cfg['skipdir'])) {
340             return array();
341         }
342         // skip dom_templates
343           
344         if (!file_exists($scandir)) {
345             return array();
346         }
347         $dh = opendir($scandir);
348         if(!$dh){
349             return array(); // something went wrong!?
350         }
351         $ret = array();
352             
353         while (($fn = readdir($dh)) !== false) {
354             // do we care that it will try and parse the template directory??? - not really..
355             // as we are only looking for php files..
356             if(empty($fn) || $fn[0] == '.'){
357                 continue;
358             }
359             
360             $fullpath = $scandir.(empty($scandir) ? '' : "/").$fn;
361             // echo "filename:  $fullpath \n";
362            
363             if (is_link($fullpath)) {
364                 continue;
365             }
366             
367             if(is_dir($fullpath)){
368                 // then recursively call self...
369                 $cfg['subdir'] = $subdir . (empty($subdir) ? '' : '/') . $fn;
370                 $children = $this->scanDir($cfg);
371                 if (count($children)) {
372                     $ret = array_merge($ret, $children);
373                     
374                 }
375                 continue;
376                  
377             }
378             
379             if (!preg_match($cfg['match'], $fn) || !is_file($fullpath)) {
380                 continue;
381             }
382             
383           
384             
385             $ret[] =  $subdir . (empty($subdir) ? '' : '/').  $fn;  /// this used to be strtolower?? why???
386
387             
388             
389         }
390 //        print_r($ret);
391         
392         return $ret;
393             
394         
395         
396          
397     }
398     
399 }