<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/MemberWidget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendSubmit.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Languages.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Member.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/widgets/MemberFrontendMultiLangDataProvider.inc.php" );

	class MyLanguagesPane extends MemberWidget {

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			if (!$this->bLoggedIn) return;
			$this->sPageName = "languages";
			$this->oMyProfileMenu->setCurrentMenuItem( $this->sPageName );

			$this->oNativeLanguage = new MemberFrontendMultiLangDataProvider( "native_language" , $this );
			$this->oNativeLanguage->setMemberId( $this->iMemberId );
			$this->oNativeLanguage->setTitle( __("Native_language" ) );
			$this->generateMemberWidgetNormalSubmit( $this->sPageName );
		}

		protected function saveDatas( $admin = false ) {
			// languages submitted
			$langs = explode( "," ,  $this->aP[ $this->getId() . "_retval" ] );

			$member = $this->member_model->getMemberFromMemberId( $this->iMemberId );

			// ha nincs belepve az admin
			if (!AdminUserModel::isAdminLoggedIn()) {
				// akkor ha reszidos, akkor baszunk menteni
				if ($member["member_type"]=="parttime") return;
			}

			$max_languages = SystemParametersModel::getVar( "max_languages_".$member["member_type"] );


			// lecsapjuk, ha tobb
			$len = count( $langs );
			if ($len>$max_languages)
				$langs = array_slice( $langs , 0 , $max_languages );

			$this->member_model->setMemberLanguages( $this->iMemberId , $langs );
			
			$dirty = true;
			$admin_dirty = false;
			$old_datas = $this->member_model->getMemberNativeLanguages( $this->iMemberId );
			$native_language = $this->oNativeLanguage->getValuesFromArr( $this->aP );
			foreach ($GLOBALS["languages"] as $langiso => $langcode) {
				if (!isset( $old_datas[$langcode])) continue;
				if (!isset( $native_language[$langcode])) continue;
				if ($old_datas[$langcode]!=$native_language[$langcode]) {
					$admin_dirty = true;
					$wdg = $this->oNativeLanguage->getSubWidget( $langcode );
					$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , $wdg->getId() );
				}
			}

			$this->member_model->setMemberNativeLanguages( $this->iMemberId , $native_language );

			if ($dirty) {
				// $this->user_model->adminAcknowledgeUser( $this->iUserId , false );		// mar a dirty csinalja
				$this->member_model->setMemberDirty( $this->iMemberId , true );
			}
			if ($admin) {
				$this->clearDirtyObjectAdmin( $this->sPageName );
			}
		}

		public function handleEvents() {
			if (!$this->bLoggedIn) return parent::handleEvents();
			$this->refreshChildValuesFromArray();

			$submitted = false;
			if (isset( $this->oSubmit ) && $this->oSubmit->isSubmitted()) $submitted = true;
			if (isset( $this->oAdminSubmit ) && $this->oAdminSubmit->isSubmitted()) $submitted = true;
			if (!$submitted) return parent::handleEvents();
			
			$this->saveDatas( isset( $this->oAdminSubmit ) && $this->oAdminSubmit->isSubmitted() );

			// clear todo
			$tlm = new TodoListModel();
			$tlm->clearTodoListItem( $this->iUserId , "languages" );
		
			// log	
			$ulm = new UserLogModel();
			$ulm->logMemberActivity( USER_LOG_GENERAL | USER_GENERAL_MODIFIED_PROFILE , "változtatott munkanyelvein" );	// LANGFIX

			FNTH_refresh();
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "myprofilepage_Error_too_much_languages" );
			fo3_add_js_langdef( "myprofilepage_Parttime_cannot_change_languages" );
		}

		protected function loadDatas() {
			$lm = new LanguagesModel();
			$languages = $lm->getLanguageOptions( false );
			$this->setParam( "languages" , $languages );
			$sel_languages = $this->member_model->getMemberLanguages( $this->iMemberId );
			$this->setParam( "selected_languages" , $sel_languages );
			$datas = array();

			$this->member = $member = $this->member_model->getMemberFromMemberId( $this->iMemberId );
			$max_languages = SystemParametersModel::getVar( "max_languages_".$member["member_type"] );
			$datas["available_languages"]=$max_languages;

			$native_languages = $this->member_model->getMemberNativeLanguages( $this->iMemberId );
			$this->oNativeLanguage->setValues( $native_languages );
			
			if (AdminUserModel::isAdminLoggedIn()) {
				foreach ($GLOBALS["languages"] as $langiso => $langcode) {
					$wdg = $this->oNativeLanguage->getSubWidget( $langcode );
					if (!isset( $wdg )) continue;
					if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , $wdg->getId() ))
						$wdg->addClass( "adminblue" );
				}
			}
			return $datas;
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			if (!$this->bLoggedIn) return parent::render();

			$datas = $this->loadDatas();

			if (!AdminUserModel::isAdminLoggedIn()) {
				// akkor ha reszidos, akkor baszunk menteni
				if ($this->member["member_type"]=="parttime") $datas["parttime"]="parttime";
			}

			$this->generateLangDefs();
			FNTHPage::addUniquePageFullInit(
				sprintf( "InitMyLanguagesPane( '%s' , %s );\n" , $this->getId() , json_encode( $datas )  ) );
			fo3_form_change_warning();	
			return parent::render();
		}
	}

?>
