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