<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/BackendLogin.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/AdminUser.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Globals.inc.php" );
	
	function getGlobalBackendPage() {
		if (!isset( $GLOBALS["BackendPage"] )) return false;
		return $GLOBALS["BackendPage"];
	}

	class BackendPage extends Widget {
		protected $bIsLoggedIn;
		protected $bIsPopup = false;
		protected $oBackendLogin;
		protected $oBackendMenu;
		protected $oBackendTodo;
		protected $aErrorMessages = array();

		protected function isLoggedin() {
			return $this->bIsLoggedIn;
		}
		
		public function __construct( $id = "BackendPage" , $parent = null ) {
			if (!isset( $GLOBALS["BackendPage"] )) $GLOBALS["BackendPage"]=$this;
			if (isset( $_SESSION["backend_error_messages"] )) {
				$this->aErrorMessages = $_SESSION["backend_error_messages"];
				unset( $_SESSION["backend_error_messages"] );
			}
			$this->addMediaDirectory( $GLOBALS["site_root"] , $GLOBALS["site_www"] , "design/rp/","pages/");
			FNTHPage::setPageFullInit( "jslangdefs" , "" ); // hack (elol legyen az asszociativ tombben a jslangdefs, hogy mindenki tudja hasznalni)
			parent::__construct( $id , $parent );
			$this->bIsLoggedIn = AdminUserModel::isAdminLoggedIn();
			$this->setParam( "is_logged_in" , $this->bIsLoggedIn );
			if ($this->bIsLoggedIn) {
			} else {
				$this->oBackendLogin = new BackendLogin( "BackendLogin" , $this );
			}
			FNTHPage::setpageBodyAttributes( "class" , "BackendPage" );
		}
		
		protected function generateLanguageEditorMode() {
			if (!fo3_get_language_editor_mode()) return;
			if (!isset( $GLOBALS["translation_needed"])) return;
			if (empty( $GLOBALS["translation_needed"])) return;
			$retval = "";
			ksort( $GLOBALS["translation_needed"] );
			foreach ($GLOBALS["translation_needed"] as $k => $v) {
				$url = sprintf( "/admin/langdefs/edit.php?mode=modify&uid=%s" , $k );
				$retval.=sprintf( "<a href=\"%s\">%s</a><br/>" ,
					$url , $v );
			}
			if (empty( $retval )) return;
			$retval=sprintf( "<div class=\"langdef_editlinks\"><div class=\"hdr\">Langdefs:</div><div class=\"cnt\">%s</div></div>", $retval );
			FNTHPage::setPageBodyEnd( "language_editor_langdefs" , $retval );
		}

		public function modifyGetPost( &$g , &$p ) {
			parent::modifyGetPost( $g , $p );
			if (!$this->bIsLoggedIn) return;
			if ($this->bIsPopup) return;
			require_once( $GLOBALS["site_root"]."/includes/widgets/BackendMenu.inc.php" );
			require_once( $GLOBALS["site_root"]."/includes/widgets/BackendTodo.inc.php");
			$this->oBackendMenu = new BackendMenu( "BackendMenu" , $this );
			$this->oBackendTodo = new BackendTodo( "BackendTodo" , $this );
		}

		public function renderContent() {
			$filename = $this->getTemplateFilename( ".content" );
			return $this->renderFromTemplate( $filename );
		}
		
		public function renderErrorMessage() {
			return implode( "<br/>" , $this->aErrorMessages );
		}

		public function render() {
			$this->setParam( "bIsPopup" , $this->bIsPopup );
			$this->renderWidgets();
			$jslang = fo3_render_js_langdefs();
			if ($jslang) FNTHPage::setPageFullInit( "jslangdefs" , $jslang );
			$this->setParam( "debug_log_queries" , (FNTHPage::isDebugPage()) ? FNTH_db_renderlog() : "" );
			$this->setParam( "content" , $this->renderContent() );
			$this->setParam( "errormessage" , $this->renderErrorMessage() );
			$content = parent::render();
			if (isset( $this->aG["dblog"] ))
				$content.=FNTH_db_renderlog();
			$this->generateLanguageEditorMode();
			$retval = FNTHPage::renderPage( $content );
			return $retval;
		}

		public function setSessionErrorMessage( $id , $str ) {
			if (!isset( $_SESSION["backend_error_messages"] ))
				$_SESSION["backend_error_messages"] = array();
			$_SESSION["backend_error_messages"][$id]=$str;
		}
		
		public function setErrorMessage( $id , $str ) {
			$this->aErrorMessages[$id]=$str;
		}

		public static function nextPageWarningMessage( $id , $str ) {
			$GLOBALS["BackendPage"]->setSessionErrorMessage( $id , $str );
		}
		
		public static function warningMessage( $id , $str ) {
			$GLOBALS["BackendPage"]->setErrorMessage( $id , $str );
		}
		
		public function refreshWindow() {
			FNTH_refresh();
		}

	}

?>
