<?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/Countries.inc.php" );

	class CountryDataProvider extends MultiLangDataProvider {
		public function addNewDataProvider( $site_language_id ) {
			$retval = new AdminTextbox( $this->getId() . "_" . $site_language_id , $this );
			$retval->setCSSAttribute( "width" , "340px" );
			return $retval;
		}
	}		

	class AdminCountryEditPage extends BackendPopup {
		protected $country_name;
		protected $country_langdefs;
		protected $country_submit;

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

			$this->country_id = new AdminTextbox( "country_id" , $this );
			$this->country_name = new AdminTextbox( "country_name" , $this );
			$this->country_langdefs = new CountryDataProvider( "country_langdefs" , $this );
			$this->country_submit = new AdminSubmit( "country_submit" , $this );
		}

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

			$this->country_id->setTitle( __("countries_Iso" ) );
			$this->country_id->setCSSAttribute( "width" , "60px" );
			$this->country_id->setShowTitle( true );

			$this->country_name->setTitle( __("countries_Official") );
			$this->country_name->setCSSAttribute( "width" , "300px" );
			$this->country_name->setShowTitle( true );

			// submit gomb
			switch ($this->aG["mode"]) {
			case "new":
				$this->country_submit->setValue( __( "New","attr" ) );
				$this->setParam( "title" , __("countries_New_country") );
				break;
			case "modify":
				$this->country_submit->setValue( __( "Modify","attr" ) );
				$this->setParam( "title" , __("countries_Modify_country" ) );
				break;
			case "delete":
				$this->country_submit->setValue( __( "Delete","attr" ) );
				$this->setParam( "title" , __("countries_Delete_country" ) );
				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 CountriesModel();
			$datas = $ml->getCountryFromId( $uid );
			$this->country_id->setValue( $datas["country_id"] );
			$this->country_name->setValue( $datas["country_name"] );
			$langdefs = $ml->getLanguageDefsFromId( $uid );
			foreach ($langdefs as $site_language_id => $v)
				$this->country_langdefs->setValue( $site_language_id , $v["langdef_name"] );
		}

		protected function saveDatas( $uid = 0 ) {
			$lm = new CountriesModel();
			$datas = array(
				"country_id" => $this->country_id->getValue(),
				"country_name" => $this->country_name->getValue()
			);
			$langdefs = $this->country_langdefs->getValuesFromArr();
			$langdef_datas = array();
			foreach ($langdefs as $site_language_id => $v)
				$langdef_datas[$site_language_id]=array( "langdef_name" => $v );

			if (!$uid) {
				$country_id = $lm->addCountry( $datas );
				$lm->setLanguageDefs( $country_id , $langdef_datas );
			} else {
				$lm->updateCountry( $uid , $datas );
				$lm->setLanguageDefs( $uid , $langdef_datas );
			}
		}

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

		public function handleEvents() {
			if (!$this->isLoggedIn()) return parent::handleEvents();
			if (!isset( $this->aP[$this->country_submit->getId()] )) return parent::handleEvents();
			$this->refreshChildValuesFromArray();
			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();
		}
	}
?>
