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