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

	class MySpecialtiesPane extends MemberWidget {
		protected $oMySpecialties;

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

			$this->oMySpecialties = new MySpecialties( "specialties" , $this );
			$this->oMySpecialties->setMemberId( $this->iMemberId );

			$this->generateMemberWidgetNormalSubmit( $this->sPageName );
		}

		protected function loadDatas() {
			if (!$this->bLoggedIn) return;
			$this->oMySpecialties->setSpecialties( $this->member_model->getMemberSpecialties( $this->iMemberId ) );

			$custom_specialties = $this->member_model->getMemberCustomSpecialties( $this->iMemberId );
			$this->oMySpecialties->setCustomSpecialties( $custom_specialties );

			if (AdminUserModel::isAdminLoggedIn()) {
				foreach ($GLOBALS["languages"] as $langiso => $langcode) {
					$wdg = $this->oMySpecialties->getCustomSpecialtiesWidget()->getSubWidget( $langcode );
					if (!isset( $wdg )) continue;
					if ($this->dirty_model->isDirtyObject( $this->iUserId , $this->sPageName , $wdg->getId() ))
						$wdg->addClass( "adminblue" );
				}
			}
		}

		protected function saveDatas( $admin  = false ) {
			$this->member_model->setMemberSpecialties( $this->iMemberId , $this->oMySpecialties->getSpecialties() );
			$this->oMySpecialties->refreshValueFromArray();

			$custom_specialties = $this->oMySpecialties->getCustomSpecialties();
			$old_datas = $this->member_model->getMemberCustomSpecialties( $this->iMemberId );

			$dirty = true;
			$admin_dirty = false;
			foreach ($GLOBALS["languages"] as $langiso => $langcode) {
				if (!isset( $old_datas[$langcode])) continue;
				if (!isset( $custom_specialties[$langcode])) continue;
				if ($old_datas[$langcode]!=$custom_specialties[$langcode]) {
					$admin_dirty = true;
					$wdg = $this->oMySpecialties->getCustomSpecialtiesWidget()->getSubWidget( $langcode );
					$this->dirty_model->setDirtyObject( $this->iUserId , $this->sPageName , $wdg->getId() );
				}
			}
			$this->member_model->setMemberCustomSpecialties( $this->iMemberId , $custom_specialties );
			// clear admin ack
			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();

			// save the stuff
			$this->saveDatas( isset( $this->oAdminSubmit ) && $this->oAdminSubmit->isSubmitted() );

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

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

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

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

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->loadDatas();
			fo3_form_change_warning( "document.frm_specialties" );
			return parent::render();
		}

	}

?>
