<?php
	require_once( $GLOBALS["site_root"]."/includes/pages/BackendPopup.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextarea.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/MultiLangDataProvider.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSubmit.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/models/Langdefs.inc.php" );

	class LangdefDataProvider extends MultiLangDataProvider {
		public function addNewDataProvider( $site_language_id ) {
			$retval = new AdminTextarea( $this->getId() . "_" . $site_language_id , $this );
			$retval->setAttribute( "cols" , 86 );
			return $retval;
		}
	}
		

	class AdminLangdefEditPage extends BackendPopup {
		protected $langdef_name;
		protected $langdef_langdefs;
		protected $langdef_submit = null;
		protected $langdef_resubmit = null;

		public function __construct( $id = "AdminLangdefEditPage" , $parent = null ) {
			parent::__construct( $id , $parent );
			if (!AdminUserModel::isAdminLoggedIn()) return;

			$this->langdef_name = new AdminTextbox( "langdef_name" , $this );
			$this->langdef_name->setWidth( 750 );
			$this->langdef_langdefs = new LangdefDataProvider( "langdef_langdefs" , $this );
			$this->langdef_submit = new AdminSubmit( "langdef_submit" , $this );
		}

		public function modifyGetPost( &$g , &$p ) {
			$retval = parent::modifyGetPost( $g , $p );
			if (isset( $g["mode"] ) && $g["mode"]=="new") {
				$this->langdef_resubmit = new AdminSubmit( "langdef_resubmit" , $this );
			}
			return $retval;
		}

		protected function initializeWidgets() {
			$lang = new LangdefsModel();

			$this->langdef_name->setTitle( __("langdefs_Name") );
			$this->langdef_name->setCSSAttribute( "width" , "300px" );
			$this->langdef_name->setShowTitle( true );
			$this->langdef_name->setAttribute( "tabindex" , "1" );

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->langdef_submit->setValue( __( "New","attr" ) );
				if (isset( $this->langdef_resubmit ))
					$this->langdef_resubmit->setValue( __( "New_and_continue","attr" ) );
				$this->setParam( "title" , __("langdefs_New_langdef","attr") );
				break;
			case "modify":
				$this->langdef_submit->setValue( __( "Modify","attr") );
				$this->setParam( "title" , __("langdefs_Modify_langdef","attr" ) );
				break;
			case "delete":
				$this->langdef_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("langdefs_Delete_langdef","attr" ) );
				break;
			}
			FNTHPage::setPageTitle( $this->getParam( "title" ) );
			// load datas
			switch ($this->aG["mode"]) {
			case "modify":
			case "delete":
				$this->loadDatas( $this->aG["uid"] );
				break;
			}
		}

		protected function loadDatas( $uid ) {
			$ml = new LangdefsModel();
			$datas = $ml->getLangdefFromId( $uid );
			if ($datas) {
				$this->langdef_name->setValue( $datas["ldef_name"] );
			} else {
				$this->langdef_name->setValue( $uid );
			}

			$langdefs = $ml->getLangdefDefsFromId( $uid );
			foreach ($langdefs as $site_langdef_id => $v)
				$this->langdef_langdefs->setValue( $site_langdef_id , $v["def"] );
		}

		protected function saveDatas( $uid = "" ) {

			$lm = new LangdefsModel();
			$ldef_name = $this->langdef_name->getValue();
			$datas = array(
				"ldef_name" => $this->aP[$this->langdef_name->getId()]
			);
			$langdefs = $this->langdef_langdefs->getValuesFromArr();
			$langdef_datas = array();
			foreach ($langdefs as $site_langdef_id => $v)
				$langdef_datas[$site_langdef_id]=array( "def" => $v );

			if (!strlen( $uid )) {
				$lm->addLangdef( $datas );
			}
			$lm->setLangdefDefs( $ldef_name , $langdef_datas );
		}

		protected function deleteDatas( $uid = "" ) {
			$lm = new LangdefsModel();
			$lm->deleteLangdef( $uid );
		}

		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			$this->initializeWidgets();
			$this->refreshChildValuesFromArray();
			$submitted = false;
			if (isset( $this->langdef_submit ) && $this->langdef_submit->isSubmitted()) $submitted = true;
			if (isset( $this->langdef_resubmit ) && $this->langdef_resubmit->isSubmitted()) $submitted = true;
			if (!$submitted) return parent::handleEvents();
			switch ($this->aG["mode"]) {
			case "new":$this->saveDatas( "" );break;
			case "modify":$this->saveDatas( $this->aG["uid"] );break;
			case "delete":$this->deleteDatas( $this->aG["uid"] );break;
			}
			if (isset( $this->langdef_submit ) && $this->langdef_submit->isSubmitted() ) {
				$this->closeAndRefreshParent();
			} else {
				$this->refreshWindow();
			}
		}

		public function render() {
			if (!$this->isLoggedIn()) return parent::render();
			return parent::render();
		}
	}
?>
