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

	class MyMembershipsPane extends MemberWidget {
		protected $oCustomMembership;

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

			$this->oMyProfileMenu->setCurrentMenuItem( $this->sPageName );
			$this->oCustomMembership = new FrontendTextbox( "custom_membership" , $this );
			$this->oCustomMembership->setAttribute( "maxlength" , 255 );
			
			$this->generateMemberWidgetNormalSubmit( $this->sPageName );
		}

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

			$memberships = array();
			$prefix = $this->getId() . "_";
			foreach ($this->aP as $k => $v ) {
				if (strrpos( $k , $prefix )!==0) continue;
				$k = str_replace( $prefix , "" , $k );
				$memberships[] = (int)$k;
			}
			$datas = array();
			$datas["memberships"]=$memberships;
			$datas["custom_membership"]=$this->oCustomMembership->getValue();
			$this->member_model->setMemberMemberships( $this->iMemberId , $datas );
			// 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 , "memberships" );
		
			// log	
			$ulm = new UserLogModel();
			$ulm->logMemberActivity( USER_LOG_GENERAL | USER_GENERAL_MODIFIED_PROFILE , "változtatott szakmai tagságain" );	// LANGFIX
			FNTH_refresh();
			return parent::handleEvents();
		}

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

			$mmm =new MembershipsModel();
			$memberships = $mmm->getMembershipOptions( false , false );
			$this->setParam( "memberships" , $memberships );

			$sel_memberships = $this->member_model->getMemberMemberships( $this->iMemberId );
			$this->setParam( "sel_memberships" , $sel_memberships["memberships"] );
			$this->oCustomMembership->setValue( $sel_memberships["custom_membership"] );
			fo3_form_change_warning( "document.frm_my_memberships" );
			return parent::render();
		}
	}

?>
