* @copyright 2004 by gERD Schaufelberger * @package wombat * @subpackage admin */ /** * admin interface * * * @version 0.1.0 * @package wombat * @subpackage Admin */ class wbAdmin { /** * patTemplate object * @var object $_tmpl */ var $_tmpl; /** * patSession object * @var object $_sess */ var $_sess; /** * wbAuth object * @var object $_auth */ var $_auth; /** * request variables * @var array $_request */ var $_request = array(); /** * store main config array * @var array $_config */ var $_config = array(); /** * store main config array * @var array $_config */ var $_configAdmin = array(); /** * current application * @var string $_app */ var $_app = 'Welcome'; /** * application meta info * @var array $_appInfo */ var $_appInfo = array(); /** * application menu items * @var array $_appMenu */ var $_appMenu = array(); /** * constructor * * @access public */ function __construct() { // load tools $this->_sess =& wbFactory::singleton( 'patSession' ); $params = array( 'tmplDir' => wbFactory::getParam( 'systemDir' ) . '/'. wbFactory::getParam( 'adminDir' ) . '/templates' ); $this->_tmpl =& wbFactory::singleton( 'patTemplate', $params ); // recieve main config $this->_config =& wbFactory::getParam( 'config' ); // load admin config $conf =& wbFactory::singleton( 'patConfiguration' ); $conf->loadCachedConfig( 'admin.xml' ); $this->_configAdmin = $conf->getConfigValue(); // get request variables $this->_request = $_GET; $this->_request = array_merge( $this->_request, $_POST ); $this->_request = array_merge( $this->_request, $_FILES ); // select application path if( isset( $this->_request['app'] ) ) { $this->_app = $this->_request['app']; } // run user authentication module first - this may affect the session-id! $params = array( 'config' => $this->_configAdmin['auth'] ); $this->_auth =& wbFactory::singleton( 'wbAuth', $params ); // load meta data $menuDir = wbFactory::getParam( 'baseDir' ) . '/' . wbFactory::getParam( 'varDir' ) . '/admin/menu'; $this->_appInfo = unserialize( file_get_contents( $menuDir . '/info.ser' ) ); $this->_appMenu = unserialize( file_get_contents( $menuDir . '/menu.ser' ) ); wbFactory::includeClass( 'wbJSTools' ); } /** * constructor wrapper for PHP4 * * @access public * @see __construct() */ function wbAdmin() { $this->__construct(); } /** * run admin interface * * @access public * @return boolean true on success */ function process() { // get applcation content $appPath = explode( '/', $this->_app ); if( !$this->_auth->isAuthenticated() ) { if( $appPath[0] != 'Login' ) { $this->_app = 'Login'; $appPath = array( 'Login' ); } wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'User not authenticated', 'Auth' ); } if( !isset( $this->_appInfo[$appPath[0] ] ) ) { $appPath = array( 'login' ); } wbDebugger::addMsg( WBSITE_DEBUG_SECTION, $this->_app, 'Application' ); $appHandler = array_shift( $appPath ); $application =& wbFactory::singleton( 'wbAdminApp_' . $appHandler ); if( patErrorManager::isError( $application ) ) { // exit programme! patErrorManager::raiseError( 'wbAdmin:1', 'Processing failed!', 'Could not load application: "' . $appHandler . '"' ); return false; } // run applicattion $application->setConfig( $this->_configAdmin ); $application->setRequest( $this->_request ); // add global template variables $this->_addGlobalContent( $application ); $res = $application->process( $appPath ); if( patErrorManager::isError( $res ) ) { return $res; } wbDebugger::addMsg( WBSITE_DEBUG_SECTION, wbDebugger::sprint_r( $this->_request ), 'Request' ); if( $this->_sess->isNew() ) { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, 'New session started', 'Session' ); } else { wbDebugger::addMsg( WBSITE_DEBUG_SECTION, $this->_sess->getCounter() . ' requests', 'Session' ); } // add messages $msg = $application->getMsg(); if( !empty( $msg ) ) { $this->_tmpl->readTemplatesFromFile( 'msg.tmpl' ); $this->_tmpl->addVar( 'msg_entry', 'msg', $msg ); $this->_tmpl->addGlobalVar( 'msg', $this->_tmpl->getParsedTemplate( 'msg' ) ); } // some applications don't need surrounding template $fullScreen = $application->isFullscreen(); $html = $application->getHtml(); if( patErrorManager::isError( $html ) ) { patErrorManager::raiseError( 'wbAdmin:3', 'Processing failed!', 'Could not get html from application: "' . $appHandler . '"' ); return false; } if( $fullScreen ) { echo $html; if( wbDebugger::isActive() ) { wbDebugger::printMsg(); } return true; } $this->_tmpl->readTemplatesFromInput( 'page.tmpl' ); // build menu content $menu = $this->_getMenuHtml( $application ); if( patErrorManager::isError( $menu ) ) { return $menu; } $this->_tmpl->addVar( 'page', 'MENU', $menu ); $this->_tmpl->addVar( 'page', 'APPLICATION', $html ); $this->_insertJavascript(); $this->_tmpl->displayParsedTemplate( 'page' ); if( wbDebugger::isActive() ) { wbDebugger::printMsg(); } return true; } /** * load menu and transform to html * * @access private * æparam object $application link to running application * @return string $html parsed template */ function _getMenuHtml( &$application ) { $appPath = explode( '/', $this->_app ); $this->_tmpl->readTemplatesFromInput( 'menu.tmpl' ); $menu = $this->_configAdmin['menu']; for( $i = 0; $i < count( $menu ); ++$i ) { $menu[$i]['state'] = '_none_'; foreach( $this->_appInfo[$menu[$i]['app'] ] as $key => $value ) { $menu[$i][$key] = $value; } // add submenu $submenu = $this->_appMenu[$appPath[0]]; if( $appPath[0] == $menu[$i]['app'] ) { // try to load dynamic submenu if( empty( $submenu ) ) { $this->_appMenu[$appPath[0]] = $application->getMenu(); $submenu = $this->_appMenu[$appPath[0]]; } if( isset( $appPath[1] ) ) { for( $j = 0; $j < count( $submenu ); ++$j ) { if( $appPath[1] == $submenu[$j]['name'] ) { $submenu[$j]['state'] = 'selected'; } } } $this->_tmpl->addRows( 'appmenu_entry', $submenu ); $menu[$i]['appmenu'] = $this->_tmpl->getParsedTemplate( 'appmenu_entry' ); $menu[$i]['state'] = 'selected'; } } $this->_tmpl->addRows( 'menu_entry', $menu ); return $this->_tmpl->getParsedTemplate( 'menu' ); } /** * add javascript to template * * @access private * @return boolean $result true on success */ function _insertJavascript() { $code = wbJSTools::getCode(); $this->_tmpl->addGlobalVar( 'javascript_code', $code['code'] ); $this->_tmpl->addGlobalVar( 'javascript_onload', $code['onload'] ); return true; } /** * insert system vars to templates * * @access protected * @param object $application link to running application * @return boolean $result true on success */ function _addGlobalContent( &$application ) { $proc = explode( '/', $this->_app ); $title = $this->_appInfo[$proc[0]]['title']; $brief = $this->_appInfo[$proc[0]]['brief']; $parent = ''; if( count( $proc ) > 1 ) { $parent = $title; // try to load submenu from application if( empty( $this->_appMenu[$proc[0]] ) ) { $this->_appMenu[$proc[0]] = $application->getMenu(); } if( !is_array( $this->_appMenu[$proc[0]] ) ) { $this->_appMenu[$proc[0]] = array(); } foreach( $this->_appMenu[$proc[0]] as $mi ) { if( $mi['name'] == $proc[1] ) { $title = $mi['title']; $brief = $mi['brief']; } } } // add some global data $globals = array( 'url' => $this->_config['url'], 'app' => $this->_app, 'app_title' => $title, 'app_brief' => $brief, 'app_parent' => $parent, 'proc' => $proc[0], 'proc_title' => $this->_appInfo[$proc[0]]['title'], 'proc_brief' => $this->_appInfo[$proc[0]]['brief'], 'self' => $_SERVER['PHP_SELF'] . '?' . $this->_sess->getQueryString(), 'sess_name' => $this->_sess->getName(), 'sess_id' => $this->_sess->getId(), 'request_get' => '' ); // add global template variables $get = array(); foreach( $_GET as $key => $value ) { if( isset( $globals[$key] ) ) { continue; } array_push( $get, $key . '=' . $value ); } $globals['request_get'] = implode( '&', $get ); $this->_tmpl->addGlobalVars( $globals ); // add user data if( $this->_auth->isAuthenticated() ) { $this->_tmpl->addGlobalVars( $this->_auth->getUserData(), 'user_' ); } return true; } } ?>