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

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

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

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

		protected function generateLangDefs() {
			foreach (array( 0 , 25 , 50 , 75 , 100 ) as $v)
				fo3_add_js_langdef( "Capacity_".$v );
		}
		public function handleEvents() {
			if (!$this->bLoggedIn) return parent::handleEvents();
			$this->refreshChildValuesFromArray();
			if (!$this->oSubmit->isSubmitted()) return parent::handleEvents();

			$name = $this->getid()."_value";
			if (!isset( $this->aP[$name] )) return parent::handleEvents();

			$val = $this->aP[$name];
			$query = sprintf( "update members set capacity=%d where member_id=%d" , $val , $this->iMemberId );
			FNTH_db_free_result( FNTH_db_query( $query ) );

			// 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 , "capacity" );
		
			// log	
			$ulm = new UserLogModel();
			$ulm->logMemberActivity( USER_LOG_GENERAL | USER_GENERAL_MODIFIED_PROFILE , "változtatott szabad kapacitásán" );	// LANGFIX


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


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

			$this->generateLangDefs();

			// load capacity
			$member = $this->member_model->getMemberFromMemberId( $this->iMemberId );
			$capacity = $member["capacity"];

			$this->setParam( "capacity" , $capacity );
			FNTHPage::addUniquePageFullInit(
				sprintf( "InitMyCapacityPane( '%s' , %d );\n", $this->getId() , $capacity ) );
			fo3_form_change_warning( );
			return parent::render();
		}
	}

?>
