<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/MemberWidget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/NotificationOptions.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendCheckbox.inc.php" );
	
	require_once( $GLOBALS["site_root"]."/includes/models/ExtendedProperties.inc.php" );

	class MyUserInterfacePane extends MemberWidget {
		protected $oNotifications;

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

			$this->oNotifications = new NotificationOptions( "notifications" , $this );	
			$this->oShowBBScore = new FrontendCheckbox( "show_blueboard_score" , $this );
			$this->oShowBBDetails = new FrontendCheckbox( "show_blueboard_details" , $this );

			$this->oSearchbotIndex = new FrontendCheckbox( "searchbot_index" , $this );
			$this->generateMemberWidgetNormalSubmit( $this->sPageName );
		}

		protected function initializeWidgets() {
			$ep = new ExtendedPropertiesModel();
			$title = $ep->getEPropLangdefFromName( $GLOBALS["langcode"] , "show_blueboard_score" );
			$this->oShowBBScore->setShowTitle( true );
			$this->oShowBBScore->setTitle( $title );
			
			$title = $ep->getEPropLangdefFromName( $GLOBALS["langcode"] , "show_blueboard_details" );
			$this->oShowBBDetails->setShowTitle( true );
			$this->oShowBBDetails->setTitle( $title );

			$this->oSearchbotIndex->setShowTitle( true );
			$this->oSearchbotIndex->setTitle( __("guioptions_Searchbot_index") );
		}

		public function handleEvents() {
			if (!$this->bLoggedIn) return parent::handleEvents();
			$this->refreshChildValuesFromArray();
			if (isset( $this->aP["ajaxevent"] ) && isset( $this->aP["inajax"] )) {
				switch ($this->aP["ajaxevent"]) {
				case "delete_my_membership":
					$mmm = new MyMemberModel();
					$member_id = $mmm->getLoggedInMemberId();
					if (!$member_id) {
						FNTH_echo_json_response( "alert('Nem tag...');" );
						fo3_exit();
					}
					$mm = new MemberModel();
					$member = $mm->getMemberFromMemberId( $member_id );
					$new_status = (strpos( $member["current_status"] , "new_user" )!==false) ?
						STATUS_NEW_USER_DELETED : STATUS_DELETED;
					$mm->updateMemberStatus( $member_id , $new_status );
					$mm->deleteMember( $member_id );
					$mmm->logoutUser();
					FNTH_echo_json_response( "window.location.href='/';" );
					fo3_exit();
				}
			}	
			if (!$this->oSubmit->isSubmitted()) return parent::handleEvents();
	
			$p = array(
				"notifications" => $this->oNotifications->getValue()
			);
			$this->member_model->updateMember( $this->iMemberId , $p );

			// show bb score
			$checked = $this->oShowBBScore->isChecked();
			$prop_id = $this->member_model->getExtendedPropertyIdFromName( "show_blueboard_score" );
			if ($checked) {
				$this->member_model->setMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , "on" );
			} else {
				$this->member_model->unsetMemberExtendedPropertyFromId( $this->iMemberId , $prop_id );
			}
			

			// show bb details
			$checked = $this->oShowBBDetails->isChecked();
			$prop_id = $this->member_model->getExtendedPropertyIdFromName( "show_blueboard_details" );
			if ($checked) {
				$this->member_model->setMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , "on" );
			} else {
				$this->member_model->unsetMemberExtendedPropertyFromId( $this->iMemberId , $prop_id );
			}
			
			// searchbot_index
			$checked = $this->oSearchbotIndex->isChecked();
			$prop_id = $this->member_model->getExtendedPropertyIdFromName( "searchbot_index" );
			if ($checked) {
				$this->member_model->setMemberExtendedPropertyFromId( $this->iMemberId , $prop_id , "on" );
			} else {
				$this->member_model->unsetMemberExtendedPropertyFromId( $this->iMemberId , $prop_id );
			}

			// 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 , "user_interface" );
		
			// log	
			$ulm = new UserLogModel();
			$ulm->logMemberActivity( USER_LOG_GENERAL | USER_GENERAL_MODIFIED_PROFILE , "változtatott felhasználói felületi beállításain" );	// LANGFIX

			FNTH_refresh();
		}

		protected function loadDatas() {
			// notifications
			$member = $this->member_model->getMemberFromMemberId( $this->iMemberId );
			$this->oNotifications->setValue( $member["notifications"] );
			// show bb score
			$prop_id = $this->member_model->getExtendedPropertyIdFromName( "show_blueboard_score" );
			$val = $this->member_model->getMemberExtendedPropertyFromId( $this->iMemberId , $prop_id );
			$this->oShowBBScore->setChecked( ($val=="on") );

			$prop_id = $this->member_model->getExtendedPropertyIdFromName( "show_blueboard_details" );
			$val = $this->member_model->getMemberExtendedPropertyFromId( $this->iMemberId , $prop_id );
			$this->oShowBBDetails->setChecked( ($val=="on") );
			
			$prop_id = $this->member_model->getExtendedPropertyIdFromName( "searchbot_index" );
			$val = $this->member_model->getMemberExtendedPropertyFromId( $this->iMemberId , $prop_id );
			$this->oSearchbotIndex->setChecked( ($val=="on") );
		}
		protected function generateLangdefs() {
			fo3_add_js_langdef( "_Yes_" );
			fo3_add_js_langdef( "_No_" );
			fo3_add_js_langdef( "Delete_my_membership" );
			fo3_add_js_langdef( "confirmation_Delete_my_membership" );
			
		}

		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->initializeWidgets();
			$this->generateLangdefs();
			$this->loadDatas();
			fo3_form_change_warning( "document.frm_my_user_interface" );
			return parent::render();
		}
	}

?>
