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