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()
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         
231         $opt_lang = empty($ff->Pman_Core_I18n) ? array( 'l' => array()) : $ff->Pman_Core_I18n;
232         
233         if($this->cli){
234             echo "Sync the Languages template.....\n";
235         }
236         
237         foreach($opt_lang['l'] as $l) {
238             if($this->cli){
239                 echo "Sync $l Language.....\n";
240             }
241
242             $tps = DB_DataObject::factory('core_templatestr');
243             $tps->syncLang($l); /// this should be configured somewhere..
244         }
245     }
246     
247     function scanPmanTemplates()
248     {
249         // the CMS stuff scanned the PHP code looking for references to templates..
250         $tp = DB_DAtaObject::Factory('core_template');
251         
252         foreach ($this->modules() as $m){
253             //var_dump($m);
254             // templates...
255             $ar = $this->scanDir(array(
256                  'tdir' => "Pman/$m/templates",
257                 'subdir' => '',
258                 'match' => '/\.(html|txt|abw)$/',
259                 'skipdir' => array('images','css','js'),
260                 
261             ));
262             //print_r($ar);
263             
264             foreach($ar as $pg) {
265                  
266                 $temp = $tp->syncTemplatePage(array(
267                     'base' =>'Pman.'.$m, 
268                     'template_dir' =>  "Pman/$m/templates",
269                     'template' => $pg
270                 ));
271                 if ($temp) {
272                     $ids[] = $temp->id;
273                 }
274             }
275             // should clean up old templates..
276             // php files..
277             $ar = $this->scanDir(array(
278                 'tdir' => "Pman/$m",
279                 'subdir' => '',
280                 'match' => '/\.(php)$/',
281                 'skipdir' => array('templates'),
282                 
283             ));
284             
285             
286             foreach($ar as $pg) {
287                 
288                 $temp = $tp->syncPhpGetText(array(
289                     'base' =>'Pman.'.$m, 
290                     'template_dir' =>  "Pman/$m",
291                     'template' => $pg
292                 ));
293                 if ($temp) {
294                     $ids[] = $temp->id;
295                 }
296                 
297             }
298             
299             
300             
301             
302             
303             //$tp->syncTemplatePage($pg);
304         }
305         $del = DB_DataObject::factory('core_template');
306         $del->whereAddIn('!id', $ids, 'int');
307         $del->whereAddIn('view_name', $this->modules(), 'string');
308         $del->whereAddIn('filetype' , array( 'php', 'html' ), 'string');
309         $delids = $del->fetchAll('id');
310         if ($delids) {
311             DB_DataObject::factory('core_template')->query(
312                 'update core_template set is_deleted =  1 where id in('. implode(',', $delids). ')'
313             );
314         }
315          
316     }
317     
318     
319     
320     function scanDir($cfg) //$view, $tdir , $subdir='', $match)
321     {
322         //echo "TSCAN base= $base subdir =$subdir\n ";
323         $ff = HTML_FlexyFramework::get();
324          
325          $subdir = $cfg['subdir'];
326         $scandir = $cfg['tdir']. (empty($subdir) ? '' : '/') . $subdir;
327        
328         if (in_array($subdir, $cfg['skipdir'])) {
329             return array();
330         }
331         // skip dom_templates
332           
333         if (!file_exists($scandir)) {
334             return array();
335         }
336         $dh = opendir($scandir);
337         if(!$dh){
338             return array(); // something went wrong!?
339         }
340         $ret = array();
341             
342         while (($fn = readdir($dh)) !== false) {
343             // do we care that it will try and parse the template directory??? - not really..
344             // as we are only looking for php files..
345             if(empty($fn) || $fn[0] == '.'){
346                 continue;
347             }
348             
349             $fullpath = $scandir.(empty($scandir) ? '' : "/").$fn;
350             // echo "filename:  $fullpath \n";
351            
352             if (is_link($fullpath)) {
353                 continue;
354             }
355             
356             if(is_dir($fullpath)){
357                 // then recursively call self...
358                 $cfg['subdir'] = $subdir . (empty($subdir) ? '' : '/') . $fn;
359                 $children = $this->scanDir($cfg);
360                 if (count($children)) {
361                     $ret = array_merge($ret, $children);
362                     
363                 }
364                 continue;
365                  
366             }
367             
368             if (!preg_match($cfg['match'], $fn) || !is_file($fullpath)) {
369                 continue;
370             }
371             
372           
373             
374             $ret[] =  $subdir . (empty($subdir) ? '' : '/').  $fn;  /// this used to be strtolower?? why???
375
376             
377             
378         }
379 //        print_r($ret);
380         
381         return $ret;
382             
383         
384         
385          
386     }
387     
388 }