UpdateTemplates.php
[Pman.Cms] / UpdateTemplates.php
1 <?php
2
3 /**
4  *
5  
6  *
7  */
8
9 require_once 'Pman.php';
10 class Pman_Cms_UpdateTemplates extends Pman
11 {
12     
13     static $cli_desc = "Update Templates";
14  
15     static $cli_opts = array(
16         'template' => array(
17             'desc' => 'Compile a specific template',
18             'default' => '',
19             'short' => 't',
20             'min' => 1,
21             'max' => 1,
22             
23         ),
24         
25         'force-content-update' => array(
26             'desc' => 'Force updating of content from templates',
27             'default' => '',
28             'short' => 'f',
29             'min' => 1,
30             'max' => 1,
31             
32         ),
33         'debug' => array(
34             'desc' => 'Turn on debuggin',
35             'default' => '',
36             'short' => 'd',
37             'min' => 1,
38             'max' => 1,
39             
40         )
41     );
42     
43     var $cli = false;
44     
45     var $opts;
46     
47     function getAuth() {
48         
49         
50         $ff = HTML_FlexyFramework::get();
51         if (!empty($ff->cli)) {
52             $this->cli = true;
53             return true;
54         }
55         die("no web access");;
56         return true;
57     }
58      
59     function get($tbl, $opts)
60     {
61         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
62         
63         
64         if (!empty($opts['debug'])) {
65             DB_DataObject::debugLevel(1);
66             
67         }
68        /* if (!empty($opts['template'])) {
69              $tp = DB_DataObject::factory('cms_template');
70              //$opts = HTML_FlexyFramework::get()->Pman_Cms;
71              $tp->syncTemplate($opts['template'], true, '');
72              die("done");
73             
74         }
75         */
76         $this->opts = $opts;
77         $this->updateData();
78     }
79     
80     function fixOldNames()
81     {
82         $ff = HTML_FlexyFramework::get();
83         $base = $ff->Pman_Cms['project_name'];
84         $tdirs = $ff->Pman_Cms['templateDir'];
85         if (!is_array($tdirs)) {
86             return;
87         }
88         foreach(array_keys($tdirs) as $ver) {
89             $c = DB_DAtaObject::Factory('cms_page');
90             $c->query("UPDATE cms_page set page_link = '$ver' WHERE page_link = '{$ver}/Site'");
91             
92             $c->query("UPDATE cms_page set page_link = '{$ver}/index' WHERE page_link = '{$ver}/{$base}'");
93             // parents??
94             
95         }
96          
97         
98     }
99     
100     
101     function updateData()
102     {
103        
104         // fix any old data..
105         $this->fixOldNames();
106         
107        
108         
109         
110         
111         $pages = $this->scanCode();
112         
113         //print_R($pages);         exit;
114         
115         $page_templates = array();
116         foreach($pages as $pg) {
117             $page_templates[$pg['template']] =1;
118         }
119         
120         //print_R($pages); exit;;
121         
122         
123         
124         // add in templates that are not related to pages.
125         $all_templates = $this->scanTemplates();
126         foreach($all_templates as $pg) {
127             if (isset($page_templates[$pg['template']])) {
128                 continue;
129             }
130             $pages[] = $pg;
131         }
132         
133         
134         
135          
136         // now generate all the templates..
137         
138         // create the templates..
139         $tp = DB_DataObject::factory('cms_template');
140         foreach($pages as $i=>$pg) {
141             if (empty($pg['template'])) {
142                 continue;
143             }
144             if (!empty($this->opts['template']) && $this->opts['template'] != ($pg['template_dir'] . '/'. $pg['template'])) {
145                 continue;
146             }
147             
148             $pages[$i]['template_object'] = $tp->syncTemplatePage($pg);
149         }
150         
151         //print_R($pages); exit;
152         $pgo = DB_DataObject::factory('cms_page');
153         foreach($pages as $i=>$pg) {
154             
155             if (!empty($this->opts['template']) && $this->opts['template'] != ($pg['template_dir'] . '/'. $pg['template'])) {
156                 continue;
157             }
158             
159             $pages[$i]['page'] = $pgo->syncTemplatePage($pg);
160             $pg = $pages[$i];
161             if (empty($pg['template_object'])) {
162                 continue;
163             }
164             $cur_els = array();
165             foreach($pg['template_object']->elements as $e) {
166 //                print_r($pg);
167 //                print_r($e);exit;
168                 $cur_els[] = $e->id;
169                 $e->syncTemplateFromPage($pg);
170             }
171             // now find all elements of this page, that are not in cur_els'
172             // and disabled them?
173             $cur_pg = DB_DataObject::factory('cms_page');
174             $cur_pg->parent_id = $pg->page->id;
175 //            $cur_
176             $cur_pg->whereAddIn('!element_id', $cur_els, 'INT');
177             
178             print_r($cur_pg->fetchAll());exit;
179         }
180         
181         $opts = HTML_FlexyFramework::get()->Pman_Cms;
182          //sync the tables to cms_templatestr
183         if(isset($opts['DataObjects_Cms_templatestr']['tables'])){
184             $cts = DB_DataObject::factory('cms_templatestr');
185             echo "Sync tables.....\n";
186             foreach($opts['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){
187                 $t = DB_DataObject::factory($table);
188                 foreach($t->fetchAll() as $d) {
189                     $cts->onTableChange($this, $d, 'update');
190                 }
191             }
192         }
193         $ff = HTML_FlexyFramework::get();
194         $opt_lang = empty($ff->Pman_Core_I18n) ? array( 'l' => array()) : $ff->Pman_Core_I18n;
195         // templates.. -- assume the above does the template syncing..
196         //DB_DataObject::DebugLevel(1);
197         echo "Sync the Languages template.....\n";
198         
199         foreach($opt_lang['l'] as $l) {
200             echo "Sync $l Language.....\n";
201             $tps = DB_DataObject::factory('cms_templatestr');
202             $tps->syncLang($l); /// this should be configured somewhere..
203         }
204         
205          
206         exit;
207         
208         
209         
210         
211     }
212     
213     function scanCode($base = false, $subdir = '')
214     {
215         echo "SCAN base= $base subdir =$subdir\n ";
216         $ff = HTML_FlexyFramework::get();
217         $root = $ff->page->rootDir;
218         if ($base === false) {
219             $base = $ff->Pman_Cms['project_name'];
220             
221         }
222         
223         $tdirs = $ff->Pman_Cms['templateDir'];
224         
225         $tdir = $tdirs;
226         $prefixes  = array($ff->Pman_Cms['project_name']);
227         if (is_array($tdirs)) {
228             $tdir = array_shift(array_values($tdirs));
229             $prefixes = array_keys($tdirs);
230         }
231         
232         //$tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
233         
234         
235         
236         $class_dir = $root . '/'. $base . (empty($subdir) ? '' : '/') . $subdir;
237         
238         
239         $class_base = str_replace('/', '_', $base . (empty($subdir) ? '' : '_') . $subdir);
240         
241         if(!is_dir($class_dir)){
242             return array();
243         }
244         if (preg_match('/templates/i', $subdir)) { // skip directories that look like templates..
245             return array();
246         }
247         // this inserts the master...
248         $ret = array();
249         if (empty($subdir)) {
250             foreach($prefixes as $pr) {
251                 $ret[] = array(
252                     'base' => $pr,
253                     'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
254                     'page_link' => $pr,
255                     'template' =>  'master.html'
256                 );
257             }
258             
259              
260              // next check the base class..
261              $fullpath = $class_dir.".php";
262              require_once $fullpath;
263                  
264              $cls =  $ff->Pman_Cms['project_name'];
265              if (!class_exists($cls)) {
266                  echo "SKIP top level class - not a PHP class $cls\n";
267              } else {
268                  $x = new $cls();
269                  
270                  if (!empty($x->template)) {
271                          
272                      foreach($prefixes as $pr) {
273                          
274                          
275                          
276                          $add =  array(
277                              'base' => $pr,
278                              'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
279                              'page_link' => $pr .'/index',
280                              'template' =>  $x->template
281                          );
282                          if (!file_exists($add['template_dir'].'/' . $add['template'])) {
283                              echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
284                              continue;
285                          }
286                         // print_R($add);
287                          
288                          $ret[] = $add;
289                      }  
290                  }
291              }
292             // print_r($ret);exit;
293              
294         }   
295         
296         
297        
298
299         $dh = opendir($class_dir);
300         if(!$dh){
301             return array(); // something went wrong!?
302         }
303                 
304                 
305         while (($fn = readdir($dh)) !== false) {
306             // do we care that it will try and parse the template directory??? - not really..
307             // as we are only looking for php files..
308             if(empty($fn) || $fn[0] == '.'){
309                 continue;
310             }
311             
312             $fullpath = $class_dir."/".$fn;
313             ///echo "filename:  $fullpath \n";
314             //var_Dump($fullpath);
315             
316             
317             if(is_dir($fullpath)){
318                 // then recursively call self...
319              
320                 $ret =array_merge($ret, $this->scanCode($base, $subdir . (empty($subdir) ? '' : '/'). $fn));
321                 continue;
322             }
323             
324             
325             
326             if (!preg_match('/\.php$/', $fn) || !is_file($fullpath)) {
327                 // skip non-php files..
328                 echo "  skipped = not a php file \n";
329                 continue;
330             }
331             
332             // load the file and check the template
333             
334             require_once $fullpath;
335             
336             $cls = $class_base .'_' . str_replace('/', '_', preg_replace('/.php$/', '', $fn));
337             if (!class_exists($cls)) {
338                 echo "SKIP - not a PHP class $cls\n";
339             }
340             
341             $x = new $cls();
342             
343             
344             if (empty($x->template)) {
345                 echo "SKIP - no template defined for $cls\n";
346                 continue;
347             }
348             foreach($prefixes as $pr) {
349                 
350                 
351                 
352                 $add =  array(
353                     'base' => $pr,
354                     'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
355                     'page_link' => $pr .'/'. $subdir . (empty($subdir) ? '' : '/').  preg_replace('/.php$/', '', $fn),
356                     'template' =>  $x->template
357                 );
358                 if (!file_exists($add['template_dir'].'/' . $add['template'])) {
359                     echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
360                     continue;
361                 }
362                 ////print_R($add);
363                 $ret[] = $add;
364             }    
365             
366         }
367         return $ret;
368             
369         
370         
371         
372     }
373     
374     function scanTemplates($base=false, $subdir='')
375     {
376         echo "TSCAN base= $base subdir =$subdir\n ";
377         $ff = HTML_FlexyFramework::get();
378         
379         $generate_cms_page_for = $ff->Pman_Cms['generate_cms_page_for'];
380         
381         
382         $tdirs = $ff->Pman_Cms['templateDir'];
383          
384         if ($base == false && is_array($tdirs)) {
385             $ret = array();
386             foreach($tdirs as $k=>$v) {
387                 $ret = array_merge($this->scanTemplates($k));
388             }
389             return $ret;
390          
391         }
392         if ($base === false) {
393             $base = $ff->Pman_Cms['project_name'];   
394         }
395         $tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
396         
397         $scandir = $tdir. (empty($subdir) ? '' : '/') . $subdir;
398        
399         if ($subdir == 'images') {
400             return array();
401         }
402         // skip dom_templates
403         $generate_page = true;
404         if (preg_match('/templates/i', $subdir)) { // skip directories that look like templates..
405             $generate_page = false;
406         }
407         if (preg_match('/_pages$/i', $subdir)) { // skip directories that look like templates only..
408             $generate_page = false;
409         }
410         
411         
412         
413        
414         $dh = opendir($scandir);
415         if(!$dh){
416             return array(); // something went wrong!?
417         }
418         $ret = array();
419             
420         while (($fn = readdir($dh)) !== false) {
421             // do we care that it will try and parse the template directory??? - not really..
422             // as we are only looking for php files..
423             if(empty($fn) || $fn[0] == '.'){
424                 continue;
425             }
426             
427             $fullpath = $scandir.(empty($scandir) ? '' : "/").$fn;
428             echo "filename:  $fullpath \n";
429             //var_Dump($fullpath);
430             if ($fullpath == 'master.html') {
431                 // it's covered by the root object..
432                 continue;
433             }
434             
435             if (is_link($fullpath)) {
436                 continue;
437             }
438             
439             if(is_dir($fullpath)){
440                 // then recursively call self...
441                 $children = $this->scanTemplates($base, $subdir . (empty($subdir) ? '' : '/'). $fn);
442                 if (count($children)) {
443                     $nc = 0;
444                     foreach($children as $c) {
445                         if (!empty($c->page_link)) {
446                             $nc++;
447                         }
448                     }
449                     // children do not have page links..
450                     if (!$nc) {
451                         $ret =array_merge($ret, $children);
452                         continue;
453                     }
454                     
455                     
456                     
457                     
458                     // create a dummy page..
459                     $ret =array_merge($ret,array( array(
460                             'base' => $base,
461                             'template_dir' => $tdir ,
462                             'page_link' => $base .'/'. $subdir . (empty($subdir) ? '' : '/').   $fn,
463                             'template' =>  'error.html'
464                         )));
465                 }
466                 
467                 
468                 
469                 $ret =array_merge($ret, $children);
470                 continue;
471             }
472             
473             
474             
475             if (!preg_match('/\.(html|txt)$/', $fn) || !is_file($fullpath)) {
476                 // skip non-php files..
477                 echo "  skipped = not a html or txt file \n";
478                 continue;
479             }
480             $pn = preg_replace('/.html/', '', $fn);
481             // exclude apage.zh_HK.html -- 
482             if (!preg_match('/mail/',$subdir) && strpos($pn, '.') !== false) {
483                 echo "SKIP - not a transalatable page\n";
484                 continue;
485             }
486             $plsub = $subdir . (empty($subdir) ? '' : '/').  preg_replace('/.html/', '', $fn);
487             
488             $generate_this_page = $generate_page;
489             
490             if ($generate_this_page) {
491                 if (is_string($generate_cms_page_for) && $generate_cms_page_for == '*') {
492                     // no change..
493                 } else if (is_array($generate_cms_page_for)) {
494                     if (!in_array($plsub, $generate_cms_page_for)) {
495                         $generate_this_page = false;
496                     }
497                 
498                 } else {
499                     $generate_this_page = false;
500                 }
501             }    
502             
503             
504             $pl = $base .'/'. $plsub;
505             
506             // load the file and check the template
507             
508             
509             
510             
511             $ret[] = array(
512                 'base' => $base,
513                 'template_dir' => $tdir ,
514                 'page_link' => $generate_this_page ? $pl : false,
515                 'template' =>  $subdir . (empty($subdir) ? '' : '/').  $fn  /// this used to be strtolower?? why???
516             );
517             
518             
519         }
520 //        print_r($ret);
521         
522         return $ret;
523             
524         
525         
526          
527     }
528     
529 }