<?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/widgets/LanguagePairs.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" );

	class MyLanguagePairsPane extends MemberWidget {
		protected $oLangPairMatrix;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			if (!$this->bLoggedIn) return;

			$this->sPageName = "language_pairs";
			$this->oMyProfileMenu->setCurrentMenuItem( $this->sPageName );


			$this->oLangPairMatrix = new LanguagePairs( "langpairmatrix" , $this );
			$this->oLangPairMatrix->setShowIntro( true );
			
			$this->generateMemberWidgetNormalSubmit( $this->sPageName );
		}

		protected function filterMatrix( $langmatrix ) {
			$retval = array();
			$langs = $this->member_model->getMemberLanguages( $this->iMemberId );
			$lk = array();
			foreach ($langs as $langid) $lk[$langid]=true;

			$services = array(1,2,3);
			foreach ($services as $service_id) {
				$retval[$service_id]=array();
				foreach ($langmatrix[$service_id] as $src_id => $dst_langs) {
					if (!isset( $lk[$src_id] )) continue;
					foreach ($dst_langs as $dst_id => $v) {
						if (!isset( $lk[$dst_id] )) continue;
						if (!isset( $retval[$service_id][$src_id] )) $retval[$service_id][$src_id]=array();
						if (!isset( $retval[$service_id][$src_id][$dst_id] ))
							$retval[$service_id][$src_id][$dst_id]=true;
					}
				}
			}

			return $retval;
		}

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

			// get the stuff
			$langmatrix = $this->oLangPairMatrix->getValue();
			$langmatrix = $this->filterMatrix( $langmatrix );
			$this->member_model->setMemberLanguagePairs3D( $this->iMemberId , $langmatrix );

			// clear admin ack
			// $this->user_model->adminAcknowledgeUser( $this->iUserId , false );		// mar a dirty csinalja

			// set dirty
			$this->member_model->setMemberDirty( $this->iMemberId , true );

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

			FNTH_refresh();
			return parent::handleEvents();
		}


		public function render() {
			if ($this->sRendered) return $this->sRendered;
			if (!$this->bLoggedIn) return parent::render();
			// load the data for javascript
			// FNTH_da( $datas["language_matrix"] );
			$this->oLangPairMatrix->setSelectedLanguages( $this->member_model->getMemberLanguages( $this->iMemberId ) );
			$this->oLangPairMatrix->setValue( $this->member_model->getMemberLanguagePairs3D( $this->iMemberId ) );
	
			fo3_form_change_warning( "document.frm_my_language_pairs" );
			return parent::render();
		}
	}

?>
