array( 'desc' => 'Compile a specific template', 'default' => '', 'short' => 't', 'min' => 1, 'max' => 1, ), 'force-content-update' => array( 'desc' => 'Force updating of content from templates', 'default' => '', 'short' => 'f', 'min' => 1, 'max' => 1, ), 'debug' => array( 'desc' => 'Turn on debugging', 'default' => '', 'short' => 'd', 'min' => 1, 'max' => 1, ) ); var $cli = false; var $opts; function getAuth() { $ff = HTML_FlexyFramework::get(); if (!empty($ff->cli)) { $this->cli = true; return true; } die("no web access");; return true; } function get($tbl, $opts=array()) { PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError')); if (!empty($opts['debug'])) { DB_DataObject::debugLevel(1); } /* if (!empty($opts['template'])) { $tp = DB_DataObject::factory('cms_template'); //$opts = HTML_FlexyFramework::get()->Pman_Cms; $tp->syncTemplate($opts['template'], true, ''); die("done"); } */ $this->opts = $opts; $this->updateData(); } function fixOldNames() { $ff = HTML_FlexyFramework::get(); if (empty($ff->Pman_Cms)) { $this->jerr("config[Pman_Cms] is not set"); } $base = $ff->Pman_Cms['project_name']; $tdirs = $ff->Pman_Cms['templateDir']; if (!is_array($tdirs)) { return; } foreach(array_keys($tdirs) as $ver) { $c = DB_DAtaObject::Factory('cms_page'); $c->query("UPDATE cms_page set page_link = '$ver' WHERE page_link = '{$ver}/Site'"); $c->query("UPDATE cms_page set page_link = '{$ver}/index' WHERE page_link = '{$ver}/{$base}'"); // parents?? } } function updateData() { // fix any old data.. $this->fixOldNames(); $pages = $this->scanCode(); //print_R($pages); exit; $page_templates = array(); foreach($pages as $pg) { $page_templates[$pg['template']] =1; } // add in templates that are not related to pages. $all_templates = $this->scanTemplates(); foreach($all_templates as $pg) { if (isset($page_templates[$pg['template']])) { continue; } $pages[] = $pg; } //print_R($pages); exit;; // now generate all the templates.. // create the templates.. $tp = DB_DataObject::factory('cms_template'); foreach($pages as $i=>$pg) { if (empty($pg['template'])) { continue; } if (!empty($this->opts['template']) && $this->opts['template'] != ($pg['template_dir'] . '/'. $pg['template'])) { continue; } $pages[$i]['template_object'] = $tp->syncTemplatePage($pg); // print_r($pages[$i]['template_object'] );exit; } $pgo = DB_DataObject::factory('cms_page'); foreach($pages as $i=>$pg) { if (!empty($this->opts['template']) && $this->opts['template'] != ($pg['template_dir'] . '/'. $pg['template'])) { continue; } $pages[$i]['page'] = $pgo->syncTemplatePage($pg); $pg = $pages[$i]; if (empty($pg['template_object'])) { continue; } $cur_els = array(); foreach($pg['template_object']->elements as $e) { $cur_els[] = $e->id; $e->syncTemplateFromPage($pg); } // #2012 disable the unused item....... // needs testing if(!empty($pg['page']->id)){ $cur_pg = DB_DataObject::factory('cms_page'); $cur_pg->parent_id = $pg['page']->id; $cur_pg->whereAddIn("!cms_page.element_id", $cur_els, 'int'); foreach($cur_pg->fetchAll() as $unused){ $old = clone($unused); $unused->is_deleted = 1; $unused->update($old); } } } $opts = HTML_FlexyFramework::get()->Pman_Cms; //sync the tables to cms_templatestr if(isset($opts['DataObjects_Cms_templatestr']['tables'])){ $cts = DB_DataObject::factory('cms_templatestr'); echo "Sync tables.....\n"; foreach($opts['DataObjects_Cms_templatestr']['tables'] as $table=>$cols){ $t = DB_DataObject::factory($table); foreach($t->fetchAll() as $d) { $cts->onTableChange($this, $d, 'update'); } } } $ff = HTML_FlexyFramework::get(); $opt_lang = empty($ff->Pman_Core_I18n) ? array( 'l' => array()) : $ff->Pman_Core_I18n; $langs = $opt_lang['l'] ; // see what languages are in there already!? $cl = DB_DataObject::factory('cms_templatestr'); $cl->selectAdd(); $cl->selectAdd('distinct(lang) as lang'); $cl->whereAdd("lang != ''"); $dblangs = $cl->fetchAll('lang'); foreach($langs as $l) { if (!in_array($$l, $dblangs)) { $dblangs[] = $l; } } // templates.. -- assume the above does the template syncing.. //DB_DataObject::DebugLevel(1); echo "Sync the Languages template.....\n"; foreach($dblangs as $l) { echo "Sync $l Language.....\n"; $tps = DB_DataObject::factory('cms_templatestr'); $tps->syncLang($l); /// this should be configured somewhere.. } exit; } function scanCode($base = false, $subdir = '') { echo "SCAN base= $base subdir =$subdir\n "; $ff = HTML_FlexyFramework::get(); $root = $ff->page->rootDir; if ($base === false) { $base = $ff->Pman_Cms['project_name']; } $tdirs = $ff->Pman_Cms['templateDir']; $tdir = $tdirs; $prefixes = array($ff->Pman_Cms['project_name']); if (is_array($tdirs)) { $tdir = array_shift(array_values($tdirs)); $prefixes = array_keys($tdirs); } //$tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs; $class_dir = $root . '/'. $base . (empty($subdir) ? '' : '/') . $subdir; $class_base = str_replace('/', '_', $base . (empty($subdir) ? '' : '_') . $subdir); if(!is_dir($class_dir)){ return array(); } if (preg_match('/templates/i', $subdir)) { // skip directories that look like templates.. return array(); } // this inserts the master... $ret = array(); if (empty($subdir)) { foreach($prefixes as $pr) { $ret[] = array( 'base' => $pr, 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir , 'page_link' => $pr, 'template' => 'master.html' ); } // next check the base class.. $fullpath = $class_dir.".php"; require_once $fullpath; $cls = $ff->Pman_Cms['project_name']; if (!class_exists($cls)) { echo "SKIP top level class - not a PHP class $cls\n"; } else { $x = new $cls(); if (!empty($x->template)) { foreach($prefixes as $pr) { // print_r($x); $add = array( 'base' => $pr, 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir , 'page_link' => $pr .'/index', 'template' => $x->template ); if (!file_exists($add['template_dir'].'/' . $add['template'])) { echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n"; continue; } // print_R($add); $ret[] = $add; } } } // print_r($ret);exit; } //print_r($ret);exit; $dh = opendir($class_dir); if(!$dh){ return array(); // something went wrong!? } while (($fn = readdir($dh)) !== false) { // do we care that it will try and parse the template directory??? - not really.. // as we are only looking for php files.. if(empty($fn) || $fn[0] == '.'){ continue; } $fullpath = $class_dir."/".$fn; ///echo "filename: $fullpath \n"; //var_Dump($fullpath); if(is_dir($fullpath)){ // then recursively call self... $ret =array_merge($ret, $this->scanCode($base, $subdir . (empty($subdir) ? '' : '/'). $fn)); continue; } if (!preg_match('/\.php$/', $fn) || !is_file($fullpath)) { // skip non-php files.. echo " skipped = not a php file \n"; continue; } // load the file and check the template require_once $fullpath; $cls = $class_base .'_' . str_replace('/', '_', preg_replace('/.php$/', '', $fn)); if (!class_exists($cls)) { echo "SKIP - not a PHP class $cls\n"; continue; } if (!is_a($cls,'HTML_FlexyFramework_Page')) { echo "SKIP - not a PHP class $cls\n"; continue; } $x = new $cls(); if (empty($x->template)) { echo "SKIP - no template defined for $cls\n"; continue; } foreach($prefixes as $pr) { $add = array( 'base' => $pr, 'template_dir' => is_array($tdirs) ? $tdirs[$pr] : $tdir , 'page_link' => $pr .'/'. $subdir . (empty($subdir) ? '' : '/'). preg_replace('/.php$/', '', $fn), 'template' => $x->template ); if (!file_exists($add['template_dir'].'/' . $add['template'])) { echo "SKIP - template does not exist : " . $add['template_dir'].'/' . $add['template'] . "\n"; continue; } ////print_R($add); $ret[] = $add; } } return $ret; } function scanTemplates($base=false, $subdir='') { echo "TSCAN base= $base subdir =$subdir\n "; $ff = HTML_FlexyFramework::get(); $generate_cms_page_for = isset($ff->Pman_Cms['generate_cms_page_for']) ? $ff->Pman_Cms['generate_cms_page_for'] : false; $tdirs = $ff->Pman_Cms['templateDir']; if ($base == false && is_array($tdirs)) { $ret = array(); foreach($tdirs as $k=>$v) { $ret = array_merge($this->scanTemplates($k)); } return $ret; } if ($base === false) { $base = $ff->Pman_Cms['project_name']; } $tdir = is_array($tdirs) ? $tdirs[$base] : $tdirs; $scandir = $tdir. (empty($subdir) ? '' : '/') . $subdir; if ($subdir == 'images') { return array(); } // skip dom_templates $generate_page = true; if (preg_match('/templates/i', $subdir)) { // skip directories that look like templates.. $generate_page = false; } if (preg_match('/_pages$/i', $subdir)) { // skip directories that look like templates only.. $generate_page = false; } $dh = opendir($scandir); if(!$dh){ return array(); // something went wrong!? } $ret = array(); while (($fn = readdir($dh)) !== false) { // do we care that it will try and parse the template directory??? - not really.. // as we are only looking for php files.. if(empty($fn) || $fn[0] == '.'){ continue; } $fullpath = $scandir.(empty($scandir) ? '' : "/").$fn; echo "filename: $fullpath \n"; //var_Dump($fullpath); if ($fullpath == 'master.html') { // it's covered by the root object.. continue; } if (is_link($fullpath)) { continue; } if(is_dir($fullpath)){ // then recursively call self... $children = $this->scanTemplates($base, $subdir . (empty($subdir) ? '' : '/'). $fn); if (count($children)) { $nc = 0; foreach($children as $c) { if (!empty($c->page_link)) { $nc++; } } // children do not have page links.. if (!$nc) { $ret =array_merge($ret, $children); continue; } // create a dummy page.. $ret =array_merge($ret,array( array( 'base' => $base, 'template_dir' => $tdir , 'page_link' => $base .'/'. $subdir . (empty($subdir) ? '' : '/'). $fn, 'template' => 'error.html' ))); } $ret =array_merge($ret, $children); continue; } if (!preg_match('/\.(html|txt)$/', $fn) || !is_file($fullpath)) { // skip non-php files.. echo " skipped = not a html or txt file \n"; continue; } $pn = preg_replace('/.html/', '', $fn); // exclude apage.zh_HK.html -- if (!preg_match('/mail/',$subdir) && strpos($pn, '.') !== false) { echo "SKIP - not a transalatable page\n"; continue; } $plsub = $subdir . (empty($subdir) ? '' : '/'). preg_replace('/.html/', '', $fn); $generate_this_page = $generate_page; if ($generate_this_page) { if (is_string($generate_cms_page_for) && $generate_cms_page_for == '*') { // no change.. } else if (is_array($generate_cms_page_for)) { if (!in_array($plsub, $generate_cms_page_for)) { $generate_this_page = false; } } else { $generate_this_page = false; } } $pl = $base .'/'. $plsub; // load the file and check the template $ret[] = array( 'base' => $base, 'template_dir' => $tdir , 'page_link' => $generate_this_page ? $pl : false, 'template' => $subdir . (empty($subdir) ? '' : '/'). $fn /// this used to be strtolower?? why??? ); } // print_r($ret); return $ret; } }