9 require_once 'Pman.php';
10 class Pman_Cms_UpdateTemplates extends Pman
13 static $cli_desc = "Update Templates";
15 static $cli_opts = array(
17 'desc' => 'Compile a specific template',
25 'force-content-update' => array(
26 'desc' => 'Force updating of content from templates',
34 'desc' => 'Turn on debuggin',
50 $ff = HTML_FlexyFramework::get();
51 if (!empty($ff->cli)) {
55 die("no web access");;
59 function get($tbl, $opts)
61 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
64 if (!empty($opts['debug'])) {
65 DB_DataObject::debugLevel(1);
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, '');
80 function fixOldNames()
82 $ff = HTML_FlexyFramework::get();
83 $base = $ff->Pman_Cms['project_name'];
84 $tdirs = $ff->Pman_Cms['templateDir'];
85 if (!is_array($tdirs)) {
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'");
92 $c->query("UPDATE cms_page set page_link = '{$ver}/index' WHERE page_link = '{$ver}/{$base}'");
101 function updateData()
104 // fix any old data..
105 $this->fixOldNames();
111 $pages = $this->scanCode();
113 //print_R($pages); exit;
115 $page_templates = array();
116 foreach($pages as $pg) {
117 $page_templates[$pg['template']] =1;
120 //print_R($pages); exit;;
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']])) {
136 // now generate all the templates..
138 // create the templates..
139 $tp = DB_DataObject::factory('cms_template');
140 foreach($pages as $i=>$pg) {
141 if (empty($pg['template'])) {
144 if (!empty($this->opts['template']) && $this->opts['template'] != ($pg['template_dir'] . '/'. $pg['template'])) {
148 $pages[$i]['template_object'] = $tp->syncTemplatePage($pg);
151 //print_R($pages); exit;
152 $pgo = DB_DataObject::factory('cms_page');
153 foreach($pages as $i=>$pg) {
155 if (!empty($this->opts['template']) && $this->opts['template'] != ($pg['template_dir'] . '/'. $pg['template'])) {
159 $pages[$i]['page'] = $pgo->syncTemplatePage($pg);
161 if (empty($pg['template_object'])) {
165 foreach($pg['template_object']->elements as $e) {
169 $e->syncTemplateFromPage($pg);
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;
176 $cur_pg->whereAddIn('!element_id', $cur_els, 'INT');
178 print_r($cur_pg->fetchAll());exit;
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');
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";
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..
213 function scanCode($base = false, $subdir = '')
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'];
223 $tdirs = $ff->Pman_Cms['templateDir'];
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);
232 //$tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
236 $class_dir = $root . '/'. $base . (empty($subdir) ? '' : '/') . $subdir;
239 $class_base = str_replace('/', '_', $base . (empty($subdir) ? '' : '_') . $subdir);
241 if(!is_dir($class_dir)){
244 if (preg_match('/templates/i', $subdir)) { // skip directories that look like templates..
247 // this inserts the master...
249 if (empty($subdir)) {
250 foreach($prefixes as $pr) {
253 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
255 'template' => 'master.html'
260 // next check the base class..
261 $fullpath = $class_dir.".php";
262 require_once $fullpath;
264 $cls = $ff->Pman_Cms['project_name'];
265 if (!class_exists($cls)) {
266 echo "SKIP top level class - not a PHP class $cls\n";
270 if (!empty($x->template)) {
272 foreach($prefixes as $pr) {
278 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
279 'page_link' => $pr .'/index',
280 'template' => $x->template
282 if (!file_exists($add['template_dir'].'/' . $add['template'])) {
283 echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
292 // print_r($ret);exit;
299 $dh = opendir($class_dir);
301 return array(); // something went wrong!?
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] == '.'){
312 $fullpath = $class_dir."/".$fn;
313 ///echo "filename: $fullpath \n";
314 //var_Dump($fullpath);
317 if(is_dir($fullpath)){
318 // then recursively call self...
320 $ret =array_merge($ret, $this->scanCode($base, $subdir . (empty($subdir) ? '' : '/'). $fn));
326 if (!preg_match('/\.php$/', $fn) || !is_file($fullpath)) {
327 // skip non-php files..
328 echo " skipped = not a php file \n";
332 // load the file and check the template
334 require_once $fullpath;
336 $cls = $class_base .'_' . str_replace('/', '_', preg_replace('/.php$/', '', $fn));
337 if (!class_exists($cls)) {
338 echo "SKIP - not a PHP class $cls\n";
344 if (empty($x->template)) {
345 echo "SKIP - no template defined for $cls\n";
348 foreach($prefixes as $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
358 if (!file_exists($add['template_dir'].'/' . $add['template'])) {
359 echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
374 function scanTemplates($base=false, $subdir='')
376 echo "TSCAN base= $base subdir =$subdir\n ";
377 $ff = HTML_FlexyFramework::get();
379 $generate_cms_page_for = $ff->Pman_Cms['generate_cms_page_for'];
382 $tdirs = $ff->Pman_Cms['templateDir'];
384 if ($base == false && is_array($tdirs)) {
386 foreach($tdirs as $k=>$v) {
387 $ret = array_merge($this->scanTemplates($k));
392 if ($base === false) {
393 $base = $ff->Pman_Cms['project_name'];
395 $tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
397 $scandir = $tdir. (empty($subdir) ? '' : '/') . $subdir;
399 if ($subdir == 'images') {
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;
407 if (preg_match('/_pages$/i', $subdir)) { // skip directories that look like templates only..
408 $generate_page = false;
414 $dh = opendir($scandir);
416 return array(); // something went wrong!?
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] == '.'){
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..
435 if (is_link($fullpath)) {
439 if(is_dir($fullpath)){
440 // then recursively call self...
441 $children = $this->scanTemplates($base, $subdir . (empty($subdir) ? '' : '/'). $fn);
442 if (count($children)) {
444 foreach($children as $c) {
445 if (!empty($c->page_link)) {
449 // children do not have page links..
451 $ret =array_merge($ret, $children);
458 // create a dummy page..
459 $ret =array_merge($ret,array( array(
461 'template_dir' => $tdir ,
462 'page_link' => $base .'/'. $subdir . (empty($subdir) ? '' : '/'). $fn,
463 'template' => 'error.html'
469 $ret =array_merge($ret, $children);
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";
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";
486 $plsub = $subdir . (empty($subdir) ? '' : '/'). preg_replace('/.html/', '', $fn);
488 $generate_this_page = $generate_page;
490 if ($generate_this_page) {
491 if (is_string($generate_cms_page_for) && $generate_cms_page_for == '*') {
493 } else if (is_array($generate_cms_page_for)) {
494 if (!in_array($plsub, $generate_cms_page_for)) {
495 $generate_this_page = false;
499 $generate_this_page = false;
504 $pl = $base .'/'. $plsub;
506 // load the file and check the template
513 'template_dir' => $tdir ,
514 'page_link' => $generate_this_page ? $pl : false,
515 'template' => $subdir . (empty($subdir) ? '' : '/'). $fn /// this used to be strtolower?? why???