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)
62 if (!empty($opts['debug'])) {
63 DB_DataObject::debugLevel(1);
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, '');
78 function fixOldNames()
80 $ff = HTML_FlexyFramework::get();
81 $base = $ff->Pman_Cms['project_name'];
82 $tdirs = $ff->Pman_Cms['templateDir'];
83 if (!is_array($tdirs)) {
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'");
90 $c->query("UPDATE cms_page set page_link = '{$ver}/index' WHERE page_link = '{$ver}/{$base}'");
102 // fix any old data..
103 $this->fixOldNames();
109 $pages = $this->scanCode();
111 //print_R($pages); exit;
113 $page_templates = array();
114 foreach($pages as $pg) {
115 $page_templates[$pg['template']] =1;
118 //print_R($pages); exit;;
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']])) {
133 // print_R($pages); exit;
134 // now generate all the templates..
136 // create the templates..
137 $tp = DB_DataObject::factory('cms_template');
138 foreach($pages as $i=>$pg) {
139 if (empty($pg['template'])) {
142 if (!empty($this->opts['template']) && $this->opts['template'] != ($pg['template_dir'] . '/'. $pg['template'])) {
146 $pages[$i]['template_object'] = $tp->syncTemplatePage($pg);
150 $pgo = DB_DataObject::factory('cms_page');
151 foreach($pages as $i=>$pg) {
153 if (!empty($this->opts['template']) && $this->opts['template'] != ($pg['template_dir'] . '/'. $pg['template'])) {
157 $pages[$i]['page'] = $pgo->syncTemplatePage($pg);
159 if (empty($pg['template_object'])) {
163 foreach($pg['template_object']->elements as $e) {
165 $e->syncTemplateFromPage($pg);
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');
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";
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..
201 function scanCode($base = false, $subdir = '')
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'];
211 $tdirs = $ff->Pman_Cms['templateDir'];
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);
220 //$tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
224 $class_dir = $root . '/'. $base . (empty($subdir) ? '' : '/') . $subdir;
227 $class_base = str_replace('/', '_', $base . (empty($subdir) ? '' : '_') . $subdir);
228 print_r($class_base);exit;
229 if(!is_dir($class_dir)){
232 if (preg_match('/templates/i', $subdir)) { // skip directories that look like templates..
235 // this inserts the master...
237 if (empty($subdir)) {
238 foreach($prefixes as $pr) {
241 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
243 'template' => 'master.html'
248 // next check the base class..
249 $fullpath = $class_dir.".php";
250 require_once $fullpath;
252 $cls = $ff->Pman_Cms['project_name'];
253 if (!class_exists($cls)) {
254 echo "SKIP top level class - not a PHP class $cls\n";
258 if (!empty($x->template)) {
260 foreach($prefixes as $pr) {
266 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
267 'page_link' => $pr .'/index',
268 'template' => $x->template
270 if (!file_exists($add['template_dir'].'/' . $add['template'])) {
271 echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
280 // print_r($ret);exit;
287 $dh = opendir($class_dir);
289 return array(); // something went wrong!?
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] == '.'){
300 $fullpath = $class_dir."/".$fn;
301 ///echo "filename: $fullpath \n";
302 //var_Dump($fullpath);
305 if(is_dir($fullpath)){
306 // then recursively call self...
308 $ret =array_merge($ret, $this->scanCode($base, $subdir . (empty($subdir) ? '' : '/'). $fn));
314 if (!preg_match('/\.php$/', $fn) || !is_file($fullpath)) {
315 // skip non-php files..
316 echo " skipped = not a php file \n";
320 // load the file and check the template
322 require_once $fullpath;
324 $cls = $class_base .'_' . str_replace('/', '_', preg_replace('/.php$/', '', $fn));
325 if (!class_exists($cls)) {
326 echo "SKIP - not a PHP class $cls\n";
332 if (empty($x->template)) {
333 echo "SKIP - no template defined for $cls\n";
336 foreach($prefixes as $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
346 if (!file_exists($add['template_dir'].'/' . $add['template'])) {
347 echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
362 function scanTemplates($base=false, $subdir='')
364 echo "TSCAN base= $base subdir =$subdir\n ";
365 $ff = HTML_FlexyFramework::get();
367 $generate_cms_page_for = $ff->Pman_Cms['generate_cms_page_for'];
370 $tdirs = $ff->Pman_Cms['templateDir'];
372 if ($base == false && is_array($tdirs)) {
374 foreach($tdirs as $k=>$v) {
375 $ret = array_merge($this->scanTemplates($k));
380 if ($base === false) {
381 $base = $ff->Pman_Cms['project_name'];
383 $tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
385 $scandir = $tdir. (empty($subdir) ? '' : '/') . $subdir;
387 if ($subdir == 'images') {
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;
395 if (preg_match('/_pages$/i', $subdir)) { // skip directories that look like templates only..
396 $generate_page = false;
402 $dh = opendir($scandir);
404 return array(); // something went wrong!?
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] == '.'){
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..
423 if (is_link($fullpath)) {
427 if(is_dir($fullpath)){
428 // then recursively call self...
429 $children = $this->scanTemplates($base, $subdir . (empty($subdir) ? '' : '/'). $fn);
430 if (count($children)) {
432 foreach($children as $c) {
433 if (!empty($c->page_link)) {
437 // children do not have page links..
439 $ret =array_merge($ret, $children);
446 // create a dummy page..
447 $ret =array_merge($ret,array( array(
449 'template_dir' => $tdir ,
450 'page_link' => $base .'/'. $subdir . (empty($subdir) ? '' : '/'). $fn,
451 'template' => 'error.html'
457 $ret =array_merge($ret, $children);
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";
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";
474 $plsub = $subdir . (empty($subdir) ? '' : '/'). preg_replace('/.html/', '', $fn);
476 $generate_this_page = $generate_page;
478 if ($generate_this_page) {
479 if (is_string($generate_cms_page_for) && $generate_cms_page_for == '*') {
481 } else if (is_array($generate_cms_page_for)) {
482 if (!in_array($plsub, $generate_cms_page_for)) {
483 $generate_this_page = false;
487 $generate_this_page = false;
492 $pl = $base .'/'. $plsub;
494 // load the file and check the template
501 'template_dir' => $tdir ,
502 'page_link' => $generate_this_page ? $pl : false,
503 'template' => $subdir . (empty($subdir) ? '' : '/'). strtolower($fn)