<?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/MultiLangDataProvider.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSubmit.inc.php" );

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

	class AdminLanguageEditPage extends BackendPopup {
		protected $language_name;
		protected $language_langdefs;
		protected $language_submit;

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

			$this->language_name = new AdminTextbox( "language_name" , $this );
			$this->language_langdefs = new MultiLangDataProvider( "language_langdefs" , $this );
			$this->language_submit = new AdminSubmit( "language_submit" , $this );
		}

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

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

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->language_submit->setValue( __( "New","attr" ) );
				$this->setParam( "title" , __("languages_New_language") );
				break;
			case "modify":
				$this->language_submit->setValue( __( "Modify","attr") );
				$this->setParam( "title" , __("languages_Modify_language" ) );
				break;
			case "delete":
				$this->language_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("languages_Delete_language" ) );
				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 LanguagesModel();
			$datas = $ml->getLanguageFromId( $uid );
			$this->language_name->setValue( $datas["language_name"] );
			$langdefs = $ml->getLanguageDefsFromId( $uid );
			foreach ($langdefs as $site_language_id => $v)
				$this->language_langdefs->setValue( $site_language_id , $v["langdef_name"] );
		}

		protected function saveDatas( $uid = 0 ) {
			$lm = new LanguagesModel();
			$datas = array(
				"language_name" => $this->aP[$this->language_name->getId()]
			);
			$langdefs = $this->language_langdefs->getValuesFromArr();
			$langdef_datas = array();
			foreach ($langdefs as $site_language_id => $v)
				$langdef_datas[$site_language_id]=array( "langdef_name" => $v );

			if (!$uid) {
				$language_id = $lm->addLanguage( $datas );
				$lm->setLanguageDefs( $language_id , $langdef_datas );
			} else {
				$lm->updateLanguage( $uid , $datas );
				$lm->setLanguageDefs( $uid , $langdef_datas );
			}
		}

		protected function deleteDatas( $uid = 0 ) {
			$lm = new LanguagesModel();
			$lm->deleteLanguage( $uid );
		}

		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (!isset( $this->aP[$this->language_submit->getId()] )) return parent::handleEvents();
			switch ($this->aG["mode"]) {
			case "new":$this->saveDatas( 0 );break;
			case "modify":$this->saveDatas( $this->aG["uid"] );break;
			case "delete":$this->deleteDatas( $this->aG["uid"] );break;
			}
			$this->closeAndRefreshParent();
		}

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