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 debugging',
50 $ff = HTML_FlexyFramework::get();
51 if (!empty($ff->cli)) {
55 die("no web access");;
59 function get($tbl, $opts=array())
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 if (empty($ff->Pman_Cms)) {
84 $this->jerr("config[Pman_Cms] is not set");
87 $base = $ff->Pman_Cms['project_name'];
88 $tdirs = $ff->Pman_Cms['templateDir'];
89 if (!is_array($tdirs)) {
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'");
96 $c->query("UPDATE cms_page set page_link = '{$ver}/index' WHERE page_link = '{$ver}/{$base}'");
105 function updateData()
108 // fix any old data..
109 $this->fixOldNames();
115 $pages = $this->scanCode();
117 //print_R($pages); exit;
119 $page_templates = array();
120 foreach($pages as $pg) {
121 $page_templates[$pg['template']] =1;
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']])) {
137 //print_R($pages); exit;;
140 // now generate all the templates..
142 // create the templates..
143 $tp = DB_DataObject::factory('cms_template');
144 foreach($pages as $i=>$pg) {
145 if (empty($pg['template'])) {
148 if (!empty($this->opts['template']) && $this->opts['template'] != ($pg['template_dir'] . '/'. $pg['template'])) {
153 $pages[$i]['template_object'] = $tp->syncTemplatePage($pg);
154 // print_r($pages[$i]['template_object'] );exit;
158 $pgo = DB_DataObject::factory('cms_page');
159 foreach($pages as $i=>$pg) {
161 if (!empty($this->opts['template']) && $this->opts['template'] != ($pg['template_dir'] . '/'. $pg['template'])) {
165 $pages[$i]['page'] = $pgo->syncTemplatePage($pg);
167 if (empty($pg['template_object'])) {
171 foreach($pg['template_object']->elements as $e) {
173 $e->syncTemplateFromPage($pg);
176 // #2012 disable the unused item.......
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);
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');
202 $ff = HTML_FlexyFramework::get();
203 $opt_lang = empty($ff->Pman_Core_I18n) ? array( 'l' => array()) : $ff->Pman_Core_I18n;
205 $langs = $opt_lang['l'] ;
206 // see what languages are in there already!?
207 $cl = DB_DataObject::factory('cms_templatestr');
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)) {
220 // templates.. -- assume the above does the template syncing..
221 //DB_DataObject::DebugLevel(1);
222 echo "Sync the Languages template.....\n";
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..
238 function scanCode($base = false, $subdir = '')
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'];
248 $tdirs = $ff->Pman_Cms['templateDir'];
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);
257 //$tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
261 $class_dir = $root . '/'. $base . (empty($subdir) ? '' : '/') . $subdir;
264 $class_base = str_replace('/', '_', $base . (empty($subdir) ? '' : '_') . $subdir);
266 if(!is_dir($class_dir)){
269 if (preg_match('/templates/i', $subdir)) { // skip directories that look like templates..
272 // this inserts the master...
274 if (empty($subdir)) {
275 foreach($prefixes as $pr) {
278 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
280 'template' => 'master.html'
285 // next check the base class..
286 $fullpath = $class_dir.".php";
287 require_once $fullpath;
289 $cls = $ff->Pman_Cms['project_name'];
290 if (!class_exists($cls)) {
291 echo "SKIP top level class - not a PHP class $cls\n";
295 if (!empty($x->template)) {
297 foreach($prefixes as $pr) {
303 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
304 'page_link' => $pr .'/index',
305 'template' => $x->template
307 if (!file_exists($add['template_dir'].'/' . $add['template'])) {
308 echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
317 // print_r($ret);exit;
321 //print_r($ret);exit;
324 $dh = opendir($class_dir);
326 return array(); // something went wrong!?
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] == '.'){
337 $fullpath = $class_dir."/".$fn;
338 ///echo "filename: $fullpath \n";
339 //var_Dump($fullpath);
342 if(is_dir($fullpath)){
343 // then recursively call self...
345 $ret =array_merge($ret, $this->scanCode($base, $subdir . (empty($subdir) ? '' : '/'). $fn));
351 if (!preg_match('/\.php$/', $fn) || !is_file($fullpath)) {
352 // skip non-php files..
353 echo " skipped = not a php file \n";
357 // load the file and check the template
359 require_once $fullpath;
361 $cls = $class_base .'_' . str_replace('/', '_', preg_replace('/.php$/', '', $fn));
362 if (!class_exists($cls)) {
363 echo "SKIP - not a PHP class $cls\n";
367 if (!is_a($cls,'HTML_FlexyFramework_Page')) {
368 echo "SKIP - not a PHP class $cls\n";
375 if (empty($x->template)) {
376 echo "SKIP - no template defined for $cls\n";
379 foreach($prefixes as $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
389 if (!file_exists($add['template_dir'].'/' . $add['template'])) {
390 echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
405 function scanTemplates($base=false, $subdir='')
407 echo "TSCAN base= $base subdir =$subdir\n ";
408 $ff = HTML_FlexyFramework::get();
410 $generate_cms_page_for = isset($ff->Pman_Cms['generate_cms_page_for']) ? $ff->Pman_Cms['generate_cms_page_for'] : false;
413 $tdirs = $ff->Pman_Cms['templateDir'];
415 if ($base == false && is_array($tdirs)) {
417 foreach($tdirs as $k=>$v) {
418 $ret = array_merge($this->scanTemplates($k));
423 if ($base === false) {
424 $base = $ff->Pman_Cms['project_name'];
426 $tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
428 $scandir = $tdir. (empty($subdir) ? '' : '/') . $subdir;
430 if ($subdir == 'images') {
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;
438 if (preg_match('/_pages$/i', $subdir)) { // skip directories that look like templates only..
439 $generate_page = false;
445 $dh = opendir($scandir);
447 return array(); // something went wrong!?
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] == '.'){
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..
466 if (is_link($fullpath)) {
470 if(is_dir($fullpath)){
471 // then recursively call self...
472 $children = $this->scanTemplates($base, $subdir . (empty($subdir) ? '' : '/'). $fn);
473 if (count($children)) {
475 foreach($children as $c) {
476 if (!empty($c->page_link)) {
480 // children do not have page links..
482 $ret =array_merge($ret, $children);
489 // create a dummy page..
490 $ret =array_merge($ret,array( array(
492 'template_dir' => $tdir ,
493 'page_link' => $base .'/'. $subdir . (empty($subdir) ? '' : '/'). $fn,
494 'template' => 'error.html'
500 $ret =array_merge($ret, $children);
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";
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";
517 $plsub = $subdir . (empty($subdir) ? '' : '/'). preg_replace('/.html/', '', $fn);
519 $generate_this_page = $generate_page;
521 if ($generate_this_page) {
522 if (is_string($generate_cms_page_for) && $generate_cms_page_for == '*') {
524 } else if (is_array($generate_cms_page_for)) {
525 if (!in_array($plsub, $generate_cms_page_for)) {
526 $generate_this_page = false;
530 $generate_this_page = false;
535 $pl = $base .'/'. $plsub;
537 // load the file and check the template
544 'template_dir' => $tdir ,
545 'page_link' => $generate_this_page ? $pl : false,
546 'template' => $subdir . (empty($subdir) ? '' : '/'). $fn /// this used to be strtolower?? why???