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']])) {
135 // print_R($pages); exit;
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);
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) {
167 $e->syncTemplateFromPage($pg);
171 $opts = HTML_FlexyFramework::get()->Pman_Cms;
172 //sync the tables to cms_templatestr
173 if(isset($opts['DataObjects_Cms_templatestr']['tables'])){
174 $cts = DB_DataObject::factory('cms_templatestr');
175 echo "Sync tables.....\n";
176 foreach($opts['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){
177 $t = DB_DataObject::factory($table);
178 foreach($t->fetchAll() as $d) {
179 $cts->onTableChange($this, $d, 'update');
183 $ff = HTML_FlexyFramework::get();
184 $opt_lang = empty($ff->Pman_Core_I18n) ? array( 'l' => array()) : $ff->Pman_Core_I18n;
185 // templates.. -- assume the above does the template syncing..
186 //DB_DataObject::DebugLevel(1);
187 echo "Sync the Languages template.....\n";
189 foreach($opt_lang['l'] as $l) {
190 echo "Sync $l Language.....\n";
191 $tps = DB_DataObject::factory('cms_templatestr');
192 $tps->syncLang($l); /// this should be configured somewhere..
203 function scanCode($base = false, $subdir = '')
205 echo "SCAN base= $base subdir =$subdir\n ";
206 $ff = HTML_FlexyFramework::get();
207 $root = $ff->page->rootDir;
208 if ($base === false) {
209 $base = $ff->Pman_Cms['project_name'];
213 $tdirs = $ff->Pman_Cms['templateDir'];
216 $prefixes = array($ff->Pman_Cms['project_name']);
217 if (is_array($tdirs)) {
218 $tdir = array_shift(array_values($tdirs));
219 $prefixes = array_keys($tdirs);
222 //$tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
226 $class_dir = $root . '/'. $base . (empty($subdir) ? '' : '/') . $subdir;
229 $class_base = str_replace('/', '_', $base . (empty($subdir) ? '' : '_') . $subdir);
231 if(!is_dir($class_dir)){
234 if (preg_match('/templates/i', $subdir)) { // skip directories that look like templates..
237 // this inserts the master...
239 if (empty($subdir)) {
240 foreach($prefixes as $pr) {
243 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
245 'template' => 'master.html'
250 // next check the base class..
251 $fullpath = $class_dir.".php";
252 require_once $fullpath;
254 $cls = $ff->Pman_Cms['project_name'];
255 if (!class_exists($cls)) {
256 echo "SKIP top level class - not a PHP class $cls\n";
260 if (!empty($x->template)) {
262 foreach($prefixes as $pr) {
268 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
269 'page_link' => $pr .'/index',
270 'template' => $x->template
272 if (!file_exists($add['template_dir'].'/' . $add['template'])) {
273 echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
282 // print_r($ret);exit;
289 $dh = opendir($class_dir);
291 return array(); // something went wrong!?
295 while (($fn = readdir($dh)) !== false) {
296 // do we care that it will try and parse the template directory??? - not really..
297 // as we are only looking for php files..
298 if(empty($fn) || $fn[0] == '.'){
302 $fullpath = $class_dir."/".$fn;
303 ///echo "filename: $fullpath \n";
304 //var_Dump($fullpath);
307 if(is_dir($fullpath)){
308 // then recursively call self...
310 $ret =array_merge($ret, $this->scanCode($base, $subdir . (empty($subdir) ? '' : '/'). $fn));
316 if (!preg_match('/\.php$/', $fn) || !is_file($fullpath)) {
317 // skip non-php files..
318 echo " skipped = not a php file \n";
322 // load the file and check the template
324 require_once $fullpath;
326 $cls = $class_base .'_' . str_replace('/', '_', preg_replace('/.php$/', '', $fn));
327 if (!class_exists($cls)) {
328 echo "SKIP - not a PHP class $cls\n";
334 if (empty($x->template)) {
335 echo "SKIP - no template defined for $cls\n";
338 foreach($prefixes as $pr) {
344 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir ,
345 'page_link' => $pr .'/'. $subdir . (empty($subdir) ? '' : '/'). preg_replace('/.php$/', '', $fn),
346 'template' => $x->template
348 if (!file_exists($add['template_dir'].'/' . $add['template'])) {
349 echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n";
364 function scanTemplates($base=false, $subdir='')
366 echo "TSCAN base= $base subdir =$subdir\n ";
367 $ff = HTML_FlexyFramework::get();
369 $generate_cms_page_for = $ff->Pman_Cms['generate_cms_page_for'];
372 $tdirs = $ff->Pman_Cms['templateDir'];
374 if ($base == false && is_array($tdirs)) {
376 foreach($tdirs as $k=>$v) {
377 $ret = array_merge($this->scanTemplates($k));
382 if ($base === false) {
383 $base = $ff->Pman_Cms['project_name'];
385 $tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs;
387 $scandir = $tdir. (empty($subdir) ? '' : '/') . $subdir;
389 if ($subdir == 'images') {
392 // skip dom_templates
393 $generate_page = true;
394 if (preg_match('/templates/i', $subdir)) { // skip directories that look like templates..
395 $generate_page = false;
397 if (preg_match('/_pages$/i', $subdir)) { // skip directories that look like templates only..
398 $generate_page = false;
404 $dh = opendir($scandir);
406 return array(); // something went wrong!?
410 while (($fn = readdir($dh)) !== false) {
411 // do we care that it will try and parse the template directory??? - not really..
412 // as we are only looking for php files..
413 if(empty($fn) || $fn[0] == '.'){
417 $fullpath = $scandir.(empty($scandir) ? '' : "/").$fn;
418 echo "filename: $fullpath \n";
419 //var_Dump($fullpath);
420 if ($fullpath == 'master.html') {
421 // it's covered by the root object..
425 if (is_link($fullpath)) {
429 if(is_dir($fullpath)){
430 // then recursively call self...
431 $children = $this->scanTemplates($base, $subdir . (empty($subdir) ? '' : '/'). $fn);
432 if (count($children)) {
434 foreach($children as $c) {
435 if (!empty($c->page_link)) {
439 // children do not have page links..
441 $ret =array_merge($ret, $children);
448 // create a dummy page..
449 $ret =array_merge($ret,array( array(
451 'template_dir' => $tdir ,
452 'page_link' => $base .'/'. $subdir . (empty($subdir) ? '' : '/'). $fn,
453 'template' => 'error.html'
459 $ret =array_merge($ret, $children);
465 if (!preg_match('/\.(html|txt)$/', $fn) || !is_file($fullpath)) {
466 // skip non-php files..
467 echo " skipped = not a html or txt file \n";
470 $pn = preg_replace('/.html/', '', $fn);
471 // exclude apage.zh_HK.html --
472 if (!preg_match('/mail/',$subdir) && strpos($pn, '.') !== false) {
473 echo "SKIP - not a transalatable page\n";
476 $plsub = $subdir . (empty($subdir) ? '' : '/'). preg_replace('/.html/', '', $fn);
478 $generate_this_page = $generate_page;
480 if ($generate_this_page) {
481 if (is_string($generate_cms_page_for) && $generate_cms_page_for == '*') {
483 } else if (is_array($generate_cms_page_for)) {
484 if (!in_array($plsub, $generate_cms_page_for)) {
485 $generate_this_page = false;
489 $generate_this_page = false;
494 $pl = $base .'/'. $plsub;
496 // load the file and check the template
503 'template_dir' => $tdir ,
504 'page_link' => $generate_this_page ? $pl : false,
505 'template' => $subdir . (empty($subdir) ? '' : '/'). strtolower($fn)