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         // print_R($pages); exit;
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         
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             
165             foreach($pg['template_object']->elements as $e) {
166                 //print_r($e);
167                 $e->syncTemplateFromPage($pg);
168             }
169         }
170         
171         $opts = HTML_FlexyFramework::get()->Pman_Cms;
172          //sync the tables to cms_templatestr
173         if(isset($opts['DataObjects_Cms_templatestr']['tables'])){
174             $cts = DB_DataObject::factory('cms_templatestr');
175             echo "Sync tables.....\n";
176             foreach($opts['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){
177                 $t = DB_DataObject::factory($table);
178                 foreach($t->fetchAll() as $d) {
179                     $cts->onTableChange($this, $d, 'update');
180                 }
181             }
182         }
183         $ff = HTML_FlexyFramework::get();
184         $opt_lang = empty($ff->Pman_Core_I18n) ? array( 'l' => array()) : $ff->Pman_Core_I18n;
185         // templates.. -- assume the above does the template syncing..
186         //DB_DataObject::DebugLevel(1);
187         echo "Sync the Languages template.....\n";
188         
189         foreach($opt_lang['l'] as $l) {
190             echo "Sync $l Language.....\n";
191             $tps = DB_DataObject::factory('cms_templatestr');
192             $tps->syncLang($l); /// this should be configured somewhere..
193         }
194         
195          
196         exit;
197         
198         
199         
200         
201     }
202     
203     function scanCode($base = false, $subdir = '')
204     {
205         echo "SCAN base= $base subdir =$subdir\n ";
206         $ff = HTML_FlexyFramework::get();
207         $root = $ff->page->rootDir;
208         if ($base === false) {
209             $base = $ff->Pman_Cms['project_name'];
210             
211         }
212         
213         $tdirs = $ff->Pman_Cms['templateDir'];
214         
215         $tdir = $tdirs;
216         $prefixes  = array($ff->Pman_Cms['project_name']);
217         if (is_array($tdirs)) {
218             $tdir = array_shift(array_values($tdirs));
219             $prefixes = array_keys($tdirs);
220         }
221         
222         //$tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
223         
224         
225         
226         $class_dir = $root . '/'. $base . (empty($subdir) ? '' : '/') . $subdir;
227         
228         
229         $class_base = str_replace('/', '_', $base . (empty($subdir) ? '' : '_') . $subdir);
230         
231         if(!is_dir($class_dir)){
232             return array();
233         }
234         if (preg_match('/templates/i', $subdir)) { // skip directories that look like templates..
235             return array();
236         }
237         // this inserts the master...
238         $ret = array();
239         if (empty($subdir)) {
240             foreach($prefixes as $pr) {
241                 $ret[] = array(
242                     'base' => $pr,
243                     'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
244                     'page_link' => $pr,
245                     'template' =>  'master.html'
246                 );
247             }
248             
249              
250              // next check the base class..
251              $fullpath = $class_dir.".php";
252              require_once $fullpath;
253                  
254              $cls =  $ff->Pman_Cms['project_name'];
255              if (!class_exists($cls)) {
256                  echo "SKIP top level class - not a PHP class $cls\n";
257              } else {
258                  $x = new $cls();
259                  
260                  if (!empty($x->template)) {
261                          
262                      foreach($prefixes as $pr) {
263                          
264                          
265                          
266                          $add =  array(
267                              'base' => $pr,
268                              'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
269                              'page_link' => $pr .'/index',
270                              'template' =>  $x->template
271                          );
272                          if (!file_exists($add['template_dir'].'/' . $add['template'])) {
273                              echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
274                              continue;
275                          }
276                         // print_R($add);
277                          
278                          $ret[] = $add;
279                      }  
280                  }
281              }
282             // print_r($ret);exit;
283              
284         }   
285         
286         
287        
288
289         $dh = opendir($class_dir);
290         if(!$dh){
291             return array(); // something went wrong!?
292         }
293                 
294                 
295         while (($fn = readdir($dh)) !== false) {
296             // do we care that it will try and parse the template directory??? - not really..
297             // as we are only looking for php files..
298             if(empty($fn) || $fn[0] == '.'){
299                 continue;
300             }
301             
302             $fullpath = $class_dir."/".$fn;
303             ///echo "filename:  $fullpath \n";
304             //var_Dump($fullpath);
305             
306             
307             if(is_dir($fullpath)){
308                 // then recursively call self...
309              
310                 $ret =array_merge($ret, $this->scanCode($base, $subdir . (empty($subdir) ? '' : '/'). $fn));
311                 continue;
312             }
313             
314             
315             
316             if (!preg_match('/\.php$/', $fn) || !is_file($fullpath)) {
317                 // skip non-php files..
318                 echo "  skipped = not a php file \n";
319                 continue;
320             }
321             
322             // load the file and check the template
323             
324             require_once $fullpath;
325             
326             $cls = $class_base .'_' . str_replace('/', '_', preg_replace('/.php$/', '', $fn));
327             if (!class_exists($cls)) {
328                 echo "SKIP - not a PHP class $cls\n";
329             }
330             
331             $x = new $cls();
332             
333             
334             if (empty($x->template)) {
335                 echo "SKIP - no template defined for $cls\n";
336                 continue;
337             }
338             foreach($prefixes as $pr) {
339                 
340                 
341                 
342                 $add =  array(
343                     'base' => $pr,
344                     'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
345                     'page_link' => $pr .'/'. $subdir . (empty($subdir) ? '' : '/').  preg_replace('/.php$/', '', $fn),
346                     'template' =>  $x->template
347                 );
348                 if (!file_exists($add['template_dir'].'/' . $add['template'])) {
349                     echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
350                     continue;
351                 }
352                 ////print_R($add);
353                 $ret[] = $add;
354             }    
355             
356         }
357         return $ret;
358             
359         
360         
361         
362     }
363     
364     function scanTemplates($base=false, $subdir='')
365     {
366         echo "TSCAN base= $base subdir =$subdir\n ";
367         $ff = HTML_FlexyFramework::get();
368         
369         $generate_cms_page_for = $ff->Pman_Cms['generate_cms_page_for'];
370         
371         
372         $tdirs = $ff->Pman_Cms['templateDir'];
373          
374         if ($base == false && is_array($tdirs)) {
375             $ret = array();
376             foreach($tdirs as $k=>$v) {
377                 $ret = array_merge($this->scanTemplates($k));
378             }
379             return $ret;
380          
381         }
382         if ($base === false) {
383             $base = $ff->Pman_Cms['project_name'];   
384         }
385         $tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
386         
387         $scandir = $tdir. (empty($subdir) ? '' : '/') . $subdir;
388        
389         if ($subdir == 'images') {
390             return array();
391         }
392         // skip dom_templates
393         $generate_page = true;
394         if (preg_match('/templates/i', $subdir)) { // skip directories that look like templates..
395             $generate_page = false;
396         }
397         if (preg_match('/_pages$/i', $subdir)) { // skip directories that look like templates only..
398             $generate_page = false;
399         }
400         
401         
402         
403        
404         $dh = opendir($scandir);
405         if(!$dh){
406             return array(); // something went wrong!?
407         }
408         $ret = array();
409             
410         while (($fn = readdir($dh)) !== false) {
411             // do we care that it will try and parse the template directory??? - not really..
412             // as we are only looking for php files..
413             if(empty($fn) || $fn[0] == '.'){
414                 continue;
415             }
416             
417             $fullpath = $scandir.(empty($scandir) ? '' : "/").$fn;
418             echo "filename:  $fullpath \n";
419             //var_Dump($fullpath);
420             if ($fullpath == 'master.html') {
421                 // it's covered by the root object..
422                 continue;
423             }
424             
425             if (is_link($fullpath)) {
426                 continue;
427             }
428             
429             if(is_dir($fullpath)){
430                 // then recursively call self...
431                 $children = $this->scanTemplates($base, $subdir . (empty($subdir) ? '' : '/'). $fn);
432                 if (count($children)) {
433                     $nc = 0;
434                     foreach($children as $c) {
435                         if (!empty($c->page_link)) {
436                             $nc++;
437                         }
438                     }
439                     // children do not have page links..
440                     if (!$nc) {
441                         $ret =array_merge($ret, $children);
442                         continue;
443                     }
444                     
445                     
446                     
447                     
448                     // create a dummy page..
449                     $ret =array_merge($ret,array( array(
450                             'base' => $base,
451                             'template_dir' => $tdir ,
452                             'page_link' => $base .'/'. $subdir . (empty($subdir) ? '' : '/').   $fn,
453                             'template' =>  'error.html'
454                         )));
455                 }
456                 
457                 
458                 
459                 $ret =array_merge($ret, $children);
460                 continue;
461             }
462             
463             
464             
465             if (!preg_match('/\.(html|txt)$/', $fn) || !is_file($fullpath)) {
466                 // skip non-php files..
467                 echo "  skipped = not a html or txt file \n";
468                 continue;
469             }
470             $pn = preg_replace('/.html/', '', $fn);
471             // exclude apage.zh_HK.html -- 
472             if (!preg_match('/mail/',$subdir) && strpos($pn, '.') !== false) {
473                 echo "SKIP - not a transalatable page\n";
474                 continue;
475             }
476             $plsub = $subdir . (empty($subdir) ? '' : '/').  preg_replace('/.html/', '', $fn);
477             
478             $generate_this_page = $generate_page;
479             
480             if ($generate_this_page) {
481                 if (is_string($generate_cms_page_for) && $generate_cms_page_for == '*') {
482                     // no change..
483                 } else if (is_array($generate_cms_page_for)) {
484                     if (!in_array($plsub, $generate_cms_page_for)) {
485                         $generate_this_page = false;
486                     }
487                 
488                 } else {
489                     $generate_this_page = false;
490                 }
491             }    
492             
493             
494             $pl = $base .'/'. $plsub;
495             
496             // load the file and check the template
497             
498             
499             
500             
501             $ret[] = array(
502                 'base' => $base,
503                 'template_dir' => $tdir ,
504                 'page_link' => $generate_this_page ? $pl : false,
505                 'template' =>  $subdir . (empty($subdir) ? '' : '/').  strtolower($fn)
506             );
507             
508             
509         }
510         //print_r($ret);
511         
512         return $ret;
513             
514         
515         
516          
517     }
518     
519 }