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